diff --git a/.gitignore b/.gitignore index cd531cf..e627a43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# mon machin a moi + +audio/t + # ---> C # Prerequisites *.d diff --git a/audio/Makefile b/audio/Makefile new file mode 100644 index 0000000..09be2a3 --- /dev/null +++ b/audio/Makefile @@ -0,0 +1,11 @@ + +CC = gcc +CCOPT = -Wall -g -DDEBUG_LEVEL=1 +LIBS = -lao -lsndfile -lm + +ao_output.o: ao_output.c Makefile + $(CC) ${CCOPT} -c $< + +t: t.c ao_output.o Makefile + $(CC) ${CCOPT} $< ao_output.o ${LIBS} -o $@ + diff --git a/audio/ao_output.c b/audio/ao_output.c new file mode 100644 index 0000000..93dca41 --- /dev/null +++ b/audio/ao_output.c @@ -0,0 +1,91 @@ +/* + * NcLooper fonctions audio + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include "ao_output.h" + +/* --------------------------------------------------------------------- */ + +static ao_device *device; /* never alone with a singleton */ + +/* --------------------------------------------------------------------- */ +int init_ao_output(int smplrate) +{ +int default_driver; +ao_sample_format format; + +#if DEBUG_LEVEL +fprintf(stderr, ">>> %s ( %d )\n", __func__, smplrate); +#endif + +ao_initialize(); + +default_driver = ao_default_driver_id(); +#if DEBUG_LEVEL +fprintf(stderr, "%s : ao default driver #%d\n", __func__, default_driver); +#endif + +memset(&format, 0, sizeof(format)); +format.bits = 16; +format.channels = 2; +format.rate = smplrate; +format.byte_format = AO_FMT_LITTLE; + +device = ao_open_live(default_driver, &format, NULL); +if (device == NULL) { + fprintf(stderr, "Error opening AO device.\n"); + return -1; + } + + +return 0; +} +/* --------------------------------------------------------------------- */ +int close_ao_output(void) +{ + +ao_close(device); +ao_shutdown(); + +device = NULL; + +return 0; +} +/* --------------------------------------------------------------------- */ +int play_some_stuff(int notused) +{ +int i, sample; +char *buffer; +float freq = 440.0; + +#define NB_SAMPLES 44100 + +if (NULL == device) { + fprintf(stderr, "%s : please call 'init_ao_output' first\n", + __func__); + exit(1); + } + +buffer = calloc(NB_SAMPLES*2, sizeof(short)); + +for (i = 0; i < NB_SAMPLES; i++) { + sample = (int)(0.75 * 32768.0 * + sin(2 * M_PI * freq * ((float) i/44100))); + + /* 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, NB_SAMPLES); + +return 0; +} \ No newline at end of file diff --git a/audio/ao_output.h b/audio/ao_output.h new file mode 100644 index 0000000..cb49b27 --- /dev/null +++ b/audio/ao_output.h @@ -0,0 +1,10 @@ + +/* generic output */ + +int init_ao_output(int smplrate); +int close_ao_output(void); + + +/* tests functions */ + +int play_some_stuff(int notused); diff --git a/audio/t.c b/audio/t.c new file mode 100644 index 0000000..ee536e5 --- /dev/null +++ b/audio/t.c @@ -0,0 +1,30 @@ +/* + * NcLooper test des fonctions audio + */ + +#include + +#include "ao_output.h" + +/* --------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------- */ + +int main(int argc, char *argv[]) +{ +int foo; + + +foo = init_ao_output(44100); +fprintf(stderr, "AO init -> %d\n", foo); + +foo = play_some_stuff(0); +fprintf(stderr, "play stuff -> %d\n", foo); + +foo = close_ao_output(); +fprintf(stderr, "AO close -> %d\n", foo); + + +return 0; +} +/* --------------------------------------------------------------------- */