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