NcLooper/audio/ao_output.c

105 lines
2.2 KiB
C
Raw Normal View History

2019-10-23 22:17:57 +11:00
/*
2019-10-24 03:46:27 +11:00
* NcLooper fonctions audio output
2019-10-23 22:17:57 +11:00
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <math.h>
#include <ao/ao.h>
#include <sndfile.h>
#include "ao_output.h"
/* --------------------------------------------------------------------- */
2019-10-24 03:46:27 +11:00
extern int verbosity;
2019-10-23 22:17:57 +11:00
/* --------------------------------------------------------------------- */
2019-10-24 03:46:27 +11:00
ao_device *init_ao_output(int smplrate)
2019-10-23 22:17:57 +11:00
{
int default_driver;
ao_sample_format format;
2019-10-24 03:46:27 +11:00
ao_device *device;
2019-10-23 22:17:57 +11:00
#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");
2019-10-24 03:46:27 +11:00
return NULL;
2019-10-23 22:17:57 +11:00
}
2019-10-24 03:46:27 +11:00
#if DEBUG_LEVEL
fprintf(stderr, "%s : device at %p\n", __func__, device);
#endif
2019-10-23 22:17:57 +11:00
2019-10-24 03:46:27 +11:00
return device;
2019-10-23 22:17:57 +11:00
}
/* --------------------------------------------------------------------- */
2019-10-24 03:46:27 +11:00
int close_ao_output(ao_device *dev)
2019-10-23 22:17:57 +11:00
{
2019-10-24 03:46:27 +11:00
if (NULL == dev) {
fprintf(stderr, "%s : no output to close\n", __func__);
return -1;
}
2019-10-23 22:17:57 +11:00
2019-10-24 03:46:27 +11:00
ao_close(dev);
ao_shutdown();
2019-10-23 22:17:57 +11:00
return 0;
}
/* --------------------------------------------------------------------- */
2019-10-24 03:46:27 +11:00
int play_some_stuff(ao_device *dev, int notused)
2019-10-23 22:17:57 +11:00
{
int i, sample;
char *buffer;
float freq = 440.0;
2019-10-24 03:46:27 +11:00
#define NB_SAMPLES (44100*2)
2019-10-23 22:17:57 +11:00
2019-10-24 03:46:27 +11:00
if (NULL == dev) {
2019-10-23 22:17:57 +11:00
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;
}
2019-10-24 03:46:27 +11:00
ao_play(dev, buffer, NB_SAMPLES);
free(buffer);
2019-10-23 22:17:57 +11:00
return 0;
2019-10-24 03:46:27 +11:00
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */