91 lines
1.9 KiB
C
91 lines
1.9 KiB
C
/*
|
|
* NcLooper fonctions audio
|
|
*/
|
|
|
|
#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"
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
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;
|
|
} |