Mercurial > hg > auditok
comparison tests/test_audio_source.py @ 2:edee860b9f61
First release on Github
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Thu, 17 Sep 2015 22:01:30 +0200 |
parents | |
children | 56ede0db2d19 |
comparison
equal
deleted
inserted
replaced
1:78ba0ead5f9f | 2:edee860b9f61 |
---|---|
1 ''' | |
2 @author: Amine Sehili <amine.sehili@gmail.com> | |
3 September 2015 | |
4 | |
5 ''' | |
6 import unittest | |
7 | |
8 from auditok import BufferAudioSource | |
9 | |
10 | |
11 class TestBufferAudioSource_SR10_SW1_CH1(unittest.TestCase): | |
12 | |
13 | |
14 def setUp(self): | |
15 self.signal = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345" | |
16 self.audio_source = BufferAudioSource(data_buffer=self.signal, | |
17 sampling_rate=10, sample_width=1, channels=1) | |
18 self.audio_source.open() | |
19 | |
20 def tearDown(self): | |
21 self.audio_source.close() | |
22 | |
23 | |
24 | |
25 def test_sr10_sw1_ch1_read_1(self): | |
26 | |
27 block = self.audio_source.read(1) | |
28 self.assertEqual(block, "A", msg="wrong block, expected: 'A', found: {0} ".format(block)) | |
29 | |
30 | |
31 def test_sr10_sw1_ch1_read_6(self): | |
32 | |
33 block = self.audio_source.read(6) | |
34 self.assertEqual(block, "ABCDEF", msg="wrong block, expected: 'ABCDEF', found: {0} ".format(block)) | |
35 | |
36 | |
37 def test_sr10_sw1_ch1_read_multiple(self): | |
38 | |
39 block = self.audio_source.read(1) | |
40 self.assertEqual(block, "A", msg="wrong block, expected: 'A', found: {0} ".format(block)) | |
41 | |
42 block = self.audio_source.read(6) | |
43 self.assertEqual(block, "BCDEFG", msg="wrong block, expected: 'BCDEFG', found: {0} ".format(block)) | |
44 | |
45 block = self.audio_source.read(13) | |
46 self.assertEqual(block, "HIJKLMNOPQRST", msg="wrong block, expected: 'HIJKLMNOPQRST', found: {0} ".format(block)) | |
47 | |
48 block = self.audio_source.read(9999) | |
49 self.assertEqual(block, "UVWXYZ012345", msg="wrong block, expected: 'UVWXYZ012345', found: {0} ".format(block)) | |
50 | |
51 | |
52 def test_sr10_sw1_ch1_read_all(self): | |
53 block = self.audio_source.read(9999) | |
54 self.assertEqual(block, self.signal, msg="wrong block, expected: {0}, found: {1} ".format(self.signal, block)) | |
55 | |
56 block = self.audio_source.read(1) | |
57 self.assertEqual(block, None, msg="wrong block, expected: {0}, found: {1} ".format(None, block)) | |
58 | |
59 | |
60 def test_sr10_sw1_ch1_get_sampling_rate(self): | |
61 | |
62 srate = self.audio_source.get_sampling_rate() | |
63 self.assertEqual(srate, 10, msg="wrong sampling rate, expected: 10, found: {0} ".format(srate)) | |
64 | |
65 | |
66 def test_sr10_sw1_ch1_get_sample_width(self): | |
67 | |
68 swidth = self.audio_source.get_sample_width() | |
69 self.assertEqual(swidth, 1, msg="wrong sample width, expected: 1, found: {0} ".format(swidth)) | |
70 | |
71 | |
72 def test_sr10_sw1_ch1_get_channels(self): | |
73 | |
74 channels = self.audio_source.get_channels() | |
75 self.assertEqual(channels, 1, msg="wrong number of channels, expected: 1, found: {0} ".format(channels)) | |
76 | |
77 | |
78 def test_sr10_sw1_ch1_get_position_0(self): | |
79 | |
80 pos = self.audio_source.get_position() | |
81 self.assertEqual(pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)) | |
82 | |
83 def test_sr10_sw1_ch1_get_position_5(self): | |
84 | |
85 self.audio_source.read(5) | |
86 pos = self.audio_source.get_position() | |
87 self.assertEqual(pos, 5, msg="wrong position, expected: 5, found: {0} ".format(pos)) | |
88 | |
89 def test_sr10_sw1_ch1_get_position_25(self): | |
90 | |
91 self.audio_source.read(5) | |
92 self.audio_source.read(20) | |
93 | |
94 pos = self.audio_source.get_position() | |
95 self.assertEqual(pos, 25, msg="wrong position, expected: 5, found: {0} ".format(pos)) | |
96 | |
97 | |
98 def test_sr10_sw1_ch1_set_position_0(self): | |
99 | |
100 self.audio_source.read(10) | |
101 self.audio_source.set_position(0) | |
102 pos = self.audio_source.get_position() | |
103 self.assertEqual(pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)) | |
104 | |
105 | |
106 def test_sr10_sw1_ch1_set_position_10(self): | |
107 | |
108 self.audio_source.set_position(10) | |
109 pos = self.audio_source.get_position() | |
110 self.assertEqual(pos, 10, msg="wrong position, expected: 10, found: {0} ".format(pos)) | |
111 | |
112 | |
113 def test_sr10_sw1_ch1_get_time_position_0(self): | |
114 | |
115 tp = self.audio_source.get_time_position() | |
116 self.assertEqual(tp, 0.0, msg="wrong time position, expected: 0.0, found: {0} ".format(tp)) | |
117 | |
118 def test_sr10_sw1_ch1_get_time_position_1(self): | |
119 | |
120 srate = self.audio_source.get_sampling_rate() | |
121 # read one second | |
122 self.audio_source.read(srate) | |
123 tp = self.audio_source.get_time_position() | |
124 self.assertEqual(tp, 1.0, msg="wrong time position, expected: 1.0, found: {0} ".format(tp)) | |
125 | |
126 | |
127 def test_sr10_sw1_ch1_get_time_position_2_5(self): | |
128 | |
129 # read 2.5 seconds | |
130 self.audio_source.read(25) | |
131 tp = self.audio_source.get_time_position() | |
132 self.assertEqual(tp, 2.5, msg="wrong time position, expected: 2.5, found: {0} ".format(tp)) | |
133 | |
134 | |
135 def test_sr10_sw1_ch1_set_time_position_0(self): | |
136 | |
137 self.audio_source.read(10) | |
138 self.audio_source.set_time_position(0) | |
139 tp = self.audio_source.get_time_position() | |
140 self.assertEqual(tp, 0.0, msg="wrong time position, expected: 0.0, found: {0} ".format(tp)) | |
141 | |
142 | |
143 def test_sr10_sw1_ch1_set_time_position_1(self): | |
144 | |
145 self.audio_source.set_time_position(1) | |
146 tp = self.audio_source.get_time_position() | |
147 self.assertEqual(tp, 1.0, msg="wrong time position, expected: 1.0, found: {0} ".format(tp)) | |
148 | |
149 def test_sr10_sw1_ch1_set_time_position_end(self): | |
150 | |
151 self.audio_source.set_time_position(100) | |
152 tp = self.audio_source.get_time_position() | |
153 self.assertEqual(tp, 3.2, msg="wrong time position, expected: 3.2, found: {0} ".format(tp)) | |
154 | |
155 def test_sr10_sw1_ch1_rewind(self): | |
156 | |
157 self.audio_source.read(10) | |
158 self.audio_source.rewind() | |
159 tp = self.audio_source.get_position() | |
160 self.assertEqual(tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)) | |
161 | |
162 def test_sr10_sw1_ch1_set_data(self): | |
163 self.audio_source.set_data("12345") | |
164 block = self.audio_source.read(9999) | |
165 self.assertEqual(block, "12345", msg="wrong block, expected: '12345', found: {0} ".format(block)) | |
166 | |
167 | |
168 def test_sr10_sw1_ch1_read_closed(self): | |
169 self.audio_source.close() | |
170 with self.assertRaises(Exception): | |
171 self.audio_source.read(1) | |
172 | |
173 | |
174 | |
175 class TestBufferAudioSource_SR16_SW2_CH1(unittest.TestCase): | |
176 | |
177 | |
178 def setUp(self): | |
179 self.signal = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345" | |
180 self.audio_source = BufferAudioSource(data_buffer=self.signal, | |
181 sampling_rate=16, sample_width=2, channels=1) | |
182 self.audio_source.open() | |
183 | |
184 def tearDown(self): | |
185 self.audio_source.close() | |
186 | |
187 | |
188 | |
189 def test_sr16_sw2_ch1_read_1(self): | |
190 | |
191 block = self.audio_source.read(1) | |
192 self.assertEqual(block, "AB", msg="wrong block, expected: 'AB', found: {0} ".format(block)) | |
193 | |
194 | |
195 def test_sr16_sw2_ch1_read_6(self): | |
196 | |
197 block = self.audio_source.read(6) | |
198 self.assertEqual(block, "ABCDEFGHIJKL", msg="wrong block, expected: 'ABCDEFGHIJKL', found: {0} ".format(block)) | |
199 | |
200 | |
201 def test_sr16_sw2_ch1_read_multiple(self): | |
202 | |
203 block = self.audio_source.read(1) | |
204 self.assertEqual(block, "AB", msg="wrong block, expected: 'AB', found: {0} ".format(block)) | |
205 | |
206 block = self.audio_source.read(6) | |
207 self.assertEqual(block, "CDEFGHIJKLMN", msg="wrong block, expected: 'CDEFGHIJKLMN', found: {0} ".format(block)) | |
208 | |
209 block = self.audio_source.read(5) | |
210 self.assertEqual(block, "OPQRSTUVWX", msg="wrong block, expected: 'OPQRSTUVWX', found: {0} ".format(block)) | |
211 | |
212 block = self.audio_source.read(9999) | |
213 self.assertEqual(block, "YZ012345", msg="wrong block, expected: 'YZ012345', found: {0} ".format(block)) | |
214 | |
215 | |
216 def test_sr16_sw2_ch1_read_all(self): | |
217 block = self.audio_source.read(9999) | |
218 self.assertEqual(block, self.signal, msg="wrong block, expected: {0}, found: {1} ".format(self.signal, block)) | |
219 | |
220 block = self.audio_source.read(1) | |
221 self.assertEqual(block, None, msg="wrong block, expected: {0}, found: {1} ".format(None, block)) | |
222 | |
223 | |
224 def test_sr16_sw2_ch1_get_sampling_rate(self): | |
225 | |
226 srate = self.audio_source.get_sampling_rate() | |
227 self.assertEqual(srate, 16, msg="wrong sampling rate, expected: 10, found: {0} ".format(srate)) | |
228 | |
229 | |
230 def test_sr16_sw2_ch1_get_sample_width(self): | |
231 | |
232 swidth = self.audio_source.get_sample_width() | |
233 self.assertEqual(swidth, 2, msg="wrong sample width, expected: 1, found: {0} ".format(swidth)) | |
234 | |
235 | |
236 def test_sr16_sw2_ch1_get_channels(self): | |
237 | |
238 channels = self.audio_source.get_channels() | |
239 self.assertEqual(channels, 1, msg="wrong number of channels, expected: 1, found: {0} ".format(channels)) | |
240 | |
241 | |
242 def test_sr16_sw2_ch1_get_position_0(self): | |
243 | |
244 pos = self.audio_source.get_position() | |
245 self.assertEqual(pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)) | |
246 | |
247 def test_sr16_sw2_ch1_get_position_5(self): | |
248 | |
249 self.audio_source.read(5) | |
250 pos = self.audio_source.get_position() | |
251 self.assertEqual(pos, 5, msg="wrong position, expected: 5, found: {0} ".format(pos)) | |
252 | |
253 def test_sr16_sw2_ch1_get_position_15(self): | |
254 | |
255 self.audio_source.read(5) | |
256 self.audio_source.read(10) | |
257 | |
258 pos = self.audio_source.get_position() | |
259 self.assertEqual(pos, 15, msg="wrong position, expected: 5, found: {0} ".format(pos)) | |
260 | |
261 | |
262 def test_sr16_sw2_ch1_set_position_0(self): | |
263 | |
264 self.audio_source.read(10) | |
265 self.audio_source.set_position(0) | |
266 pos = self.audio_source.get_position() | |
267 self.assertEqual(pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)) | |
268 | |
269 | |
270 def test_sr16_sw2_ch1_set_position_10(self): | |
271 | |
272 self.audio_source.set_position(10) | |
273 pos = self.audio_source.get_position() | |
274 self.assertEqual(pos, 10, msg="wrong position, expected: 10, found: {0} ".format(pos)) | |
275 | |
276 | |
277 def test_sr16_sw2_ch1_get_time_position_0(self): | |
278 | |
279 tp = self.audio_source.get_time_position() | |
280 self.assertEqual(tp, 0.0, msg="wrong time position, expected: 0.0, found: {0} ".format(tp)) | |
281 | |
282 def test_sr16_sw2_ch1_get_time_position_1(self): | |
283 | |
284 srate = self.audio_source.get_sampling_rate() | |
285 # read one second | |
286 self.audio_source.read(srate) | |
287 tp = self.audio_source.get_time_position() | |
288 self.assertEqual(tp, 1.0, msg="wrong time position, expected: 1.0, found: {0} ".format(tp)) | |
289 | |
290 | |
291 def test_sr16_sw2_ch1_get_time_position_0_75(self): | |
292 | |
293 # read 2.5 seconds | |
294 self.audio_source.read(12) | |
295 tp = self.audio_source.get_time_position() | |
296 self.assertEqual(tp, 0.75, msg="wrong time position, expected: 0.75, found: {0} ".format(tp)) | |
297 | |
298 | |
299 def test_sr16_sw2_ch1_set_time_position_0(self): | |
300 | |
301 self.audio_source.read(10) | |
302 self.audio_source.set_time_position(0) | |
303 tp = self.audio_source.get_time_position() | |
304 self.assertEqual(tp, 0.0, msg="wrong time position, expected: 0.0, found: {0} ".format(tp)) | |
305 | |
306 | |
307 def test_sr16_sw2_ch1_set_time_position_1(self): | |
308 | |
309 self.audio_source.set_time_position(1) | |
310 tp = self.audio_source.get_time_position() | |
311 self.assertEqual(tp, 1.0, msg="wrong time position, expected: 1.0, found: {0} ".format(tp)) | |
312 | |
313 def test_sr16_sw2_ch1_set_time_position_end(self): | |
314 | |
315 self.audio_source.set_time_position(100) | |
316 tp = self.audio_source.get_time_position() | |
317 self.assertEqual(tp, 1.0, msg="wrong time position, expected: 1.0, found: {0} ".format(tp)) | |
318 | |
319 def test_sr16_sw2_ch1_rewind(self): | |
320 | |
321 self.audio_source.read(10) | |
322 self.audio_source.rewind() | |
323 tp = self.audio_source.get_position() | |
324 self.assertEqual(tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)) | |
325 | |
326 def test_sr16_sw2_ch1_set_data(self): | |
327 | |
328 self.audio_source.set_data("abcdef") | |
329 block = self.audio_source.read(9999) | |
330 self.assertEqual(block, "abcdef", msg="wrong block, expected: 'abcdef', found: {0} ".format(block)) | |
331 | |
332 def test_sr16_sw2_ch1_set_data_exception(self): | |
333 | |
334 with self.assertRaises(Exception): | |
335 self.assertRaises(ValueError, self.audio_source.set_data("abcde")) | |
336 | |
337 | |
338 | |
339 class TestBufferAudioSource_SR11_SW4_CH1(unittest.TestCase): | |
340 | |
341 | |
342 def setUp(self): | |
343 self.signal = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefgh" | |
344 self.audio_source = BufferAudioSource(data_buffer=self.signal, | |
345 sampling_rate=11, sample_width=4, channels=1) | |
346 self.audio_source.open() | |
347 | |
348 def tearDown(self): | |
349 self.audio_source.close() | |
350 | |
351 | |
352 | |
353 def test_sr11_sw4_ch1_read_1(self): | |
354 | |
355 block = self.audio_source.read(1) | |
356 self.assertEqual(block, "ABCD", msg="wrong block, expected: 'ABCD', found: {0} ".format(block)) | |
357 | |
358 | |
359 def test_sr11_sw4_ch1_read_6(self): | |
360 | |
361 block = self.audio_source.read(6) | |
362 self.assertEqual(block, "ABCDEFGHIJKLMNOPQRSTUVWX", msg="wrong block, expected: 'ABCDEFGHIJKLMNOPQRSTUVWX', found: {0} ".format(block)) | |
363 | |
364 | |
365 def test_sr11_sw4_ch1_read_multiple(self): | |
366 | |
367 block = self.audio_source.read(1) | |
368 self.assertEqual(block, "ABCD", msg="wrong block, expected: 'AB', found: {0} ".format(block)) | |
369 | |
370 block = self.audio_source.read(6) | |
371 self.assertEqual(block, "EFGHIJKLMNOPQRSTUVWXYZ01", msg="wrong block, expected: 'EFGHIJKLMNOPQRSTUVWXYZ01', found: {0} ".format(block)) | |
372 | |
373 block = self.audio_source.read(3) | |
374 self.assertEqual(block, "23456789abcd", msg="wrong block, expected: '23456789abcd', found: {0} ".format(block)) | |
375 | |
376 block = self.audio_source.read(9999) | |
377 self.assertEqual(block, "efgh", msg="wrong block, expected: 'efgh', found: {0} ".format(block)) | |
378 | |
379 | |
380 def test_sr11_sw4_ch1_read_all(self): | |
381 block = self.audio_source.read(9999) | |
382 self.assertEqual(block, self.signal, msg="wrong block, expected: {0}, found: {1} ".format(self.signal, block)) | |
383 | |
384 block = self.audio_source.read(1) | |
385 self.assertEqual(block, None, msg="wrong block, expected: {0}, found: {1} ".format(None, block)) | |
386 | |
387 | |
388 def test_sr11_sw4_ch1_get_sampling_rate(self): | |
389 | |
390 srate = self.audio_source.get_sampling_rate() | |
391 self.assertEqual(srate, 11, msg="wrong sampling rate, expected: 10, found: {0} ".format(srate)) | |
392 | |
393 | |
394 def test_sr11_sw4_ch1_get_sample_width(self): | |
395 | |
396 swidth = self.audio_source.get_sample_width() | |
397 self.assertEqual(swidth, 4, msg="wrong sample width, expected: 1, found: {0} ".format(swidth)) | |
398 | |
399 | |
400 def test_sr11_sw4_ch1_get_channels(self): | |
401 | |
402 channels = self.audio_source.get_channels() | |
403 self.assertEqual(channels, 1, msg="wrong number of channels, expected: 1, found: {0} ".format(channels)) | |
404 | |
405 | |
406 def test_sr11_sw4_ch1_get_position_0(self): | |
407 | |
408 pos = self.audio_source.get_position() | |
409 self.assertEqual(pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)) | |
410 | |
411 def test_sr11_sw4_ch1_get_position_5(self): | |
412 | |
413 self.audio_source.read(5) | |
414 pos = self.audio_source.get_position() | |
415 self.assertEqual(pos, 5, msg="wrong position, expected: 5, found: {0} ".format(pos)) | |
416 | |
417 def test_sr11_sw4_ch1_get_position_9(self): | |
418 | |
419 self.audio_source.read(5) | |
420 self.audio_source.read(4) | |
421 | |
422 pos = self.audio_source.get_position() | |
423 self.assertEqual(pos, 9, msg="wrong position, expected: 5, found: {0} ".format(pos)) | |
424 | |
425 | |
426 def test_sr11_sw4_ch1_set_position_0(self): | |
427 | |
428 self.audio_source.read(10) | |
429 self.audio_source.set_position(0) | |
430 pos = self.audio_source.get_position() | |
431 self.assertEqual(pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)) | |
432 | |
433 | |
434 def test_sr11_sw4_ch1_set_position_10(self): | |
435 | |
436 self.audio_source.set_position(10) | |
437 pos = self.audio_source.get_position() | |
438 self.assertEqual(pos, 10, msg="wrong position, expected: 10, found: {0} ".format(pos)) | |
439 | |
440 | |
441 def test_sr11_sw4_ch1_get_time_position_0(self): | |
442 | |
443 tp = self.audio_source.get_time_position() | |
444 self.assertEqual(tp, 0.0, msg="wrong time position, expected: 0.0, found: {0} ".format(tp)) | |
445 | |
446 def test_sr11_sw4_ch1_get_time_position_1(self): | |
447 | |
448 srate = self.audio_source.get_sampling_rate() | |
449 # read one second | |
450 self.audio_source.read(srate) | |
451 tp = self.audio_source.get_time_position() | |
452 self.assertEqual(tp, 1.0, msg="wrong time position, expected: 1.0, found: {0} ".format(tp)) | |
453 | |
454 | |
455 def test_sr11_sw4_ch1_get_time_position_0_63(self): | |
456 | |
457 # read 2.5 seconds | |
458 self.audio_source.read(7) | |
459 tp = self.audio_source.get_time_position() | |
460 self.assertAlmostEqual(tp, 0.636363636364, msg="wrong time position, expected: 0.636363636364, found: {0} ".format(tp)) | |
461 | |
462 | |
463 def test_sr11_sw4_ch1_set_time_position_0(self): | |
464 | |
465 self.audio_source.read(10) | |
466 self.audio_source.set_time_position(0) | |
467 tp = self.audio_source.get_time_position() | |
468 self.assertEqual(tp, 0.0, msg="wrong time position, expected: 0.0, found: {0} ".format(tp)) | |
469 | |
470 | |
471 def test_sr11_sw4_ch1_set_time_position_1(self): | |
472 | |
473 self.audio_source.set_time_position(1) | |
474 tp = self.audio_source.get_time_position() | |
475 self.assertEqual(tp, 1.0, msg="wrong time position, expected: 1.0, found: {0} ".format(tp)) | |
476 | |
477 def test_sr11_sw4_ch1_set_time_position_end(self): | |
478 | |
479 self.audio_source.set_time_position(100) | |
480 tp = self.audio_source.get_time_position() | |
481 self.assertEqual(tp, 1.0, msg="wrong time position, expected: 1.0, found: {0} ".format(tp)) | |
482 | |
483 def test_sr11_sw4_ch1_rewind(self): | |
484 | |
485 self.audio_source.read(10) | |
486 self.audio_source.rewind() | |
487 tp = self.audio_source.get_position() | |
488 self.assertEqual(tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)) | |
489 | |
490 def test_sr11_sw4_ch1_set_data(self): | |
491 | |
492 self.audio_source.set_data("abcdefgh") | |
493 block = self.audio_source.read(9999) | |
494 self.assertEqual(block, "abcdefgh", msg="wrong block, expected: 'abcdef', found: {0} ".format(block)) | |
495 | |
496 def test_sr11_sw4_ch1_set_data_exception(self): | |
497 | |
498 with self.assertRaises(Exception): | |
499 self.assertRaises(ValueError, self.audio_source.set_data("abcdef")) | |
500 | |
501 | |
502 | |
503 | |
504 | |
505 | |
506 if __name__ == "__main__": | |
507 #import sys;sys.argv = ['', 'Test.testName'] | |
508 unittest.main() |