Showing the changes to python/tests/test_source.py (1 of 1 file). Show all
Unified · Split
| @@ -1,19 +1,17 @@ | |||
| 1 | #! /usr/bin/env python | 1 | #! /usr/bin/env python |
| 2 | 2 | ||
| 3 | from nose2 import main | 3 | |
| 4 | from nose2.tools import params | ||
| 5 | from numpy.testing import TestCase, assert_equal | 4 | from numpy.testing import TestCase, assert_equal |
| 6 | from aubio import source | 5 | from aubio import source |
| 7 | from .utils import list_all_sounds | 6 | from utils import list_all_sounds |
| 8 | 7 | import unittest | |
| 9 | import warnings | 8 | from _tools import parametrize, assert_raises, assert_equal, skipTest |
| 10 | warnings.filterwarnings('ignore', category=UserWarning, append=True) | ||
| 11 | 9 | ||
| 12 | list_of_sounds = list_all_sounds('sounds') | 10 | list_of_sounds = list_all_sounds('sounds') |
| 13 | samplerates = [0, 44100, 8000, 32000] | 11 | samplerates = [0, 44100, 8000, 32000] |
| 14 | hop_sizes = [512, 1024, 64] | 12 | hop_sizes = [512, 1024, 64] |
| 15 | 13 | ||
| 16 | path = None | 14 | default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None |
| 17 | 15 | ||
| 18 | all_params = [] | 16 | all_params = [] |
| 19 | for soundfile in list_of_sounds: | 17 | for soundfile in list_of_sounds: |
| @@ -21,24 +19,20 @@ for soundfile in list_of_sounds: · 1 unmodified line | |||
| 21 | for samplerate in samplerates: | 19 | for samplerate in samplerates: |
| 22 | all_params.append((hop_size, samplerate, soundfile)) | 20 | all_params.append((hop_size, samplerate, soundfile)) |
| 23 | 21 | ||
| 22 | no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!" | ||
| 24 | 23 | ||
| 25 | class aubio_source_test_case_base(TestCase): | 24 | _debug = False |
| 26 | |||
| 27 | def setUp(self): | ||
| 28 | if not len(list_of_sounds): | ||
| 29 | self.skipTest('add some sound files in \'python/tests/sounds\'') | ||
| 30 | self.default_test_sound = list_of_sounds[0] | ||
| 31 | 25 | ||
| 32 | class aubio_source_test_case(aubio_source_test_case_base): | 26 | class Test_aubio_source_test_case: |
| 33 | 27 | ||
| 34 | @params(*list_of_sounds) | 28 | @parametrize('filename', list_of_sounds) |
| 35 | def test_close_file(self, filename): | 29 | def test_close_file(self, filename): |
| 36 | samplerate = 0 # use native samplerate | 30 | samplerate = 0 # use native samplerate |
| 37 | hop_size = 256 | 31 | hop_size = 256 |
| 38 | f = source(filename, samplerate, hop_size) | 32 | f = source(filename, samplerate, hop_size) |
| 39 | f.close() | 33 | f.close() |
| 40 | 34 | ||
| 41 | @params(*list_of_sounds) | 35 | @parametrize('filename', list_of_sounds) |
| 42 | def test_close_file_twice(self, filename): | 36 | def test_close_file_twice(self, filename): |
| 43 | samplerate = 0 # use native samplerate | 37 | samplerate = 0 # use native samplerate |
| 44 | hop_size = 256 | 38 | hop_size = 256 |
| @@ -46,7 +40,7 @@ class aubio_source_test_case(aubio_source_test_case_base): · 1 unmodified line | |||
| 46 | f.close() | 40 | f.close() |
| 47 | f.close() | 41 | f.close() |
| 48 | 42 | ||
| 49 | class aubio_source_read_test_case(aubio_source_test_case_base): | 43 | class Test_aubio_source_read: |
| 50 | 44 | ||
| 51 | def read_from_source(self, f): | 45 | def read_from_source(self, f): |
| 52 | total_frames = 0 | 46 | total_frames = 0 |
| @@ -56,17 +50,21 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 3 unmodified lines | |||
| 56 | if read < f.hop_size: | 50 | if read < f.hop_size: |
| 57 | assert_equal(samples[read:], 0) | 51 | assert_equal(samples[read:], 0) |
| 58 | break | 52 | break |
| 59 | #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}" | 53 | if _debug: |
| 60 | #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri | 54 | result_str = "read {:.2f}s ({:d} frames" |
| 61 | #print (result_str.format(*result_params)) | 55 | result_str += " in {:d} blocks at {:d}Hz) from {:s}" |
| 56 | result_params = total_frames / float(f.samplerate), total_frames, \ | ||
| 57 | total_frames//f.hop_size, f.samplerate, f.uri | ||
| 58 | print (result_str.format(*result_params)) | ||
| 62 | return total_frames | 59 | return total_frames |
| 63 | 60 | ||
| 64 | @params(*all_params) | 61 | @parametrize('hop_size, samplerate, soundfile', all_params) |
| 65 | def test_samplerate_hopsize(self, hop_size, samplerate, soundfile): | 62 | def test_samplerate_hopsize(self, hop_size, samplerate, soundfile): |
| 66 | try: | 63 | try: |
| 67 | f = source(soundfile, samplerate, hop_size) | 64 | f = source(soundfile, samplerate, hop_size) |
| 68 | except RuntimeError as e: | 65 | except RuntimeError as e: |
| 69 | self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e))) | 66 | err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})' |
| 67 | skipTest(err_msg.format(hop_size, samplerate, str(e))) | ||
| 70 | assert f.samplerate != 0 | 68 | assert f.samplerate != 0 |
| 71 | read_frames = self.read_from_source(f) | 69 | read_frames = self.read_from_source(f) |
| 72 | if 'f_' in soundfile and samplerate == 0: | 70 | if 'f_' in soundfile and samplerate == 0: |
| @@ -75,28 +73,28 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 2 unmodified lines | |||
| 75 | match_f = re.findall('([0-9]*)f_', soundfile) | 73 | match_f = re.findall('([0-9]*)f_', soundfile) |
| 76 | if len(match_f) == 1: | 74 | if len(match_f) == 1: |
| 77 | expected_frames = int(match_f[0]) | 75 | expected_frames = int(match_f[0]) |
| 78 | self.assertEqual(expected_frames, read_frames) | 76 | assert_equal(expected_frames, read_frames) |
| 79 | 77 | ||
| 80 | @params(*list_of_sounds) | 78 | @parametrize('p', list_of_sounds) |
| 81 | def test_samplerate_none(self, p): | 79 | def test_samplerate_none(self, p): |
| 82 | f = source(p) | 80 | f = source(p) |
| 83 | assert f.samplerate != 0 | 81 | assert f.samplerate != 0 |
| 84 | self.read_from_source(f) | 82 | self.read_from_source(f) |
| 85 | 83 | ||
| 86 | @params(*list_of_sounds) | 84 | @parametrize('p', list_of_sounds) |
| 87 | def test_samplerate_0(self, p): | 85 | def test_samplerate_0(self, p): |
| 88 | f = source(p, 0) | 86 | f = source(p, 0) |
| 89 | assert f.samplerate != 0 | 87 | assert f.samplerate != 0 |
| 90 | self.read_from_source(f) | 88 | self.read_from_source(f) |
| 91 | 89 | ||
| 92 | @params(*list_of_sounds) | 90 | @parametrize('p', list_of_sounds) |
| 93 | def test_zero_hop_size(self, p): | 91 | def test_zero_hop_size(self, p): |
| 94 | f = source(p, 0, 0) | 92 | f = source(p, 0, 0) |
| 95 | assert f.samplerate != 0 | 93 | assert f.samplerate != 0 |
| 96 | assert f.hop_size != 0 | 94 | assert f.hop_size != 0 |
| 97 | self.read_from_source(f) | 95 | self.read_from_source(f) |
| 98 | 96 | ||
| 99 | @params(*list_of_sounds) | 97 | @parametrize('p', list_of_sounds) |
| 100 | def test_seek_to_half(self, p): | 98 | def test_seek_to_half(self, p): |
| 101 | from random import randint | 99 | from random import randint |
| 102 | f = source(p, 0, 0) | 100 | f = source(p, 0, 0) |
| @@ -108,7 +106,7 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 5 unmodified lines | |||
| 108 | b = self.read_from_source(f) | 106 | b = self.read_from_source(f) |
| 109 | assert a == b + c | 107 | assert a == b + c |
| 110 | 108 | ||
| 111 | @params(*list_of_sounds) | 109 | @parametrize('p', list_of_sounds) |
| 112 | def test_duration(self, p): | 110 | def test_duration(self, p): |
| 113 | total_frames = 0 | 111 | total_frames = 0 |
| 114 | f = source(p) | 112 | f = source(p) |
| @@ -117,43 +115,44 @@ class aubio_source_read_test_case(aubio_source_test_case_base): · 2 unmodified lines | |||
| 117 | _, read = f() | 115 | _, read = f() |
| 118 | total_frames += read | 116 | total_frames += read |
| 119 | if read < f.hop_size: break | 117 | if read < f.hop_size: break |
| 120 | self.assertEqual(duration, total_frames) | 118 | assert_equal (duration, total_frames) |
| 121 | 119 | ||
| 122 | 120 | ||
| 123 | class aubio_source_test_wrong_params(TestCase): | 121 | class Test_aubio_source_wrong_params: |
| 124 | 122 | ||
| 125 | def test_wrong_file(self): | 123 | def test_wrong_file(self): |
| 126 | with self.assertRaises(RuntimeError): | 124 | with assert_raises(RuntimeError): |
| 127 | source('path_to/unexisting file.mp3') | 125 | source('path_to/unexisting file.mp3') |
| 128 | 126 | ||
| 129 | class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base): | 127 | @unittest.skipIf(default_test_sound is None, no_sounds_msg) |
| 128 | class Test_aubio_source_wrong_params_with_file(TestCase): | ||
| 130 | 129 | ||
| 131 | def test_wrong_samplerate(self): | 130 | def test_wrong_samplerate(self): |
| 132 | with self.assertRaises(ValueError): | 131 | with assert_raises(ValueError): |
| 133 | source(self.default_test_sound, -1) | 132 | source(default_test_sound, -1) |
| 134 | 133 | ||
| 135 | def test_wrong_hop_size(self): | 134 | def test_wrong_hop_size(self): |
| 136 | with self.assertRaises(ValueError): | 135 | with assert_raises(ValueError): |
| 137 | source(self.default_test_sound, 0, -1) | 136 | source(default_test_sound, 0, -1) |
| 138 | 137 | ||
| 139 | def test_wrong_channels(self): | 138 | def test_wrong_channels(self): |
| 140 | with self.assertRaises(ValueError): | 139 | with assert_raises(ValueError): |
| 141 | source(self.default_test_sound, 0, 0, -1) | 140 | source(default_test_sound, 0, 0, -1) |
| 142 | 141 | ||
| 143 | def test_wrong_seek(self): | 142 | def test_wrong_seek(self): |
| 144 | f = source(self.default_test_sound) | 143 | f = source(default_test_sound) |
| 145 | with self.assertRaises(ValueError): | 144 | with assert_raises(ValueError): |
| 146 | f.seek(-1) | 145 | f.seek(-1) |
| 147 | 146 | ||
| 148 | def test_wrong_seek_too_large(self): | 147 | def test_wrong_seek_too_large(self): |
| 149 | f = source(self.default_test_sound) | 148 | f = source(default_test_sound) |
| 150 | try: | 149 | try: |
| 151 | with self.assertRaises(ValueError): | 150 | with assert_raises(ValueError): |
| 152 | f.seek(f.duration + f.samplerate * 10) | 151 | f.seek(f.duration + f.samplerate * 10) |
| 153 | except AssertionError: | 152 | except: |
| 154 | self.skipTest('seeking after end of stream failed raising ValueError') | 153 | skipTest('seeking after end of stream failed raising ValueError') |
| 155 | 154 | ||
| 156 | class aubio_source_readmulti_test_case(aubio_source_read_test_case): | 155 | class Test_aubio_source_readmulti(Test_aubio_source_read): |
| 157 | 156 | ||
| 158 | def read_from_source(self, f): | 157 | def read_from_source(self, f): |
| 159 | total_frames = 0 | 158 | total_frames = 0 |
| @@ -163,15 +162,18 @@ class aubio_source_readmulti_test_case(aubio_source_read_test_case): · 3 unmodified lines | |||
| 163 | if read < f.hop_size: | 162 | if read < f.hop_size: |
| 164 | assert_equal(samples[:,read:], 0) | 163 | assert_equal(samples[:,read:], 0) |
| 165 | break | 164 | break |
| 166 | #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}" | 165 | if _debug: |
| 167 | #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri | 166 | result_str = "read {:.2f}s ({:d} frames in {:d} channels" |
| 168 | #print (result_str.format(*result_params)) | 167 | result_str += " and {:d} blocks at {:d}Hz) from {:s}" |
| 168 | result_params = total_frames / float(f.samplerate), total_frames, \ | ||
| 169 | f.channels, int(total_frames/f.hop_size), \ | ||
| 170 | f.samplerate, f.uri | ||
| 171 | print (result_str.format(*result_params)) | ||
| 169 | return total_frames | 172 | return total_frames |
| 170 | 173 | ||
| 171 | class aubio_source_with(aubio_source_test_case_base): | 174 | class Test_aubio_source_with: |
| 172 | 175 | ||
| 173 | #@params(*list_of_sounds) | 176 | @parametrize('filename', list_of_sounds) |
| 174 | @params(*list_of_sounds) | ||
| 175 | def test_read_from_mono(self, filename): | 177 | def test_read_from_mono(self, filename): |
| 176 | total_frames = 0 | 178 | total_frames = 0 |
| 177 | hop_size = 2048 | 179 | hop_size = 2048 |
| @@ -185,4 +187,5 @@ class aubio_source_with(aubio_source_test_case_base): · 7 unmodified lines | |||
| 185 | assert_equal(total_frames, input_source.duration) | 187 | assert_equal(total_frames, input_source.duration) |
| 186 | 188 | ||
| 187 | if __name__ == '__main__': | 189 | if __name__ == '__main__': |
| 188 | main() | 190 | import sys, pytest |
| 191 | pytest.main(sys.argv) | ||