playing a WAV file

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

View File

@ -2,9 +2,21 @@
Les scratchmen utilisent des loopers en Flash 11.
Il est temps qu'ils découvrent une interface ncurses
pilotée au joystick :)
pilotable au joystick ou par OSC :)
## Prérequis :
libsndfile, libao, libcurses.
libsndfile, libao, liblo, libcurses.avec les bons -dev !
## Compilation :
Pour le moment, vous êtes laissé à vous-même.
## Hacking :
La voie est libre.

View File

@ -6,6 +6,11 @@ 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 $@
playfile.o: playfile.c Makefile
$(CC) ${CCOPT} -c $<
OBJS = ao_output.o playfile.o
t: t.c ${OBJS} Makefile
$(CC) ${CCOPT} $< ${OBJS} ${LIBS} -o $@

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;
}
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */

View File

@ -1,10 +1,10 @@
/* generic output */
int init_ao_output(int smplrate);
int close_ao_output(void);
ao_device * init_ao_output(int smplrate);
int close_ao_output(ao_device *dev);
/* tests functions */
int play_some_stuff(int notused);
int play_some_stuff(ao_device *dev, int notused);

70
audio/playfile.c Normal file
View File

@ -0,0 +1,70 @@
/*
* NcLooper fonctions audio output
*/
#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"
/* --------------------------------------------------------------------- */
extern int verbosity;
#define T_BUFF_WAVES 16384
/* --------------------------------------------------------------------- */
int blast_this_file(char *fname, ao_device *dev, int loop)
{
SNDFILE *sndf;
SF_INFO sfinfo;
int foo, lus;
short *buffer;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( '%s' %p %d )\n", __func__, fname, dev, loop);
#endif
sndf = sf_open(fname, SFM_READ, &sfinfo);
if (NULL==sndf)
{
perror("sf_open");
abort();
}
buffer = calloc(T_BUFF_WAVES, sizeof(short)*2);
#if DEBUG_LEVEL
fprintf(stderr, "samplerate : %d\n", sfinfo.samplerate);
fprintf(stderr, "frames : %ld\n", sfinfo.frames);
fprintf(stderr, "seekable : %d\n", sfinfo.seekable);
#endif
while ((lus = sf_read_short(sndf, buffer, T_BUFF_WAVES))) {
#if DEBUG_LEVEL
fprintf(stderr, "%s : %d bytes read\n", fname, lus);
#endif
foo = ao_play(dev, buffer, T_BUFF_WAVES*2);
#if DEBUG_LEVEL
fprintf(stderr, "%s : %d played\n", fname, foo);
#endif
}
/* do some cleanup */
free(buffer);
sf_close(sndf);
return 0;
}
/* --------------------------------------------------------------------- */

3
audio/playfile.h Normal file
View File

@ -0,0 +1,3 @@
int blast_this_file(char *fname, ao_device *dev, int loop);

View File

@ -3,25 +3,37 @@
*/
#include <stdio.h>
#include <unistd.h>
#include <ao/ao.h>
#include "ao_output.h"
#include "playfile.h"
/* --------------------------------------------------------------------- */
int verbosity;
/* --------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo;
int foo;
ao_device *device;
foo = init_ao_output(44100);
fprintf(stderr, "AO init -> %d\n", foo);
device = init_ao_output(44100);
fprintf(stderr, "AO init -> %p\n", device);
foo = play_some_stuff(0);
foo = play_some_stuff(device, 0);
fprintf(stderr, "play stuff -> %d\n", foo);
foo = close_ao_output();
sleep(1);
foo = blast_this_file("/home/tth/BU/vrac/1337.wav", device, 0);
fprintf(stderr, "blast file -> %d\n", foo);
foo = close_ao_output(device);
fprintf(stderr, "AO close -> %d\n", foo);