shithub: aubio

Download patch

ref: a9fd272915d06287c5e0b9384c383fb1468ddb60
parent: 082c88b71ad547d44e52ef4e888dd94d2fa756a8
author: Paul Brossier <piem@piem.org>
date: Sun Jan 26 11:16:59 EST 2014

src/io/sink*: add _close function, improve error messages

--- a/src/io/sink.c
+++ b/src/io/sink.c
@@ -38,6 +38,7 @@
 typedef uint_t (*aubio_sink_get_samplerate_t)(aubio_sink_t * s);
 typedef uint_t (*aubio_sink_get_channels_t)(aubio_sink_t * s);
 #endif
+typedef uint_t (*aubio_sink_close_t)(aubio_sink_t * s);
 typedef void (*del_aubio_sink_t)(aubio_sink_t * s);
 
 struct _aubio_sink_t { 
@@ -48,6 +49,7 @@
   aubio_sink_get_samplerate_t s_get_samplerate;
   aubio_sink_get_channels_t s_get_channels;
 #endif
+  aubio_sink_close_t s_close;
   del_aubio_sink_t s_del;
 };
 
@@ -57,6 +59,7 @@
   s->sink = (void *)new_aubio_sink_apple_audio(uri, samplerate);
   if (s->sink) {
     s->s_do = (aubio_sink_do_t)(aubio_sink_apple_audio_do);
+    s->s_close = (aubio_sink_close_t)(aubio_sink_apple_audio_close);
     s->s_del = (del_aubio_sink_t)(del_aubio_sink_apple_audio);
     return s;
   }
@@ -65,6 +68,7 @@
   s->sink = (void *)new_aubio_sink_sndfile(uri, samplerate);
   if (s->sink) {
     s->s_do = (aubio_sink_do_t)(aubio_sink_sndfile_do);
+    s->s_close = (aubio_sink_close_t)(aubio_sink_sndfile_close);
     s->s_del = (del_aubio_sink_t)(del_aubio_sink_sndfile);
     return s;
   }
@@ -73,11 +77,13 @@
   s->sink = (void *)new_aubio_sink_wavwrite(uri, samplerate);
   if (s->sink) {
     s->s_do = (aubio_sink_do_t)(aubio_sink_wavwrite_do);
+    s->s_close = (aubio_sink_close_t)(aubio_sink_wavwrite_close);
     s->s_del = (del_aubio_sink_t)(del_aubio_sink_wavwrite);
     return s;
   }
 #endif /* HAVE_WAVWRITE */
-  AUBIO_ERROR("sink: failed creating aubio sink with %s\n", uri);
+  AUBIO_ERROR("sink: failed creating %s with samplerate %dHz\n",
+      uri, samplerate);
   AUBIO_FREE(s);
   return NULL;
 }
@@ -84,6 +90,10 @@
 
 void aubio_sink_do(aubio_sink_t * s, fvec_t * write_data, uint_t write) {
   s->s_do((void *)s->sink, write_data, write);
+}
+
+uint_t aubio_sink_close(aubio_sink_t *s) {
+  return s->s_close((void *)s->sink);
 }
 
 void del_aubio_sink(aubio_sink_t * s) {
--- a/src/io/sink.h
+++ b/src/io/sink.h
@@ -63,9 +63,20 @@
 
 /**
 
+  close sink
+
+  \param s sink object, created with ::new_aubio_sink
+
+  \return 0 on success, non-zero on failure
+
+*/
+uint_t aubio_sink_close(aubio_sink_t * s);
+
+/**
+
   close sink and cleanup memory
 
-  \param s source object, created with ::new_aubio_source
+  \param s sink object, created with ::new_aubio_sink
 
 */
 void del_aubio_sink(aubio_sink_t * s);
--- a/src/io/sink_apple_audio.c
+++ b/src/io/sink_apple_audio.c
@@ -141,11 +141,10 @@
   return;
 }
 
