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

index d958fbe..d1b1f3a 100644 (file)
@@ -52,6 +52,8 @@ struct _aubio_source_wavread_t {
   uint_t read_index;
   uint_t eof;
 
+  uint_t duration;
+
   size_t seek_start;
 
   unsigned char *short_output;
@@ -71,7 +73,7 @@ aubio_source_wavread_t * new_aubio_source_wavread(const char_t * path, uint_t sa
   aubio_source_wavread_t * s = AUBIO_NEW(aubio_source_wavread_t);
   size_t bytes_read = 0, bytes_expected = 44;
   unsigned char buf[5];
-  unsigned int format, channels, sr, byterate, blockalign, bitspersample;//, data_size;
+  unsigned int format, channels, sr, byterate, blockalign, duration, bitspersample;//, data_size;
 
   if (path == NULL) {
     AUBIO_ERR("source_wavread: Aborted opening null path\n");
@@ -215,6 +217,8 @@ aubio_source_wavread_t * new_aubio_source_wavread(const char_t * path, uint_t sa
 
   // Subchunk2Size
   bytes_read += fread(buf, 1, 4, s->fid);
+  duration = read_little_endian(buf, 4) / blockalign;
+
   //data_size = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
   //AUBIO_MSG("found %d frames in %s\n", 8 * data_size / bitspersample / channels, s->path);
 
@@ -235,6 +239,8 @@ aubio_source_wavread_t * new_aubio_source_wavread(const char_t * path, uint_t sa
   s->blockalign= blockalign;
   s->bitspersample = bitspersample;
 
+  s->duration = duration;
+
   s->short_output = (unsigned char *)calloc(s->blockalign, AUBIO_WAVREAD_BUFSIZE);
   s->read_index = 0;
   s->read_samples = 0;
@@ -374,6 +380,13 @@ uint_t aubio_source_wavread_seek (aubio_source_wavread_t * s, uint_t pos) {
   return AUBIO_OK;
 }
 
+uint_t aubio_source_wavread_get_duration (const aubio_source_wavread_t * s) {
+  if (s && s->duration) {
+    return s->duration;
+  }
+  return 0;
+}
+
 uint_t aubio_source_wavread_close (aubio_source_wavread_t * s) {
   if (!s->fid) {
     return AUBIO_FAIL;
index a0ec141..65f752f 100644 (file)
@@ -125,6 +125,16 @@ uint_t aubio_source_wavread_seek (aubio_source_wavread_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_wavread_get_duration (const aubio_source_wavread_t *s);
+
+/**
+
   close source
 
   \param s source object, created with ::new_aubio_source_wavread
index 50da589..23511ca 100644 (file)
@@ -35,9 +35,12 @@ int main (int argc, char **argv)
 
   aubio_source_wavread_t * s =
     new_aubio_source_wavread(source_path, samplerate, hop_size);
+
   if (!s) { err = 1; goto beach; }
   fvec_t *vec = new_fvec(hop_size);
 
+  uint_t n_frames_expected = aubio_source_wavread_get_duration(s);
+
   samplerate = aubio_source_wavread_get_samplerate(s);
 
   do {
@@ -46,8 +49,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_wavread (s);