[source_sndfile] set error message when reading after close
[aubio.git] / src / io / source_sndfile.c
index eea2ffc..d2b98a5 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "fvec.h"
 #include "fmat.h"
+#include "ioutils.h"
 #include "source_sndfile.h"
 
 #include "temporal/resampler.h"
@@ -86,7 +87,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);
 
@@ -170,7 +170,18 @@ beach:
 void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uint_t * read){
   uint_t i,j, input_channels = s->input_channels;
   /* read from file into scratch_data */
-  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
+  uint_t length = aubio_source_validate_input_length("source_sndfile", s->path,
+      s->hop_size, read_data->length);
+  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data,
+      s->scratch_size);
+  if (!s->handle) {
+    AUBIO_ERR("source_sndfile: could not read from %s (file was closed)\n",
+        s->path);
+    *read = 0;
+    return;
+  }
+
+  uint_t read_length = read_samples / s->input_channels;
 
   /* where to store de-interleaved data */
   smpl_t *ptr_data;
@@ -180,11 +191,12 @@ void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uin
   } else
 #endif /* HAVE_SAMPLERATE */
   {
+    read_length = MIN(length, read_length);
     ptr_data = read_data->data;
   }
 
   /* de-interleaving and down-mixing data  */
-  for (j = 0; j < read_samples / input_channels; j++) {
+  for (j = 0; j < read_length; j++) {
     ptr_data[j] = 0;
     for (i = 0; i < input_channels; i++) {
       ptr_data[j] += s->scratch_data[input_channels*j+i];
@@ -198,20 +210,29 @@ void aubio_source_sndfile_do(aubio_source_sndfile_t * s, fvec_t * read_data, uin
   }
 #endif /* HAVE_SAMPLERATE */
 
-  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
+  *read = MIN(length, (uint_t)FLOOR(s->ratio * read_length + .5));
 
-  if (*read < s->hop_size) {
-    for (j = *read; j < s->hop_size; j++) {
-      read_data->data[j] = 0;
-    }
-  }
+  aubio_source_pad_output (read_data, *read);
 
 }
 
 void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_data, uint_t * read){
   uint_t i,j, input_channels = s->input_channels;
   /* do actual reading */
-  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data, s->scratch_size);
+  uint_t length = aubio_source_validate_input_length("source_sndfile", s->path,
+      s->hop_size, read_data->length);
+  uint_t channels = aubio_source_validate_input_channels("source_sndfile",
+      s->path, s->input_channels, read_data->height);
+  sf_count_t read_samples = aubio_sf_read_smpl (s->handle, s->scratch_data,
+      s->scratch_size);
+  if (!s->handle) {
+    AUBIO_ERR("source_sndfile: could not read from %s (file was closed)\n",
+        s->path);
+    *read = 0;
+    return;
+  }
+
+  uint_t read_length = read_samples / s->input_channels;
 
   /* where to store de-interleaved data */
   smpl_t **ptr_data;
@@ -221,34 +242,13 @@ void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_dat
   } else
 #endif /* HAVE_SAMPLERATE */
   {
+    read_length = MIN(read_length, length);
     ptr_data = read_data->data;
   }
 
-  if (read_data->height < input_channels) {
-    // destination matrix has less channels than the file; copy only first
-    // channels of the file, de-interleaving data
-    for (j = 0; j < read_samples / input_channels; j++) {
-      for (i = 0; i < read_data->height; i++) {
-        ptr_data[i][j] = s->scratch_data[j * input_channels + i];
-      }
-    }
-  } else {
-    // destination matrix has as many or more channels than the file; copy each
-    // channel from the file to the destination matrix, de-interleaving data
-    for (j = 0; j < read_samples / input_channels; j++) {
-      for (i = 0; i < input_channels; i++) {
-        ptr_data[i][j] = s->scratch_data[j * input_channels + i];
-      }
-    }
-  }
-
-  if (read_data->height > input_channels) {
-    // destination matrix has more channels than the file; copy last channel
-    // of the file to each additional channels, de-interleaving data
-    for (j = 0; j < read_samples / input_channels; j++) {
-      for (i = input_channels; i < read_data->height; i++) {
-        ptr_data[i][j] = s->scratch_data[j * input_channels + (input_channels - 1)];
-      }
+  for (j = 0; j < read_length; j++) {
+    for (i = 0; i < channels; i++) {
+      ptr_data[i][j] = s->scratch_data[j * input_channels + i];
     }
   }
 
@@ -265,16 +265,9 @@ void aubio_source_sndfile_do_multi(aubio_source_sndfile_t * s, fmat_t * read_dat
   }
 #endif /* HAVE_SAMPLERATE */
 
-  *read = (int)FLOOR(s->ratio * read_samples / input_channels + .5);
-
-  if (*read < s->hop_size) {
-    for (i = 0; i < read_data->height; i++) {
-      for (j = *read; j < s->hop_size; j++) {
-        read_data->data[i][j] = 0.;
-      }
-    }
-  }
+  *read = MIN(length, (uint_t)FLOOR(s->ratio * read_length + .5));
 
+  aubio_source_pad_multi_output(read_data, input_channels, *read);
 }
 
 uint_t aubio_source_sndfile_get_samplerate(aubio_source_sndfile_t * s) {
@@ -331,7 +324,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) {