playing a WAV file

This commit is contained in:
2019-10-23 18:46:27 +02:00
parent 2e538157c5
commit 1bd06fb67b
7 changed files with 141 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
/*
* NcLooper fonctions audio
* NcLooper fonctions audio output
*/
#include <stdio.h>
@@ -15,13 +15,14 @@
/* --------------------------------------------------------------------- */
static ao_device *device; /* never alone with a singleton */
extern int verbosity;
/* --------------------------------------------------------------------- */
int init_ao_output(int smplrate)
ao_device *init_ao_output(int smplrate)
{
int default_driver;
ao_sample_format format;
ao_device *device;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %d )\n", __func__, smplrate);
@@ -43,33 +44,39 @@ 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 NULL;
}
#if DEBUG_LEVEL
fprintf(stderr, "%s : device at %p\n", __func__, device);
#endif
return 0;
return device;
}
/* --------------------------------------------------------------------- */
int close_ao_output(void)
int close_ao_output(ao_device *dev)
{
ao_close(device);
ao_shutdown();
if (NULL == dev) {
fprintf(stderr, "%s : no output to close\n", __func__);
return -1;
}
device = NULL;
ao_close(dev);
ao_shutdown();
return 0;
}
/* --------------------------------------------------------------------- */
int play_some_stuff(int notused)
int play_some_stuff(ao_device *dev, int notused)
{
int i, sample;
char *buffer;
float freq = 440.0;
#define NB_SAMPLES 44100
#define NB_SAMPLES (44100*2)
if (NULL == device) {
if (NULL == dev) {
fprintf(stderr, "%s : please call 'init_ao_output' first\n",
__func__);
exit(1);
@@ -85,7 +92,13 @@ for (i = 0; i < NB_SAMPLES; i++) {
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);
ao_play(dev, buffer, NB_SAMPLES);
free(buffer);
return 0;
}
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */