shithub: aubio

Download patch

ref: b4e143863574e3275dc895f5d193ee88ea1f5935
parent: c6e7ba1f0cc0958fffd30e6bef57da9e238a5763
author: Paul Brossier <piem@piem.org>
date: Mon Apr 25 12:45:22 EDT 2016

src/io/source_wavread.h: add _get_duration

--- a/src/io/source_wavread.c
+++ b/src/io/source_wavread.c
@@ -52,6 +52,8 @@
   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 * 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 @@
 
   // 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 @@
   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;
@@ -372,6 +378,13 @@
   s->eof = 0;
   s->read_index = 0;
   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) {
--- a/src/io/source_wavread.h
+++ b/src/io/source_wavread.h
@@ -125,6 +125,16 @@
 
 /**
 
+  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
--- a/tests/src/io/test-source_wavread.c
+++ b/tests/src/io/test-source_wavread.c
@@ -35,9 +35,12 @@
 
   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 @@
     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);