[sink_wavwrite] check fseek and fwrite return values
[aubio.git] / src / io / source_sndfile.c
index 2f09d13..23cd73c 100644 (file)
@@ -86,7 +86,6 @@ aubio_source_sndfile_t * new_aubio_source_sndfile(const char_t * path, uint_t sa
   s->hop_size = hop_size;
   s->channels = 1;
 
-  if (s->path) AUBIO_FREE(s->path);
   s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
   strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
 
@@ -294,7 +293,18 @@ uint_t aubio_source_sndfile_get_duration (const aubio_source_sndfile_t * s) {
 
 uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
   uint_t resampled_pos = (uint_t)ROUND(pos / s->ratio);
-  sf_count_t sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
+  sf_count_t sf_ret;
+  if (s->handle == NULL) {
+    AUBIO_ERR("source_sndfile: failed seeking in %s (file not opened?)\n",
+        s->path);
+    return AUBIO_FAIL;
+  }
+  if ((sint_t)pos < 0) {
+    AUBIO_ERR("source_sndfile: could not seek %s at %d (seeking position"
+       " should be >= 0)\n", s->path, pos);
+    return AUBIO_FAIL;
+  }
+  sf_ret = sf_seek (s->handle, resampled_pos, SEEK_SET);
   if (sf_ret == -1) {
     AUBIO_ERR("source_sndfile: Failed seeking %s at %d: %s\n", s->path, pos, sf_strerror (NULL));
     return AUBIO_FAIL;
@@ -309,7 +319,7 @@ uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t * s, uint_t pos) {
 
 uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
   if (!s->handle) {
-    return AUBIO_FAIL;
+    return AUBIO_OK;
   }
   if(sf_close(s->handle)) {
     AUBIO_ERR("source_sndfile: Error closing file %s: %s\n", s->path, sf_strerror (NULL));
@@ -320,7 +330,7 @@ uint_t aubio_source_sndfile_close (aubio_source_sndfile_t *s) {
 }
 
 void del_aubio_source_sndfile(aubio_source_sndfile_t * s){
-  if (!s) return;
+  AUBIO_ASSERT(s);
   aubio_source_sndfile_close(s);
 #ifdef HAVE_SAMPLERATE
   if (s->resamplers != NULL) {