From 8aed26d0f87080653ecfa3d43e21296c13580e94 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sun, 15 Jul 2012 12:18:28 -0600 Subject: [PATCH] src/io/sink*: read write size instead of returning it --- src/io/sink.c | 24 +++++++++++++++++++----- src/io/sink.h | 4 ++-- src/io/sink_sndfile.c | 31 ++++++++++++++++++++----------- src/io/sink_sndfile.h | 4 ++-- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/io/sink.c b/src/io/sink.c index 8d678264..c4129d24 100644 --- a/src/io/sink.c +++ b/src/io/sink.c @@ -22,18 +22,32 @@ #include "aubio_priv.h" #include "fvec.h" #include "io/sink.h" +#ifdef __APPLE__ +#include "io/sink_apple_audio.h" +#endif /* __APPLE__ */ +#ifdef HAVE_SNDFILE +#include "io/sink_sndfile.h" +#endif struct _aubio_sink_t { - uint_t hopsize; - uint_t samplerate; + void *sink; }; -aubio_sink_t * new_aubio_sink(char_t * uri, uint_t hop_size, uint_t samplerate) { +aubio_sink_t * new_aubio_sink(char_t * uri, uint_t samplerate) { aubio_sink_t * s = AUBIO_NEW(aubio_sink_t); - return s; +#ifdef __APPLE__ + s->sink = (void *)new_aubio_sink_apple_audio(uri, samplerate); + if (s->sink) return s; +#else /* __APPLE__ */ +#if HAVE_SNDFILE + s->sink = (void *)new_aubio_sink_sndfile(uri, samplerate); + if (s->sink) return s; +#endif /* HAVE_SNDFILE */ +#endif /* __APPLE__ */ + if (s->sink == NULL) { AUBIO_FREE(s); return NULL; } } -void aubio_sink_do(aubio_sink_t * s, fvec_t * write_data, uint_t * written) { +void aubio_sink_do(aubio_sink_t * s, fvec_t * write_data, uint_t write) { } void del_aubio_sink(aubio_sink_t * s) { diff --git a/src/io/sink.h b/src/io/sink.h index e330015f..f1403baf 100644 --- a/src/io/sink.h +++ b/src/io/sink.h @@ -32,8 +32,8 @@ extern "C" { */ typedef struct _aubio_sink_t aubio_sink_t; -aubio_sink_t * new_aubio_sink(char_t * method, uint_t hop_size, uint_t samplerate); -void aubio_sink_do(aubio_sink_t * s, fvec_t * write_data, uint_t * written); +aubio_sink_t * new_aubio_sink(char_t * method, uint_t samplerate); +void aubio_sink_do(aubio_sink_t * s, fvec_t * write_data, uint_t written); void del_aubio_sink(aubio_sink_t * s); #ifdef __cplusplus diff --git a/src/io/sink_sndfile.c b/src/io/sink_sndfile.c index 155af55b..f992baf9 100644 --- a/src/io/sink_sndfile.c +++ b/src/io/sink_sndfile.c @@ -33,7 +33,6 @@ #define MAX_SIZE 4096 struct _aubio_sink_sndfile_t { - uint_t hop_size; uint_t samplerate; uint_t channels; char_t *path; @@ -42,7 +41,7 @@ struct _aubio_sink_sndfile_t { smpl_t *scratch_data; }; -aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate, uint_t hop_size) { +aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate) { aubio_sink_sndfile_t * s = AUBIO_NEW(aubio_sink_sndfile_t); if (path == NULL) { @@ -50,8 +49,8 @@ aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate, return NULL; } - s->hop_size = hop_size; s->samplerate = samplerate; + s->max_size = MAX_SIZE; s->channels = 1; s->path = path; @@ -71,11 +70,11 @@ aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate, return NULL; } - s->scratch_size = s->hop_size*s->channels; + s->scratch_size = s->max-size*s->channels; /* allocate data for de/interleaving reallocated when needed. */ if (s->scratch_size >= MAX_SIZE * MAX_CHANNELS) { AUBIO_ERR("%d x %d exceeds maximum aubio_sink_sndfile buffer size %d\n", - s->hop_size, s->channels, MAX_CHANNELS * MAX_CHANNELS); + s->max_size, s->channels, MAX_CHANNELS * MAX_CHANNELS); return NULL; } s->scratch_data = AUBIO_ARRAY(float,s->scratch_size); @@ -83,21 +82,31 @@ aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * path, uint_t samplerate, return s; } -void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write, uint_t * written){ +void aubio_sink_sndfile_do(aubio_sink_sndfile_t *s, fvec_t * write_data, uint_t write){ sf_count_t written_frames = 0; int i, j, channels = s->channels; - int nsamples = channels*(*written); + int nsamples = channels*write; smpl_t *pwrite; + if (write > s->max_size) { + write = s->max_size; + AUBIO_WRN("trying to write %d frames, but only %d can be written at a time", + write, s->max_frames); + } + /* interleaving data */ for ( i = 0; i < channels; i++) { - pwrite = (smpl_t *)write->data; - for (j=0; j < s->hop_size; j++) { + pwrite = (smpl_t *)write_data->data; + for (j=0; j < write; j++) { s->scratch_data[channels*j+i] = pwrite[j]; } } - written_frames = sf_write_float (s->handle, s->scratch_data, nsamples); - *written = written_frames/channels; + + uint_t written = sf_write_float (s->handle, s->scratch_data, nsamples); + if (written/channels != write) { + AUBIO_WRN("trying to write %d frames to %s, but only %d could be written", + write, s->path, written); + } return; } diff --git a/src/io/sink_sndfile.h b/src/io/sink_sndfile.h index b9ee89d4..df913489 100644 --- a/src/io/sink_sndfile.h +++ b/src/io/sink_sndfile.h @@ -32,8 +32,8 @@ extern "C" { */ typedef struct _aubio_sink_sndfile_t aubio_sink_sndfile_t; -aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * method, uint_t hop_size, uint_t samplerate); -void aubio_sink_sndfile_do(aubio_sink_sndfile_t * s, fvec_t * write_data, uint_t * written); +aubio_sink_sndfile_t * new_aubio_sink_sndfile(char_t * method, uint_t samplerate); +void aubio_sink_sndfile_do(aubio_sink_sndfile_t * s, fvec_t * write_data, uint_t write); void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s); #ifdef __cplusplus -- 2.26.2