python/tests/test_pitchshift.py: run in a few modes
authorPaul Brossier <piem@piem.org>
Tue, 20 Sep 2016 23:32:57 +0000 (01:32 +0200)
committerPaul Brossier <piem@piem.org>
Tue, 20 Sep 2016 23:32:57 +0000 (01:32 +0200)
python/tests/test_pitchshift.py

index 27b675a..37041bd 100755 (executable)
@@ -1,13 +1,15 @@
 #! /usr/bin/env python
 
 from numpy.testing import TestCase
+from nose2.tools import params
+import numpy as np
 import aubio
 
 class aubio_pitchshift(TestCase):
 
     def setUp(self):
         try:
-            self.o = aubio.pitchshift()
+            self.o = aubio.pitchshift(hop_size = 128)
         except RuntimeError as e:
             self.skipTest("creating aubio.pitchshift failed (recompile with rubberband?)")
 
@@ -16,10 +18,10 @@ class aubio_pitchshift(TestCase):
         self.assertEqual(self.o.get_transpose(), 0)
 
     def test_on_zeros(self):
-        test_length = 20000
+        test_length = self.o.hop_size * 100
         read = 0
         # test on zeros
-        vec = aubio.fvec(512)
+        vec = aubio.fvec(self.o.hop_size)
         transpose_range = 24
         while read < test_length:
             # transpose the samples
@@ -33,6 +35,23 @@ class aubio_pitchshift(TestCase):
             self.o.set_transpose(transpose)
             read += len(vec)
 
+    def test_on_ones(self):
+        test_length = self.o.hop_size * 100
+        read = 0
+        # test on zeros
+        vec = aubio.fvec(self.o.hop_size) + 1
+        transpose_range = 1.24
+        while read < test_length:
+            # transpose the samples
+            out = self.o(vec)
+            # position in the file (between 0. and 1.)
+            percent_read = read / float(test_length)
+            # variable transpose rate (in semitones)
+            transpose = 2 * transpose_range * percent_read - transpose_range
+            # set transpose rate
+            self.o.set_transpose(transpose)
+            read += len(vec)
+
     def test_transpose_too_high(self):
         with self.assertRaises(ValueError):
             self.o.set_transpose(24.3)
@@ -41,6 +60,34 @@ class aubio_pitchshift(TestCase):
         with self.assertRaises(ValueError):
             self.o.set_transpose(-24.3)
 
+
+class aubio_pitchshift_testruns(TestCase):
+
+    @params(
+            ("default",     1.2,  128,  44100),
+            ("crispness:0", 0.43,  64,   8000),
+            ("crispness:3", 0.53, 256,   8000),
+            ("crispness:3", 1.53, 512,   8000),
+            ("crispness:6", 2.3, 4096, 192000),
+            )
+    def test_run_with_params(self, mode, pitchscale, hop_size, samplerate):
+        self.o = aubio.pitchshift(mode, pitchscale, hop_size, samplerate)
+        test_length = self.o.hop_size * 50
+        read = 0
+        # test on random
+        vec = np.random.rand(self.o.hop_size).astype(aubio.float_type)
+        transpose_range = self.o.get_transpose()
+        while read < test_length:
+            # transpose the samples
+            out = self.o(vec)
+            # position in the file (between 0. and 1.)
+            percent_read = read / float(test_length)
+            # variable transpose rate (in semitones)
+            transpose =  transpose_range - 2 * transpose_range * percent_read
+            # set transpose rate
+            self.o.set_transpose(transpose)
+            read += len(vec)
+
 if __name__ == '__main__':
     from nose2 import main
     main()