4 from numpy.testing import TestCase, assert_equal
5 from aubio import source
6 from utils import list_all_sounds
8 from _tools import parametrize, assert_raises, assert_equal, skipTest
10 list_of_sounds = list_all_sounds('sounds')
11 samplerates = [0, 44100, 8000, 32000]
12 hop_sizes = [512, 1024, 64]
14 default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None
17 for soundfile in list_of_sounds:
18 for hop_size in hop_sizes:
19 for samplerate in samplerates:
20 all_params.append((hop_size, samplerate, soundfile))
22 no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!"
26 class Test_aubio_source_test_case:
28 @parametrize('filename', list_of_sounds)
29 def test_close_file(self, filename):
30 samplerate = 0 # use native samplerate
32 f = source(filename, samplerate, hop_size)
35 @parametrize('filename', list_of_sounds)
36 def test_close_file_twice(self, filename):
37 samplerate = 0 # use native samplerate
39 f = source(filename, samplerate, hop_size)
43 class Test_aubio_source_read:
45 def read_from_source(self, f):
51 assert_equal(samples[read:], 0)
54 result_str = "read {:.2f}s ({:d} frames"
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))
61 @parametrize('hop_size, samplerate, soundfile', all_params)
62 def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
64 f = source(soundfile, samplerate, hop_size)
65 except RuntimeError as e:
66 err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})'
67 skipTest(err_msg.format(hop_size, samplerate, str(e)))
68 assert f.samplerate != 0
69 read_frames = self.read_from_source(f)
70 if 'f_' in soundfile and samplerate == 0:
72 f = re.compile(r'.*_\([0:9]*f\)_.*')
73 match_f = re.findall('([0-9]*)f_', soundfile)
75 expected_frames = int(match_f[0])
76 assert_equal(expected_frames, read_frames)
78 @parametrize('p', list_of_sounds)
79 def test_samplerate_none(self, p):
81 assert f.samplerate != 0
82 self.read_from_source(f)
84 @parametrize('p', list_of_sounds)
85 def test_samplerate_0(self, p):
87 assert f.samplerate != 0
88 self.read_from_source(f)
90 @parametrize('p', list_of_sounds)
91 def test_zero_hop_size(self, p):
93 assert f.samplerate != 0
94 assert f.hop_size != 0
95 self.read_from_source(f)
97 @parametrize('p', list_of_sounds)
98 def test_seek_to_half(self, p):
99 from random import randint
101 assert f.samplerate != 0
102 assert f.hop_size != 0
103 a = self.read_from_source(f)
106 b = self.read_from_source(f)
109 @parametrize('p', list_of_sounds)
110 def test_duration(self, p):
113 duration = f.duration
117 if read < f.hop_size: break
118 assert_equal (duration, total_frames)
121 class Test_aubio_source_wrong_params:
123 def test_wrong_file(self):
124 with assert_raises(RuntimeError):
125 source('path_to/unexisting file.mp3')
127 @unittest.skipIf(default_test_sound is None, no_sounds_msg)
128 class Test_aubio_source_wrong_params_with_file(TestCase):
130 def test_wrong_samplerate(self):
131 with assert_raises(ValueError):
132 source(default_test_sound, -1)
134 def test_wrong_hop_size(self):
135 with assert_raises(ValueError):
136 source(default_test_sound, 0, -1)
138 def test_wrong_channels(self):
139 with assert_raises(ValueError):
140 source(default_test_sound, 0, 0, -1)
142 def test_wrong_seek(self):
143 f = source(default_test_sound)
144 with assert_raises(ValueError):
147 def test_wrong_seek_too_large(self):
148 f = source(default_test_sound)
150 with assert_raises(ValueError):
151 f.seek(f.duration + f.samplerate * 10)
153 skipTest('seeking after end of stream failed raising ValueError')
155 class Test_aubio_source_readmulti(Test_aubio_source_read):
157 def read_from_source(self, f):
160 samples, read = f.do_multi()
162 if read < f.hop_size:
163 assert_equal(samples[:,read:], 0)
166 result_str = "read {:.2f}s ({:d} frames in {:d} channels"
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), \
171 print (result_str.format(*result_params))
174 class Test_aubio_source_with:
176 @parametrize('filename', list_of_sounds)
177 def test_read_from_mono(self, filename):
180 with source(filename, 0, hop_size) as input_source:
181 assert_equal(input_source.hop_size, hop_size)
182 #assert_equal(input_source.samplerate, samplerate)
184 for frames in input_source:
185 total_frames += frames.shape[-1]
186 # check we read as many samples as we expected
187 assert_equal(total_frames, input_source.duration)
189 if __name__ == '__main__':
191 pytest.main(sys.argv)