python/ext/py-source.c: added duration
authorPaul Brossier <piem@piem.org>
Mon, 25 Apr 2016 16:05:02 +0000 (18:05 +0200)
committerPaul Brossier <piem@piem.org>
Mon, 25 Apr 2016 16:05:02 +0000 (18:05 +0200)
python/ext/py-source.c
python/tests/test_source.py

index a3096a1..6553b40 100644 (file)
@@ -8,6 +8,7 @@ typedef struct
   uint_t samplerate;
   uint_t channels;
   uint_t hop_size;
+  uint_t duration;
   fvec_t *read_to;
   fmat_t *mread_to;
 } Py_source;
@@ -146,6 +147,7 @@ Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
   if (self->channels == 0) {
     self->channels = aubio_source_get_channels ( self->o );
   }
+  self->duration = aubio_source_get_duration ( self->o );
 
   self->read_to = new_fvec(self->hop_size);
   self->mread_to = new_fmat (self->channels, self->hop_size);
@@ -226,6 +228,8 @@ static PyMemberDef Py_source_members[] = {
     "number of channels found in the source"},
   {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY,
     "number of consecutive frames that will be read at each do or do_multi call"},
+  {"duration", T_INT, offsetof (Py_source, duration), READONLY,
+    "total number of frames in the source (estimated)"},
   { NULL } // sentinel
 };
 
index 62b1820..48bdc67 100755 (executable)
@@ -99,6 +99,17 @@ class aubio_source_read_test_case(aubio_source_test_case_base):
             b = self.read_from_source(f)
             assert a == b + c
 
+    def test_duration(self):
+        for p in list_of_sounds:
+            total_frames = 0
+            f = source(p)
+            duration = f.duration
+            while True:
+                vec, read = f()
+                total_frames += read
+                if read < f.hop_size: break
+            self.assertEqual(duration, total_frames)
+
 class aubio_source_readmulti_test_case(aubio_source_read_test_case):
 
     def read_from_source(self, f):