-void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
+uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) {
   OSStatus err = noErr;
-  if (!s || !s->audioFile) {
-    AUBIO_ERR("sink_apple_audio: failed erasing\n");
-    return;
+  if (!s->audioFile) {
+    return AUBIO_FAIL;
   }
   err = ExtAudioFileDispose(s->audioFile);
   if (err) {
@@ -155,6 +154,11 @@
         getPrintableOSStatusError(errorstr, err));
   }
   s->audioFile = NULL;
+  return err;
+}
+
+void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
+  if (s->audioFile) aubio_sink_apple_audio_close (s);
   freeAudioBufferList(&s->bufferList);
   AUBIO_FREE(s);
   return;
--- a/src/io/sink_apple_audio.h
+++ b/src/io/sink_apple_audio.h
@@ -68,6 +68,17 @@
 
 /**
 
+  close sink
+
+  \param s sink_apple_audio object, created with ::new_aubio_sink_apple_audio
+
+  \return 0 on success, non-zero on failure
+
+*/
+uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s);
+
+/**
+
   close sink and cleanup memory
 
   \param s sink, created with ::new_aubio_sink_apple_audio
--- a/src/io/sink_sndfile.c
+++ b/src/io/sink_sndfile.c
@@ -115,11 +115,21 @@
   return;
 }
 
-void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
-  if (!s) return;
+uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) {
+  if (!s->handle) {
+    return AUBIO_FAIL;
+  }
   if (sf_close(s->handle)) {
     AUBIO_ERR("Error closing file %s: %s", s->path, sf_strerror (NULL));
+    return AUBIO_FAIL;
   }
+  s->handle = NULL;
+  return AUBIO_OK;
+}
+
+void del_aubio_sink_sndfile(aubio_sink_sndfile_t * s){
+  if (!s) return;
+  aubio_sink_sndfile_close(s);
   AUBIO_FREE(s->scratch_data);
   AUBIO_FREE(s);
 }
--- a/src/io/sink_sndfile.h
+++ b/src/io/sink_sndfile.h
@@ -67,6 +67,17 @@
 
 /**
 
+  close sink
+
+  \param s sink_sndfile object, created with ::new_aubio_sink_sndfile
+
+  \return 0 on success, non-zero on failure
+
+*/
+uint_t aubio_sink_sndfile_close(aubio_sink_sndfile_t * s);
+
+/**
+
   close sink and cleanup memory
 
   \param s sink, created with ::new_aubio_sink_sndfile
--- a/src/io/sink_wavwrite.c
+++ b/src/io/sink_wavwrite.c
@@ -152,8 +152,8 @@
   return s;
 
 beach:
-  AUBIO_ERR("sink_wavwrite: can not write %s at samplerate %dHz\n",
-      s->path, s->samplerate);
+  //AUBIO_ERR("sink_wavwrite: failed creating %s with samplerate %dHz\n",
+  //    s->path, s->samplerate);
   del_aubio_sink_wavwrite(s);
   return NULL;
 }
@@ -181,10 +181,10 @@
   return;
 }
 
-void aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
+uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s) {
   uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8;
   unsigned char buf[5];
-  if (!s->fid) return;
+  if (!s->fid) return AUBIO_FAIL;
   // ChunkSize
   fseek(s->fid, 4, SEEK_SET);
   fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);
@@ -196,6 +196,7 @@
     AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));
   }
   s->fid = NULL;
+  return AUBIO_OK;
 }
 
 void del_aubio_sink_wavwrite(aubio_sink_wavwrite_t * s){
--- a/src/io/sink_wavwrite.h
+++ b/src/io/sink_wavwrite.h
@@ -67,6 +67,17 @@
 
 /**
 
+  close sink
+
+  \param s sink_wavwrite object, create with ::new_aubio_sink_wavwrite
+
+  \return 0 on success, non-zero on failure
+
+*/
+uint_t aubio_sink_wavwrite_close(aubio_sink_wavwrite_t * s);
+
+/**
+
   close sink and cleanup memory
 
   \param s sink, created with ::new_aubio_sink_wavwrite