diff options
author | Jedidiah Barber <contact@jedbarber.id.au> | 2023-06-19 22:15:44 +1200 |
---|---|---|
committer | Jedidiah Barber <contact@jedbarber.id.au> | 2023-06-19 22:15:44 +1200 |
commit | 74af58587359206ef92249d18e4830c40cac0bc5 (patch) | |
tree | 8dfae06813f8e9f41787e45e7e31354b017f5713 /example |
Initial commit
Diffstat (limited to 'example')
-rw-r--r-- | example/aao_example.adb | 101 | ||||
-rw-r--r-- | example/ao_example.c | 87 |
2 files changed, 188 insertions, 0 deletions
diff --git a/example/aao_example.adb b/example/aao_example.adb new file mode 100644 index 0000000..7d12b56 --- /dev/null +++ b/example/aao_example.adb @@ -0,0 +1,101 @@ + + +-- Programmed by Jedidiah Barber +-- Released into the public domain + +-- This program opens the default libao driver and plays a 440 Hz tone for one second + + +with + + Ada.Command_Line, + Ada.Numerics.Elementary_Functions, + Ada.Text_IO, + Libao; + + +procedure AAO_Example is + + package ACom renames Ada.Command_Line; + package Math renames Ada.Numerics.Elementary_Functions; + package TIO renames Ada.Text_IO; + + My_Device : Libao.Device; + My_Format : Libao.Sample_Format; + + Default_Driver : Libao.Driver_ID_Number; + +begin + + -- Initialize + + TIO.Put_Line ("libao example program"); + + Libao.Startup; + + + -- Setup for default driver + + Default_Driver := Libao.Default_Driver_ID; + + My_Format := Libao.Create + (Bits => 16, + Rate => 44100, + Channels => 2, + Byte_Format => Libao.Little_Endian, + Channel_Matrix => Libao.Stereo); + + + -- Open driver + + begin + My_Device := Libao.Open_Live + (Driver_ID => Default_Driver, + Format => My_Format, + Options => Libao.Empty_Options); + exception + when Libao.Open_Device_Error | Libao.General_Failure => + TIO.Put_Line ("Error opening device."); + ACom.Set_Exit_Status (ACom.Failure); + return; + end; + + + -- Play some stuff + + -- This sine wave generation was directly translated from the C example, + -- but it ends up being a little messy playing fast and loose like this. + + declare + Buffer : Libao.Data_Buffer (1 .. 16 / 8 * 2 * 44100); + + type Wraparound is mod 65536; + Sample : Wraparound; + begin + for I in Integer range 0 .. 44100 - 1 loop + Sample := Wraparound (Integer (0.75 * 32768.0 * + Math.Sin (2.0 * Ada.Numerics.Pi * 440.0 * Float (I) / 44100.0)) mod 65536); + + -- Put the same stuff in left and right channel + Buffer (4 * I + 1) := Character'Val (Sample and 16#FF#); + Buffer (4 * I + 2) := Character'Val ((Sample / 256) and 16#FF#); + Buffer (4 * I + 3) := Character'Val (Sample and 16#FF#); + Buffer (4 * I + 4) := Character'Val ((Sample / 256) and 16#FF#); + end loop; + + Libao.Play (My_Device, Buffer); + end; + + + -- Close and shutdown + + -- Technically the binding will take care of closing open devices at shutdown, + -- but it is always good practice to close them anyway. + + Libao.Close (My_Device); + + Libao.Shutdown; + +end AAO_Example; + + diff --git a/example/ao_example.c b/example/ao_example.c new file mode 100644 index 0000000..a5642a0 --- /dev/null +++ b/example/ao_example.c @@ -0,0 +1,87 @@ +/* + * + * ao_example.c + * + * Written by Stan Seibert - July 2001 + * + * Legal Terms: + * + * This source file is released into the public domain. It is + * distributed without any warranty; without even the implied + * warranty * of merchantability or fitness for a particular + * purpose. + * + * Function: + * + * This program opens the default driver and plays a 440 Hz tone for + * one second. + * + * Compilation command line (for Linux systems): + * + * gcc -o ao_example ao_example.c -lao -ldl -lm + * + */ + +#include <stdio.h> +#include <string.h> +#include <ao/ao.h> +#include <math.h> + +#define BUF_SIZE 4096 + +int main() +{ + ao_device *device; + ao_sample_format format; + int default_driver; + char *buffer; + int buf_size; + int sample; + float freq = 440.0; + int i; + + /* -- Initialize -- */ + + fprintf(stderr, "libao example program\n"); + + ao_initialize(); + + /* -- Setup for default driver -- */ + + default_driver = ao_default_driver_id(); + + memset(&format, 0, sizeof(format)); + format.bits = 16; + format.channels = 2; + format.rate = 44100; + format.byte_format = AO_FMT_LITTLE; + + /* -- Open driver -- */ + device = ao_open_live(default_driver, &format, NULL /* no options */); + if (device == NULL) { + fprintf(stderr, "Error opening device.\n"); + return 1; + } + + /* -- Play some stuff -- */ + buf_size = format.bits/8 * format.channels * format.rate; + buffer = calloc(buf_size, + sizeof(char)); + + for (i = 0; i < format.rate; i++) { + sample = (int)(0.75 * 32768.0 * + sin(2 * M_PI * freq * ((float) i/format.rate))); + + /* Put the same stuff in left and right channel */ + buffer[4*i] = buffer[4*i+2] = sample & 0xff; + buffer[4*i+1] = buffer[4*i+3] = (sample >> 8) & 0xff; + } + ao_play(device, buffer, buf_size); + + /* -- Close and shutdown -- */ + ao_close(device); + + ao_shutdown(); + + return (0); +} |