Showing the changes to python/tests (4 of 24 files). Show all
Unified · Split
| @@ -141,6 +141,37 @@ class aubio_fft_test_case(TestCase): · 140 unmodified lines | |||
| 141 | assert_almost_equal ( r[0], impulse, decimal = 6) | 141 | assert_almost_equal ( r[0], impulse, decimal = 6) |
| 142 | assert_almost_equal ( r[1:], 0) | 142 | assert_almost_equal ( r[1:], 0) |
| 143 | 143 | ||
| 144 | class aubio_fft_odd_sizes(TestCase): | ||
| 145 | |||
| 146 | def test_reconstruct_with_odd_size(self): | ||
| 147 | win_s = 29 | ||
| 148 | self.recontruct(win_s, 'odd sizes not supported') | ||
| 149 | |||
| 150 | def test_reconstruct_with_radix15(self): | ||
| 151 | win_s = 2 ** 4 * 15 | ||
| 152 | self.recontruct(win_s, 'radix 15 supported') | ||
| 153 | |||
| 154 | def test_reconstruct_with_radix5(self): | ||
| 155 | win_s = 2 ** 4 * 5 | ||
| 156 | self.recontruct(win_s, 'radix 5 supported') | ||
| 157 | |||
| 158 | def test_reconstruct_with_radix3(self): | ||
| 159 | win_s = 2 ** 4 * 3 | ||
| 160 | self.recontruct(win_s, 'radix 3 supported') | ||
| 161 | |||
| 162 | def recontruct(self, win_s, skipMessage): | ||
| 163 | try: | ||
| 164 | f = fft(win_s) | ||
| 165 | except RuntimeError: | ||
| 166 | self.skipTest(skipMessage) | ||
| 167 | input_signal = fvec(win_s) | ||
| 168 | input_signal[win_s//2] = 1 | ||
| 169 | c = f(input_signal) | ||
| 170 | output_signal = f.rdo(c) | ||
| 171 | assert_almost_equal(input_signal, output_signal) | ||
| 172 | |||
| 173 | class aubio_fft_wrong_params(TestCase): | ||
| 174 | |||
| 144 | def test_large_input_timegrain(self): | 175 | def test_large_input_timegrain(self): |
| 145 | win_s = 1024 | 176 | win_s = 1024 |
| 146 | f = fft(win_s) | 177 | f = fft(win_s) |
| @@ -169,22 +200,11 @@ class aubio_fft_test_case(TestCase): · 22 unmodified lines | |||
| 169 | with self.assertRaises(ValueError): | 200 | with self.assertRaises(ValueError): |
| 170 | f.rdo(s) | 201 | f.rdo(s) |
| 171 | 202 | ||
| 172 | class aubio_fft_wrong_params(TestCase): | ||
| 173 | |||
| 174 | def test_wrong_buf_size(self): | 203 | def test_wrong_buf_size(self): |
| 175 | win_s = -1 | 204 | win_s = -1 |
| 176 | with self.assertRaises(ValueError): | 205 | with self.assertRaises(ValueError): |
| 177 | fft(win_s) | 206 | fft(win_s) |
| 178 | 207 | ||
| 179 | def test_buf_size_not_power_of_two(self): | ||
| 180 | # when compiled with fftw3, aubio supports non power of two fft sizes | ||
| 181 | win_s = 320 | ||
| 182 | try: | ||
| 183 | with self.assertRaises(RuntimeError): | ||
| 184 | fft(win_s) | ||
| 185 | except AssertionError: | ||
| 186 | self.skipTest('creating aubio.fft with size %d did not fail' % win_s) | ||
| 187 | |||
| 188 | def test_buf_size_too_small(self): | 208 | def test_buf_size_too_small(self): |
| 189 | win_s = 1 | 209 | win_s = 1 |
| 190 | with self.assertRaises(RuntimeError): | 210 | with self.assertRaises(RuntimeError): |
| @@ -109,6 +109,63 @@ class aubio_filterbank_mel_test_case(TestCase): · 108 unmodified lines | |||
| 109 | f.set_triangle_bands(fvec(freq_list), samplerate) | 109 | f.set_triangle_bands(fvec(freq_list), samplerate) |
| 110 | 110 | ||
| 111 | 111 | ||
| 112 | def test_triangle_freqs_without_norm(self): | ||
| 113 | """make sure set_triangle_bands works without """ | ||
| 114 | samplerate = 22050 | ||
| 115 | freq_list = fvec([0, 100, 1000, 10000]) | ||
| 116 | f = filterbank(len(freq_list) - 2, 1024) | ||
| 117 | f.set_norm(0) | ||
| 118 | f.set_triangle_bands(freq_list, samplerate) | ||
| 119 | expected = f.get_coeffs() | ||
| 120 | f.set_norm(1) | ||
| 121 | f.set_triangle_bands(fvec(freq_list), samplerate) | ||
| 122 | assert_almost_equal(f.get_coeffs().T, | ||
| 123 | expected.T * 2. / (freq_list[2:] - freq_list[:-2])) | ||
| 124 | |||
| 125 | def test_triangle_freqs_wrong_norm(self): | ||
| 126 | f = filterbank(10, 1024) | ||
| 127 | with self.assertRaises(ValueError): | ||
| 128 | f.set_norm(-1) | ||
| 129 | |||
| 130 | def test_triangle_freqs_with_power(self): | ||
| 131 | f = filterbank(9, 1024) | ||
| 132 | freqs = fvec([40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, | ||
| 133 | 24000]) | ||
| 134 | f.set_power(2) | ||
| 135 | f.set_triangle_bands(freqs, 48000) | ||
| 136 | spec = cvec(1024) | ||
| 137 | spec.norm[:] = .1 | ||
| 138 | expected = fvec([0.02070313, 0.02138672, 0.02127604, 0.02135417, | ||
| 139 | 0.02133301, 0.02133301, 0.02133311, 0.02133334, 0.02133345]) | ||
| 140 | expected /= 100. | ||
| 141 | assert_almost_equal(f(spec), expected) | ||
| 142 | |||
| 143 | def test_mel_coeffs(self): | ||
| 144 | f = filterbank(40, 1024) | ||
| 145 | f.set_mel_coeffs(44100, 0, 44100 / 2) | ||
| 146 | |||
| 147 | def test_zero_fmax(self): | ||
| 148 | f = filterbank(40, 1024) | ||
| 149 | f.set_mel_coeffs(44100, 0, 0) | ||
| 150 | |||
| 151 | def test_wrong_mel_coeffs(self): | ||
| 152 | f = filterbank(40, 1024) | ||
| 153 | with self.assertRaises(ValueError): | ||
| 154 | f.set_mel_coeffs_slaney(0) | ||
| 155 | with self.assertRaises(ValueError): | ||
| 156 | f.set_mel_coeffs(44100, 0, -44100 / 2) | ||
| 157 | with self.assertRaises(ValueError): | ||
| 158 | f.set_mel_coeffs(44100, -0.1, 44100 / 2) | ||
| 159 | with self.assertRaises(ValueError): | ||
| 160 | f.set_mel_coeffs(-44100, 0.1, 44100 / 2) | ||
| 161 | with self.assertRaises(ValueError): | ||
| 162 | f.set_mel_coeffs_htk(-1, 0, 0) | ||
| 163 | |||
| 164 | def test_mel_coeffs_htk(self): | ||
| 165 | f = filterbank(40, 1024) | ||
| 166 | f.set_mel_coeffs_htk(44100, 0, 44100 / 2) | ||
| 167 | |||
| 168 | |||
| 112 | if __name__ == '__main__': | 169 | if __name__ == '__main__': |
| 113 | from unittest import main | 170 | from unittest import main |
| 114 | main() | 171 | 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() | ||
| @@ -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() | ||