shithub: aubio

Download patch

ref: 8aed26d0f87080653ecfa3d43e21296c13580e94
parent: 11a1abeed4f2c07cef3779baea79f777ead327ed
author: Paul Brossier <piem@piem.org>
date: Sun Jul 15 08:18:28 EDT 2012

src/io/sink*: read write size instead of returning it

--- 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) {
--- a/src/io/sink.h
+++ b/src/io/sink.h
@@ -32,8 +32,8 @@
 */
 
 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
--- 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 @@
   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 @@
     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 @@
     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 @@
   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;
 }
 
--- a/src/io/sink_sndfile.h
+++ b/src/io/sink_sndfile.h
@@ -32,8 +32,8 @@
 */
 
 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