4 from nose2.tools import params
5 from numpy.testing import TestCase, assert_equal
6 from aubio import source
7 from .utils import list_all_sounds
10 warnings.filterwarnings('ignore', category=UserWarning, append=True)
12 list_of_sounds = list_all_sounds('sounds')
13 samplerates = [0, 44100, 8000, 32000]
14 hop_sizes = [512, 1024, 64]
19 for soundfile in list_of_sounds:
20 for hop_size in hop_sizes:
21 for samplerate in samplerates:
22 all_params.append((hop_size, samplerate, soundfile))
25 class aubio_source_test_case_base(TestCase):
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]
32 class aubio_source_test_case(aubio_source_test_case_base):
34 @params(*list_of_sounds)
35 def test_close_file(self, filename):
36 samplerate = 0 # use native samplerate
38 f = source(filename, samplerate, hop_size)
41 @params(*list_of_sounds)
42 def test_close_file_twice(self, filename):
43 samplerate = 0 # use native samplerate
45 f = source(filename, samplerate, hop_size)
49 class aubio_source_read_test_case(aubio_source_test_case_base):
51 def read_from_source(self, f):
57 assert_equal(samples[read:], 0)
59 #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
60 #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri
61 #print (result_str.format(*result_params))
65 def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
67 f = source(soundfile, samplerate, hop_size)
68 except RuntimeError as e:
69 self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
70 assert f.samplerate != 0
71 read_frames = self.read_from_source(f)
72 if 'f_' in soundfile and samplerate == 0:
74 f = re.compile('.*_\([0:9]*f\)_.*')
75 match_f = re.findall('([0-9]*)f_', soundfile)
77 expected_frames = int(match_f[0])
78 self.assertEqual(expected_frames, read_frames)
80 @params(*list_of_sounds)
81 def test_samplerate_none(self, p):
83 assert f.samplerate != 0
84 self.read_from_source(f)
86 @params(*list_of_sounds)
87 def test_samplerate_0(self, p):
89 assert f.samplerate != 0
90 self.read_from_source(f)
92 @params(*list_of_sounds)
93 def test_zero_hop_size(self, p):
95 assert f.samplerate != 0
96 assert f.hop_size != 0
97 self.read_from_source(f)
99 @params(*list_of_sounds)
100 def test_seek_to_half(self, p):
101 from random import randint
103 assert f.samplerate != 0
104 assert f.hop_size != 0
105 a = self.read_from_source(f)
108 b = self.read_from_source(f)
111 @params(*list_of_sounds)
112 def test_duration(self, p):
115 duration = f.duration
119 if read < f.hop_size: break
120 self.assertEqual(duration, total_frames)
123 class aubio_source_test_wrong_params(TestCase):
125 def test_wrong_file(self):
126 with self.assertRaises(RuntimeError):
127 source('path_to/unexisting file.mp3')
129 class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base):
131 def test_wrong_samplerate(self):
132 with self.assertRaises(ValueError):
133 source(self.default_test_sound, -1)
135 def test_wrong_hop_size(self):
136 with self.assertRaises(ValueError):
137 source(self.default_test_sound, 0, -1)
139 def test_wrong_channels(self):
140 with self.assertRaises(ValueError):
141 source(self.default_test_sound, 0, 0, -1)
143 def test_wrong_seek(self):
144 f = source(self.default_test_sound)
145 with self.assertRaises(ValueError):
148 def test_wrong_seek_too_large(self):
149 f = source(self.default_test_sound)
151 with self.assertRaises(ValueError):
152 f.seek(f.duration + f.samplerate * 10)
153 except AssertionError:
154 self.skipTest('seeking after end of stream failed raising ValueError')
156 class aubio_source_readmulti_test_case(aubio_source_read_test_case):
158 def read_from_source(self, f):
161 samples, read = f.do_multi()
163 if read < f.hop_size:
164 assert_equal(samples[:,read:], 0)
166 #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
167 #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
168 #print (result_str.format(*result_params))
171 class aubio_source_with(aubio_source_test_case_base):
173 #@params(*list_of_sounds)
174 @params(*list_of_sounds)
175 def test_read_from_mono(self, filename):
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)
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)
187 if __name__ == '__main__':