Showing the changes to python/tests (25 of 230 files). Show all
Unified · Split
| @@ -0,0 +1 @@ | |||
| 1 | |||
| @@ -24,7 +24,7 @@ import sys · 23 unmodified lines | |||
| 24 | import time | 24 | import time |
| 25 | import os.path | 25 | import os.path |
| 26 | import numpy | 26 | import numpy |
| 27 | from utils import array_from_text_file, array_from_yaml_file | 27 | from .utils import array_from_text_file, array_from_yaml_file |
| 28 | from aubio import source, pitch, freqtomidi | 28 | from aubio import source, pitch, freqtomidi |
| 29 | 29 | ||
| 30 | start = time.time() | 30 | start = time.time() |
| @@ -9,6 +9,11 @@ class aubiomodule_test_case(TestCase): · 8 unmodified lines | |||
| 9 | """ try importing aubio """ | 9 | """ try importing aubio """ |
| 10 | import aubio | 10 | import aubio |
| 11 | 11 | ||
| 12 | def test_version(self): | ||
| 13 | """ test aubio.version """ | ||
| 14 | import aubio | ||
| 15 | self.assertEqual('0', aubio.version[0]) | ||
| 16 | |||
| 12 | if __name__ == '__main__': | 17 | if __name__ == '__main__': |
| 13 | main() | 18 | main() |
| 14 | 19 | ||
| @@ -0,0 +1,34 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | import aubio.cmd | ||
| 4 | from nose2 import main | ||
| 5 | from numpy.testing import TestCase | ||
| 6 | |||
| 7 | class aubio_cmd(TestCase): | ||
| 8 | |||
| 9 | def setUp(self): | ||
| 10 | self.a_parser = aubio.cmd.aubio_parser() | ||
| 11 | |||
| 12 | def test_default_creation(self): | ||
| 13 | try: | ||
| 14 | assert self.a_parser.parse_args(['-V']).show_version | ||
| 15 | except SystemExit: | ||
| 16 | url = 'https://bugs.python.org/issue9253' | ||
| 17 | self.skipTest('subcommand became optional in py3, see %s' % url) | ||
| 18 | |||
| 19 | class aubio_cmd_utils(TestCase): | ||
| 20 | |||
| 21 | def test_samples2seconds(self): | ||
| 22 | self.assertEqual(aubio.cmd.samples2seconds(3200, 32000), | ||
| 23 | "0.100000\t") | ||
| 24 | |||
| 25 | def test_samples2milliseconds(self): | ||
| 26 | self.assertEqual(aubio.cmd.samples2milliseconds(3200, 32000), | ||
| 27 | "100.000000\t") | ||
| 28 | |||
| 29 | def test_samples2samples(self): | ||
| 30 | self.assertEqual(aubio.cmd.samples2samples(3200, 32000), | ||
| 31 | "3200\t") | ||
| 32 | |||
| 33 | if __name__ == '__main__': | ||
| 34 | main() | ||
| @@ -0,0 +1,16 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | import aubio.cut | ||
| 4 | from nose2 import main | ||
| 5 | from numpy.testing import TestCase | ||
| 6 | |||
| 7 | class aubio_cut(TestCase): | ||
| 8 | |||
| 9 | def setUp(self): | ||
| 10 | self.a_parser = aubio.cut.aubio_cut_parser() | ||
| 11 | |||
| 12 | def test_default_creation(self): | ||
| 13 | assert self.a_parser.parse_args(['-v']).verbose | ||
| 14 | |||
| 15 | if __name__ == '__main__': | ||
| 16 | main() | ||
| @@ -0,0 +1,68 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | |||
| 4 | import numpy as np | ||
| 5 | from numpy.testing import TestCase, assert_almost_equal | ||
| 6 | import aubio | ||
| 7 | |||
| 8 | precomputed_arange = [ 9.89949512, -6.44232273, 0., -0.67345482, 0., | ||
| 9 | -0.20090288, 0., -0.05070186] | ||
| 10 | |||
| 11 | precomputed_some_ones = [ 4.28539848, 0.2469689, -0.14625292, -0.58121818, | ||
| 12 | -0.83483052, -0.75921834, -0.35168475, 0.24087936, | ||
| 13 | 0.78539824, 1.06532764, 0.97632152, 0.57164496, 0.03688532, | ||
| 14 | -0.39446154, -0.54619485, -0.37771079] | ||
| 15 | |||
| 16 | class aubio_dct(TestCase): | ||
| 17 | |||
| 18 | def test_init(self): | ||
| 19 | """ test that aubio.dct() is created with expected size """ | ||
| 20 | a_dct = aubio.dct() | ||
| 21 | self.assertEqual(a_dct.size, 1024) | ||
| 22 | |||
| 23 | def test_arange(self): | ||
| 24 | """ test that dct(arange(8)) is computed correctly | ||
| 25 | |||
| 26 | >>> from scipy.fftpack import dct | ||
| 27 | >>> a_in = np.arange(8).astype(aubio.float_type) | ||
| 28 | >>> precomputed = dct(a_in, norm='ortho') | ||
| 29 | """ | ||
| 30 | N = len(precomputed_arange) | ||
| 31 | a_dct = aubio.dct(8) | ||
| 32 | a_in = np.arange(8).astype(aubio.float_type) | ||
| 33 | a_expected = aubio.fvec(precomputed_arange) | ||
| 34 | assert_almost_equal(a_dct(a_in), a_expected, decimal=5) | ||
| 35 | |||
| 36 | def test_some_ones(self): | ||
| 37 | """ test that dct(somevector) is computed correctly """ | ||
| 38 | a_dct = aubio.dct(16) | ||
| 39 | a_in = np.ones(16).astype(aubio.float_type) | ||
| 40 | a_in[1] = 0 | ||
| 41 | a_in[3] = np.pi | ||
| 42 | a_expected = aubio.fvec(precomputed_some_ones) | ||
| 43 | assert_almost_equal(a_dct(a_in), a_expected, decimal=6) | ||
| 44 | |||
| 45 | def test_reconstruction(self): | ||
| 46 | """ test that some_ones vector can be recontructed """ | ||
| 47 | a_dct = aubio.dct(16) | ||
| 48 | a_in = np.ones(16).astype(aubio.float_type) | ||
| 49 | a_in[1] = 0 | ||
| 50 | a_in[3] = np.pi | ||
| 51 | a_dct_in = a_dct(a_in) | ||
| 52 | a_dct_reconstructed = a_dct.rdo(a_dct_in) | ||
| 53 | assert_almost_equal(a_dct_reconstructed, a_in, decimal=6) | ||
| 54 | |||
| 55 | def test_negative_size(self): | ||
| 56 | """ test that creation fails with a negative size """ | ||
| 57 | with self.assertRaises(ValueError): | ||
| 58 | aubio.dct(-1) | ||
| 59 | |||
| 60 | def test_wrong_size(self): | ||
| 61 | """ test that creation fails with a non power-of-two size """ | ||
| 62 | # supports for non 2** fft sizes only when compiled with fftw3 | ||
| 63 | size = 13 | ||
| 64 | try: | ||
| 65 | with self.assertRaises(RuntimeError): | ||
| 66 | aubio.dct(size) | ||
| 67 | except AssertionError: | ||
| 68 | self.skipTest('creating aubio.dct with size %d did not fail' % size) | ||
| @@ -33,7 +33,14 @@ class aubio_fft_test_case(TestCase): · 32 unmodified lines | |||
| 33 | f = fft (win_s) | 33 | f = fft (win_s) |
| 34 | fftgrain = f (timegrain) | 34 | fftgrain = f (timegrain) |
| 35 | assert_equal ( fftgrain.norm, 0 ) | 35 | assert_equal ( fftgrain.norm, 0 ) |
| 36 | assert_equal ( fftgrain.phas, 0 ) | 36 | try: |
| 37 | assert_equal ( fftgrain.phas, 0 ) | ||
| 38 | except AssertionError: | ||
| 39 | assert_equal (fftgrain.phas[fftgrain.phas > 0], +pi) | ||
| 40 | assert_equal (fftgrain.phas[fftgrain.phas < 0], -pi) | ||
| 41 | assert_equal (np.abs(fftgrain.phas[np.abs(fftgrain.phas) != pi]), 0) | ||
| 42 | self.skipTest('fft(fvec(%d)).phas != +0, ' % win_s \ | ||
| 43 | + 'This is expected when using fftw3 on powerpc.') | ||
| 37 | 44 | ||
| 38 | def test_impulse(self): | 45 | def test_impulse(self): |
| 39 | """ check the transform of one impulse at a random place """ | 46 | """ check the transform of one impulse at a random place """ |
| @@ -135,6 +142,37 @@ class aubio_fft_test_case(TestCase): · 95 unmodified lines | |||
| 135 | assert_almost_equal ( r[0], impulse, decimal = 6) | 142 | assert_almost_equal ( r[0], impulse, decimal = 6) |
| 136 | assert_almost_equal ( r[1:], 0) | 143 | assert_almost_equal ( r[1:], 0) |
| 137 | 144 | ||
| 145 | class aubio_fft_odd_sizes(TestCase): | ||
| 146 | |||
| 147 | def test_reconstruct_with_odd_size(self): | ||
| 148 | win_s = 29 | ||
| 149 | self.recontruct(win_s, 'odd sizes not supported') | ||
| 150 | |||
| 151 | def test_reconstruct_with_radix15(self): | ||
| 152 | win_s = 2 ** 4 * 15 | ||
| 153 | self.recontruct(win_s, 'radix 15 supported') | ||
| 154 | |||
| 155 | def test_reconstruct_with_radix5(self): | ||
| 156 | win_s = 2 ** 4 * 5 | ||
| 157 | self.recontruct(win_s, 'radix 5 supported') | ||
| 158 | |||
| 159 | def test_reconstruct_with_radix3(self): | ||
| 160 | win_s = 2 ** 4 * 3 | ||
| 161 | self.recontruct(win_s, 'radix 3 supported') | ||
| 162 | |||
| 163 | def recontruct(self, win_s, skipMessage): | ||
| 164 | try: | ||
| 165 | f = fft(win_s) | ||
| 166 | except RuntimeError: | ||
| 167 | self.skipTest(skipMessage) | ||
| 168 | input_signal = fvec(win_s) | ||
| 169 | input_signal[win_s//2] = 1 | ||
| 170 | c = f(input_signal) | ||
| 171 | output_signal = f.rdo(c) | ||
| 172 | assert_almost_equal(input_signal, output_signal) | ||
| 173 | |||
| 174 | class aubio_fft_wrong_params(TestCase): | ||
| 175 | |||
| 138 | def test_large_input_timegrain(self): | 176 | def test_large_input_timegrain(self): |
| 139 | win_s = 1024 | 177 | win_s = 1024 |
| 140 | f = fft(win_s) | 178 | f = fft(win_s) |
| @@ -163,22 +201,11 @@ class aubio_fft_test_case(TestCase): · 22 unmodified lines | |||
| 163 | with self.assertRaises(ValueError): | 201 | with self.assertRaises(ValueError): |
| 164 | f.rdo(s) | 202 | f.rdo(s) |
| 165 | 203 | ||
| 166 | class aubio_fft_wrong_params(TestCase): | ||
| 167 | |||
| 168 | def test_wrong_buf_size(self): | 204 | def test_wrong_buf_size(self): |
| 169 | win_s = -1 | 205 | win_s = -1 |
| 170 | with self.assertRaises(ValueError): | 206 | with self.assertRaises(ValueError): |
| 171 | fft(win_s) | 207 | fft(win_s) |
| 172 | 208 | ||
| 173 | def test_buf_size_not_power_of_two(self): | ||
| 174 | # when compiled with fftw3, aubio supports non power of two fft sizes | ||
| 175 | win_s = 320 | ||
| 176 | try: | ||
| 177 | with self.assertRaises(RuntimeError): | ||
| 178 | fft(win_s) | ||
| 179 | except AssertionError: | ||
| 180 | self.skipTest('creating aubio.fft with size %d did not fail' % win_s) | ||
| 181 | |||
| 182 | def test_buf_size_too_small(self): | 209 | def test_buf_size_too_small(self): |
| 183 | win_s = 1 | 210 | win_s = 1 |
| 184 | with self.assertRaises(RuntimeError): | 211 | with self.assertRaises(RuntimeError): |
| @@ -3,7 +3,7 @@ · 2 unmodified lines | |||
| 3 | from unittest import main | 3 | from unittest import main |
| 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
| 5 | from aubio import fvec, digital_filter | 5 | from aubio import fvec, digital_filter |
| 6 | from utils import array_from_text_file | 6 | from .utils import array_from_text_file |
| 7 | 7 | ||
| 8 | class aubio_filter_test_case(TestCase): | 8 | class aubio_filter_test_case(TestCase): |
| 9 | 9 | ||
| @@ -77,6 +77,16 @@ class aubio_filter_test_case(TestCase): · 67 unmodified lines | |||
| 77 | with self.assertRaises(ValueError): | 77 | with self.assertRaises(ValueError): |
| 78 | f.set_biquad(0., 0., 0, 0., 0.) | 78 | f.set_biquad(0., 0., 0, 0., 0.) |
| 79 | 79 | ||
| 80 | def test_all_available_presets(self): | ||
| 81 | f = digital_filter(7) | ||
| 82 | for sr in [8000, 11025, 16000, 22050, 24000, 32000, | ||
| 83 | 44100, 48000, 88200, 96000, 192000]: | ||
| 84 | f.set_a_weighting(sr) | ||
| 85 | f = digital_filter(5) | ||
| 86 | for sr in [8000, 11025, 16000, 22050, 24000, 32000, | ||
| 87 | 44100, 48000, 88200, 96000, 192000]: | ||
| 88 | f.set_c_weighting(sr) | ||
| 89 | |||
| 80 | class aubio_filter_wrong_params(TestCase): | 90 | class aubio_filter_wrong_params(TestCase): |
| 81 | 91 | ||
| 82 | def test_negative_order(self): | 92 | def test_negative_order(self): |
| @@ -1,11 +1,10 @@ | |||
| 1 | #! /usr/bin/env python | 1 | #! /usr/bin/env python |
| 2 | 2 | ||
| 3 | from unittest import main | ||
| 4 | from numpy.testing import TestCase | ||
| 5 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 6 | import numpy as np | 3 | import numpy as np |
| 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | ||
| 5 | |||
| 7 | from aubio import cvec, filterbank, float_type | 6 | from aubio import cvec, filterbank, float_type |
| 8 | from utils import array_from_text_file | 7 | from .utils import array_from_text_file |
| 9 | 8 | ||
| 10 | class aubio_filterbank_test_case(TestCase): | 9 | class aubio_filterbank_test_case(TestCase): |
| 11 | 10 | ||
| @@ -62,6 +61,13 @@ class aubio_filterbank_test_case(TestCase): · 50 unmodified lines | |||
| 62 | f.set_mel_coeffs_slaney(16000) | 61 | f.set_mel_coeffs_slaney(16000) |
| 63 | assert_almost_equal ( expected, f.get_coeffs() ) | 62 | assert_almost_equal ( expected, f.get_coeffs() ) |
| 64 | 63 | ||
| 64 | def test_mfcc_coeffs_get_coeffs(self): | ||
| 65 | f = filterbank(40, 512) | ||
| 66 | coeffs = f.get_coeffs() | ||
| 67 | self.assertIsInstance(coeffs, np.ndarray) | ||
| 68 | assert_equal (coeffs, 0) | ||
| 69 | assert_equal (np.shape(coeffs), (40, 512 / 2 + 1)) | ||
| 70 | |||
| 65 | class aubio_filterbank_wrong_values(TestCase): | 71 | class aubio_filterbank_wrong_values(TestCase): |
| 66 | 72 | ||
| 67 | def test_negative_window(self): | 73 | def test_negative_window(self): |
| @@ -81,4 +87,5 @@ class aubio_filterbank_wrong_values(TestCase): · 13 unmodified lines | |||
| 81 | f(cvec(256)) | 87 | f(cvec(256)) |
| 82 | 88 | ||
| 83 | if __name__ == '__main__': | 89 | if __name__ == '__main__': |
| 84 | main() | 90 | import nose2 |
| 91 | nose2.main() | ||
| @@ -1,10 +1,12 @@ | |||
| 1 | #! /usr/bin/env python | 1 | #! /usr/bin/env python |
| 2 | 2 | ||
| 3 | from unittest import main | 3 | import numpy as np |
| 4 | from numpy.testing import TestCase | 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
| 5 | from numpy.testing import assert_equal, assert_almost_equal | 5 | |
| 6 | from numpy import array, shape | 6 | from aubio import fvec, cvec, filterbank, float_type |
| 7 | from aubio import cvec, filterbank, float_type | 7 | |
| 8 | import warnings | ||
| 9 | warnings.filterwarnings('ignore', category=UserWarning, append=True) | ||
| 8 | 10 | ||
| 9 | class aubio_filterbank_mel_test_case(TestCase): | 11 | class aubio_filterbank_mel_test_case(TestCase): |
| 10 | 12 | ||
| @@ -12,38 +14,159 @@ class aubio_filterbank_mel_test_case(TestCase): · 1 unmodified line | |||
| 12 | f = filterbank(40, 512) | 14 | f = filterbank(40, 512) |
| 13 | f.set_mel_coeffs_slaney(16000) | 15 | f.set_mel_coeffs_slaney(16000) |
| 14 | a = f.get_coeffs() | 16 | a = f.get_coeffs() |
| 15 | assert_equal(shape (a), (40, 512/2 + 1) ) | 17 | assert_equal(np.shape (a), (40, 512/2 + 1) ) |
| 16 | 18 | ||
| 17 | def test_other_slaney(self): | 19 | def test_other_slaney(self): |
| 18 | f = filterbank(40, 512*2) | 20 | f = filterbank(40, 512*2) |
| 19 | f.set_mel_coeffs_slaney(44100) | 21 | f.set_mel_coeffs_slaney(44100) |
| 20 | _ = f.get_coeffs() | 22 | self.assertIsInstance(f.get_coeffs(), np.ndarray) |
| 21 | #print "sum is", sum(sum(a)) | 23 | #print "sum is", sum(sum(a)) |
| 22 | for win_s in [256, 512, 1024, 2048, 4096]: | 24 | for win_s in [256, 512, 1024, 2048, 4096]: |
| 23 | f = filterbank(40, win_s) | 25 | f = filterbank(40, win_s) |
| 24 | f.set_mel_coeffs_slaney(32000) | 26 | f.set_mel_coeffs_slaney(32000) |
| 25 | _ = f.get_coeffs() | ||
| 26 | #print "sum is", sum(sum(a)) | 27 | #print "sum is", sum(sum(a)) |
| 28 | self.assertIsInstance(f.get_coeffs(), np.ndarray) | ||
| 27 | 29 | ||
| 28 | def test_triangle_freqs_zeros(self): | 30 | def test_triangle_freqs_zeros(self): |
| 29 | f = filterbank(9, 1024) | 31 | f = filterbank(9, 1024) |
| 30 | freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] | 32 | freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] |
| 31 | freqs = array(freq_list, dtype = float_type) | 33 | freqs = np.array(freq_list, dtype = float_type) |
| 32 | f.set_triangle_bands(freqs, 48000) | 34 | f.set_triangle_bands(freqs, 48000) |
| 33 | _ = f.get_coeffs().T | ||
| 34 | assert_equal ( f(cvec(1024)), 0) | 35 | assert_equal ( f(cvec(1024)), 0) |
| 36 | self.assertIsInstance(f.get_coeffs(), np.ndarray) | ||
| 35 | 37 | ||
| 36 | def test_triangle_freqs_ones(self): | 38 | def test_triangle_freqs_ones(self): |
| 37 | f = filterbank(9, 1024) | 39 | f = filterbank(9, 1024) |
| 38 | freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] | 40 | freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000] |
| 39 | freqs = array(freq_list, dtype = float_type) | 41 | freqs = np.array(freq_list, dtype = float_type) |
| 40 | f.set_triangle_bands(freqs, 48000) | 42 | f.set_triangle_bands(freqs, 48000) |
| 41 | _ = f.get_coeffs().T | 43 | self.assertIsInstance(f.get_coeffs(), np.ndarray) |
| 42 | spec = cvec(1024) | 44 | spec = cvec(1024) |
| 43 | spec.norm[:] = 1 | 45 | spec.norm[:] = 1 |
| 44 | assert_almost_equal ( f(spec), | 46 | assert_almost_equal ( f(spec), |
| 45 | [ 0.02070313, 0.02138672, 0.02127604, 0.02135417, | 47 | [ 0.02070313, 0.02138672, 0.02127604, 0.02135417, |
| 46 | 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) | 48 | 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) |
| 47 | 49 | ||
| 50 | def test_triangle_freqs_with_zeros(self): | ||
| 51 | """make sure set_triangle_bands works when list starts with 0""" | ||
| 52 | freq_list = [0, 40, 80] | ||
| 53 | freqs = np.array(freq_list, dtype = float_type) | ||
| 54 | f = filterbank(len(freqs)-2, 1024) | ||
| 55 | f.set_triangle_bands(freqs, 48000) | ||
| 56 | assert_equal ( f(cvec(1024)), 0) | ||
| 57 | self.assertIsInstance(f.get_coeffs(), np.ndarray) | ||
| 58 | |||
| 59 | def test_triangle_freqs_with_wrong_negative(self): | ||
| 60 | """make sure set_triangle_bands fails when list contains a negative""" | ||
| 61 | freq_list = [-10, 0, 80] | ||
| 62 | f = filterbank(len(freq_list)-2, 1024) | ||
| 63 | with self.assertRaises(ValueError): | ||
| 64 | f.set_triangle_bands(fvec(freq_list), 48000) | ||
| 65 | |||
| 66 | def test_triangle_freqs_with_wrong_ordering(self): | ||
| 67 | """make sure set_triangle_bands fails when list not ordered""" | ||
| 68 | freq_list = [0, 80, 40] | ||
| 69 | f = filterbank(len(freq_list)-2, 1024) | ||
| 70 | with self.assertRaises(ValueError): | ||
| 71 | f.set_triangle_bands(fvec(freq_list), 48000) | ||
| 72 | |||
| 73 | def test_triangle_freqs_with_large_freq(self): | ||
| 74 | """make sure set_triangle_bands warns when freq > nyquist""" | ||
| 75 | samplerate = 22050 | ||
| 76 | freq_list = [0, samplerate//4, samplerate // 2 + 1] | ||
| 77 | f = filterbank(len(freq_list)-2, 1024) | ||
| 78 | # TODO add assert_warns | ||
| 79 | f.set_triangle_bands(fvec(freq_list), samplerate) | ||
| 80 | |||
| 81 | def test_triangle_freqs_with_not_enough_filters(self): | ||
| 82 | """make sure set_triangle_bands warns when not enough filters""" | ||
| 83 | samplerate = 22050 | ||
| 84 | freq_list = [0, 100, 1000, 4000, 8000, 10000] | ||
| 85 | f = filterbank(len(freq_list)-3, 1024) | ||
| 86 | # TODO add assert_warns | ||
| 87 | f.set_triangle_bands(fvec(freq_list), samplerate) | ||
| 88 | |||
| 89 | def test_triangle_freqs_with_too_many_filters(self): | ||
| 90 | """make sure set_triangle_bands warns when too many filters""" | ||
| 91 | samplerate = 22050 | ||
| 92 | freq_list = [0, 100, 1000, 4000, 8000, 10000] | ||
| 93 | f = filterbank(len(freq_list)-1, 1024) | ||
| 94 | # TODO add assert_warns | ||
| 95 | f.set_triangle_bands(fvec(freq_list), samplerate) | ||
| 96 | |||
| 97 | def test_triangle_freqs_with_double_value(self): | ||
| 98 | """make sure set_triangle_bands works with 2 duplicate freqs""" | ||
| 99 | samplerate = 22050 | ||
| 100 | freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000] | ||
| 101 | f = filterbank(len(freq_list)-2, 1024) | ||
| 102 | # TODO add assert_warns | ||
| 103 | f.set_triangle_bands(fvec(freq_list), samplerate) | ||
| 104 | |||
| 105 | def test_triangle_freqs_with_triple(self): | ||
| 106 | """make sure set_triangle_bands works with 3 duplicate freqs""" | ||
| 107 | samplerate = 22050 | ||
| 108 | freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000] | ||
| 109 | f = filterbank(len(freq_list)-2, 1024) | ||
| 110 | # TODO add assert_warns | ||
| 111 | f.set_triangle_bands(fvec(freq_list), samplerate) | ||
| 112 | |||
| 113 | def test_triangle_freqs_without_norm(self): | ||
| 114 | """make sure set_triangle_bands works without """ | ||
| 115 | samplerate = 22050 | ||
| 116 | freq_list = fvec([0, 100, 1000, 10000]) | ||
| 117 | f = filterbank(len(freq_list) - 2, 1024) | ||
| 118 | f.set_norm(0) | ||
| 119 | f.set_triangle_bands(freq_list, samplerate) | ||
| 120 | expected = f.get_coeffs() | ||
| 121 | f.set_norm(1) | ||
| 122 | f.set_triangle_bands(fvec(freq_list), samplerate) | ||
| 123 | assert_almost_equal(f.get_coeffs().T, | ||
| 124 | expected.T * 2. / (freq_list[2:] - freq_list[:-2])) | ||
| 125 | |||
| 126 | def test_triangle_freqs_wrong_norm(self): | ||
| 127 | f = filterbank(10, 1024) | ||
| 128 | with self.assertRaises(ValueError): | ||
| 129 | f.set_norm(-1) | ||
| 130 | |||
| 131 | def test_triangle_freqs_with_power(self): | ||
| 132 | f = filterbank(9, 1024) | ||
| 133 | freqs = fvec([40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, | ||
| 134 | 24000]) | ||
| 135 | f.set_power(2) | ||
| 136 | f.set_triangle_bands(freqs, 48000) | ||
| 137 | spec = cvec(1024) | ||
| 138 | spec.norm[:] = .1 | ||
| 139 | expected = fvec([0.02070313, 0.02138672, 0.02127604, 0.02135417, | ||
| 140 | 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) | ||
| 141 | expected /= 100. | ||
| 142 | assert_almost_equal(f(spec), expected) | ||
| 143 | |||
| 144 | def test_mel_coeffs(self): | ||
| 145 | f = filterbank(40, 1024) | ||
| 146 | f.set_mel_coeffs(44100, 0, 44100 / 2) | ||
| 147 | |||
| 148 | def test_zero_fmax(self): | ||
| 149 | f = filterbank(40, 1024) | ||
| 150 | f.set_mel_coeffs(44100, 0, 0) | ||
| 151 | |||
| 152 | def test_wrong_mel_coeffs(self): | ||
| 153 | f = filterbank(40, 1024) | ||
| 154 | with self.assertRaises(ValueError): | ||
| 155 | f.set_mel_coeffs_slaney(0) | ||
| 156 | with self.assertRaises(ValueError): | ||
| 157 | f.set_mel_coeffs(44100, 0, -44100 / 2) | ||
| 158 | with self.assertRaises(ValueError): | ||
| 159 | f.set_mel_coeffs(44100, -0.1, 44100 / 2) | ||
| 160 | with self.assertRaises(ValueError): | ||
| 161 | f.set_mel_coeffs(-44100, 0.1, 44100 / 2) | ||
| 162 | with self.assertRaises(ValueError): | ||
| 163 | f.set_mel_coeffs_htk(-1, 0, 0) | ||
| 164 | |||
| 165 | def test_mel_coeffs_htk(self): | ||
| 166 | f = filterbank(40, 1024) | ||
| 167 | f.set_mel_coeffs_htk(44100, 0, 44100 / 2) | ||
| 168 | |||
| 169 | |||
| 48 | if __name__ == '__main__': | 170 | if __name__ == '__main__': |
| 49 | main() | 171 | import nose2 |
| 172 | nose2.main() | ||
| @@ -60,6 +60,14 @@ class aubio_fvec_wrong_values(TestCase): · 59 unmodified lines | |||
| 60 | self.assertRaises(IndexError, a.__getitem__, 3) | 60 | self.assertRaises(IndexError, a.__getitem__, 3) |
| 61 | self.assertRaises(IndexError, a.__getitem__, 2) | 61 | self.assertRaises(IndexError, a.__getitem__, 2) |
| 62 | 62 | ||
| 63 | def test_wrong_dimensions(self): | ||
| 64 | a = np.array([[[1, 2], [3, 4]]], dtype=float_type) | ||
| 65 | self.assertRaises(ValueError, fvec, a) | ||
| 66 | |||
| 67 | def test_wrong_size(self): | ||
| 68 | a = np.ndarray([0,], dtype=float_type) | ||
| 69 | self.assertRaises(ValueError, fvec, a) | ||
| 70 | |||
| 63 | class aubio_wrong_fvec_input(TestCase): | 71 | class aubio_wrong_fvec_input(TestCase): |
| 64 | """ uses min_removal to test PyAubio_IsValidVector """ | 72 | """ uses min_removal to test PyAubio_IsValidVector """ |
| 65 | 73 | ||
| @@ -0,0 +1,35 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | import numpy as np | ||
| 4 | from numpy.testing import TestCase, assert_equal | ||
| 5 | import aubio | ||
| 6 | |||
| 7 | class aubio_shift_test_case(TestCase): | ||
| 8 | |||
| 9 | def run_shift_ishift(self, n): | ||
| 10 | ramp = np.arange(n, dtype=aubio.float_type) | ||
| 11 | # construct expected output | ||
| 12 | # even length: [5. 6. 7. 8. 9. 0. 1. 2. 3. 4.] | ||
| 13 | # odd length: [4. 5. 6. 0. 1. 2. 3.] | ||
| 14 | half = n - n//2 | ||
| 15 | expected = np.concatenate([np.arange(half, n), np.arange(half)]) | ||
| 16 | # shift in place, returns modified copy | ||
| 17 | assert_equal(aubio.shift(ramp), expected) | ||
| 18 | # check input was changed as expected | ||
| 19 | assert_equal(ramp, expected) | ||
| 20 | # construct expected output | ||
| 21 | expected = np.arange(n) | ||
| 22 | # revert shift in place, returns modifed copy | ||
| 23 | assert_equal(aubio.ishift(ramp), expected) | ||
| 24 | # check input was shifted back | ||
| 25 | assert_equal(ramp, expected) | ||
| 26 | |||
| 27 | def test_can_shift_fvec(self): | ||
| 28 | self.run_shift_ishift(10) | ||
| 29 | |||
| 30 | def test_can_shift_fvec_odd(self): | ||
| 31 | self.run_shift_ishift(7) | ||
| 32 | |||
| 33 | from unittest import main | ||
| 34 | if __name__ == '__main__': | ||
| 35 | main() | ||
| @@ -0,0 +1,109 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from unittest import main | ||
| 4 | from numpy.testing import TestCase | ||
| 5 | from numpy.testing import assert_equal, assert_almost_equal | ||
| 6 | import numpy as np | ||
| 7 | import aubio | ||
| 8 | |||
| 9 | from aubio import hztomel, meltohz | ||
| 10 | from aubio import hztomel_htk, meltohz_htk | ||
| 11 | |||
| 12 | |||
| 13 | class aubio_hztomel_test_case(TestCase): | ||
| 14 | |||
| 15 | def test_hztomel(self): | ||
| 16 | assert_equal(hztomel(0.), 0.) | ||
| 17 | assert_almost_equal(hztomel(400. / 3.), 2., decimal=5) | ||
| 18 | assert_almost_equal(hztomel(1000. / 3), 5.) | ||
| 19 | assert_equal(hztomel(200.), 3.) | ||
| 20 | assert_almost_equal(hztomel(1000.), 15) | ||
| 21 | assert_almost_equal(hztomel(6400), 42) | ||
| 22 | assert_almost_equal(hztomel(40960), 69) | ||
| 23 | |||
| 24 | for m in np.linspace(0, 1000, 100): | ||
| 25 | assert_almost_equal(hztomel(meltohz(m)) - m, 0, decimal=3) | ||
| 26 | |||
| 27 | def test_meltohz(self): | ||
| 28 | assert_equal(meltohz(0.), 0.) | ||
| 29 | assert_almost_equal(meltohz(2), 400. / 3., decimal=4) | ||
| 30 | assert_equal(meltohz(3.), 200.) | ||
| 31 | assert_almost_equal(meltohz(5), 1000. / 3., decimal=4) | ||
| 32 | assert_almost_equal(meltohz(15), 1000., decimal=4) | ||
| 33 | assert_almost_equal(meltohz(42), 6400., decimal=2) | ||
| 34 | assert_almost_equal(meltohz(69), 40960., decimal=1) | ||
| 35 | |||
| 36 | for f in np.linspace(0, 20000, 1000): | ||
| 37 | assert_almost_equal(meltohz(hztomel(f)) - f, 0, decimal=1) | ||
| 38 | |||
| 39 | def test_meltohz_negative(self): | ||
| 40 | # TODO add assert_warns | ||
| 41 | assert_equal(meltohz(-1), 0) | ||
| 42 | |||
| 43 | def test_hztomel_negative(self): | ||
| 44 | # TODO add assert_warns | ||
| 45 | assert_equal(hztomel(-1), 0) | ||
| 46 | |||
| 47 | |||
| 48 | class aubio_hztomel_htk_test_case(TestCase): | ||
| 49 | |||
| 50 | def test_meltohz(self): | ||
| 51 | assert_equal(meltohz(0, htk=True), 0) | ||
| 52 | assert_almost_equal(meltohz(2595, htk=True), 6300., decimal=1) | ||
| 53 | |||
| 54 | def test_hztomel(self): | ||
| 55 | assert_equal(hztomel(0, htk=True), 0) | ||
| 56 | assert_almost_equal(hztomel(3428.7, htk=True), 2000., decimal=1) | ||
| 57 | assert_almost_equal(hztomel(6300, htk=True), 2595., decimal=1) | ||
| 58 | |||
| 59 | def test_meltohz_negative(self): | ||
| 60 | # TODO add assert_warns | ||
| 61 | assert_equal(meltohz(-1, htk=True), 0) | ||
| 62 | assert_almost_equal(meltohz(2000, htk=True), 3428.7, decimal=1) | ||
| 63 | assert_almost_equal(meltohz(1000, htk=True), 1000., decimal=1) | ||
| 64 | |||
| 65 | def test_hztomel_negative(self): | ||
| 66 | # TODO add assert_warns | ||
| 67 | assert_equal(hztomel(-1, htk=True), 0) | ||
| 68 | assert_almost_equal(hztomel(1000, htk=True), 1000., decimal=1) | ||
| 69 | |||
| 70 | def test_hztomel_htk(self): | ||
| 71 | for f in np.linspace(0, 20000, 1000): | ||
| 72 | assert_almost_equal(meltohz_htk(hztomel_htk(f)) - f, 0, decimal=1) | ||
| 73 | for f in np.linspace(0, 20000, 1000): | ||
| 74 | assert_almost_equal(hztomel_htk(meltohz_htk(f)) - f, 0, decimal=1) | ||
| 75 | |||
| 76 | |||
| 77 | class aubio_hztomel_wrong_values(TestCase): | ||
| 78 | """ more tests to cover all branches """ | ||
| 79 | |||
| 80 | def test_hztomel_wrong_values(self): | ||
| 81 | with self.assertRaises(TypeError): | ||
| 82 | hztomel('s') | ||
| 83 | |||
| 84 | def test_meltohz_wrong_values(self): | ||
| 85 | with self.assertRaises(TypeError): | ||
| 86 | meltohz(bytes('ad')) | ||
| 87 | |||
| 88 | def test_meltohz_no_arg(self): | ||
| 89 | with self.assertRaises(TypeError): | ||
| 90 | meltohz() | ||
| 91 | |||
| 92 | def test_meltohz_htk_no_arg(self): | ||
| 93 | with self.assertRaises(TypeError): | ||
| 94 | meltohz_htk() | ||
| 95 | |||
| 96 | def test_hztomel_htk_wrong_values(self): | ||
| 97 | with self.assertRaises(TypeError): | ||
| 98 | hztomel_htk('0') | ||
| 99 | |||
| 100 | def test_hztomel_htk_false(self): | ||
| 101 | assert hztomel(120, htk=False) == hztomel(120) | ||
| 102 | |||
| 103 | def test_meltohz_htk_false(self): | ||
| 104 | assert meltohz(12, htk=False) == meltohz(12) | ||
| 105 | |||
| 106 | |||
| 107 | if __name__ == '__main__': | ||
| 108 | from unittest import main | ||
| 109 | main() | ||
| @@ -110,5 +110,42 @@ class aubio_mfcc_all_parameters(TestCase): · 109 unmodified lines | |||
| 110 | o(spec) | 110 | o(spec) |
| 111 | #print coeffs | 111 | #print coeffs |
| 112 | 112 | ||
| 113 | |||
| 114 | class aubio_mfcc_fb_params(TestCase): | ||
| 115 | |||
| 116 | def test_set_scale(self): | ||
| 117 | buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000 | ||
| 118 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) | ||
| 119 | m.set_scale(10.5) | ||
| 120 | assert m.get_scale() == 10.5 | ||
| 121 | m(cvec(buf_size)) | ||
| 122 | |||
| 123 | def test_set_power(self): | ||
| 124 | buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000 | ||
| 125 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) | ||
| 126 | m.set_power(2.5) | ||
| 127 | assert m.get_power() == 2.5 | ||
| 128 | m(cvec(buf_size)) | ||
| 129 | |||
| 130 | def test_set_mel_coeffs(self): | ||
| 131 | buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000 | ||
| 132 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) | ||
| 133 | m.set_mel_coeffs(0., samplerate/2.) | ||
| 134 | m(cvec(buf_size)) | ||
| 135 | |||
| 136 | def test_set_mel_coeffs_htk(self): | ||
| 137 | buf_size, n_filters, n_coeffs, samplerate = 512, 20, 10, 16000 | ||
| 138 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) | ||
| 139 | m.set_mel_coeffs_htk(0., samplerate/2.) | ||
| 140 | m(cvec(buf_size)) | ||
| 141 | |||
| 142 | def test_set_mel_coeffs_slaney(self): | ||
| 143 | buf_size, n_filters, n_coeffs, samplerate = 512, 40, 10, 16000 | ||
| 144 | m = mfcc(buf_size, n_filters, n_coeffs, samplerate) | ||
| 145 | m.set_mel_coeffs_slaney() | ||
| 146 | m(cvec(buf_size)) | ||
| 147 | assert m.get_power() == 1 | ||
| 148 | assert m.get_scale() == 1 | ||
| 149 | |||
| 113 | if __name__ == '__main__': | 150 | if __name__ == '__main__': |
| 114 | main() | 151 | main() |
| @@ -2,6 +2,7 @@ · 1 unmodified line | |||
| 2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- |
| 3 | 3 | ||
| 4 | from aubio import midi2note | 4 | from aubio import midi2note |
| 5 | from nose2.tools import params | ||
| 5 | import unittest | 6 | import unittest |
| 6 | 7 | ||
| 7 | list_of_known_midis = ( | 8 | list_of_known_midis = ( |
| @@ -16,10 +17,10 @@ list_of_known_midis = ( · 8 unmodified lines | |||
| 16 | 17 | ||
| 17 | class midi2note_good_values(unittest.TestCase): | 18 | class midi2note_good_values(unittest.TestCase): |
| 18 | 19 | ||
| 19 | def test_midi2note_known_values(self): | 20 | @params(*list_of_known_midis) |
| 21 | def test_midi2note_known_values(self, midi, note): | ||
| 20 | " known values are correctly converted " | 22 | " known values are correctly converted " |
| 21 | for midi, note in list_of_known_midis: | 23 | self.assertEqual ( midi2note(midi), note ) |
| 22 | self.assertEqual ( midi2note(midi), note ) | ||
| 23 | 24 | ||
| 24 | class midi2note_wrong_values(unittest.TestCase): | 25 | class midi2note_wrong_values(unittest.TestCase): |
| 25 | 26 | ||
| @@ -40,4 +41,5 @@ class midi2note_wrong_values(unittest.TestCase): · 14 unmodified lines | |||
| 40 | self.assertRaises(TypeError, midi2note, "a") | 41 | self.assertRaises(TypeError, midi2note, "a") |
| 41 | 42 | ||
| 42 | if __name__ == '__main__': | 43 | if __name__ == '__main__': |
| 43 | unittest.main() | 44 | import nose2 |
| 45 | nose2.main() | ||
| @@ -3,7 +3,8 @@ · 2 unmodified lines | |||
| 3 | 3 | ||
| 4 | from __future__ import unicode_literals | 4 | from __future__ import unicode_literals |
| 5 | 5 | ||
| 6 | from aubio import note2midi, freq2note | 6 | from aubio import note2midi, freq2note, note2freq, float_type |
| 7 | from nose2.tools import params | ||
| 7 | import unittest | 8 | import unittest |
| 8 | 9 | ||
| 9 | list_of_known_notes = ( | 10 | list_of_known_notes = ( |
| @@ -13,24 +14,57 @@ list_of_known_notes = ( · 3 unmodified lines | |||
| 13 | ( 'C3', 48 ), | 14 | ( 'C3', 48 ), |
| 14 | ( 'B3', 59 ), | 15 | ( 'B3', 59 ), |
| 15 | ( 'B#3', 60 ), | 16 | ( 'B#3', 60 ), |
| 17 | ( 'C♯4', 61 ), | ||
| 16 | ( 'A4', 69 ), | 18 | ( 'A4', 69 ), |
| 17 | ( 'A#4', 70 ), | 19 | ( 'A#4', 70 ), |
| 20 | ( 'A♯4', 70 ), | ||
| 21 | ( 'A\u266f4', 70 ), | ||
| 18 | ( 'Bb4', 70 ), | 22 | ( 'Bb4', 70 ), |
| 19 | ( 'B♭4', 70 ), | 23 | ( 'B♭4', 70 ), |
| 24 | ( 'B\u266d4', 70 ), | ||
| 20 | ( 'G8', 115 ), | 25 | ( 'G8', 115 ), |
| 21 | ( 'G♯8', 116 ), | 26 | ( 'G♯8', 116 ), |
| 22 | ( 'G9', 127 ), | 27 | ( 'G9', 127 ), |
| 23 | ( 'G\udd2a2', 45 ), | ||
| 24 | ( 'B\ufffd2', 45 ), | ||
| 25 | ( 'A♮2', 45 ), | 28 | ( 'A♮2', 45 ), |
| 26 | ) | 29 | ) |
| 27 | 30 | ||
| 31 | list_of_known_notes_with_unicode_issues = ( | ||
| 32 | ('C𝄪4', 62 ), | ||
| 33 | ('E𝄫4', 62 ), | ||
| 34 | ) | ||
| 35 | |||
| 36 | list_of_unknown_notes = ( | ||
| 37 | ( 'G\udd2a2' ), | ||
| 38 | ( 'B\ufffd2' ), | ||
| 39 | ( 'B\u266e\u266e2' ), | ||
| 40 | ( 'B\u266f\u266d3' ), | ||
| 41 | ( 'B33' ), | ||
| 42 | ( 'C.3' ), | ||
| 43 | ( 'A' ), | ||
| 44 | ( '2' ), | ||
| 45 | ) | ||
| 46 | |||
| 28 | class note2midi_good_values(unittest.TestCase): | 47 | class note2midi_good_values(unittest.TestCase): |
| 29 | 48 | ||
| 30 | def test_note2midi_known_values(self): | 49 | @params(*list_of_known_notes) |
| 50 | def test_note2midi_known_values(self, note, midi): | ||
| 31 | " known values are correctly converted " | 51 | " known values are correctly converted " |
| 32 | for note, midi in list_of_known_notes: | 52 | self.assertEqual ( note2midi(note), midi ) |
| 53 | |||
| 54 | @params(*list_of_known_notes_with_unicode_issues) | ||
| 55 | def test_note2midi_known_values_with_unicode_issues(self, note, midi): | ||
| 56 | " known values are correctly converted, unless decoding is expected to fail" | ||
| 57 | try: | ||
| 33 | self.assertEqual ( note2midi(note), midi ) | 58 | self.assertEqual ( note2midi(note), midi ) |
| 59 | except UnicodeEncodeError as e: | ||
| 60 | import sys | ||
| 61 | strfmt = "len(u'\\U0001D12A') != 1, excpected decoding failure | {:s} | {:s} {:s}" | ||
| 62 | strres = strfmt.format(e, sys.platform, sys.version) | ||
| 63 | # happens with: darwin 2.7.10, windows 2.7.12 | ||
| 64 | if len('\U0001D12A') != 1 and sys.version[0] == '2': | ||
| 65 | self.skipTest(strres + " | upgrade to Python 3 to fix") | ||
| 66 | else: | ||
| 67 | raise | ||
| 34 | 68 | ||
| 35 | class note2midi_wrong_values(unittest.TestCase): | 69 | class note2midi_wrong_values(unittest.TestCase): |
| 36 | 70 | ||
| @@ -66,12 +100,38 @@ class note2midi_wrong_values(unittest.TestCase): · 29 unmodified lines | |||
| 66 | " fails when passed a non-string value " | 100 | " fails when passed a non-string value " |
| 67 | self.assertRaises(TypeError, note2midi, 123) | 101 | self.assertRaises(TypeError, note2midi, 123) |
| 68 | 102 | ||
| 103 | def test_note2midi_wrong_data_too_long(self): | ||
| 104 | " fails when passed a note with a note name longer than expected" | ||
| 105 | self.assertRaises(ValueError, note2midi, 'CB+-3') | ||
| 106 | |||
| 107 | @params(*list_of_unknown_notes) | ||
| 108 | def test_note2midi_unknown_values(self, note): | ||
| 109 | " unknown values throw out an error " | ||
| 110 | self.assertRaises(ValueError, note2midi, note) | ||
| 69 | 111 | ||
| 70 | class freq2note_simple_test(unittest.TestCase): | 112 | class freq2note_simple_test(unittest.TestCase): |
| 71 | 113 | ||
| 72 | def test_freq2note(self): | 114 | def test_freq2note_above(self): |
| 73 | " make sure freq2note(441) == A4 " | 115 | " make sure freq2note(441) == A4 " |
| 74 | self.assertEqual("A4", freq2note(441)) | 116 | self.assertEqual("A4", freq2note(441)) |
| 75 | 117 | ||
| 118 | def test_freq2note_under(self): | ||
| 119 | " make sure freq2note(439) == A4 " | ||
| 120 | self.assertEqual("A4", freq2note(439)) | ||
| 121 | |||
| 122 | class note2freq_simple_test(unittest.TestCase): | ||
| 123 | |||
| 124 | def test_note2freq(self): | ||
| 125 | " make sure note2freq('A3') == 220" | ||
| 126 | self.assertEqual(220, note2freq("A3")) | ||
| 127 | |||
| 128 | def test_note2freq_under(self): | ||
| 129 | " make sure note2freq(A4) == 440" | ||
| 130 | if float_type == 'float32': | ||
| 131 | self.assertEqual(440, note2freq("A4")) | ||
| 132 | else: | ||
| 133 | self.assertLess(abs(note2freq("A4")-440), 1.e-12) | ||
| 134 | |||
| 76 | if __name__ == '__main__': | 135 | if __name__ == '__main__': |
| 77 | unittest.main() | 136 | import nose2 |
| 137 | nose2.main() | ||
| @@ -0,0 +1,95 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from unittest import main | ||
| 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | ||
| 5 | from aubio import notes | ||
| 6 | |||
| 7 | AUBIO_DEFAULT_NOTES_SILENCE = -70. | ||
| 8 | AUBIO_DEFAULT_NOTES_RELEASE_DROP = 10. | ||
| 9 | AUBIO_DEFAULT_NOTES_MINIOI_MS = 30. | ||
| 10 | |||
| 11 | class aubio_notes_default(TestCase): | ||
| 12 | |||
| 13 | def test_members(self): | ||
| 14 | o = notes() | ||
| 15 | assert_equal ([o.buf_size, o.hop_size, o.method, o.samplerate], | ||
| 16 | [1024,512,'default',44100]) | ||
| 17 | |||
| 18 | |||
| 19 | class aubio_notes_params(TestCase): | ||
| 20 | |||
| 21 | samplerate = 44100 | ||
| 22 | |||
| 23 | def setUp(self): | ||
| 24 | self.o = notes(samplerate = self.samplerate) | ||
| 25 | |||
| 26 | def test_get_minioi_ms(self): | ||
| 27 | assert_equal (self.o.get_minioi_ms(), AUBIO_DEFAULT_NOTES_MINIOI_MS) | ||
| 28 | |||
| 29 | def test_set_minioi_ms(self): | ||
| 30 | val = 40. | ||
| 31 | self.o.set_minioi_ms(val) | ||
| 32 | assert_almost_equal (self.o.get_minioi_ms(), val) | ||
| 33 | |||
| 34 | def test_get_silence(self): | ||
| 35 | assert_equal (self.o.get_silence(), AUBIO_DEFAULT_NOTES_SILENCE) | ||
| 36 | |||
| 37 | def test_set_silence(self): | ||
| 38 | val = -50 | ||
| 39 | self.o.set_silence(val) | ||
| 40 | assert_equal (self.o.get_silence(), val) | ||
| 41 | |||
| 42 | def test_get_release_drop(self): | ||
| 43 | assert_equal (self.o.get_release_drop(), AUBIO_DEFAULT_NOTES_RELEASE_DROP) | ||
| 44 | |||
| 45 | def test_set_release_drop(self): | ||
| 46 | val = 50 | ||
| 47 | self.o.set_release_drop(val) | ||
| 48 | assert_equal (self.o.get_release_drop(), val) | ||
| 49 | |||
| 50 | def test_set_release_drop_wrong(self): | ||
| 51 | val = -10 | ||
| 52 | with self.assertRaises(ValueError): | ||
| 53 | self.o.set_release_drop(val) | ||
| 54 | |||
| 55 | from .utils import list_all_sounds | ||
| 56 | list_of_sounds = list_all_sounds('sounds') | ||
| 57 | |||
| 58 | class aubio_notes_sinewave(TestCase): | ||
| 59 | |||
| 60 | def analyze_file(self, filepath, samplerate=0): | ||
| 61 | from aubio import source | ||
| 62 | import numpy as np | ||
| 63 | win_s = 512 # fft size | ||
| 64 | hop_s = 256 # hop size | ||
| 65 | |||
| 66 | s = source(filepath, samplerate, hop_s) | ||
| 67 | samplerate = s.samplerate | ||
| 68 | |||
| 69 | tolerance = 0.8 | ||
| 70 | |||
| 71 | notes_o = notes("default", win_s, hop_s, samplerate) | ||
| 72 | total_frames = 0 | ||
| 73 | |||
| 74 | results = [] | ||
| 75 | while True: | ||
| 76 | samples, read = s() | ||
| 77 | new_note = notes_o(samples) | ||
| 78 | if (new_note[0] != 0): | ||
| 79 | note_str = ' '.join(["%.2f" % i for i in new_note]) | ||
| 80 | results.append( [total_frames, np.copy(new_note)] ) | ||
| 81 | total_frames += read | ||
| 82 | if read < hop_s: break | ||
| 83 | return results | ||
| 84 | |||
| 85 | def test_sinewave(self): | ||
| 86 | for filepath in list_of_sounds: | ||
| 87 | if '44100Hz_44100f_sine441.wav' in filepath: | ||
| 88 | results = self.analyze_file(filepath) | ||
| 89 | assert_equal (len(results), 1) | ||
| 90 | assert_equal (len(results[0]), 2) | ||
| 91 | assert_equal (results[0][0], 1280) | ||
| 92 | assert_equal (results[0][1], [69, 123, -1]) | ||
| 93 | |||
| 94 | if __name__ == '__main__': | ||
| 95 | main() | ||
| @@ -2,7 +2,7 @@ · 1 unmodified line | |||
| 2 | 2 | ||
| 3 | from unittest import main | 3 | from unittest import main |
| 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
| 5 | from aubio import onset | 5 | from aubio import onset, fvec |
| 6 | 6 | ||
| 7 | class aubio_onset_default(TestCase): | 7 | class aubio_onset_default(TestCase): |
| 8 | 8 | ||
| @@ -19,25 +19,25 @@ class aubio_onset_params(TestCase): · 10 unmodified lines | |||
| 19 | self.o = onset(samplerate = self.samplerate) | 19 | self.o = onset(samplerate = self.samplerate) |
| 20 | 20 | ||
| 21 | def test_get_delay(self): | 21 | def test_get_delay(self): |
| 22 | assert_equal (self.o.get_delay(), int(4.3 * self.o.hop_size)) | 22 | self.assertGreater(self.o.get_delay(), 0) |
| 23 | 23 | ||
| 24 | def test_get_delay_s(self): | 24 | def test_get_delay_s(self): |
| 25 | assert_almost_equal (self.o.get_delay_s(), self.o.get_delay() / float(self.samplerate)) | 25 | self.assertGreater(self.o.get_delay_s(), 0.) |
| 26 | 26 | ||
| 27 | def test_get_delay_ms(self): | 27 | def test_get_delay_ms(self): |
| 28 | assert_almost_equal (self.o.get_delay_ms(), self.o.get_delay() * 1000. / self.samplerate, 5) | 28 | self.assertGreater(self.o.get_delay_ms(), 0.) |
| 29 | 29 | ||
| 30 | def test_get_minioi(self): | 30 | def test_get_minioi(self): |
| 31 | assert_almost_equal (self.o.get_minioi(), 0.02 * self.samplerate) | 31 | self.assertGreater(self.o.get_minioi(), 0) |
| 32 | 32 | ||
| 33 | def test_get_minioi_s(self): | 33 | def test_get_minioi_s(self): |
| 34 | assert_almost_equal (self.o.get_minioi_s(), 0.02) | 34 | self.assertGreater(self.o.get_minioi_s(), 0.) |
| 35 | 35 | ||
| 36 | def test_get_minioi_ms(self): | 36 | def test_get_minioi_ms(self): |
| 37 | assert_equal (self.o.get_minioi_ms(), 20.) | 37 | self.assertGreater(self.o.get_minioi_ms(), 0.) |
| 38 | 38 | ||
| 39 | def test_get_threshold(self): | 39 | def test_get_threshold(self): |
| 40 | assert_almost_equal (self.o.get_threshold(), 0.3) | 40 | self.assertGreater(self.o.get_threshold(), 0.) |
| 41 | 41 | ||
| 42 | def test_set_delay(self): | 42 | def test_set_delay(self): |
| 43 | val = 256 | 43 | val = 256 |
| @@ -83,5 +83,37 @@ class aubio_onset_32000(aubio_onset_params): · 39 unmodified lines | |||
| 83 | class aubio_onset_8000(aubio_onset_params): | 83 | class aubio_onset_8000(aubio_onset_params): |
| 84 | samplerate = 8000 | 84 | samplerate = 8000 |
| 85 | 85 | ||
| 86 | class aubio_onset_coverate(TestCase): | ||
| 87 | # extra tests to execute the C routines and improve coverage | ||
| 88 | |||
| 89 | def test_all_methods(self): | ||
| 90 | for method in ['default', 'energy', 'hfc', 'complexdomain', 'complex', | ||
| 91 | 'phase', 'wphase', 'mkl', 'kl', 'specflux', 'specdiff', | ||
| 92 | 'old_default']: | ||
| 93 | o = onset(method=method, buf_size=512, hop_size=256) | ||
| 94 | o(fvec(256)) | ||
| 95 | |||
| 96 | def test_get_methods(self): | ||
| 97 | o = onset(method='default', buf_size=512, hop_size=256) | ||
| 98 | |||
| 99 | assert o.get_silence() == -70 | ||
| 100 | o.set_silence(-20) | ||
| 101 | assert_almost_equal(o.get_silence(), -20) | ||
| 102 | |||
| 103 | assert o.get_compression() == 1 | ||
| 104 | o.set_compression(.99) | ||
| 105 | assert_almost_equal(o.get_compression(), .99) | ||
| 106 | |||
| 107 | assert o.get_awhitening() == 0 | ||
| 108 | o.set_awhitening(1) | ||
| 109 | assert o.get_awhitening() == 1 | ||
| 110 | |||
| 111 | o.get_last() | ||
| 112 | o.get_last_ms() | ||
| 113 | o.get_last_s() | ||
| 114 | o.get_descriptor() | ||
| 115 | o.get_thresholded_descriptor() | ||
| 116 | |||
| 117 | |||
| 86 | if __name__ == '__main__': | 118 | if __name__ == '__main__': |
| 87 | main() | 119 | main() |
| @@ -46,9 +46,25 @@ class aubio_pvoc_test_case(TestCase): · 45 unmodified lines | |||
| 46 | r = f.rdo(s) | 46 | r = f.rdo(s) |
| 47 | assert_equal ( t, 0.) | 47 | assert_equal ( t, 0.) |
| 48 | assert_equal ( s.norm, 0.) | 48 | assert_equal ( s.norm, 0.) |
| 49 | assert_equal ( s.phas, 0.) | 49 | try: |
| 50 | assert_equal ( s.phas, 0 ) | ||
| 51 | except AssertionError: | ||
| 52 | assert_equal (s.phas[s.phas > 0], +np.pi) | ||
| 53 | assert_equal (s.phas[s.phas < 0], -np.pi) | ||
| 54 | assert_equal (np.abs(s.phas[np.abs(s.phas) != np.pi]), 0) | ||
| 55 | self.skipTest('pvoc(fvec(%d)).phas != +0, ' % win_s \ | ||
| 56 | + 'This is expected when using fftw3 on powerpc.') | ||
| 50 | assert_equal ( r, 0.) | 57 | assert_equal ( r, 0.) |
| 51 | 58 | ||
| 59 | def test_no_overlap(self): | ||
| 60 | win_s, hop_s = 1024, 1024 | ||
| 61 | f = pvoc (win_s, hop_s) | ||
| 62 | t = fvec (hop_s) | ||
| 63 | for _ in range(4): | ||
| 64 | s = f(t) | ||
| 65 | r = f.rdo(s) | ||
| 66 | assert_equal ( t, 0.) | ||
| 67 | |||
| 52 | @params( | 68 | @params( |
| 53 | ( 256, 8), | 69 | ( 256, 8), |
| 54 | ( 256, 4), | 70 | ( 256, 4), |
| @@ -70,8 +70,8 @@ class aubio_pitch_Sinusoid(TestCase): · 69 unmodified lines | |||
| 70 | #print 'len(pitches), cut:', len(pitches), cut | 70 | #print 'len(pitches), cut:', len(pitches), cut |
| 71 | #print 'median errors: ', median(errors), 'median pitches: ', median(pitches) | 71 | #print 'median errors: ', median(errors), 'median pitches: ', median(pitches) |
| 72 | 72 | ||
| 73 | pitch_algorithms = [ "default", "yinfft", "yin", "schmitt", "mcomb", "fcomb" , "specacf" ] | 73 | pitch_algorithms = [ "default", "yinfft", "yin", "yinfast", "schmitt", "mcomb", "fcomb" , "specacf" ] |
| 74 | pitch_algorithms = [ "default", "yinfft", "yin", "schmitt", "mcomb", "fcomb" ] | 74 | pitch_algorithms = [ "default", "yinfft", "yin", "yinfast", "schmitt", "mcomb", "fcomb" ] |
| 75 | 75 | ||
| 76 | #freqs = [ 27.5, 55., 110., 220., 440., 880., 1760., 3520. ] | 76 | #freqs = [ 27.5, 55., 110., 220., 440., 880., 1760., 3520. ] |
| 77 | freqs = [ 110., 220., 440., 880., 1760., 3520. ] | 77 | freqs = [ 110., 220., 440., 880., 1760., 3520. ] |
| @@ -4,7 +4,10 @@ from nose2 import main · 3 unmodified lines | |||
| 4 | from nose2.tools import params | 4 | from nose2.tools import params |
| 5 | from numpy.testing import TestCase | 5 | from numpy.testing import TestCase |
| 6 | from aubio import fvec, source, sink | 6 | from aubio import fvec, source, sink |
| 7 | from utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path | 7 | from .utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path |
| 8 | |||
| 9 | import warnings | ||
| 10 | warnings.filterwarnings('ignore', category=UserWarning, append=True) | ||
| 8 | 11 | ||
| 9 | list_of_sounds = list_all_sounds('sounds') | 12 | list_of_sounds = list_all_sounds('sounds') |
| 10 | samplerates = [0, 44100, 8000, 32000] | 13 | samplerates = [0, 44100, 8000, 32000] |
| @@ -26,6 +29,26 @@ class aubio_sink_test_case(TestCase): · 15 unmodified lines | |||
| 26 | if not len(list_of_sounds): | 29 | if not len(list_of_sounds): |
| 27 | self.skipTest('add some sound files in \'python/tests/sounds\'') | 30 | self.skipTest('add some sound files in \'python/tests/sounds\'') |
| 28 | 31 | ||
| 32 | def test_wrong_filename(self): | ||
| 33 | with self.assertRaises(RuntimeError): | ||
| 34 | sink('') | ||
| 35 | |||
| 36 | def test_wrong_samplerate(self): | ||
| 37 | with self.assertRaises(RuntimeError): | ||
| 38 | sink(get_tmp_sink_path(), -1) | ||
| 39 | |||
| 40 | def test_wrong_samplerate_too_large(self): | ||
| 41 | with self.assertRaises(RuntimeError): | ||
| 42 | sink(get_tmp_sink_path(), 1536001, 2) | ||
| 43 | |||
| 44 | def test_wrong_channels(self): | ||
| 45 | with self.assertRaises(RuntimeError): | ||
| 46 | sink(get_tmp_sink_path(), 44100, -1) | ||
| 47 | |||
| 48 | def test_wrong_channels_too_large(self): | ||
| 49 | with self.assertRaises(RuntimeError): | ||
| 50 | sink(get_tmp_sink_path(), 44100, 202020) | ||
| 51 | |||
| 29 | def test_many_sinks(self): | 52 | def test_many_sinks(self): |
| 30 | from tempfile import mkdtemp | 53 | from tempfile import mkdtemp |
| 31 | import os.path | 54 | import os.path |
| @@ -93,5 +116,13 @@ class aubio_sink_test_case(TestCase): · 61 unmodified lines | |||
| 93 | g.close() | 116 | g.close() |
| 94 | del_tmp_sink_path(sink_path) | 117 | del_tmp_sink_path(sink_path) |
| 95 | 118 | ||
| 119 | def test_read_with(self): | ||
| 120 | samplerate = 44100 | ||
| 121 | sink_path = get_tmp_sink_path() | ||
| 122 | vec = fvec(128) | ||
| 123 | with sink(sink_path, samplerate) as g: | ||
| 124 | for _ in range(10): | ||
| 125 | g(vec, 128) | ||
| 126 | |||
| 96 | if __name__ == '__main__': | 127 | if __name__ == '__main__': |
| 97 | main() | 128 | main() |
| @@ -3,8 +3,8 @@ · 2 unmodified lines | |||
| 3 | from unittest import main | 3 | from unittest import main |
| 4 | from numpy.testing import TestCase, assert_equal | 4 | from numpy.testing import TestCase, assert_equal |
| 5 | from aubio import slice_source_at_stamps | 5 | from aubio import slice_source_at_stamps |
| 6 | from utils import count_files_in_directory, get_default_test_sound | 6 | from .utils import count_files_in_directory, get_default_test_sound |
| 7 | from utils import count_samples_in_directory, count_samples_in_file | 7 | from .utils import count_samples_in_directory, count_samples_in_file |
| 8 | 8 | ||
| 9 | import tempfile | 9 | import tempfile |
| 10 | import shutil | 10 | import shutil |
| @@ -23,19 +23,27 @@ class aubio_slicing_test_case(TestCase): · 12 unmodified lines | |||
| 23 | 23 | ||
| 24 | def test_slice_start_only_no_zero(self): | 24 | def test_slice_start_only_no_zero(self): |
| 25 | regions_start = [i*1000 for i in range(1, n_slices)] | 25 | regions_start = [i*1000 for i in range(1, n_slices)] |
| 26 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir) | 26 | slice_source_at_stamps(self.source_file, regions_start, |
| 27 | output_dir = self.output_dir, create_first=True) | ||
| 27 | 28 | ||
| 28 | def test_slice_start_beyond_end(self): | 29 | def test_slice_start_beyond_end(self): |
| 29 | regions_start = [i*1000 for i in range(1, n_slices)] | 30 | regions_start = [i*1000 for i in range(1, n_slices)] |
| 30 | regions_start += [count_samples_in_file(self.source_file) + 1000] | 31 | regions_start += [count_samples_in_file(self.source_file) + 1000] |
| 31 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir) | 32 | slice_source_at_stamps(self.source_file, regions_start, |
| 33 | output_dir = self.output_dir, create_first=True) | ||
| 32 | 34 | ||
| 33 | def test_slice_start_every_blocksize(self): | 35 | def test_slice_start_every_blocksize(self): |
| 34 | hopsize = 200 | 36 | hopsize = 200 |
| 35 | regions_start = [i*hopsize for i in range(1, n_slices)] | 37 | regions_start = [i*hopsize for i in range(0, n_slices)] |
| 36 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, | 38 | slice_source_at_stamps(self.source_file, regions_start, output_dir = self.output_dir, |
| 37 | hopsize = 200) | 39 | hopsize = 200) |
| 38 | 40 | ||
| 41 | def test_slice_start_every_half_blocksize(self): | ||
| 42 | hopsize = 200 | ||
| 43 | regions_start = [i*hopsize//2 for i in range(0, n_slices)] | ||
| 44 | slice_source_at_stamps(self.source_file, regions_start, | ||
| 45 | output_dir = self.output_dir, hopsize = 200) | ||
| 46 | |||
| 39 | def tearDown(self): | 47 | def tearDown(self): |
| 40 | original_samples = count_samples_in_file(self.source_file) | 48 | original_samples = count_samples_in_file(self.source_file) |
| 41 | written_samples = count_samples_in_directory(self.output_dir) | 49 | written_samples = count_samples_in_directory(self.output_dir) |
| @@ -91,6 +99,19 @@ class aubio_slicing_with_ends_test_case(TestCase): · 49 unmodified lines | |||
| 91 | assert_equal(written_samples, expected_samples, | 99 | assert_equal(written_samples, expected_samples, |
| 92 | "number of samples written different from number of original samples") | 100 | "number of samples written different from number of original samples") |
| 93 | 101 | ||
| 102 | def test_slice_start_and_ends_with_missing_end(self): | ||
| 103 | regions_start = [i*1000 for i in range(n_slices)] | ||
| 104 | regions_ends = [r-1 for r in regions_start[1:]] | ||
| 105 | slice_source_at_stamps(self.source_file, regions_start, regions_ends, | ||
| 106 | output_dir = self.output_dir) | ||
| 107 | written_samples = count_samples_in_directory(self.output_dir) | ||
| 108 | original_samples = count_samples_in_file(self.source_file) | ||
| 109 | total_files = count_files_in_directory(self.output_dir) | ||
| 110 | assert_equal(n_slices, total_files, | ||
| 111 | "number of slices created different from expected") | ||
| 112 | assert_equal(written_samples, original_samples, | ||
| 113 | "number of samples written different from number of original samples") | ||
| 114 | |||
| 94 | def tearDown(self): | 115 | def tearDown(self): |
| 95 | shutil.rmtree(self.output_dir) | 116 | shutil.rmtree(self.output_dir) |
| 96 | 117 | ||
| @@ -133,7 +154,7 @@ class aubio_slicing_wrong_ends_test_case(TestCase): · 36 unmodified lines | |||
| 133 | regions_start = [i*1000 for i in range(1, n_slices)] | 154 | regions_start = [i*1000 for i in range(1, n_slices)] |
| 134 | regions_end = None | 155 | regions_end = None |
| 135 | slice_source_at_stamps (self.source_file, regions_start, regions_end, | 156 | slice_source_at_stamps (self.source_file, regions_start, regions_end, |
| 136 | output_dir = self.output_dir) | 157 | output_dir = self.output_dir, create_first=True) |
| 137 | total_files = count_files_in_directory(self.output_dir) | 158 | total_files = count_files_in_directory(self.output_dir) |
| 138 | assert_equal(n_slices, total_files, | 159 | assert_equal(n_slices, total_files, |
| 139 | "number of slices created different from expected") | 160 | "number of slices created different from expected") |
| @@ -2,9 +2,9 @@ · 1 unmodified line | |||
| 2 | 2 | ||
| 3 | from nose2 import main | 3 | from nose2 import main |
| 4 | from nose2.tools import params | 4 | from nose2.tools import params |
| 5 | from numpy.testing import TestCase | 5 | from numpy.testing import TestCase, assert_equal |
| 6 | from aubio import source | 6 | from aubio import source |
| 7 | from utils import list_all_sounds | 7 | from .utils import list_all_sounds |
| 8 | 8 | ||
| 9 | import warnings | 9 | import warnings |
| 10 | warnings.filterwarnings('ignore', category=UserWarning, append=True) | 10 | warnings.filterwarnings('ignore', category=UserWarning, append=True) |
| @@ -51,9 +51,11 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 40 unmodified lines | |||
| 51 | def read_from_source(self, f): | 51 | def read_from_source(self, f): |
| 52 | total_frames = 0 | 52 | total_frames = 0 |
| 53 | while True: | 53 | while True: |
| 54 | _ , read = f() | 54 | samples , read = f() |
| 55 | total_frames += read | 55 | total_frames += read |
| 56 | if read < f.hop_size: break | 56 | if read < f.hop_size: |
| 57 | assert_equal(samples[read:], 0) | ||
| 58 | break | ||
| 57 | #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}" | 59 | #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}" |
| 58 | #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri | 60 | #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri |
| 59 | #print (result_str.format(*result_params)) | 61 | #print (result_str.format(*result_params)) |
| @@ -66,7 +68,14 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 6 unmodified lines | |||
| 66 | except RuntimeError as e: | 68 | except RuntimeError as e: |
| 67 | self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e))) | 69 | self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e))) |
| 68 | assert f.samplerate != 0 | 70 | assert f.samplerate != 0 |
| 69 | self.read_from_source(f) | 71 | read_frames = self.read_from_source(f) |
| 72 | if 'f_' in soundfile and samplerate == 0: | ||
| 73 | import re | ||
| 74 | f = re.compile('.*_\([0:9]*f\)_.*') | ||
| 75 | match_f = re.findall('([0-9]*)f_', soundfile) | ||
| 76 | if len(match_f) == 1: | ||
| 77 | expected_frames = int(match_f[0]) | ||
| 78 | self.assertEqual(expected_frames, read_frames) | ||
| 70 | 79 | ||
| 71 | @params(*list_of_sounds) | 80 | @params(*list_of_sounds) |
| 72 | def test_samplerate_none(self, p): | 81 | def test_samplerate_none(self, p): |
| @@ -149,13 +158,31 @@ class aubio_source_readmulti_test_case(aubio_source_read_test_case): · 76 unmodified lines | |||
| 149 | def read_from_source(self, f): | 158 | def read_from_source(self, f): |
| 150 | total_frames = 0 | 159 | total_frames = 0 |
| 151 | while True: | 160 | while True: |
| 152 | _, read = f.do_multi() | 161 | samples, read = f.do_multi() |
| 153 | total_frames += read | 162 | total_frames += read |
| 154 | if read < f.hop_size: break | 163 | if read < f.hop_size: |
| 164 | assert_equal(samples[:,read:], 0) | ||
| 165 | break | ||
| 155 | #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}" | 166 | #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}" |
| 156 | #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri | 167 | #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri |
| 157 | #print (result_str.format(*result_params)) | 168 | #print (result_str.format(*result_params)) |
| 158 | return total_frames | 169 | return total_frames |
| 159 | 170 | ||
| 171 | class aubio_source_with(aubio_source_test_case_base): | ||
| 172 | |||
| 173 | #@params(*list_of_sounds) | ||
| 174 | @params(*list_of_sounds) | ||
| 175 | def test_read_from_mono(self, filename): | ||
| 176 | total_frames = 0 | ||
| 177 | hop_size = 2048 | ||
| 178 | with source(filename, 0, hop_size) as input_source: | ||
| 179 | assert_equal(input_source.hop_size, hop_size) | ||
| 180 | #assert_equal(input_source.samplerate, samplerate) | ||
| 181 | total_frames = 0 | ||
| 182 | for frames in input_source: | ||
| 183 | total_frames += frames.shape[-1] | ||
| 184 | # check we read as many samples as we expected | ||
| 185 | assert_equal(total_frames, input_source.duration) | ||
| 186 | |||
| 160 | if __name__ == '__main__': | 187 | if __name__ == '__main__': |
| 161 | main() | 188 | main() |
| @@ -0,0 +1,93 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | """A brute force test using `sink` to create and write samples to a stereo | ||
| 4 | file, then `source` to check the correct content is read from the files.""" | ||
| 5 | |||
| 6 | import os.path | ||
| 7 | import unittest | ||
| 8 | import aubio | ||
| 9 | import numpy as np | ||
| 10 | from numpy.testing import assert_equal | ||
| 11 | from .utils import get_tmp_sink_path | ||
| 12 | |||
| 13 | class aubio_source_test_case(unittest.TestCase): | ||
| 14 | |||
| 15 | def test_read_from_mono(self): | ||
| 16 | out = get_tmp_sink_path() | ||
| 17 | samplerate = 44100 | ||
| 18 | hop_size = 256 | ||
| 19 | blocks = 10 | ||
| 20 | channels = 1 | ||
| 21 | write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | ||
| 22 | write_samples *= .5 | ||
| 23 | self.check_write_and_read(samplerate, channels, hop_size, blocks, | ||
| 24 | write_samples) | ||
| 25 | |||
| 26 | def test_read_from_stereo(self): | ||
| 27 | out = get_tmp_sink_path() | ||
| 28 | samplerate = 44100 | ||
| 29 | hop_size = 256 | ||
| 30 | blocks = 10 | ||
| 31 | channels = 1 | ||
| 32 | write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | ||
| 33 | write_samples *= .5 | ||
| 34 | self.check_write_and_read(samplerate, channels, hop_size, blocks, | ||
| 35 | write_samples) | ||
| 36 | |||
| 37 | def test_read_from_half_stereo(self): | ||
| 38 | samplerate = 16000 | ||
| 39 | channels = 2 | ||
| 40 | hop_size = 512 | ||
| 41 | blocks = 10 | ||
| 42 | write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | ||
| 43 | write_samples *= .5 | ||
| 44 | write_samples[1, :] = 0 | ||
| 45 | self.check_write_and_read(samplerate, channels, hop_size, blocks, | ||
| 46 | write_samples) | ||
| 47 | |||
| 48 | def test_read_from_cancelling_channels(self): | ||
| 49 | samplerate = 16000 | ||
| 50 | channels = 2 | ||
| 51 | hop_size = 512 | ||
| 52 | blocks = 10 | ||
| 53 | write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | ||
| 54 | write_samples *= .5 | ||
| 55 | write_samples[1] *= -1 | ||
| 56 | self.check_write_and_read(samplerate, channels, hop_size, blocks, | ||
| 57 | write_samples) | ||
| 58 | |||
| 59 | def test_read_from_strange_three_channels(self): | ||
| 60 | samplerate = 8000 | ||
| 61 | channels = 3 | ||
| 62 | hop_size = 123 | ||
| 63 | blocks = 10 | ||
| 64 | write_samples = np.ones([channels, hop_size], dtype=aubio.float_type) | ||
| 65 | write_samples *= .5 | ||
| 66 | write_samples[1, :] = 0 | ||
| 67 | self.check_write_and_read(samplerate, channels, hop_size, blocks, | ||
| 68 | write_samples) | ||
| 69 | |||
| 70 | def check_write_and_read(self, samplerate, channels, | ||
| 71 | hop_size, blocks, write_samples): | ||
| 72 | expected_mono = np.sum(write_samples, axis=0)/write_samples.shape[0] | ||
| 73 | out = get_tmp_sink_path() | ||
| 74 | snk = aubio.sink(out, samplerate, channels=channels) | ||
| 75 | for i in range(blocks): | ||
| 76 | snk.do_multi(write_samples, hop_size) | ||
| 77 | # close the sink before reading from it | ||
| 78 | snk.close() | ||
| 79 | |||
| 80 | src = aubio.source(out, samplerate, hop_size) | ||
| 81 | for i in range(blocks): | ||
| 82 | read_samples, read = src.do_multi() | ||
| 83 | assert_equal (read_samples, write_samples) | ||
| 84 | assert_equal (read, hop_size) | ||
| 85 | |||
| 86 | src.seek(0) | ||
| 87 | for i in range(blocks): | ||
| 88 | read_samples, read = src() | ||
| 89 | assert_equal (read, hop_size) | ||
| 90 | assert_equal (read_samples, expected_mono) | ||
| 91 | |||
| 92 | if __name__ == '__main__': | ||
| 93 | unittest.main() | ||
| @@ -0,0 +1,91 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from unittest import main | ||
| 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal | ||
| 5 | import aubio | ||
| 6 | |||
| 7 | class aubio_tempo_default(TestCase): | ||
| 8 | |||
| 9 | def test_members(self): | ||
| 10 | o = aubio.tempo() | ||
| 11 | assert_equal ([o.buf_size, o.hop_size, o.method, o.samplerate], | ||
| 12 | [1024,512,'default',44100]) | ||
| 13 | |||
| 14 | class aubio_tempo_params(TestCase): | ||
| 15 | |||
| 16 | samplerate = 44100 | ||
| 17 | |||
| 18 | def setUp(self): | ||
| 19 | self.o = aubio.tempo(samplerate = self.samplerate) | ||
| 20 | |||
| 21 | def test_get_delay(self): | ||
| 22 | self.assertEqual(self.o.get_delay(), 0) | ||
| 23 | |||
| 24 | def test_set_delay(self): | ||
| 25 | val = 256 | ||
| 26 | self.o.set_delay(val) | ||
| 27 | assert_equal (self.o.get_delay(), val) | ||
| 28 | |||
| 29 | def test_get_delay_s(self): | ||
| 30 | self.assertEqual(self.o.get_delay_s(), 0.) | ||
| 31 | |||
| 32 | def test_set_delay_s(self): | ||
| 33 | val = .05 | ||
| 34 | self.o.set_delay_s(val) | ||
| 35 | assert_almost_equal (self.o.get_delay_s(), val) | ||
| 36 | |||
| 37 | def test_get_delay_ms(self): | ||
| 38 | self.assertEqual(self.o.get_delay_ms(), 0.) | ||
| 39 | |||
| 40 | def test_set_delay_ms(self): | ||
| 41 | val = 50. | ||
| 42 | self.o.set_delay_ms(val) | ||
| 43 | assert_almost_equal (self.o.get_delay_ms(), val) | ||
| 44 | |||
| 45 | def test_get_threshold(self): | ||
| 46 | assert_almost_equal(self.o.get_threshold(), 0.3) | ||
| 47 | |||
| 48 | def test_set_threshold(self): | ||
| 49 | val = .1 | ||
| 50 | self.o.set_threshold(val) | ||
| 51 | assert_almost_equal (self.o.get_threshold(), val) | ||
| 52 | |||
| 53 | def test_get_silence(self): | ||
| 54 | self.assertEqual(self.o.get_silence(), -90.) | ||
| 55 | |||
| 56 | def test_set_silence(self): | ||
| 57 | val = -50. | ||
| 58 | self.o.set_silence(val) | ||
| 59 | assert_almost_equal (self.o.get_silence(), val) | ||
| 60 | |||
| 61 | def test_get_last(self): | ||
| 62 | self.assertEqual(self.o.get_last(), 0.) | ||
| 63 | |||
| 64 | def test_get_last_s(self): | ||
| 65 | self.assertEqual(self.o.get_last_s(), 0.) | ||
| 66 | |||
| 67 | def test_get_last_ms(self): | ||
| 68 | self.assertEqual(self.o.get_last_ms(), 0.) | ||
| 69 | |||
| 70 | def test_get_period(self): | ||
| 71 | self.assertEqual(self.o.get_period(), 0.) | ||
| 72 | |||
| 73 | def test_get_period_s(self): | ||
| 74 | self.assertEqual(self.o.get_period_s(), 0.) | ||
| 75 | |||
| 76 | def test_get_last_tatum(self): | ||
| 77 | self.assertEqual(self.o.get_last_tatum(), 0.) | ||
| 78 | |||
| 79 | def test_set_tatum_signature(self): | ||
| 80 | self.o.set_tatum_signature(8) | ||
| 81 | self.o.set_tatum_signature(64) | ||
| 82 | self.o.set_tatum_signature(1) | ||
| 83 | |||
| 84 | def test_set_wrong_tatum_signature(self): | ||
| 85 | with self.assertRaises(ValueError): | ||
| 86 | self.o.set_tatum_signature(101) | ||
| 87 | with self.assertRaises(ValueError): | ||
| 88 | self.o.set_tatum_signature(0) | ||
| 89 | |||
| 90 | if __name__ == '__main__': | ||
| 91 | main() | ||