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