amine@164
|
1 """
|
amine@164
|
2 @author: Amine Sehili <amine.sehili@gmail.com>
|
amine@164
|
3 """
|
amine@164
|
4 from array import array
|
amine@164
|
5 import unittest
|
amine@164
|
6 from genty import genty, genty_dataset
|
amine@164
|
7 from auditok.io import (
|
amine@164
|
8 AudioParameterError,
|
amine@164
|
9 _array_to_bytes,
|
amine@164
|
10 DATA_FORMAT,
|
amine@164
|
11 BufferAudioSource,
|
amine@164
|
12 RawAudioSource,
|
amine@164
|
13 WaveAudioSource,
|
amine@164
|
14 )
|
amine@164
|
15 from test_util import PURE_TONE_DICT
|
amine@164
|
16
|
amine@164
|
17
|
amine@164
|
18 def audio_source_read_all_gen(audio_source, size=None):
|
amine@164
|
19 if size is None:
|
amine@164
|
20 size = int(audio_source.sr * 0.1) # 100ms
|
amine@164
|
21 while True:
|
amine@164
|
22 data = audio_source.read(size)
|
amine@164
|
23 if data is None:
|
amine@164
|
24 break
|
amine@164
|
25 yield data
|
amine@164
|
26
|
amine@164
|
27
|
amine@164
|
28 @genty
|
amine@164
|
29 class TestAudioSource(unittest.TestCase):
|
amine@164
|
30
|
amine@164
|
31 # TODO when use_channel is None, return samples from all channels
|
amine@164
|
32
|
amine@164
|
33 @genty_dataset(
|
amine@164
|
34 mono_default=("mono_400Hz", 1, None, 400),
|
amine@164
|
35 mono_mix=("mono_400Hz", 1, "mix", 400),
|
amine@164
|
36 mono_channel_selection=("mono_400Hz", 1, 2, 400),
|
amine@164
|
37 multichannel_default=("3channel_400-800-1600Hz", 3, None, 400),
|
amine@208
|
38 multichannel_channel_select_1st=("3channel_400-800-1600Hz", 3, 1, 400),
|
amine@208
|
39 multichannel_channel_select_2nd=("3channel_400-800-1600Hz", 3, 2, 800),
|
amine@209
|
40 multichannel_channel_select_3rd=(
|
amine@209
|
41 "3channel_400-800-1600Hz",
|
amine@209
|
42 3,
|
amine@209
|
43 3,
|
amine@209
|
44 1600,
|
amine@209
|
45 ),
|
amine@164
|
46 )
|
amine@164
|
47 def test_RawAudioSource(
|
amine@164
|
48 self, file_suffix, channels, use_channel, frequency
|
amine@164
|
49 ):
|
amine@164
|
50 file = "tests/data/test_16KHZ_{}.raw".format(file_suffix)
|
amine@164
|
51 audio_source = RawAudioSource(file, 16000, 2, channels, use_channel)
|
amine@164
|
52 audio_source.open()
|
amine@164
|
53 data = b"".join(audio_source_read_all_gen(audio_source))
|
amine@164
|
54 audio_source.close()
|
amine@164
|
55 expected = _array_to_bytes(PURE_TONE_DICT[frequency])
|
amine@164
|
56 self.assertEqual(data, expected)
|
amine@164
|
57
|
amine@164
|
58 def test_RawAudioSource_mix(self):
|
amine@164
|
59 file = "tests/data/test_16KHZ_3channel_400-800-1600Hz.raw"
|
amine@164
|
60 audio_source = RawAudioSource(file, 16000, 2, 3, use_channel="mix")
|
amine@164
|
61 audio_source.open()
|
amine@164
|
62 data = b"".join(audio_source_read_all_gen(audio_source))
|
amine@164
|
63 audio_source.close()
|
amine@164
|
64
|
amine@164
|
65 mono_channels = [PURE_TONE_DICT[freq] for freq in [400, 800, 1600]]
|
amine@164
|
66 fmt = DATA_FORMAT[2]
|
amine@164
|
67 expected = _array_to_bytes(
|
amine@164
|
68 array(fmt, (sum(samples) // 3 for samples in zip(*mono_channels)))
|
amine@164
|
69 )
|
amine@164
|
70 expected = expected
|
amine@164
|
71 self.assertEqual(data, expected)
|
amine@164
|
72
|
amine@164
|
73 @genty_dataset(
|
amine@164
|
74 mono_default=("mono_400Hz", 1, None, 400),
|
amine@164
|
75 mono_mix=("mono_400Hz", 1, "mix", 400),
|
amine@164
|
76 mono_channel_selection=("mono_400Hz", 1, 2, 400),
|
amine@164
|
77 multichannel_default=("3channel_400-800-1600Hz", 3, None, 400),
|
amine@208
|
78 multichannel_channel_select_1st=("3channel_400-800-1600Hz", 3, 1, 400),
|
amine@208
|
79 multichannel_channel_select_2nd=("3channel_400-800-1600Hz", 3, 2, 800),
|
amine@209
|
80 multichannel_channel_select_3rd=(
|
amine@209
|
81 "3channel_400-800-1600Hz",
|
amine@209
|
82 3,
|
amine@209
|
83 3,
|
amine@209
|
84 1600,
|
amine@209
|
85 ),
|
amine@164
|
86 )
|
amine@164
|
87 def test_WaveAudioSource(
|
amine@164
|
88 self, file_suffix, channels, use_channel, frequency
|
amine@164
|
89 ):
|
amine@164
|
90 file = "tests/data/test_16KHZ_{}.wav".format(file_suffix)
|
amine@164
|
91 audio_source = WaveAudioSource(file, use_channel)
|
amine@164
|
92 audio_source.open()
|
amine@164
|
93 data = b"".join(audio_source_read_all_gen(audio_source))
|
amine@164
|
94 audio_source.close()
|
amine@164
|
95 expected = _array_to_bytes(PURE_TONE_DICT[frequency])
|
amine@164
|
96 self.assertEqual(data, expected)
|
amine@164
|
97
|
amine@164
|
98 def test_WaveAudioSource_mix(self):
|
amine@164
|
99 file = "tests/data/test_16KHZ_3channel_400-800-1600Hz.wav"
|
amine@164
|
100 audio_source = WaveAudioSource(file, use_channel="mix")
|
amine@164
|
101 audio_source.open()
|
amine@164
|
102 data = b"".join(audio_source_read_all_gen(audio_source))
|
amine@164
|
103 audio_source.close()
|
amine@164
|
104
|
amine@164
|
105 mono_channels = [PURE_TONE_DICT[freq] for freq in [400, 800, 1600]]
|
amine@164
|
106 fmt = DATA_FORMAT[2]
|
amine@164
|
107 expected = _array_to_bytes(
|
amine@164
|
108 array(fmt, (sum(samples) // 3 for samples in zip(*mono_channels)))
|
amine@164
|
109 )
|
amine@164
|
110 self.assertEqual(data, expected)
|
amine@164
|
111
|
amine@164
|
112
|
amine@166
|
113 @genty
|
amine@164
|
114 class TestBufferAudioSource_SR10_SW1_CH1(unittest.TestCase):
|
amine@164
|
115 def setUp(self):
|
amine@164
|
116 self.data = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"
|
amine@164
|
117 self.audio_source = BufferAudioSource(
|
amine@164
|
118 data_buffer=self.data, sampling_rate=10, sample_width=1, channels=1
|
amine@164
|
119 )
|
amine@164
|
120 self.audio_source.open()
|
amine@164
|
121
|
amine@164
|
122 def tearDown(self):
|
amine@164
|
123 self.audio_source.close()
|
amine@164
|
124
|
amine@164
|
125 def test_sr10_sw1_ch1_read_1(self):
|
amine@164
|
126 block = self.audio_source.read(1)
|
amine@164
|
127 exp = b"A"
|
amine@164
|
128 self.assertEqual(
|
amine@164
|
129 block,
|
amine@164
|
130 exp,
|
amine@164
|
131 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
132 )
|
amine@164
|
133
|
amine@164
|
134 def test_sr10_sw1_ch1_read_6(self):
|
amine@164
|
135 block = self.audio_source.read(6)
|
amine@164
|
136 exp = b"ABCDEF"
|
amine@164
|
137 self.assertEqual(
|
amine@164
|
138 block,
|
amine@164
|
139 exp,
|
amine@164
|
140 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
141 )
|
amine@164
|
142
|
amine@164
|
143 def test_sr10_sw1_ch1_read_multiple(self):
|
amine@164
|
144 block = self.audio_source.read(1)
|
amine@164
|
145 exp = b"A"
|
amine@164
|
146 self.assertEqual(
|
amine@164
|
147 block,
|
amine@164
|
148 exp,
|
amine@164
|
149 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
150 )
|
amine@164
|
151
|
amine@164
|
152 block = self.audio_source.read(6)
|
amine@164
|
153 exp = b"BCDEFG"
|
amine@164
|
154 self.assertEqual(
|
amine@164
|
155 block,
|
amine@164
|
156 exp,
|
amine@164
|
157 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
158 )
|
amine@164
|
159
|
amine@164
|
160 block = self.audio_source.read(13)
|
amine@164
|
161 exp = b"HIJKLMNOPQRST"
|
amine@164
|
162 self.assertEqual(
|
amine@164
|
163 block,
|
amine@164
|
164 exp,
|
amine@164
|
165 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
166 )
|
amine@164
|
167
|
amine@164
|
168 block = self.audio_source.read(9999)
|
amine@164
|
169 exp = b"UVWXYZ012345"
|
amine@164
|
170 self.assertEqual(
|
amine@164
|
171 block,
|
amine@164
|
172 exp,
|
amine@164
|
173 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
174 )
|
amine@164
|
175
|
amine@164
|
176 def test_sr10_sw1_ch1_read_all(self):
|
amine@164
|
177 block = self.audio_source.read(9999)
|
amine@164
|
178 self.assertEqual(
|
amine@164
|
179 block,
|
amine@164
|
180 self.data,
|
amine@164
|
181 msg="wrong block, expected: {}, found: {} ".format(
|
amine@164
|
182 self.data, block
|
amine@164
|
183 ),
|
amine@164
|
184 )
|
amine@164
|
185
|
amine@164
|
186 block = self.audio_source.read(1)
|
amine@164
|
187 self.assertEqual(
|
amine@164
|
188 block,
|
amine@164
|
189 None,
|
amine@164
|
190 msg="wrong block, expected: {}, found: {} ".format(None, block),
|
amine@164
|
191 )
|
amine@164
|
192
|
amine@164
|
193 def test_sr10_sw1_ch1_get_sampling_rate(self):
|
amine@164
|
194 srate = self.audio_source.get_sampling_rate()
|
amine@164
|
195 self.assertEqual(
|
amine@164
|
196 srate,
|
amine@164
|
197 10,
|
amine@164
|
198 msg="wrong sampling rate, expected: 10, found: {0} ".format(srate),
|
amine@164
|
199 )
|
amine@164
|
200
|
amine@164
|
201 def test_sr10_sw1_ch1_get_sample_width(self):
|
amine@164
|
202 swidth = self.audio_source.get_sample_width()
|
amine@164
|
203 self.assertEqual(
|
amine@164
|
204 swidth,
|
amine@164
|
205 1,
|
amine@164
|
206 msg="wrong sample width, expected: 1, found: {0} ".format(swidth),
|
amine@164
|
207 )
|
amine@164
|
208
|
amine@164
|
209 def test_sr10_sw1_ch1_get_channels(self):
|
amine@164
|
210 channels = self.audio_source.get_channels()
|
amine@164
|
211 self.assertEqual(
|
amine@164
|
212 channels,
|
amine@164
|
213 1,
|
amine@164
|
214 msg="wrong number of channels, expected: 1, found: {0} ".format(
|
amine@164
|
215 channels
|
amine@164
|
216 ),
|
amine@164
|
217 )
|
amine@164
|
218
|
amine@166
|
219 @genty_dataset(
|
amine@166
|
220 empty=([], 0, 0, 0),
|
amine@166
|
221 zero=([0], 0, 0, 0),
|
amine@166
|
222 five=([5], 5, 0.5, 500),
|
amine@166
|
223 multiple=([5, 20], 25, 2.5, 2500),
|
amine@166
|
224 )
|
amine@166
|
225 def test_position(
|
amine@166
|
226 self, block_sizes, expected_sample, expected_second, expected_ms
|
amine@166
|
227 ):
|
amine@166
|
228 for block_size in block_sizes:
|
amine@166
|
229 self.audio_source.read(block_size)
|
amine@166
|
230 position = self.audio_source.position
|
amine@166
|
231 self.assertEqual(
|
amine@166
|
232 position,
|
amine@166
|
233 expected_sample,
|
amine@166
|
234 msg="wrong stream position, expected: {}, found: {}".format(
|
amine@166
|
235 expected_sample, position
|
amine@166
|
236 ),
|
amine@166
|
237 )
|
amine@166
|
238
|
amine@166
|
239 position_s = self.audio_source.position_s
|
amine@166
|
240 self.assertEqual(
|
amine@166
|
241 position_s,
|
amine@166
|
242 expected_second,
|
amine@166
|
243 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@166
|
244 expected_second, position_s
|
amine@166
|
245 ),
|
amine@166
|
246 )
|
amine@166
|
247
|
amine@166
|
248 position_ms = self.audio_source.position_ms
|
amine@166
|
249 self.assertEqual(
|
amine@166
|
250 position_ms,
|
amine@166
|
251 expected_ms,
|
amine@166
|
252 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@166
|
253 expected_ms, position_ms
|
amine@166
|
254 ),
|
amine@166
|
255 )
|
amine@166
|
256
|
amine@166
|
257 @genty_dataset(
|
amine@166
|
258 zero=(0, 0, 0, 0),
|
amine@166
|
259 one=(1, 1, 0.1, 100),
|
amine@166
|
260 ten=(10, 10, 1, 1000),
|
amine@166
|
261 negative_1=(-1, 31, 3.1, 3100),
|
amine@166
|
262 negative_2=(-7, 25, 2.5, 2500),
|
amine@166
|
263 )
|
amine@166
|
264 def test_position_setter(
|
amine@166
|
265 self, position, expected_sample, expected_second, expected_ms
|
amine@166
|
266 ):
|
amine@166
|
267 self.audio_source.position = position
|
amine@166
|
268
|
amine@166
|
269 position = self.audio_source.position
|
amine@166
|
270 self.assertEqual(
|
amine@166
|
271 position,
|
amine@166
|
272 expected_sample,
|
amine@166
|
273 msg="wrong stream position, expected: {}, found: {}".format(
|
amine@166
|
274 expected_sample, position
|
amine@166
|
275 ),
|
amine@166
|
276 )
|
amine@166
|
277
|
amine@166
|
278 position_s = self.audio_source.position_s
|
amine@166
|
279 self.assertEqual(
|
amine@166
|
280 position_s,
|
amine@166
|
281 expected_second,
|
amine@166
|
282 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@166
|
283 expected_second, position_s
|
amine@166
|
284 ),
|
amine@166
|
285 )
|
amine@166
|
286
|
amine@166
|
287 position_ms = self.audio_source.position_ms
|
amine@166
|
288 self.assertEqual(
|
amine@166
|
289 position_ms,
|
amine@166
|
290 expected_ms,
|
amine@166
|
291 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@166
|
292 expected_ms, position_ms
|
amine@166
|
293 ),
|
amine@166
|
294 )
|
amine@166
|
295
|
amine@166
|
296 @genty_dataset(
|
amine@166
|
297 zero=(0, 0, 0, 0),
|
amine@166
|
298 one=(0.1, 1, 0.1, 100),
|
amine@166
|
299 ten=(1, 10, 1, 1000),
|
amine@166
|
300 negative_1=(-0.1, 31, 3.1, 3100),
|
amine@166
|
301 negative_2=(-0.7, 25, 2.5, 2500),
|
amine@166
|
302 )
|
amine@166
|
303 def test_position_s_setter(
|
amine@166
|
304 self, position_s, expected_sample, expected_second, expected_ms
|
amine@166
|
305 ):
|
amine@166
|
306 self.audio_source.position_s = position_s
|
amine@166
|
307
|
amine@166
|
308 position = self.audio_source.position
|
amine@166
|
309 self.assertEqual(
|
amine@166
|
310 position,
|
amine@166
|
311 expected_sample,
|
amine@166
|
312 msg="wrong stream position, expected: {}, found: {}".format(
|
amine@166
|
313 expected_sample, position
|
amine@166
|
314 ),
|
amine@166
|
315 )
|
amine@166
|
316
|
amine@166
|
317 position_s = self.audio_source.position_s
|
amine@166
|
318 self.assertEqual(
|
amine@166
|
319 position_s,
|
amine@166
|
320 expected_second,
|
amine@166
|
321 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@166
|
322 expected_second, position_s
|
amine@166
|
323 ),
|
amine@166
|
324 )
|
amine@166
|
325
|
amine@166
|
326 position_ms = self.audio_source.position_ms
|
amine@166
|
327 self.assertEqual(
|
amine@166
|
328 position_ms,
|
amine@166
|
329 expected_ms,
|
amine@166
|
330 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@166
|
331 expected_ms, position_ms
|
amine@166
|
332 ),
|
amine@166
|
333 )
|
amine@166
|
334
|
amine@166
|
335 @genty_dataset(
|
amine@166
|
336 zero=(0, 0, 0, 0),
|
amine@166
|
337 one=(100, 1, 0.1, 100),
|
amine@166
|
338 ten=(1000, 10, 1, 1000),
|
amine@166
|
339 negative_1=(-100, 31, 3.1, 3100),
|
amine@166
|
340 negative_2=(-700, 25, 2.5, 2500),
|
amine@166
|
341 )
|
amine@166
|
342 def test_position_ms_setter(
|
amine@166
|
343 self, position_ms, expected_sample, expected_second, expected_ms
|
amine@166
|
344 ):
|
amine@166
|
345 self.audio_source.position_ms = position_ms
|
amine@166
|
346
|
amine@166
|
347 position = self.audio_source.position
|
amine@166
|
348 self.assertEqual(
|
amine@166
|
349 position,
|
amine@166
|
350 expected_sample,
|
amine@166
|
351 msg="wrong stream position, expected: {}, found: {}".format(
|
amine@166
|
352 expected_sample, position
|
amine@166
|
353 ),
|
amine@166
|
354 )
|
amine@166
|
355
|
amine@166
|
356 position_s = self.audio_source.position_s
|
amine@166
|
357 self.assertEqual(
|
amine@166
|
358 position_s,
|
amine@166
|
359 expected_second,
|
amine@166
|
360 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@166
|
361 expected_second, position_s
|
amine@166
|
362 ),
|
amine@166
|
363 )
|
amine@166
|
364
|
amine@166
|
365 position_ms = self.audio_source.position_ms
|
amine@166
|
366 self.assertEqual(
|
amine@166
|
367 position_ms,
|
amine@166
|
368 expected_ms,
|
amine@166
|
369 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@166
|
370 expected_ms, position_ms
|
amine@166
|
371 ),
|
amine@166
|
372 )
|
amine@166
|
373
|
amine@166
|
374 @genty_dataset(positive=((100,)), negative=(-100,))
|
amine@166
|
375 def test_position_setter_out_of_range(self, position):
|
amine@166
|
376 with self.assertRaises(IndexError):
|
amine@166
|
377 self.audio_source.position = position
|
amine@166
|
378
|
amine@166
|
379 @genty_dataset(positive=((100,)), negative=(-100,))
|
amine@166
|
380 def test_position_s_setter_out_of_range(self, position_s):
|
amine@166
|
381 with self.assertRaises(IndexError):
|
amine@166
|
382 self.audio_source.position_s = position_s
|
amine@166
|
383
|
amine@166
|
384 @genty_dataset(positive=((10000,)), negative=(-10000,))
|
amine@166
|
385 def test_position_ms_setter_out_of_range(self, position_ms):
|
amine@166
|
386 with self.assertRaises(IndexError):
|
amine@166
|
387 self.audio_source.position_ms = position_ms
|
amine@166
|
388
|
amine@216
|
389 def test_sr10_sw1_ch1_position_s_0(self):
|
amine@216
|
390 tp = self.audio_source.position_s
|
amine@164
|
391 self.assertEqual(
|
amine@164
|
392 tp,
|
amine@164
|
393 0.0,
|
amine@164
|
394 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
|
amine@164
|
395 )
|
amine@164
|
396
|
amine@216
|
397 def test_sr10_sw1_ch1_position_s_1(self):
|
amine@164
|
398 srate = self.audio_source.get_sampling_rate()
|
amine@164
|
399 # read one second
|
amine@164
|
400 self.audio_source.read(srate)
|
amine@216
|
401 tp = self.audio_source.position_s
|
amine@164
|
402 self.assertEqual(
|
amine@164
|
403 tp,
|
amine@164
|
404 1.0,
|
amine@164
|
405 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
|
amine@164
|
406 )
|
amine@164
|
407
|
amine@216
|
408 def test_sr10_sw1_ch1_position_s_2_5(self):
|
amine@164
|
409 # read 2.5 seconds
|
amine@164
|
410 self.audio_source.read(25)
|
amine@216
|
411 tp = self.audio_source.position_s
|
amine@164
|
412 self.assertEqual(
|
amine@164
|
413 tp,
|
amine@164
|
414 2.5,
|
amine@164
|
415 msg="wrong time position, expected: 2.5, found: {0} ".format(tp),
|
amine@164
|
416 )
|
amine@164
|
417
|
amine@216
|
418 def test_sr10_sw1_ch1_position_s_0(self):
|
amine@164
|
419 self.audio_source.read(10)
|
amine@216
|
420 self.audio_source.position_s = 0
|
amine@216
|
421 tp = self.audio_source.position_s
|
amine@164
|
422 self.assertEqual(
|
amine@164
|
423 tp,
|
amine@164
|
424 0.0,
|
amine@164
|
425 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
|
amine@164
|
426 )
|
amine@164
|
427
|
amine@216
|
428 def test_sr10_sw1_ch1_position_s_1(self):
|
amine@216
|
429 self.audio_source.position_s = 1
|
amine@216
|
430 tp = self.audio_source.position_s
|
amine@164
|
431 self.assertEqual(
|
amine@164
|
432 tp,
|
amine@164
|
433 1.0,
|
amine@164
|
434 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
|
amine@164
|
435 )
|
amine@164
|
436
|
amine@164
|
437 def test_sr10_sw1_ch1_rewind(self):
|
amine@164
|
438 self.audio_source.read(10)
|
amine@164
|
439 self.audio_source.rewind()
|
amine@169
|
440 tp = self.audio_source.position
|
amine@164
|
441 self.assertEqual(
|
amine@164
|
442 tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)
|
amine@164
|
443 )
|
amine@164
|
444
|
amine@164
|
445 def test_sr10_sw1_ch1_set_data(self):
|
amine@164
|
446 self.audio_source.set_data(b"12345")
|
amine@164
|
447 block = self.audio_source.read(9999)
|
amine@164
|
448 self.assertEqual(
|
amine@164
|
449 block,
|
amine@164
|
450 b"12345",
|
amine@164
|
451 msg="wrong block, expected: '12345', found: {0} ".format(block),
|
amine@164
|
452 )
|
amine@164
|
453
|
amine@164
|
454 def test_sr10_sw1_ch1_read_closed(self):
|
amine@164
|
455 self.audio_source.close()
|
amine@164
|
456 with self.assertRaises(Exception):
|
amine@164
|
457 self.audio_source.read(1)
|
amine@164
|
458
|
amine@209
|
459
|
amine@169
|
460 @genty
|
amine@164
|
461 class TestBufferAudioSource_SR16_SW2_CH1(unittest.TestCase):
|
amine@164
|
462 def setUp(self):
|
amine@164
|
463 self.data = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"
|
amine@164
|
464 self.audio_source = BufferAudioSource(
|
amine@164
|
465 data_buffer=self.data, sampling_rate=16, sample_width=2, channels=1
|
amine@164
|
466 )
|
amine@164
|
467 self.audio_source.open()
|
amine@164
|
468
|
amine@164
|
469 def tearDown(self):
|
amine@164
|
470 self.audio_source.close()
|
amine@164
|
471
|
amine@164
|
472 def test_sr16_sw2_ch1_read_1(self):
|
amine@164
|
473 block = self.audio_source.read(1)
|
amine@164
|
474 exp = b"AB"
|
amine@164
|
475 self.assertEqual(
|
amine@164
|
476 block,
|
amine@164
|
477 exp,
|
amine@164
|
478 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
479 )
|
amine@164
|
480
|
amine@164
|
481 def test_sr16_sw2_ch1_read_6(self):
|
amine@164
|
482 block = self.audio_source.read(6)
|
amine@164
|
483 exp = b"ABCDEFGHIJKL"
|
amine@164
|
484 self.assertEqual(
|
amine@164
|
485 block,
|
amine@164
|
486 exp,
|
amine@164
|
487 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
488 )
|
amine@164
|
489
|
amine@164
|
490 def test_sr16_sw2_ch1_read_multiple(self):
|
amine@164
|
491 block = self.audio_source.read(1)
|
amine@164
|
492 exp = b"AB"
|
amine@164
|
493 self.assertEqual(
|
amine@164
|
494 block,
|
amine@164
|
495 exp,
|
amine@164
|
496 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
497 )
|
amine@164
|
498
|
amine@164
|
499 block = self.audio_source.read(6)
|
amine@164
|
500 exp = b"CDEFGHIJKLMN"
|
amine@164
|
501 self.assertEqual(
|
amine@164
|
502 block,
|
amine@164
|
503 exp,
|
amine@164
|
504 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
505 )
|
amine@164
|
506
|
amine@164
|
507 block = self.audio_source.read(5)
|
amine@164
|
508 exp = b"OPQRSTUVWX"
|
amine@164
|
509 self.assertEqual(
|
amine@164
|
510 block,
|
amine@164
|
511 exp,
|
amine@164
|
512 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
513 )
|
amine@164
|
514
|
amine@164
|
515 block = self.audio_source.read(9999)
|
amine@164
|
516 exp = b"YZ012345"
|
amine@164
|
517 self.assertEqual(
|
amine@164
|
518 block,
|
amine@164
|
519 exp,
|
amine@164
|
520 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
521 )
|
amine@164
|
522
|
amine@164
|
523 def test_sr16_sw2_ch1_read_all(self):
|
amine@164
|
524 block = self.audio_source.read(9999)
|
amine@164
|
525 self.assertEqual(
|
amine@164
|
526 block,
|
amine@164
|
527 self.data,
|
amine@164
|
528 msg="wrong block, expected: {0}, found: {1} ".format(
|
amine@164
|
529 self.data, block
|
amine@164
|
530 ),
|
amine@164
|
531 )
|
amine@164
|
532
|
amine@164
|
533 block = self.audio_source.read(1)
|
amine@164
|
534 self.assertEqual(
|
amine@164
|
535 block,
|
amine@164
|
536 None,
|
amine@164
|
537 msg="wrong block, expected: {0}, found: {1} ".format(None, block),
|
amine@164
|
538 )
|
amine@164
|
539
|
amine@164
|
540 def test_sr16_sw2_ch1_get_sampling_rate(self):
|
amine@164
|
541 srate = self.audio_source.get_sampling_rate()
|
amine@164
|
542 self.assertEqual(
|
amine@164
|
543 srate,
|
amine@164
|
544 16,
|
amine@164
|
545 msg="wrong sampling rate, expected: 10, found: {0} ".format(srate),
|
amine@164
|
546 )
|
amine@164
|
547
|
amine@164
|
548 def test_sr16_sw2_ch1_get_sample_width(self):
|
amine@164
|
549 swidth = self.audio_source.get_sample_width()
|
amine@164
|
550 self.assertEqual(
|
amine@164
|
551 swidth,
|
amine@164
|
552 2,
|
amine@164
|
553 msg="wrong sample width, expected: 1, found: {0} ".format(swidth),
|
amine@164
|
554 )
|
amine@164
|
555
|
amine@164
|
556 def test_sr16_sw2_ch1_get_channels(self):
|
amine@164
|
557
|
amine@164
|
558 channels = self.audio_source.get_channels()
|
amine@164
|
559 self.assertEqual(
|
amine@164
|
560 channels,
|
amine@164
|
561 1,
|
amine@164
|
562 msg="wrong number of channels, expected: 1, found: {0} ".format(
|
amine@164
|
563 channels
|
amine@164
|
564 ),
|
amine@164
|
565 )
|
amine@164
|
566
|
amine@169
|
567 @genty_dataset(
|
amine@169
|
568 empty=([], 0, 0, 0),
|
amine@169
|
569 zero=([0], 0, 0, 0),
|
amine@209
|
570 two=([2], 2, 2 / 16, int(2000 / 16)),
|
amine@209
|
571 eleven=([11], 11, 11 / 16, int(11 * 1000 / 16)),
|
amine@169
|
572 multiple=([4, 8], 12, 0.75, 750),
|
amine@169
|
573 )
|
amine@169
|
574 def test_position(
|
amine@169
|
575 self, block_sizes, expected_sample, expected_second, expected_ms
|
amine@169
|
576 ):
|
amine@169
|
577 for block_size in block_sizes:
|
amine@169
|
578 self.audio_source.read(block_size)
|
amine@169
|
579 position = self.audio_source.position
|
amine@169
|
580 self.assertEqual(
|
amine@169
|
581 position,
|
amine@169
|
582 expected_sample,
|
amine@169
|
583 msg="wrong stream position, expected: {}, found: {}".format(
|
amine@169
|
584 expected_sample, position
|
amine@169
|
585 ),
|
amine@169
|
586 )
|
amine@169
|
587
|
amine@169
|
588 position_s = self.audio_source.position_s
|
amine@169
|
589 self.assertEqual(
|
amine@169
|
590 position_s,
|
amine@169
|
591 expected_second,
|
amine@169
|
592 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@169
|
593 expected_second, position_s
|
amine@169
|
594 ),
|
amine@169
|
595 )
|
amine@169
|
596
|
amine@169
|
597 position_ms = self.audio_source.position_ms
|
amine@169
|
598 self.assertEqual(
|
amine@169
|
599 position_ms,
|
amine@169
|
600 expected_ms,
|
amine@169
|
601 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@169
|
602 expected_ms, position_ms
|
amine@169
|
603 ),
|
amine@169
|
604 )
|
amine@169
|
605
|
amine@216
|
606 def test_sr16_sw2_ch1_read_position_0(self):
|
amine@169
|
607 self.audio_source.read(10)
|
amine@169
|
608 self.audio_source.position = 0
|
amine@169
|
609 pos = self.audio_source.position
|
amine@164
|
610 self.assertEqual(
|
amine@164
|
611 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
|
amine@164
|
612 )
|
amine@164
|
613
|
amine@169
|
614 @genty_dataset(
|
amine@169
|
615 zero=(0, 0, 0, 0),
|
amine@209
|
616 one=(1, 1, 1 / 16, int(1000 / 16)),
|
amine@209
|
617 ten=(10, 10, 10 / 16, int(10000 / 16)),
|
amine@209
|
618 negative_1=(-1, 15, 15 / 16, int(15000 / 16)),
|
amine@209
|
619 negative_2=(-7, 9, 9 / 16, int(9000 / 16)),
|
amine@169
|
620 )
|
amine@169
|
621 def test_position_setter(
|
amine@169
|
622 self, position, expected_sample, expected_second, expected_ms
|
amine@169
|
623 ):
|
amine@169
|
624 self.audio_source.position = position
|
amine@169
|
625
|
amine@169
|
626 position = self.audio_source.position
|
amine@164
|
627 self.assertEqual(
|
amine@169
|
628 position,
|
amine@169
|
629 expected_sample,
|
amine@169
|
630 msg="wrong stream position, expected: {}, found: {}".format(
|
amine@169
|
631 expected_sample, position
|
amine@169
|
632 ),
|
amine@164
|
633 )
|
amine@164
|
634
|
amine@169
|
635 position_s = self.audio_source.position_s
|
amine@164
|
636 self.assertEqual(
|
amine@169
|
637 position_s,
|
amine@169
|
638 expected_second,
|
amine@169
|
639 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@169
|
640 expected_second, position_s
|
amine@169
|
641 ),
|
amine@164
|
642 )
|
amine@164
|
643
|
amine@169
|
644 position_ms = self.audio_source.position_ms
|
amine@164
|
645 self.assertEqual(
|
amine@169
|
646 position_ms,
|
amine@169
|
647 expected_ms,
|
amine@169
|
648 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@169
|
649 expected_ms, position_ms
|
amine@169
|
650 ),
|
amine@164
|
651 )
|
amine@164
|
652
|
amine@169
|
653 @genty_dataset(
|
amine@169
|
654 zero=(0, 0, 0, 0),
|
amine@209
|
655 one=(0.1, 1, 1 / 16, int(1000 / 16)),
|
amine@209
|
656 two=(1 / 8, 2, 1 / 8, int(1 / 8 * 1000)),
|
amine@209
|
657 twelve=(0.75, 12, 0.75, 750),
|
amine@209
|
658 negative_1=(-0.1, 15, 15 / 16, int(15000 / 16)),
|
amine@209
|
659 negative_2=(-0.7, 5, 5 / 16, int(5000 / 16)),
|
amine@169
|
660 )
|
amine@169
|
661 def test_position_s_setter(
|
amine@169
|
662 self, position_s, expected_sample, expected_second, expected_ms
|
amine@169
|
663 ):
|
amine@169
|
664 self.audio_source.position_s = position_s
|
amine@169
|
665
|
amine@169
|
666 position = self.audio_source.position
|
amine@164
|
667 self.assertEqual(
|
amine@169
|
668 position,
|
amine@169
|
669 expected_sample,
|
amine@169
|
670 msg="wrong stream position, expected: {}, found: {}".format(
|
amine@169
|
671 expected_sample, position
|
amine@169
|
672 ),
|
amine@164
|
673 )
|
amine@164
|
674
|
amine@169
|
675 position_s = self.audio_source.position_s
|
amine@164
|
676 self.assertEqual(
|
amine@169
|
677 position_s,
|
amine@169
|
678 expected_second,
|
amine@169
|
679 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@169
|
680 expected_second, position_s
|
amine@169
|
681 ),
|
amine@164
|
682 )
|
amine@164
|
683
|
amine@169
|
684 position_ms = self.audio_source.position_ms
|
amine@164
|
685 self.assertEqual(
|
amine@169
|
686 position_ms,
|
amine@169
|
687 expected_ms,
|
amine@169
|
688 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@169
|
689 expected_ms, position_ms
|
amine@169
|
690 ),
|
amine@164
|
691 )
|
amine@164
|
692
|
amine@169
|
693 @genty_dataset(
|
amine@169
|
694 zero=(0, 0, 0, 0),
|
amine@209
|
695 one=(100, 1, 1 / 16, int(1000 / 16)),
|
amine@169
|
696 ten=(1000, 16, 1, 1000),
|
amine@209
|
697 negative_1=(-100, 15, 15 / 16, int(15 * 1000 / 16)),
|
amine@169
|
698 negative_2=(-500, 8, 0.5, 500),
|
amine@209
|
699 negative_3=(-700, 5, 5 / 16, int(5 * 1000 / 16)),
|
amine@169
|
700 )
|
amine@169
|
701 def test_position_ms_setter(
|
amine@169
|
702 self, position_ms, expected_sample, expected_second, expected_ms
|
amine@169
|
703 ):
|
amine@169
|
704 self.audio_source.position_ms = position_ms
|
amine@169
|
705
|
amine@169
|
706 position = self.audio_source.position
|
amine@164
|
707 self.assertEqual(
|
amine@169
|
708 position,
|
amine@169
|
709 expected_sample,
|
amine@169
|
710 msg="wrong stream position, expected: {}, found: {}".format(
|
amine@169
|
711 expected_sample, position
|
amine@169
|
712 ),
|
amine@164
|
713 )
|
amine@164
|
714
|
amine@169
|
715 position_s = self.audio_source.position_s
|
amine@164
|
716 self.assertEqual(
|
amine@169
|
717 position_s,
|
amine@169
|
718 expected_second,
|
amine@169
|
719 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@169
|
720 expected_second, position_s
|
amine@169
|
721 ),
|
amine@164
|
722 )
|
amine@164
|
723
|
amine@169
|
724 position_ms = self.audio_source.position_ms
|
amine@164
|
725 self.assertEqual(
|
amine@169
|
726 position_ms,
|
amine@169
|
727 expected_ms,
|
amine@169
|
728 msg="wrong stream position_s, expected: {}, found: {}".format(
|
amine@169
|
729 expected_ms, position_ms
|
amine@169
|
730 ),
|
amine@164
|
731 )
|
amine@164
|
732
|
amine@164
|
733 def test_sr16_sw2_ch1_rewind(self):
|
amine@164
|
734 self.audio_source.read(10)
|
amine@164
|
735 self.audio_source.rewind()
|
amine@216
|
736 tp = self.audio_source.position
|
amine@164
|
737 self.assertEqual(
|
amine@164
|
738 tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)
|
amine@164
|
739 )
|
amine@164
|
740
|
amine@164
|
741 def test_sr16_sw2_ch1_set_data(self):
|
amine@164
|
742 self.audio_source.set_data(b"abcdef")
|
amine@164
|
743 block = self.audio_source.read(9999)
|
amine@164
|
744 self.assertEqual(
|
amine@164
|
745 block,
|
amine@164
|
746 b"abcdef",
|
amine@164
|
747 msg="wrong block, expected: 'abcdef', found: {0} ".format(block),
|
amine@164
|
748 )
|
amine@164
|
749
|
amine@164
|
750 def test_sr16_sw2_ch1_set_data_exception(self):
|
amine@164
|
751 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@164
|
752 self.audio_source.set_data("abcde")
|
amine@164
|
753 self.assertEqual(
|
amine@164
|
754 "The length of audio data must be an integer "
|
amine@164
|
755 "multiple of `sample_width * channels`",
|
amine@164
|
756 str(audio_param_err.exception),
|
amine@164
|
757 )
|
amine@164
|
758
|
amine@164
|
759 def test_sr16_sw2_ch1_append_data_exception(self):
|
amine@164
|
760 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@164
|
761 self.audio_source.append_data("abcde")
|
amine@164
|
762 self.assertEqual(
|
amine@164
|
763 "The length of audio data must be an integer "
|
amine@164
|
764 "multiple of `sample_width * channels`",
|
amine@164
|
765 str(audio_param_err.exception),
|
amine@164
|
766 )
|
amine@164
|
767
|
amine@164
|
768
|
amine@164
|
769 class TestBufferAudioSource_SR11_SW4_CH1(unittest.TestCase):
|
amine@164
|
770 def setUp(self):
|
amine@164
|
771 self.data = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefgh"
|
amine@164
|
772 self.audio_source = BufferAudioSource(
|
amine@164
|
773 data_buffer=self.data, sampling_rate=11, sample_width=4, channels=1
|
amine@164
|
774 )
|
amine@164
|
775 self.audio_source.open()
|
amine@164
|
776
|
amine@164
|
777 def tearDown(self):
|
amine@164
|
778 self.audio_source.close()
|
amine@164
|
779
|
amine@164
|
780 def test_sr11_sw4_ch1_read_1(self):
|
amine@164
|
781 block = self.audio_source.read(1)
|
amine@164
|
782 exp = b"ABCD"
|
amine@164
|
783 self.assertEqual(
|
amine@164
|
784 block,
|
amine@164
|
785 exp,
|
amine@164
|
786 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
787 )
|
amine@164
|
788
|
amine@164
|
789 def test_sr11_sw4_ch1_read_6(self):
|
amine@164
|
790 block = self.audio_source.read(6)
|
amine@164
|
791 exp = b"ABCDEFGHIJKLMNOPQRSTUVWX"
|
amine@164
|
792 self.assertEqual(
|
amine@164
|
793 block,
|
amine@164
|
794 exp,
|
amine@164
|
795 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
796 )
|
amine@164
|
797
|
amine@164
|
798 def test_sr11_sw4_ch1_read_multiple(self):
|
amine@164
|
799 block = self.audio_source.read(1)
|
amine@164
|
800 exp = b"ABCD"
|
amine@164
|
801 self.assertEqual(
|
amine@164
|
802 block,
|
amine@164
|
803 exp,
|
amine@164
|
804 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
805 )
|
amine@164
|
806
|
amine@164
|
807 block = self.audio_source.read(6)
|
amine@164
|
808 exp = b"EFGHIJKLMNOPQRSTUVWXYZ01"
|
amine@164
|
809 self.assertEqual(
|
amine@164
|
810 block,
|
amine@164
|
811 exp,
|
amine@164
|
812 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
813 )
|
amine@164
|
814
|
amine@164
|
815 block = self.audio_source.read(3)
|
amine@164
|
816 exp = b"23456789abcd"
|
amine@164
|
817 self.assertEqual(
|
amine@164
|
818 block,
|
amine@164
|
819 exp,
|
amine@164
|
820 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
821 )
|
amine@164
|
822
|
amine@164
|
823 block = self.audio_source.read(9999)
|
amine@164
|
824 exp = b"efgh"
|
amine@164
|
825 self.assertEqual(
|
amine@164
|
826 block,
|
amine@164
|
827 exp,
|
amine@164
|
828 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
829 )
|
amine@164
|
830
|
amine@164
|
831 def test_sr11_sw4_ch1_read_all(self):
|
amine@164
|
832 block = self.audio_source.read(9999)
|
amine@164
|
833 self.assertEqual(
|
amine@164
|
834 block,
|
amine@164
|
835 self.data,
|
amine@164
|
836 msg="wrong block, expected: {0}, found: {1} ".format(
|
amine@164
|
837 self.data, block
|
amine@164
|
838 ),
|
amine@164
|
839 )
|
amine@164
|
840
|
amine@164
|
841 block = self.audio_source.read(1)
|
amine@164
|
842 self.assertEqual(
|
amine@164
|
843 block,
|
amine@164
|
844 None,
|
amine@164
|
845 msg="wrong block, expected: {0}, found: {1} ".format(None, block),
|
amine@164
|
846 )
|
amine@164
|
847
|
amine@164
|
848 def test_sr11_sw4_ch1_get_sampling_rate(self):
|
amine@164
|
849 srate = self.audio_source.get_sampling_rate()
|
amine@164
|
850 self.assertEqual(
|
amine@164
|
851 srate,
|
amine@164
|
852 11,
|
amine@164
|
853 msg="wrong sampling rate, expected: 10, found: {0} ".format(srate),
|
amine@164
|
854 )
|
amine@164
|
855
|
amine@164
|
856 def test_sr11_sw4_ch1_get_sample_width(self):
|
amine@164
|
857 swidth = self.audio_source.get_sample_width()
|
amine@164
|
858 self.assertEqual(
|
amine@164
|
859 swidth,
|
amine@164
|
860 4,
|
amine@164
|
861 msg="wrong sample width, expected: 1, found: {0} ".format(swidth),
|
amine@164
|
862 )
|
amine@164
|
863
|
amine@164
|
864 def test_sr11_sw4_ch1_get_channels(self):
|
amine@164
|
865 channels = self.audio_source.get_channels()
|
amine@164
|
866 self.assertEqual(
|
amine@164
|
867 channels,
|
amine@164
|
868 1,
|
amine@164
|
869 msg="wrong number of channels, expected: 1, found: {0} ".format(
|
amine@164
|
870 channels
|
amine@164
|
871 ),
|
amine@164
|
872 )
|
amine@164
|
873
|
amine@216
|
874 def test_sr11_sw4_ch1_position_0(self):
|
amine@216
|
875 pos = self.audio_source.position
|
amine@164
|
876 self.assertEqual(
|
amine@164
|
877 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
|
amine@164
|
878 )
|
amine@164
|
879
|
amine@216
|
880 def test_sr11_sw4_ch1_position_5(self):
|
amine@164
|
881 self.audio_source.read(5)
|
amine@216
|
882 pos = self.audio_source.position
|
amine@164
|
883 self.assertEqual(
|
amine@164
|
884 pos, 5, msg="wrong position, expected: 5, found: {0} ".format(pos)
|
amine@164
|
885 )
|
amine@164
|
886
|
amine@216
|
887 def test_sr11_sw4_ch1_position_9(self):
|
amine@164
|
888 self.audio_source.read(5)
|
amine@164
|
889 self.audio_source.read(4)
|
amine@216
|
890 pos = self.audio_source.position
|
amine@164
|
891 self.assertEqual(
|
amine@164
|
892 pos, 9, msg="wrong position, expected: 5, found: {0} ".format(pos)
|
amine@164
|
893 )
|
amine@164
|
894
|
amine@216
|
895 def test_sr11_sw4_ch1_position_0(self):
|
amine@164
|
896 self.audio_source.read(10)
|
amine@216
|
897 self.audio_source.position = 0
|
amine@216
|
898 pos = self.audio_source.position
|
amine@164
|
899 self.assertEqual(
|
amine@164
|
900 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
|
amine@164
|
901 )
|
amine@164
|
902
|
amine@216
|
903 def test_sr11_sw4_ch1_position_10(self):
|
amine@216
|
904 self.audio_source.position = 10
|
amine@216
|
905 pos = self.audio_source.position
|
amine@164
|
906 self.assertEqual(
|
amine@164
|
907 pos,
|
amine@164
|
908 10,
|
amine@164
|
909 msg="wrong position, expected: 10, found: {0} ".format(pos),
|
amine@164
|
910 )
|
amine@164
|
911
|
amine@216
|
912 def test_sr11_sw4_ch1_position_s_0(self):
|
amine@216
|
913 tp = self.audio_source.position_s
|
amine@164
|
914 self.assertEqual(
|
amine@164
|
915 tp,
|
amine@164
|
916 0.0,
|
amine@164
|
917 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
|
amine@164
|
918 )
|
amine@164
|
919
|
amine@216
|
920 def test_sr11_sw4_ch1_position_s_1(self):
|
amine@164
|
921 srate = self.audio_source.get_sampling_rate()
|
amine@164
|
922 # read one second
|
amine@164
|
923 self.audio_source.read(srate)
|
amine@216
|
924 tp = self.audio_source.position_s
|
amine@164
|
925 self.assertEqual(
|
amine@164
|
926 tp,
|
amine@164
|
927 1.0,
|
amine@164
|
928 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
|
amine@164
|
929 )
|
amine@164
|
930
|
amine@216
|
931 def test_sr11_sw4_ch1_position_s_0_63(self):
|
amine@164
|
932 # read 2.5 seconds
|
amine@164
|
933 self.audio_source.read(7)
|
amine@216
|
934 tp = self.audio_source.position_s
|
amine@164
|
935 self.assertAlmostEqual(
|
amine@164
|
936 tp,
|
amine@164
|
937 0.636363636364,
|
amine@164
|
938 msg="wrong time position, expected: 0.636363636364, "
|
amine@164
|
939 "found: {0} ".format(tp),
|
amine@164
|
940 )
|
amine@164
|
941
|
amine@216
|
942 def test_sr11_sw4_ch1_position_s_0(self):
|
amine@164
|
943 self.audio_source.read(10)
|
amine@216
|
944 self.audio_source.position_s = 0
|
amine@216
|
945 tp = self.audio_source.position_s
|
amine@164
|
946 self.assertEqual(
|
amine@164
|
947 tp,
|
amine@164
|
948 0.0,
|
amine@164
|
949 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
|
amine@164
|
950 )
|
amine@164
|
951
|
amine@216
|
952 def test_sr11_sw4_ch1_position_s_1(self):
|
amine@164
|
953
|
amine@216
|
954 self.audio_source.position_s = 1
|
amine@216
|
955 tp = self.audio_source.position_s
|
amine@164
|
956 self.assertEqual(
|
amine@164
|
957 tp,
|
amine@164
|
958 1.0,
|
amine@164
|
959 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
|
amine@164
|
960 )
|
amine@164
|
961
|
amine@164
|
962 def test_sr11_sw4_ch1_rewind(self):
|
amine@164
|
963 self.audio_source.read(10)
|
amine@164
|
964 self.audio_source.rewind()
|
amine@169
|
965 tp = self.audio_source.position
|
amine@164
|
966 self.assertEqual(
|
amine@164
|
967 tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)
|
amine@164
|
968 )
|
amine@164
|
969
|
amine@164
|
970 def test_sr11_sw4_ch1_set_data(self):
|
amine@164
|
971 self.audio_source.set_data(b"abcdefgh")
|
amine@164
|
972 block = self.audio_source.read(9999)
|
amine@164
|
973 exp = b"abcdefgh"
|
amine@164
|
974 self.assertEqual(
|
amine@164
|
975 block,
|
amine@164
|
976 exp,
|
amine@164
|
977 msg="wrong block, expected: {}, found: {} ".format(exp, block),
|
amine@164
|
978 )
|
amine@164
|
979
|
amine@164
|
980 def test_sr11_sw4_ch1_set_data_exception(self):
|
amine@164
|
981 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@164
|
982 self.audio_source.set_data(b"abcdef")
|
amine@164
|
983 self.assertEqual(
|
amine@164
|
984 "The length of audio data must be an integer "
|
amine@164
|
985 "multiple of `sample_width * channels`",
|
amine@164
|
986 str(audio_param_err.exception),
|
amine@164
|
987 )
|
amine@164
|
988
|
amine@164
|
989 def test_sr11_sw4_ch1_append_data_exception(self):
|
amine@164
|
990 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@164
|
991 self.audio_source.append_data(b"abcdef")
|
amine@164
|
992 self.assertEqual(
|
amine@164
|
993 "The length of audio data must be an integer "
|
amine@164
|
994 "multiple of `sample_width * channels`",
|
amine@164
|
995 str(audio_param_err.exception),
|
amine@164
|
996 )
|
amine@164
|
997
|
amine@164
|
998
|
amine@164
|
999 class TestBufferAudioSourceCreationException(unittest.TestCase):
|
amine@164
|
1000 def test_wrong_sample_width_value(self):
|
amine@164
|
1001 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@164
|
1002 _ = BufferAudioSource(
|
amine@164
|
1003 data_buffer=b"ABCDEFGHI",
|
amine@164
|
1004 sampling_rate=9,
|
amine@164
|
1005 sample_width=3,
|
amine@164
|
1006 channels=1,
|
amine@164
|
1007 )
|
amine@164
|
1008 self.assertEqual(
|
amine@164
|
1009 "Sample width must be one of: 1, 2 or 4 (bytes)",
|
amine@164
|
1010 str(audio_param_err.exception),
|
amine@164
|
1011 )
|
amine@164
|
1012
|
amine@164
|
1013 def test_wrong_data_buffer_size(self):
|
amine@164
|
1014 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@164
|
1015 _ = BufferAudioSource(
|
amine@164
|
1016 data_buffer=b"ABCDEFGHI",
|
amine@164
|
1017 sampling_rate=8,
|
amine@164
|
1018 sample_width=2,
|
amine@164
|
1019 channels=1,
|
amine@164
|
1020 )
|
amine@164
|
1021 self.assertEqual(
|
amine@164
|
1022 "The length of audio data must be an integer "
|
amine@164
|
1023 "multiple of `sample_width * channels`",
|
amine@164
|
1024 str(audio_param_err.exception),
|
amine@164
|
1025 )
|
amine@164
|
1026
|
amine@164
|
1027
|
amine@164
|
1028 class TestAudioSourceProperties(unittest.TestCase):
|
amine@164
|
1029 def test_read_properties(self):
|
amine@164
|
1030 data = b""
|
amine@164
|
1031 sampling_rate = 8000
|
amine@164
|
1032 sample_width = 2
|
amine@164
|
1033 channels = 1
|
amine@164
|
1034 a_source = BufferAudioSource(
|
amine@164
|
1035 data, sampling_rate, sample_width, channels
|
amine@164
|
1036 )
|
amine@164
|
1037
|
amine@164
|
1038 self.assertEqual(a_source.sampling_rate, sampling_rate)
|
amine@164
|
1039 self.assertEqual(a_source.sample_width, sample_width)
|
amine@164
|
1040 self.assertEqual(a_source.channels, channels)
|
amine@164
|
1041
|
amine@164
|
1042 def test_set_readonly_properties_exception(self):
|
amine@164
|
1043 data = b""
|
amine@164
|
1044 sampling_rate = 8000
|
amine@164
|
1045 sample_width = 2
|
amine@164
|
1046 channels = 1
|
amine@164
|
1047 a_source = BufferAudioSource(
|
amine@164
|
1048 data, sampling_rate, sample_width, channels
|
amine@164
|
1049 )
|
amine@164
|
1050
|
amine@164
|
1051 with self.assertRaises(AttributeError):
|
amine@164
|
1052 a_source.sampling_rate = 16000
|
amine@164
|
1053 a_source.sample_width = 1
|
amine@164
|
1054 a_source.channels = 2
|
amine@164
|
1055
|
amine@164
|
1056
|
amine@164
|
1057 class TestAudioSourceShortProperties(unittest.TestCase):
|
amine@164
|
1058 def test_read_short_properties(self):
|
amine@164
|
1059 data = b""
|
amine@164
|
1060 sampling_rate = 8000
|
amine@164
|
1061 sample_width = 2
|
amine@164
|
1062 channels = 1
|
amine@164
|
1063 a_source = BufferAudioSource(
|
amine@164
|
1064 data, sampling_rate, sample_width, channels
|
amine@164
|
1065 )
|
amine@164
|
1066
|
amine@164
|
1067 self.assertEqual(a_source.sr, sampling_rate)
|
amine@164
|
1068 self.assertEqual(a_source.sw, sample_width)
|
amine@164
|
1069 self.assertEqual(a_source.ch, channels)
|
amine@164
|
1070
|
amine@164
|
1071 def test_set_readonly_short_properties_exception(self):
|
amine@164
|
1072 data = b""
|
amine@164
|
1073 sampling_rate = 8000
|
amine@164
|
1074 sample_width = 2
|
amine@164
|
1075 channels = 1
|
amine@164
|
1076 a_source = BufferAudioSource(
|
amine@164
|
1077 data, sampling_rate, sample_width, channels
|
amine@164
|
1078 )
|
amine@164
|
1079
|
amine@164
|
1080 with self.assertRaises(AttributeError):
|
amine@164
|
1081 a_source.sr = 16000
|
amine@164
|
1082 a_source.sw = 1
|
amine@164
|
1083 a_source.ch = 2
|
amine@164
|
1084
|
amine@164
|
1085
|
amine@164
|
1086 if __name__ == "__main__":
|
amine@209
|
1087 unittest.main()
|