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
11 warnings.filterwarnings('ignore', category=UserWarning, append=True)
13 list_of_sounds = list_all_sounds('sounds')
14 samplerates = [0, 44100, 8000, 32000]
15 hop_sizes = [512, 1024, 64]
20 for soundfile in list_of_sounds:
21 for hop_size in hop_sizes:
22 for samplerate in samplerates:
23 all_params.append((hop_size, samplerate, soundfile))
26 class aubio_source_test_case_base(TestCase):
29 if not len(list_of_sounds):
30 self.skipTest('add some sound files in \'python/tests/sounds\'')
31 self.default_test_sound = list_of_sounds[0]
33 class aubio_source_test_case(aubio_source_test_case_base):
35 @params(*list_of_sounds)
36 def test_close_file(self, filename):
37 samplerate = 0 # use native samplerate
39 f = source(filename, samplerate, hop_size)
42 @params(*list_of_sounds)
43 def test_close_file_twice(self, filename):
44 samplerate = 0 # use native samplerate
46 f = source(filename, samplerate, hop_size)
50 class aubio_source_read_test_case(aubio_source_test_case_base):
52 def read_from_source(self, f):
58 assert_equal(samples[read:], 0)
59 if 'silence' not in f.uri:
60 self.assertEquals(np.count_nonzero(samples[:read]), read)
62 #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
63 #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri
64 #print (result_str.format(*result_params))
68 def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
70 f = source(soundfile, samplerate, hop_size)
71 except RuntimeError as e:
72 self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
73 assert f.samplerate != 0
74 self.read_from_source(f)
76 @params(*list_of_sounds)
77 def test_samplerate_none(self, p):
79 assert f.samplerate != 0
80 self.read_from_source(f)
82 @params(*list_of_sounds)
83 def test_samplerate_0(self, p):
85 assert f.samplerate != 0
86 self.read_from_source(f)
88 @params(*list_of_sounds)
89 def test_zero_hop_size(self, p):
91 assert f.samplerate != 0
92 assert f.hop_size != 0
93 self.read_from_source(f)
95 @params(*list_of_sounds)
96 def test_seek_to_half(self, p):
97 from random import randint
99 assert f.samplerate != 0
100 assert f.hop_size != 0
101 a = self.read_from_source(f)
104 b = self.read_from_source(f)
107 @params(*list_of_sounds)
108 def test_duration(self, p):
111 duration = f.duration
115 if read < f.hop_size: break
116 self.assertEqual(duration, total_frames)
119 class aubio_source_test_wrong_params(TestCase):
121 def test_wrong_file(self):
122 with self.assertRaises(RuntimeError):
123 source('path_to/unexisting file.mp3')
125 class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base):
127 def test_wrong_samplerate(self):
128 with self.assertRaises(ValueError):
129 source(self.default_test_sound, -1)
131 def test_wrong_hop_size(self):
132 with self.assertRaises(ValueError):
133 source(self.default_test_sound, 0, -1)
135 def test_wrong_channels(self):
136 with self.assertRaises(ValueError):
137 source(self.default_test_sound, 0, 0, -1)
139 def test_wrong_seek(self):
140 f = source(self.default_test_sound)
141 with self.assertRaises(ValueError):
144 def test_wrong_seek_too_large(self):
145 f = source(self.default_test_sound)
147 with self.assertRaises(ValueError):
148 f.seek(f.duration + f.samplerate * 10)
149 except AssertionError:
150 self.skipTest('seeking after end of stream failed raising ValueError')
152 class aubio_source_readmulti_test_case(aubio_source_read_test_case):
154 def read_from_source(self, f):
157 samples, read = f.do_multi()
159 if read < f.hop_size:
160 assert_equal(samples[:,read:], 0)
161 if 'silence' not in f.uri:
162 self.assertEquals(np.count_nonzero(samples[:,:read]), read)
164 #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
165 #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
166 #print (result_str.format(*result_params))
169 if __name__ == '__main__':