3 from numpy.testing import TestCase
4 from aubio import fvec, source, sink
5 from utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path
6 from _tools import parametrize, skipTest, assert_raises
8 list_of_sounds = list_all_sounds('sounds')
9 samplerates = [0, 44100, 8000, 32000]
10 hop_sizes = [512, 1024, 64]
14 many_files = 300 # 256 opened files is too much
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 class Test_aubio_sink(object):
24 def test_wrong_filename(self):
25 with assert_raises(RuntimeError):
28 def test_wrong_samplerate(self):
29 with assert_raises(RuntimeError):
30 sink(get_tmp_sink_path(), -1)
32 def test_wrong_samplerate_too_large(self):
33 with assert_raises(RuntimeError):
34 sink(get_tmp_sink_path(), 1536001, 2)
36 def test_wrong_channels(self):
37 with assert_raises(RuntimeError):
38 sink(get_tmp_sink_path(), 44100, -1)
40 def test_wrong_channels_too_large(self):
41 with assert_raises(RuntimeError):
42 sink(get_tmp_sink_path(), 44100, 202020)
44 def test_many_sinks(self):
45 from tempfile import mkdtemp
50 for i in range(many_files):
51 path = os.path.join(tmpdir, 'f-' + str(i) + '.wav')
61 @parametrize('hop_size, samplerate, path', all_params)
62 def test_read_and_write(self, hop_size, samplerate, path):
64 f = source(path, samplerate, hop_size)
65 except RuntimeError as e:
66 err_msg = '{:s} (hop_s = {:d}, samplerate = {:d})'
67 skipTest(err_msg.format(str(e), hop_size, samplerate))
68 if samplerate == 0: samplerate = f.samplerate
69 sink_path = get_tmp_sink_path()
70 g = sink(sink_path, samplerate)
76 if read < f.hop_size: break
77 del_tmp_sink_path(sink_path)
79 @parametrize('hop_size, samplerate, path', all_params)
80 def test_read_and_write_multi(self, hop_size, samplerate, path):
82 f = source(path, samplerate, hop_size)
83 except RuntimeError as e:
84 err_msg = '{:s} (hop_s = {:d}, samplerate = {:d})'
85 skipTest(err_msg.format(str(e), hop_size, samplerate))
86 if samplerate == 0: samplerate = f.samplerate
87 sink_path = get_tmp_sink_path()
88 g = sink(sink_path, samplerate, channels = f.channels)
91 vec, read = f.do_multi()
94 if read < f.hop_size: break
95 del_tmp_sink_path(sink_path)
97 def test_close_file(self):
99 sink_path = get_tmp_sink_path()
100 g = sink(sink_path, samplerate)
102 del_tmp_sink_path(sink_path)
104 def test_close_file_twice(self):
106 sink_path = get_tmp_sink_path()
107 g = sink(sink_path, samplerate)
110 del_tmp_sink_path(sink_path)
112 def test_read_with(self):
114 sink_path = get_tmp_sink_path()
116 with sink(sink_path, samplerate) as g:
120 if __name__ == '__main__':
121 from _tools import run_module_suite