tests/test_{source,sink}.py: add sink, improve source, use sounds in python/tests...
authorPaul Brossier <piem@piem.org>
Wed, 6 Mar 2013 20:25:23 +0000 (15:25 -0500)
committerPaul Brossier <piem@piem.org>
Wed, 6 Mar 2013 20:25:23 +0000 (15:25 -0500)
python/tests/test_sink.py [new file with mode: 0755]
python/tests/test_source.py
python/tests/utils.py

diff --git a/python/tests/test_sink.py b/python/tests/test_sink.py
new file mode 100755 (executable)
index 0000000..21723de
--- /dev/null
@@ -0,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()
+
index ae3b79b..d26e4e7 100755 (executable)
@@ -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()
index 28c076e..fa4dc44 100644 (file)
@@ -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,'*.*'))