shithub: aubio

Download patch

ref: e1bfde5b4ebe6465f95565b3f3ba978db22bbf30
parent: 143cc436f4a9003e01c202a41fd63d1d07649235
author: Paul Brossier <piem@piem.org>
date: Wed Mar 6 10:25:23 EST 2013

tests/test_{source,sink}.py: add sink, improve source, use sounds in python/tests/sounds

--- /dev/null
+++ b/python/tests/test_sink.py
@@ -1,0 +1,37 @@
+#! /usr/bin/env python
+
+from numpy.testing import TestCase, assert_equal, assert_almost_equal
+from aubio import fvec, source, sink
+from numpy import array
+from utils import list_all_sounds
+
+list_of_sounds = list_all_sounds('sounds')
+path = None
+
+class aubio_sink_test_case(TestCase):
+
+    def setUp(self):
+        if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'')
+
+    def test_read(self):
+        for path in list_of_sounds:
+            for samplerate, hop_size in zip([0, 44100, 8000, 32000], [512, 1024, 64, 256]):
+                f = source(path, samplerate, hop_size)
+                if samplerate == 0: samplerate = f.samplerate
+                g = sink('/tmp/f.wav', samplerate)
+                total_frames = 0
+                while True:
+                    vec, read = f()
+                    #print g(vec, read)
+                    total_frames += read
+                    if read < f.hop_size: break
+                print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
+                print "(", total_frames, "frames", "in",
+                print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
+                print "from", f.uri,
+                print "to", g.uri
+
+if __name__ == '__main__':
+  from unittest import main
+  main()
+
--- a/python/tests/test_source.py
+++ b/python/tests/test_source.py
@@ -3,25 +3,67 @@
 from numpy.testing import TestCase, assert_equal, assert_almost_equal
 from aubio import fvec, source
 from numpy import array
+from utils import list_all_sounds
 
-path = "/Users/piem/archives/sounds/loops/drum_Chocolate_Milk_-_Ation_Speaks_Louder_Than_Words.wav"
+list_of_sounds = list_all_sounds('sounds')
+path = None
 
-class aubio_filter_test_case(TestCase):
+class aubio_source_test_case(TestCase):
 
-  def test_members(self):
-    f = source(path)
-    print dir(f)
+    def setUp(self):
+        if not len(list_of_sounds): self.skipTest('add some sound files in \'python/tests/sounds\'')
 
-  def test_read(self):
-    f = source(path)
-    total_frames = 0
-    while True:
-      vec, read = f()
-      total_frames += read
-      if read < f.hop_size: break
-    print "read", total_frames / float(f.samplerate), " seconds from", path
+    def read_from_sink(self, f):
+        total_frames = 0
+        while True:
+            vec, read = f()
+            total_frames += read
+            if read < f.hop_size: break
+        print "read", "%.2fs" % (total_frames / float(f.samplerate) ),
+        print "(", total_frames, "frames", "in",
+        print total_frames / f.hop_size, "blocks", "at", "%dHz" % f.samplerate, ")",
+        print "from", f.uri
 
-if __name__ == '__main__':
-  from unittest import main
-  main()
+    def test_samplerate_hopsize(self):
+        for p in list_of_sounds:
+            for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]):
+                f = source(p, samplerate, hop_size)
+                assert f.samplerate != 0
+                self.read_from_sink(f)
 
+    def test_samplerate_none(self):
+        for p in list_of_sounds:
+            f = source(p)
+            assert f.samplerate != 0
+            self.read_from_sink(f)
+
+    def test_samplerate_0(self):
+        for p in list_of_sounds:
+            f = source(p, 0)
+            assert f.samplerate != 0
+            self.read_from_sink(f)
+
+    def test_wrong_samplerate(self):
+        for p in list_of_sounds:
+            try:
+                f = source(p, -1)
+            except Exception, e:
+                print e
+            else:
+                self.fail('does not fail with wrong samplerate')
+
+    def test_wrong_hop_size(self):
+        for p in list_of_sounds:
+            f = source(p, 0, -1)
+            print f.hop_size
+
+    def test_zero_hop_size(self):
+        for p in list_of_sounds:
+            f = source(p, 0, 0)
+            assert f.samplerate != 0
+            assert f.hop_size != 0
+            self.read_from_sink(f)
+
+if __name__ == '__main__':
+    from unittest import main
+    main()
--- a/python/tests/utils.py
+++ b/python/tests/utils.py
@@ -1,9 +1,13 @@
 #! /usr/bin/env python
 
 def array_from_text_file(filename, dtype = 'float'):
-  import os.path
-  from numpy import array
-  filename = os.path.join(os.path.dirname(__file__), filename)
-  return array([line.split() for line in open(filename).readlines()], 
-      dtype = dtype)
+    import os.path
+    from numpy import array
+    filename = os.path.join(os.path.dirname(__file__), filename)
+    return array([line.split() for line in open(filename).readlines()],
+        dtype = dtype)
 
+def list_all_sounds(rel_dir):
+    import os.path, glob
+    datadir = os.path.join(os.path.dirname(__file__), rel_dir)
+    return glob.glob(os.path.join(datadir,'*.*'))