1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from nose2 import main |
---|
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 |
---|
8 | import numpy as np |
---|
9 | |
---|
10 | import warnings |
---|
11 | warnings.filterwarnings('ignore', category=UserWarning, append=True) |
---|
12 | |
---|
13 | list_of_sounds = list_all_sounds('sounds') |
---|
14 | samplerates = [0, 44100, 8000, 32000] |
---|
15 | hop_sizes = [512, 1024, 64] |
---|
16 | |
---|
17 | path = None |
---|
18 | |
---|
19 | all_params = [] |
---|
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)) |
---|
24 | |
---|
25 | |
---|
26 | class aubio_source_test_case_base(TestCase): |
---|
27 | |
---|
28 | def setUp(self): |
---|
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] |
---|
32 | |
---|
33 | class aubio_source_test_case(aubio_source_test_case_base): |
---|
34 | |
---|
35 | @params(*list_of_sounds) |
---|
36 | def test_close_file(self, filename): |
---|
37 | samplerate = 0 # use native samplerate |
---|
38 | hop_size = 256 |
---|
39 | f = source(filename, samplerate, hop_size) |
---|
40 | f.close() |
---|
41 | |
---|
42 | @params(*list_of_sounds) |
---|
43 | def test_close_file_twice(self, filename): |
---|
44 | samplerate = 0 # use native samplerate |
---|
45 | hop_size = 256 |
---|
46 | f = source(filename, samplerate, hop_size) |
---|
47 | f.close() |
---|
48 | f.close() |
---|
49 | |
---|
50 | class aubio_source_read_test_case(aubio_source_test_case_base): |
---|
51 | |
---|
52 | def read_from_source(self, f): |
---|
53 | total_frames = 0 |
---|
54 | while True: |
---|
55 | samples , read = f() |
---|
56 | total_frames += read |
---|
57 | if read < f.hop_size: |
---|
58 | assert_equal(samples[read:], 0) |
---|
59 | if 'brownnoise' in f.uri: |
---|
60 | self.assertEquals(np.count_nonzero(samples[:read]), read) |
---|
61 | break |
---|
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)) |
---|
65 | return total_frames |
---|
66 | |
---|
67 | @params(*all_params) |
---|
68 | def test_samplerate_hopsize(self, hop_size, samplerate, soundfile): |
---|
69 | try: |
---|
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) |
---|
75 | |
---|
76 | @params(*list_of_sounds) |
---|
77 | def test_samplerate_none(self, p): |
---|
78 | f = source(p) |
---|
79 | assert f.samplerate != 0 |
---|
80 | self.read_from_source(f) |
---|
81 | |
---|
82 | @params(*list_of_sounds) |
---|
83 | def test_samplerate_0(self, p): |
---|
84 | f = source(p, 0) |
---|
85 | assert f.samplerate != 0 |
---|
86 | self.read_from_source(f) |
---|
87 | |
---|
88 | @params(*list_of_sounds) |
---|
89 | def test_zero_hop_size(self, p): |
---|
90 | f = source(p, 0, 0) |
---|
91 | assert f.samplerate != 0 |
---|
92 | assert f.hop_size != 0 |
---|
93 | self.read_from_source(f) |
---|
94 | |
---|
95 | @params(*list_of_sounds) |
---|
96 | def test_seek_to_half(self, p): |
---|
97 | from random import randint |
---|
98 | f = source(p, 0, 0) |
---|
99 | assert f.samplerate != 0 |
---|
100 | assert f.hop_size != 0 |
---|
101 | a = self.read_from_source(f) |
---|
102 | c = randint(0, a) |
---|
103 | f.seek(c) |
---|
104 | b = self.read_from_source(f) |
---|
105 | assert a == b + c |
---|
106 | |
---|
107 | @params(*list_of_sounds) |
---|
108 | def test_duration(self, p): |
---|
109 | total_frames = 0 |
---|
110 | f = source(p) |
---|
111 | duration = f.duration |
---|
112 | while True: |
---|
113 | _, read = f() |
---|
114 | total_frames += read |
---|
115 | if read < f.hop_size: break |
---|
116 | self.assertEqual(duration, total_frames) |
---|
117 | |
---|
118 | |
---|
119 | class aubio_source_test_wrong_params(TestCase): |
---|
120 | |
---|
121 | def test_wrong_file(self): |
---|
122 | with self.assertRaises(RuntimeError): |
---|
123 | source('path_to/unexisting file.mp3') |
---|
124 | |
---|
125 | class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base): |
---|
126 | |
---|
127 | def test_wrong_samplerate(self): |
---|
128 | with self.assertRaises(ValueError): |
---|
129 | source(self.default_test_sound, -1) |
---|
130 | |
---|
131 | def test_wrong_hop_size(self): |
---|
132 | with self.assertRaises(ValueError): |
---|
133 | source(self.default_test_sound, 0, -1) |
---|
134 | |
---|
135 | def test_wrong_channels(self): |
---|
136 | with self.assertRaises(ValueError): |
---|
137 | source(self.default_test_sound, 0, 0, -1) |
---|
138 | |
---|
139 | def test_wrong_seek(self): |
---|
140 | f = source(self.default_test_sound) |
---|
141 | with self.assertRaises(ValueError): |
---|
142 | f.seek(-1) |
---|
143 | |
---|
144 | def test_wrong_seek_too_large(self): |
---|
145 | f = source(self.default_test_sound) |
---|
146 | try: |
---|
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') |
---|
151 | |
---|
152 | class aubio_source_readmulti_test_case(aubio_source_read_test_case): |
---|
153 | |
---|
154 | def read_from_source(self, f): |
---|
155 | total_frames = 0 |
---|
156 | while True: |
---|
157 | samples, read = f.do_multi() |
---|
158 | total_frames += read |
---|
159 | if read < f.hop_size: |
---|
160 | assert_equal(samples[:,read:], 0) |
---|
161 | if 'brownnoise' in f.uri: |
---|
162 | self.assertEquals(np.count_nonzero(samples[:,:read]), read) |
---|
163 | break |
---|
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)) |
---|
167 | return total_frames |
---|
168 | |
---|
169 | if __name__ == '__main__': |
---|
170 | main() |
---|