src/io/source_sndfile.h: add _get_duration
authorPaul Brossier <piem@piem.org>
Mon, 25 Apr 2016 14:42:43 +0000 (16:42 +0200)
committerPaul Brossier <piem@piem.org>
Mon, 25 Apr 2016 14:42:43 +0000 (16:42 +0200)
src/io/source_sndfile.c
src/io/source_sndfile.h
tests/src/io/test-source_sndfile.c

index 7f88e71..00ea42a 100644 (file)
@@ -53,6 +53,7 @@ struct _aubio_source_sndfile_t {
   int input_samplerate;
   int input_channels;
   int input_format;
+  int duration;
 
   // resampling stuff
   smpl_t ratio;
@@ -105,6 +106,7 @@ aubio_source_sndfile_t * new_aubio_source_sndfile(const char_t * path, uint_t sa
   s->input_samplerate = sfinfo.samplerate;
   s->input_channels   = sfinfo.channels;
   s->input_format     = sfinfo.format;
+  s->duration         = sfinfo.frames;
 
   if (samplerate == 0) {
     s->samplerate = s->input_samplerate;
@@ -272,6 +274,13 @@ uint_t aubio_source_sndfile_get_channels(aubio_source_sndfile_t * s) {
   return s->input_channels;
 }
 
+uint_t aubio_source_sndfile_get_duration (const aubio_source_sndfile_t * s) {
+  if (s && s->duration) {
+    return s->duration;
+  }
+  return 0;
+}
+
 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);
index e382c3b..210a40d 100644 (file)
@@ -120,6 +120,16 @@ uint_t aubio_source_sndfile_seek (aubio_source_sndfile_t *s, uint_t pos);
 
 /**
 
+  get the duration of source object, in frames
+
+  \param s source object, created with ::new_aubio_source_sndfile
+  \return number of frames in file
+
+*/
+uint_t aubio_source_sndfile_get_duration (const aubio_source_sndfile_t *s);
+
+/**
+
   close source
 
   \param s source object, created with ::new_aubio_source_sndfile
index ed227fd..6dfff59 100644 (file)
@@ -38,6 +38,8 @@ int main (int argc, char **argv)
   if (!s) { err = 1; goto beach; }
   fvec_t *vec = new_fvec(hop_size);
 
+  uint_t n_frames_expected = aubio_source_sndfile_get_duration(s);
+
   samplerate = aubio_source_sndfile_get_samplerate(s);
 
   do {
@@ -46,8 +48,9 @@ int main (int argc, char **argv)
     n_frames += read;
   } while ( read == hop_size );
 
-  PRINT_MSG("read %d frames at %dHz (%d blocks) from %s\n", n_frames, samplerate,
-    n_frames / hop_size, source_path);
+  PRINT_MSG("read %d frames (expected %d) at %dHz (%d blocks) from %s\n",
+            n_frames, n_frames_expected, samplerate, n_frames / hop_size,
+            source_path);
 
   del_fvec (vec);
   del_aubio_source_sndfile (s);