[fe6a4cdd] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[24dfc3d] | 3 | from numpy.testing import TestCase |
---|
[5f57ea9] | 4 | from _tools import parametrize, skipTest |
---|
[a0e0f56] | 5 | import numpy as np |
---|
[fe6a4cdd] | 6 | import aubio |
---|
| 7 | |
---|
| 8 | class aubio_pitchshift(TestCase): |
---|
| 9 | |
---|
| 10 | def setUp(self): |
---|
[60cbfe8] | 11 | try: |
---|
[a0e0f56] | 12 | self.o = aubio.pitchshift(hop_size = 128) |
---|
[60cbfe8] | 13 | except RuntimeError as e: |
---|
[7d01fdf] | 14 | self.skipTest("creating aubio.pitchshift {}".format(e)) |
---|
[fe6a4cdd] | 15 | |
---|
| 16 | def test_default_creation(self): |
---|
| 17 | self.assertEqual(self.o.get_pitchscale(), 1) |
---|
| 18 | self.assertEqual(self.o.get_transpose(), 0) |
---|
| 19 | |
---|
| 20 | def test_on_zeros(self): |
---|
[a0e0f56] | 21 | test_length = self.o.hop_size * 100 |
---|
[fe6a4cdd] | 22 | read = 0 |
---|
| 23 | # test on zeros |
---|
[a0e0f56] | 24 | vec = aubio.fvec(self.o.hop_size) |
---|
[fe6a4cdd] | 25 | transpose_range = 24 |
---|
| 26 | while read < test_length: |
---|
| 27 | # transpose the samples |
---|
| 28 | out = self.o(vec) |
---|
| 29 | self.assertTrue((out == 0).all()) |
---|
| 30 | # position in the file (between 0. and 1.) |
---|
| 31 | percent_read = read / float(test_length) |
---|
| 32 | # variable transpose rate (in semitones) |
---|
| 33 | transpose = 2 * transpose_range * percent_read - transpose_range |
---|
| 34 | # set transpose rate |
---|
| 35 | self.o.set_transpose(transpose) |
---|
| 36 | read += len(vec) |
---|
| 37 | |
---|
[a0e0f56] | 38 | def test_on_ones(self): |
---|
| 39 | test_length = self.o.hop_size * 100 |
---|
| 40 | read = 0 |
---|
| 41 | # test on zeros |
---|
| 42 | vec = aubio.fvec(self.o.hop_size) + 1 |
---|
| 43 | transpose_range = 1.24 |
---|
| 44 | while read < test_length: |
---|
| 45 | # transpose the samples |
---|
| 46 | out = self.o(vec) |
---|
| 47 | # position in the file (between 0. and 1.) |
---|
| 48 | percent_read = read / float(test_length) |
---|
| 49 | # variable transpose rate (in semitones) |
---|
| 50 | transpose = 2 * transpose_range * percent_read - transpose_range |
---|
| 51 | # set transpose rate |
---|
| 52 | self.o.set_transpose(transpose) |
---|
| 53 | read += len(vec) |
---|
| 54 | |
---|
[fe6a4cdd] | 55 | def test_transpose_too_high(self): |
---|
| 56 | with self.assertRaises(ValueError): |
---|
| 57 | self.o.set_transpose(24.3) |
---|
| 58 | |
---|
| 59 | def test_transpose_too_low(self): |
---|
| 60 | with self.assertRaises(ValueError): |
---|
| 61 | self.o.set_transpose(-24.3) |
---|
| 62 | |
---|
[4292013] | 63 | class aubio_pitchshift_wrong_params(TestCase): |
---|
| 64 | |
---|
| 65 | def test_wrong_transpose(self): |
---|
| 66 | with self.assertRaises(RuntimeError): |
---|
| 67 | aubio.pitchshift("default", -123) |
---|
[a0e0f56] | 68 | |
---|
[0770148] | 69 | class Test_aubio_pitchshift_testruns(object): |
---|
[a0e0f56] | 70 | |
---|
[0770148] | 71 | run_args = ['mode', 'pitchscale', 'hop_size', 'samplerate'] |
---|
| 72 | run_values = [ |
---|
[a0e0f56] | 73 | ("default", 1.2, 128, 44100), |
---|
| 74 | ("crispness:0", 0.43, 64, 8000), |
---|
| 75 | ("crispness:3", 0.53, 256, 8000), |
---|
| 76 | ("crispness:3", 1.53, 512, 8000), |
---|
| 77 | ("crispness:6", 2.3, 4096, 192000), |
---|
[0770148] | 78 | ] |
---|
| 79 | |
---|
| 80 | @parametrize(run_args, run_values) |
---|
[a0e0f56] | 81 | def test_run_with_params(self, mode, pitchscale, hop_size, samplerate): |
---|
[c2f7db8] | 82 | try: |
---|
| 83 | self.o = aubio.pitchshift(mode, pitchscale, hop_size, samplerate) |
---|
| 84 | except RuntimeError as e: |
---|
[5f57ea9] | 85 | skipTest("failed creating pitchshift ({})".format(e)) |
---|
[a0e0f56] | 86 | test_length = self.o.hop_size * 50 |
---|
| 87 | read = 0 |
---|
| 88 | # test on random |
---|
| 89 | vec = np.random.rand(self.o.hop_size).astype(aubio.float_type) |
---|
| 90 | transpose_range = self.o.get_transpose() |
---|
| 91 | while read < test_length: |
---|
| 92 | # transpose the samples |
---|
| 93 | out = self.o(vec) |
---|
| 94 | # position in the file (between 0. and 1.) |
---|
| 95 | percent_read = read / float(test_length) |
---|
| 96 | # variable transpose rate (in semitones) |
---|
| 97 | transpose = transpose_range - 2 * transpose_range * percent_read |
---|
| 98 | # set transpose rate |
---|
| 99 | self.o.set_transpose(transpose) |
---|
| 100 | read += len(vec) |
---|
| 101 | |
---|
[fe6a4cdd] | 102 | if __name__ == '__main__': |
---|
[0770148] | 103 | from _tools import run_module_suite |
---|
| 104 | run_module_suite() |
---|