first beep

This commit is contained in:
tth 2019-10-23 13:17:57 +02:00
parent bcc6295925
commit 2e538157c5
5 changed files with 146 additions and 0 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
# mon machin a moi
audio/t
# ---> C
# Prerequisites
*.d

11
audio/Makefile Normal file
View File

@ -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 $@

91
audio/ao_output.c Normal file
View File

@ -0,0 +1,91 @@
/*
* 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;
}

10
audio/ao_output.h Normal file
View File

@ -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);

30
audio/t.c Normal file
View File

@ -0,0 +1,30 @@
/*
* NcLooper test des fonctions audio
*/
#include <stdio.h>
#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;
}
/* --------------------------------------------------------------------- */