Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -4,10 +4,21 @@ from numpy.testing import TestCase, assert_equal, assert_almost_equal · 3 unmodified lines | |||
| 4 | from aubio import fvec, source | 4 | from aubio import fvec, source |
| 5 | from numpy import array | 5 | from numpy import array |
| 6 | from utils import list_all_sounds | 6 | from utils import list_all_sounds |
| 7 | from nose2.tools import params | ||
| 7 | 8 | ||
| 8 | list_of_sounds = list_all_sounds('sounds') | 9 | list_of_sounds = list_all_sounds('sounds') |
| 10 | samplerates = [0, 44100, 8000, 32000] | ||
| 11 | hop_sizes = [512, 1024, 64] | ||
| 12 | |||
| 9 | path = None | 13 | path = None |
| 10 | 14 | ||
| 15 | all_params = [] | ||
| 16 | for soundfile in list_of_sounds: | ||
| 17 | for hop_size in hop_sizes: | ||
| 18 | for samplerate in samplerates: | ||
| 19 | all_params.append((hop_size, samplerate, soundfile)) | ||
| 20 | |||
| 21 | |||
| 11 | class aubio_source_test_case_base(TestCase): | 22 | class aubio_source_test_case_base(TestCase): |
| 12 | 23 | ||
| 13 | def setUp(self): | 24 | def setUp(self): |
| @@ -39,76 +50,78 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 25 unmodified lines | |||
| 39 | total_frames += read | 50 | total_frames += read |
| 40 | if read < f.hop_size: break | 51 | if read < f.hop_size: break |
| 41 | result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}" | 52 | result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}" |
| 42 | params = total_frames / float(f.samplerate), total_frames, int(total_frames/f.hop_size), f.samplerate, f.uri | 53 | result_params = total_frames / float(f.samplerate), total_frames, int(total_frames/f.hop_size), f.samplerate, f.uri |
| 43 | print (result_str.format(*params)) | 54 | print (result_str.format(*result_params)) |
| 44 | return total_frames | 55 | return total_frames |
| 45 | 56 | ||
| 46 | def test_samplerate_hopsize(self): | 57 | @params(*all_params) |
| 47 | for p in list_of_sounds: | 58 | def test_samplerate_hopsize(self, hop_size, samplerate, soundfile): |
| 48 | for samplerate, hop_size in zip([0, 44100, 8000, 32000], [ 512, 512, 64, 256]): | 59 | try: |
| 49 | f = source(p, samplerate, hop_size) | 60 | f = source(soundfile, samplerate, hop_size) |
| 50 | assert f.samplerate != 0 | 61 | except RuntimeError as e: |
| 51 | self.read_from_source(f) | 62 | self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, e)) |
| 52 | 63 | assert f.samplerate != 0 | |
| 53 | def test_samplerate_none(self): | 64 | self.read_from_source(f) |
| 54 | for p in list_of_sounds: | 65 | |
| 55 | f = source(p) | 66 | @params(*list_of_sounds) |
| 56 | assert f.samplerate != 0 | 67 | def test_samplerate_none(self, p): |
| 57 | self.read_from_source(f) | 68 | f = source(p) |
| 58 | 69 | assert f.samplerate != 0 | |
| 59 | def test_samplerate_0(self): | 70 | self.read_from_source(f) |
| 60 | for p in list_of_sounds: | 71 | |
| 61 | f = source(p, 0) | 72 | @params(*list_of_sounds) |
| 62 | assert f.samplerate != 0 | 73 | def test_samplerate_0(self, p): |
| 63 | self.read_from_source(f) | 74 | f = source(p, 0) |
| 64 | 75 | assert f.samplerate != 0 | |
| 65 | def test_wrong_samplerate(self): | 76 | self.read_from_source(f) |
| 66 | for p in list_of_sounds: | 77 | |
| 67 | try: | 78 | @params(*list_of_sounds) |
| 68 | f = source(p, -1) | 79 | def test_wrong_samplerate(self, p): |
| 69 | except ValueError as e: | 80 | try: |
| 70 | pass | 81 | f = source(p, -1) |
| 71 | else: | 82 | except ValueError as e: |
| 72 | self.fail('negative samplerate does not raise ValueError') | 83 | pass |
| 73 | 84 | else: | |
| 74 | def test_wrong_hop_size(self): | 85 | self.fail('negative samplerate does not raise ValueError') |
| 75 | for p in list_of_sounds: | 86 | |
| 76 | try: | 87 | @params(*list_of_sounds) |
| 77 | f = source(p, 0, -1) | 88 | def test_wrong_hop_size(self, p): |
| 78 | except ValueError as e: | 89 | try: |
| 79 | pass | 90 | f = source(p, 0, -1) |
| 80 | else: | 91 | except ValueError as e: |
| 81 | self.fail('negative hop_size does not raise ValueError') | 92 | pass |
| 82 | 93 | else: | |
| 83 | def test_zero_hop_size(self): | 94 | self.fail('negative hop_size does not raise ValueError') |
| 84 | for p in list_of_sounds: | 95 | |
| 85 | f = source(p, 0, 0) | 96 | @params(*list_of_sounds) |
| 86 | assert f.samplerate != 0 | 97 | def test_zero_hop_size(self, p): |
| 87 | assert f.hop_size != 0 | 98 | f = source(p, 0, 0) |
| 88 | self.read_from_source(f) | 99 | assert f.samplerate != 0 |
| 89 | 100 | assert f.hop_size != 0 | |
| 90 | def test_seek_to_half(self): | 101 | self.read_from_source(f) |
| 102 | |||
| 103 | @params(*list_of_sounds) | ||
| 104 | def test_seek_to_half(self, p): | ||
| 91 | from random import randint | 105 | from random import randint |
| 92 | for p in list_of_sounds: | 106 | f = source(p, 0, 0) |
| 93 | f = source(p, 0, 0) | 107 | assert f.samplerate != 0 |
| 94 | assert f.samplerate != 0 | 108 | assert f.hop_size != 0 |
| 95 | assert f.hop_size != 0 | 109 | a = self.read_from_source(f) |
| 96 | a = self.read_from_source(f) | 110 | c = randint(0, a) |
| 97 | c = randint(0, a) | 111 | f.seek(c) |
| 98 | f.seek(c) | 112 | b = self.read_from_source(f) |
| 99 | b = self.read_from_source(f) | 113 | assert a == b + c |
| 100 | assert a == b + c | 114 | |
| 101 | 115 | @params(*list_of_sounds) | |
| 102 | def test_duration(self): | 116 | def test_duration(self, p): |
| 103 | for p in list_of_sounds: | 117 | total_frames = 0 |
| 104 | total_frames = 0 | 118 | f = source(p) |
| 105 | f = source(p) | 119 | duration = f.duration |
| 106 | duration = f.duration | 120 | while True: |
| 107 | while True: | 121 | vec, read = f() |
| 108 | vec, read = f() | 122 | total_frames += read |
| 109 | total_frames += read | 123 | if read < f.hop_size: break |
| 110 | if read < f.hop_size: break | 124 | self.assertEqual(duration, total_frames) |
| 111 | self.assertEqual(duration, total_frames) | ||
| 112 | 125 | ||
| 113 | class aubio_source_readmulti_test_case(aubio_source_read_test_case): | 126 | class aubio_source_readmulti_test_case(aubio_source_read_test_case): |
| 114 | 127 | ||
| @@ -119,10 +132,10 @@ class aubio_source_readmulti_test_case(aubio_source_read_test_case): · 4 unmodified lines | |||
| 119 | total_frames += read | 132 | total_frames += read |
| 120 | if read < f.hop_size: break | 133 | if read < f.hop_size: break |
| 121 | result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}" | 134 | result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}" |
| 122 | params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri | 135 | result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri |
| 123 | print (result_str.format(*params)) | 136 | print (result_str.format(*result_params)) |
| 124 | return total_frames | 137 | return total_frames |
| 125 | 138 | ||
| 126 | if __name__ == '__main__': | 139 | if __name__ == '__main__': |
| 127 | from unittest import main | 140 | from nose2 import main |
| 128 | main() | 141 | main() |