amine@192
|
1 import os
|
amine@207
|
2 from unittest import TestCase
|
amine@88
|
3 from random import random
|
amine@192
|
4 from tempfile import TemporaryDirectory
|
amine@86
|
5 from genty import genty, genty_dataset
|
amine@207
|
6 from auditok import split, AudioRegion, AudioParameterError
|
amine@212
|
7 from auditok.util import AudioDataSource
|
amine@210
|
8 from auditok.io import (
|
amine@210
|
9 _normalize_use_channel,
|
amine@210
|
10 _extract_selected_channel,
|
amine@210
|
11 get_audio_source,
|
amine@210
|
12 )
|
amine@86
|
13
|
amine@86
|
14
|
amine@88
|
15 def _make_random_length_regions(
|
amine@88
|
16 byte_seq, sampling_rate, sample_width, channels
|
amine@88
|
17 ):
|
amine@88
|
18 regions = []
|
amine@88
|
19 for b in byte_seq:
|
amine@88
|
20 duration = round(random() * 10, 6)
|
amine@95
|
21 data = b * int(duration * sampling_rate) * sample_width * channels
|
amine@88
|
22 start = round(random() * 13, 3)
|
amine@88
|
23 region = AudioRegion(
|
amine@88
|
24 data, start, sampling_rate, sample_width, channels
|
amine@88
|
25 )
|
amine@88
|
26 regions.append(region)
|
amine@88
|
27 return regions
|
amine@88
|
28
|
amine@88
|
29
|
amine@86
|
30 @genty
|
amine@207
|
31 class TestSplit(TestCase):
|
amine@207
|
32 @genty_dataset(
|
amine@207
|
33 simple=(
|
amine@207
|
34 0.2,
|
amine@207
|
35 5,
|
amine@207
|
36 0.2,
|
amine@207
|
37 False,
|
amine@207
|
38 False,
|
amine@207
|
39 {"eth": 50},
|
amine@207
|
40 [(2, 16), (17, 31), (34, 76)],
|
amine@207
|
41 ),
|
amine@207
|
42 low_energy_threshold=(
|
amine@207
|
43 0.2,
|
amine@207
|
44 5,
|
amine@207
|
45 0.2,
|
amine@207
|
46 False,
|
amine@207
|
47 False,
|
amine@207
|
48 {"energy_threshold": 40},
|
amine@207
|
49 [(0, 50), (50, 76)],
|
amine@207
|
50 ),
|
amine@207
|
51 high_energy_threshold=(
|
amine@207
|
52 0.2,
|
amine@207
|
53 5,
|
amine@207
|
54 0.2,
|
amine@207
|
55 False,
|
amine@207
|
56 False,
|
amine@207
|
57 {"energy_threshold": 60},
|
amine@207
|
58 [],
|
amine@207
|
59 ),
|
amine@207
|
60 trim_leading_and_trailing_silence=(
|
amine@207
|
61 0.2,
|
amine@207
|
62 10, # use long max_dur
|
amine@207
|
63 0.5, # and a max_silence longer than any inter-region silence
|
amine@207
|
64 True,
|
amine@207
|
65 False,
|
amine@207
|
66 {"eth": 50},
|
amine@207
|
67 [(2, 76)],
|
amine@207
|
68 ),
|
amine@207
|
69 drop_trailing_silence=(
|
amine@207
|
70 0.2,
|
amine@207
|
71 5,
|
amine@207
|
72 0.2,
|
amine@207
|
73 True,
|
amine@207
|
74 False,
|
amine@207
|
75 {"eth": 50},
|
amine@207
|
76 [(2, 14), (17, 29), (34, 76)],
|
amine@207
|
77 ),
|
amine@207
|
78 drop_trailing_silence_2=(
|
amine@207
|
79 1.5,
|
amine@207
|
80 5,
|
amine@207
|
81 0.2,
|
amine@207
|
82 True,
|
amine@207
|
83 False,
|
amine@207
|
84 {"eth": 50},
|
amine@207
|
85 [(34, 76)],
|
amine@207
|
86 ),
|
amine@207
|
87 strict_min_dur=(
|
amine@207
|
88 0.3,
|
amine@207
|
89 2,
|
amine@207
|
90 0.2,
|
amine@207
|
91 False,
|
amine@207
|
92 True,
|
amine@207
|
93 {"eth": 50},
|
amine@207
|
94 [(2, 16), (17, 31), (34, 54), (54, 74)],
|
amine@207
|
95 ),
|
amine@207
|
96 )
|
amine@207
|
97 def test_split_params(
|
amine@207
|
98 self,
|
amine@207
|
99 min_dur,
|
amine@207
|
100 max_dur,
|
amine@207
|
101 max_silence,
|
amine@207
|
102 drop_trailing_silence,
|
amine@207
|
103 strict_min_dur,
|
amine@207
|
104 kwargs,
|
amine@207
|
105 expected,
|
amine@207
|
106 ):
|
amine@207
|
107 with open("tests/data/test_split_10HZ_mono.raw", "rb") as fp:
|
amine@207
|
108 data = fp.read()
|
amine@207
|
109
|
amine@207
|
110 regions = split(
|
amine@207
|
111 data,
|
amine@207
|
112 min_dur,
|
amine@207
|
113 max_dur,
|
amine@207
|
114 max_silence,
|
amine@207
|
115 drop_trailing_silence,
|
amine@207
|
116 strict_min_dur,
|
amine@207
|
117 analysis_window=0.1,
|
amine@207
|
118 sr=10,
|
amine@207
|
119 sw=2,
|
amine@207
|
120 ch=1,
|
amine@207
|
121 **kwargs
|
amine@207
|
122 )
|
amine@207
|
123 regions = list(regions)
|
amine@207
|
124 err_msg = "Wrong number of regions after split, expected: "
|
amine@210
|
125 err_msg += "{}, found: {}".format(len(expected), len(regions))
|
amine@207
|
126 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@207
|
127
|
amine@207
|
128 sample_width = 2
|
amine@207
|
129 for reg, exp in zip(regions, expected):
|
amine@207
|
130 onset, offset = exp
|
amine@207
|
131 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@207
|
132 self.assertEqual(bytes(reg), exp_data)
|
amine@207
|
133
|
amine@211
|
134 @genty_dataset(
|
amine@211
|
135 stereo_all_default=(2, {}, [(2, 16), (17, 31), (34, 76)]),
|
amine@213
|
136 mono_max_read=(1, {"max_read": 5}, [(2, 16), (17, 31), (34, 50)]),
|
amine@213
|
137 mono_max_read_short_name=(1, {"mr": 5}, [(2, 16), (17, 31), (34, 50)]),
|
amine@211
|
138 mono_use_channel_1=(
|
amine@211
|
139 1,
|
amine@211
|
140 {"eth": 50, "use_channel": 1},
|
amine@211
|
141 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
142 ),
|
amine@211
|
143 mono_uc_1=(1, {"eth": 50, "uc": 1}, [(2, 16), (17, 31), (34, 76)]),
|
amine@211
|
144 mono_use_channel_left=(
|
amine@211
|
145 1,
|
amine@211
|
146 {"eth": 50, "use_channel": "left"},
|
amine@211
|
147 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
148 ),
|
amine@211
|
149 mono_uc_left=(
|
amine@211
|
150 1,
|
amine@211
|
151 {"eth": 50, "uc": "left"},
|
amine@211
|
152 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
153 ),
|
amine@211
|
154 mono_use_channel_None=(
|
amine@211
|
155 1,
|
amine@211
|
156 {"eth": 50, "use_channel": None},
|
amine@211
|
157 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
158 ),
|
amine@211
|
159 stereo_use_channel_1=(
|
amine@211
|
160 2,
|
amine@211
|
161 {"eth": 50, "use_channel": 1},
|
amine@211
|
162 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
163 ),
|
amine@211
|
164 stereo_use_channel_left=(
|
amine@211
|
165 2,
|
amine@211
|
166 {"eth": 50, "use_channel": "left"},
|
amine@211
|
167 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
168 ),
|
amine@211
|
169 stereo_use_channel_no_use_channel_given=(
|
amine@211
|
170 2,
|
amine@211
|
171 {"eth": 50},
|
amine@211
|
172 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
173 ),
|
amine@211
|
174 stereo_use_channel_minus_2=(
|
amine@211
|
175 2,
|
amine@211
|
176 {"eth": 50, "use_channel": -2},
|
amine@211
|
177 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
178 ),
|
amine@211
|
179 stereo_uc_2=(2, {"eth": 50, "uc": 2}, [(10, 32), (36, 76)]),
|
amine@211
|
180 stereo_use_channel_right=(
|
amine@211
|
181 2,
|
amine@211
|
182 {"eth": 50, "use_channel": "right"},
|
amine@211
|
183 [(10, 32), (36, 76)],
|
amine@211
|
184 ),
|
amine@211
|
185 stereo_uc_minus_1=(2, {"eth": 50, "uc": -1}, [(10, 32), (36, 76)]),
|
amine@213
|
186 mono_uc_mix=(
|
amine@213
|
187 1,
|
amine@213
|
188 {"eth": 50, "uc": "mix"},
|
amine@213
|
189 [(2, 16), (17, 31), (34, 76)],
|
amine@213
|
190 ),
|
amine@213
|
191 stereo_use_channel_mix=(
|
amine@213
|
192 2,
|
amine@213
|
193 {"energy_threshold": 53.5, "use_channel": "mix"},
|
amine@213
|
194 [(54, 76)],
|
amine@213
|
195 ),
|
amine@213
|
196 stereo_uc_mix=(2, {"eth": 52, "uc": "mix"}, [(17, 26), (54, 76)]),
|
amine@213
|
197 stereo_uc_mix_default_eth=(
|
amine@213
|
198 2,
|
amine@213
|
199 {"uc": "mix"},
|
amine@213
|
200 [(10, 16), (17, 31), (36, 76)],
|
amine@213
|
201 ),
|
amine@211
|
202 )
|
amine@211
|
203 def test_split_kwargs(self, channels, kwargs, expected):
|
amine@211
|
204
|
amine@211
|
205 mono_or_stereo = "mono" if channels == 1 else "stereo"
|
amine@211
|
206 filename = "tests/data/test_split_10HZ_{}.raw".format(mono_or_stereo)
|
amine@211
|
207 with open(filename, "rb") as fp:
|
amine@211
|
208 data = fp.read()
|
amine@211
|
209
|
amine@211
|
210 regions = split(
|
amine@211
|
211 data,
|
amine@211
|
212 min_dur=0.2,
|
amine@211
|
213 max_dur=5,
|
amine@211
|
214 max_silence=0.2,
|
amine@211
|
215 drop_trailing_silence=False,
|
amine@211
|
216 strict_min_dur=False,
|
amine@211
|
217 analysis_window=0.1,
|
amine@211
|
218 sr=10,
|
amine@211
|
219 sw=2,
|
amine@211
|
220 ch=channels,
|
amine@211
|
221 **kwargs
|
amine@211
|
222 )
|
amine@212
|
223 regions = list(regions)
|
amine@211
|
224 sample_width = 2
|
amine@211
|
225 import numpy as np
|
amine@211
|
226
|
amine@211
|
227 use_channel = kwargs.get("use_channel", kwargs.get("uc"))
|
amine@211
|
228 # extrat channel of interest
|
amine@211
|
229 if channels != 1:
|
amine@211
|
230 use_channel = kwargs.get("use_channel", kwargs.get("uc"))
|
amine@211
|
231 use_channel = _normalize_use_channel(use_channel)
|
amine@211
|
232 data = _extract_selected_channel(
|
amine@211
|
233 data, channels, sample_width, use_channel=use_channel
|
amine@211
|
234 )
|
amine@211
|
235 err_msg = "Wrong number of regions after split, expected: "
|
amine@211
|
236 err_msg += "{}, found: {}".format(expected, regions)
|
amine@211
|
237 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@212
|
238 for reg, exp in zip(regions, expected):
|
amine@212
|
239 onset, offset = exp
|
amine@212
|
240 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@212
|
241 self.assertEqual(bytes(reg), exp_data)
|
amine@211
|
242
|
amine@212
|
243 @genty_dataset(
|
amine@212
|
244 filename_audio_format=(
|
amine@212
|
245 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
246 {"audio_format": "raw", "sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
247 ),
|
amine@212
|
248 filename_audio_format_short_name=(
|
amine@212
|
249 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
250 {"fmt": "raw", "sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
251 ),
|
amine@212
|
252 filename_no_audio_format=(
|
amine@212
|
253 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
254 {"sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
255 ),
|
amine@212
|
256 filename_no_long_audio_params=(
|
amine@212
|
257 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
258 {"sampling_rate": 10, "sample_width": 2, "channels": 2},
|
amine@212
|
259 ),
|
amine@212
|
260 bytes_=(
|
amine@212
|
261 open("tests/data/test_split_10HZ_stereo.raw", "rb").read(),
|
amine@212
|
262 {"sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
263 ),
|
amine@212
|
264 audio_reader=(
|
amine@212
|
265 AudioDataSource(
|
amine@212
|
266 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
267 sr=10,
|
amine@212
|
268 sw=2,
|
amine@212
|
269 ch=2,
|
amine@212
|
270 block_dur=0.1,
|
amine@212
|
271 ),
|
amine@212
|
272 {},
|
amine@212
|
273 ),
|
amine@212
|
274 audio_region=(
|
amine@212
|
275 AudioRegion(
|
amine@212
|
276 open("tests/data/test_split_10HZ_stereo.raw", "rb").read(),
|
amine@212
|
277 0,
|
amine@212
|
278 10,
|
amine@212
|
279 2,
|
amine@212
|
280 2,
|
amine@212
|
281 ),
|
amine@212
|
282 {},
|
amine@212
|
283 ),
|
amine@212
|
284 audio_source=(
|
amine@212
|
285 get_audio_source(
|
amine@212
|
286 "tests/data/test_split_10HZ_stereo.raw", sr=10, sw=2, ch=2
|
amine@212
|
287 ),
|
amine@212
|
288 {},
|
amine@212
|
289 ),
|
amine@212
|
290 )
|
amine@212
|
291 def test_split_input_type(self, input, kwargs):
|
amine@212
|
292
|
amine@212
|
293 with open("tests/data/test_split_10HZ_mono.raw", "rb") as fp:
|
amine@212
|
294 data = fp.read()
|
amine@212
|
295
|
amine@212
|
296 regions = split(
|
amine@212
|
297 input,
|
amine@212
|
298 min_dur=0.2,
|
amine@212
|
299 max_dur=5,
|
amine@212
|
300 max_silence=0.2,
|
amine@212
|
301 drop_trailing_silence=False,
|
amine@212
|
302 strict_min_dur=False,
|
amine@212
|
303 analysis_window=0.1,
|
amine@212
|
304 **kwargs
|
amine@212
|
305 )
|
amine@212
|
306 regions = list(regions)
|
amine@212
|
307 expected = [(2, 16), (17, 31), (34, 76)]
|
amine@212
|
308 sample_width = 2
|
amine@212
|
309 err_msg = "Wrong number of regions after split, expected: "
|
amine@212
|
310 err_msg += "{}, found: {}".format(expected, regions)
|
amine@212
|
311 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@211
|
312 for reg, exp in zip(regions, expected):
|
amine@211
|
313 onset, offset = exp
|
amine@211
|
314 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@211
|
315 self.assertEqual(bytes(reg), exp_data)
|
amine@211
|
316
|
amine@207
|
317
|
amine@207
|
318 @genty
|
amine@207
|
319 class TestAudioRegion(TestCase):
|
amine@86
|
320 @genty_dataset(
|
amine@86
|
321 simple=(b"\0" * 8000, 0, 8000, 1, 1, 1, 1, 1000),
|
amine@86
|
322 one_ms_less_than_1_sec=(
|
amine@86
|
323 b"\0" * 7992,
|
amine@86
|
324 0,
|
amine@86
|
325 8000,
|
amine@86
|
326 1,
|
amine@86
|
327 1,
|
amine@86
|
328 0.999,
|
amine@86
|
329 0.999,
|
amine@86
|
330 999,
|
amine@86
|
331 ),
|
amine@86
|
332 tree_quarter_ms_less_than_1_sec=(
|
amine@86
|
333 b"\0" * 7994,
|
amine@86
|
334 0,
|
amine@86
|
335 8000,
|
amine@86
|
336 1,
|
amine@86
|
337 1,
|
amine@86
|
338 0.99925,
|
amine@86
|
339 0.99925,
|
amine@86
|
340 999,
|
amine@86
|
341 ),
|
amine@86
|
342 half_ms_less_than_1_sec=(
|
amine@86
|
343 b"\0" * 7996,
|
amine@86
|
344 0,
|
amine@86
|
345 8000,
|
amine@86
|
346 1,
|
amine@86
|
347 1,
|
amine@86
|
348 0.9995,
|
amine@86
|
349 0.9995,
|
amine@86
|
350 1000,
|
amine@86
|
351 ),
|
amine@86
|
352 quarter_ms_less_than_1_sec=(
|
amine@86
|
353 b"\0" * 7998,
|
amine@86
|
354 0,
|
amine@86
|
355 8000,
|
amine@86
|
356 1,
|
amine@86
|
357 1,
|
amine@86
|
358 0.99975,
|
amine@86
|
359 0.99975,
|
amine@86
|
360 1000,
|
amine@86
|
361 ),
|
amine@86
|
362 simple_sample_width_2=(b"\0" * 8000 * 2, 0, 8000, 2, 1, 1, 1, 1000),
|
amine@86
|
363 simple_stereo=(b"\0" * 8000 * 2, 0, 8000, 1, 2, 1, 1, 1000),
|
amine@86
|
364 simple_multichannel=(b"\0" * 8000 * 5, 0, 8000, 1, 5, 1, 1, 1000),
|
amine@86
|
365 simple_sample_width_2_multichannel=(
|
amine@86
|
366 b"\0" * 8000 * 2 * 5,
|
amine@86
|
367 0,
|
amine@86
|
368 8000,
|
amine@86
|
369 2,
|
amine@86
|
370 5,
|
amine@86
|
371 1,
|
amine@86
|
372 1,
|
amine@86
|
373 1000,
|
amine@86
|
374 ),
|
amine@86
|
375 one_ms_less_than_1s_sw_2_multichannel=(
|
amine@86
|
376 b"\0" * 7992 * 2 * 5,
|
amine@86
|
377 0,
|
amine@86
|
378 8000,
|
amine@86
|
379 2,
|
amine@86
|
380 5,
|
amine@86
|
381 0.999,
|
amine@86
|
382 0.999,
|
amine@86
|
383 999,
|
amine@86
|
384 ),
|
amine@86
|
385 tree_qrt_ms_lt_1_s_sw_2_multichannel=(
|
amine@86
|
386 b"\0" * 7994 * 2 * 5,
|
amine@86
|
387 0,
|
amine@86
|
388 8000,
|
amine@86
|
389 2,
|
amine@86
|
390 5,
|
amine@86
|
391 0.99925,
|
amine@86
|
392 0.99925,
|
amine@86
|
393 999,
|
amine@86
|
394 ),
|
amine@86
|
395 half_ms_lt_1s_sw_2_multichannel=(
|
amine@86
|
396 b"\0" * 7996 * 2 * 5,
|
amine@86
|
397 0,
|
amine@86
|
398 8000,
|
amine@86
|
399 2,
|
amine@86
|
400 5,
|
amine@86
|
401 0.9995,
|
amine@86
|
402 0.9995,
|
amine@86
|
403 1000,
|
amine@86
|
404 ),
|
amine@86
|
405 quarter_ms_lt_1s_sw_2_multichannel=(
|
amine@86
|
406 b"\0" * 7998 * 2 * 5,
|
amine@86
|
407 0,
|
amine@86
|
408 8000,
|
amine@86
|
409 2,
|
amine@86
|
410 5,
|
amine@86
|
411 0.99975,
|
amine@86
|
412 0.99975,
|
amine@86
|
413 1000,
|
amine@86
|
414 ),
|
amine@86
|
415 arbitrary_length_1=(
|
amine@86
|
416 b"\0" * int(8000 * 1.33),
|
amine@86
|
417 2.7,
|
amine@86
|
418 8000,
|
amine@86
|
419 1,
|
amine@86
|
420 1,
|
amine@86
|
421 4.03,
|
amine@86
|
422 1.33,
|
amine@86
|
423 1330,
|
amine@86
|
424 ),
|
amine@86
|
425 arbitrary_length_2=(
|
amine@86
|
426 b"\0" * int(8000 * 0.476),
|
amine@86
|
427 11.568,
|
amine@86
|
428 8000,
|
amine@86
|
429 1,
|
amine@86
|
430 1,
|
amine@86
|
431 12.044,
|
amine@86
|
432 0.476,
|
amine@86
|
433 476,
|
amine@86
|
434 ),
|
amine@86
|
435 arbitrary_length_sw_2_multichannel=(
|
amine@86
|
436 b"\0" * int(8000 * 1.711) * 2 * 3,
|
amine@86
|
437 9.415,
|
amine@86
|
438 8000,
|
amine@86
|
439 2,
|
amine@86
|
440 3,
|
amine@86
|
441 11.126,
|
amine@86
|
442 1.711,
|
amine@86
|
443 1711,
|
amine@86
|
444 ),
|
amine@86
|
445 arbitrary_samplig_rate=(
|
amine@86
|
446 b"\0" * int(3172 * 1.318),
|
amine@86
|
447 17.236,
|
amine@86
|
448 3172,
|
amine@86
|
449 1,
|
amine@86
|
450 1,
|
amine@86
|
451 17.236 + int(3172 * 1.318) / 3172,
|
amine@86
|
452 int(3172 * 1.318) / 3172,
|
amine@86
|
453 1318,
|
amine@86
|
454 ),
|
amine@86
|
455 arbitrary_sr_sw_2_multichannel=(
|
amine@86
|
456 b"\0" * int(11317 * 0.716) * 2 * 3,
|
amine@86
|
457 18.811,
|
amine@86
|
458 11317,
|
amine@86
|
459 2,
|
amine@86
|
460 3,
|
amine@86
|
461 18.811 + int(11317 * 0.716) / 11317,
|
amine@86
|
462 int(11317 * 0.716) / 11317,
|
amine@86
|
463 716,
|
amine@86
|
464 ),
|
amine@86
|
465 )
|
amine@86
|
466 def test_creation(
|
amine@86
|
467 self,
|
amine@86
|
468 data,
|
amine@86
|
469 start,
|
amine@86
|
470 sampling_rate,
|
amine@86
|
471 sample_width,
|
amine@86
|
472 channels,
|
amine@86
|
473 expected_end,
|
amine@86
|
474 expected_duration_s,
|
amine@86
|
475 expected_duration_ms,
|
amine@86
|
476 ):
|
amine@86
|
477 region = AudioRegion(
|
amine@86
|
478 data, start, sampling_rate, sample_width, channels
|
amine@86
|
479 )
|
amine@86
|
480 self.assertEqual(region.sampling_rate, sampling_rate)
|
amine@86
|
481 self.assertEqual(region.sr, sampling_rate)
|
amine@86
|
482 self.assertEqual(region.sample_width, sample_width)
|
amine@86
|
483 self.assertEqual(region.sw, sample_width)
|
amine@86
|
484 self.assertEqual(region.channels, channels)
|
amine@86
|
485 self.assertEqual(region.ch, channels)
|
amine@86
|
486 self.assertEqual(region.start, start)
|
amine@86
|
487 self.assertEqual(region.end, expected_end)
|
amine@86
|
488 self.assertEqual(region.duration, expected_duration_s)
|
amine@86
|
489 self.assertEqual(len(region), expected_duration_ms)
|
amine@86
|
490 self.assertEqual(bytes(region), data)
|
amine@88
|
491
|
amine@97
|
492 def test_creation_invalid_data_exception(self):
|
amine@97
|
493 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@97
|
494 _ = AudioRegion(
|
amine@97
|
495 data=b"ABCDEFGHI",
|
amine@97
|
496 start=0,
|
amine@97
|
497 sampling_rate=8,
|
amine@97
|
498 sample_width=2,
|
amine@97
|
499 channels=1,
|
amine@97
|
500 )
|
amine@97
|
501 self.assertEqual(
|
amine@97
|
502 "The length of audio data must be an integer "
|
amine@97
|
503 "multiple of `sample_width * channels`",
|
amine@97
|
504 str(audio_param_err.exception),
|
amine@97
|
505 )
|
amine@97
|
506
|
amine@88
|
507 @genty_dataset(
|
amine@192
|
508 simple=("output.wav", 1.230, "output.wav"),
|
amine@192
|
509 start=("output_{start}.wav", 1.230, "output_1.23.wav"),
|
amine@192
|
510 start_2=("output_{start}.wav", 1.233712, "output_1.233712.wav"),
|
amine@192
|
511 start_3=("output_{start}.wav", 1.2300001, "output_1.23.wav"),
|
amine@192
|
512 start_4=("output_{start:.3f}.wav", 1.233712, "output_1.234.wav"),
|
amine@192
|
513 start_5=(
|
amine@192
|
514 "output_{start:.8f}.wav",
|
amine@192
|
515 1.233712345,
|
amine@192
|
516 "output_1.23371200.wav",
|
amine@192
|
517 ),
|
amine@192
|
518 start_end_duration=(
|
amine@192
|
519 "output_{start}_{end}_{duration}.wav",
|
amine@192
|
520 1.455,
|
amine@192
|
521 "output_1.455_2.455_1.0.wav",
|
amine@192
|
522 ),
|
amine@192
|
523 start_end_duration_2=(
|
amine@192
|
524 "output_{start}_{end}_{duration}.wav",
|
amine@192
|
525 1.455321,
|
amine@192
|
526 "output_1.455321_2.455321_1.0.wav",
|
amine@192
|
527 ),
|
amine@192
|
528 )
|
amine@192
|
529 def test_save(self, format, start, expected):
|
amine@192
|
530 with TemporaryDirectory() as tmpdir:
|
amine@192
|
531 region = AudioRegion(b"0" * 160, start, 160, 1, 1)
|
amine@192
|
532 format = os.path.join(tmpdir, format)
|
amine@192
|
533 filename = region.save(format)[len(tmpdir) + 1 :]
|
amine@192
|
534 self.assertEqual(filename, expected)
|
amine@192
|
535
|
amine@193
|
536 def test_save_file_exists_exception(self):
|
amine@193
|
537 with TemporaryDirectory() as tmpdir:
|
amine@193
|
538 filename = os.path.join(tmpdir, "output.wav")
|
amine@193
|
539 open(filename, "w").close()
|
amine@193
|
540 region = AudioRegion(b"0" * 160, 0, 160, 1, 1)
|
amine@193
|
541 with self.assertRaises(FileExistsError):
|
amine@193
|
542 region.save(filename, exists_ok=False)
|
amine@193
|
543
|
amine@192
|
544 @genty_dataset(
|
amine@194
|
545 first_half=(
|
amine@194
|
546 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
547 slice(0, 500),
|
amine@194
|
548 0,
|
amine@194
|
549 b"a" * 80,
|
amine@194
|
550 ),
|
amine@194
|
551 second_half=(
|
amine@194
|
552 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
553 slice(500, None),
|
amine@194
|
554 0.5,
|
amine@194
|
555 b"b" * 80,
|
amine@194
|
556 ),
|
amine@194
|
557 second_half_negative=(
|
amine@194
|
558 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
559 slice(-500, None),
|
amine@194
|
560 0.5,
|
amine@194
|
561 b"b" * 80,
|
amine@194
|
562 ),
|
amine@194
|
563 middle=(
|
amine@194
|
564 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
565 slice(200, 750),
|
amine@194
|
566 0.2,
|
amine@194
|
567 b"a" * 48 + b"b" * 40,
|
amine@194
|
568 ),
|
amine@194
|
569 middle_negative=(
|
amine@194
|
570 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
571 slice(-800, -250),
|
amine@194
|
572 0.2,
|
amine@194
|
573 b"a" * 48 + b"b" * 40,
|
amine@194
|
574 ),
|
amine@194
|
575 middle_sw2=(
|
amine@194
|
576 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 2, 1),
|
amine@194
|
577 slice(200, 750),
|
amine@194
|
578 0.2,
|
amine@194
|
579 b"a" * 96 + b"b" * 80,
|
amine@194
|
580 ),
|
amine@194
|
581 middle_ch2=(
|
amine@194
|
582 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 1, 2),
|
amine@194
|
583 slice(200, 750),
|
amine@194
|
584 0.2,
|
amine@194
|
585 b"a" * 96 + b"b" * 80,
|
amine@194
|
586 ),
|
amine@194
|
587 middle_sw2_ch2=(
|
amine@194
|
588 AudioRegion(b"a" * 320 + b"b" * 320, 0, 160, 2, 2),
|
amine@194
|
589 slice(200, 750),
|
amine@194
|
590 0.2,
|
amine@194
|
591 b"a" * 192 + b"b" * 160,
|
amine@194
|
592 ),
|
amine@194
|
593 but_first_sample=(
|
amine@194
|
594 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
595 slice(1, None),
|
amine@194
|
596 0.001,
|
amine@194
|
597 b"a" * (4000 - 8) + b"b" * 4000,
|
amine@194
|
598 ),
|
amine@194
|
599 but_first_sample_negative=(
|
amine@194
|
600 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
601 slice(-999, None),
|
amine@194
|
602 0.001,
|
amine@194
|
603 b"a" * (4000 - 8) + b"b" * 4000,
|
amine@194
|
604 ),
|
amine@194
|
605 but_last_sample=(
|
amine@194
|
606 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
607 slice(0, 999),
|
amine@194
|
608 0,
|
amine@194
|
609 b"a" * 4000 + b"b" * (4000 - 8),
|
amine@194
|
610 ),
|
amine@194
|
611 but_last_sample_negative=(
|
amine@194
|
612 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
613 slice(0, -1),
|
amine@194
|
614 0,
|
amine@194
|
615 b"a" * 4000 + b"b" * (4000 - 8),
|
amine@194
|
616 ),
|
amine@194
|
617 big_negative_start=(
|
amine@194
|
618 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@194
|
619 slice(-5000, None),
|
amine@194
|
620 0,
|
amine@194
|
621 b"a" * 160,
|
amine@194
|
622 ),
|
amine@194
|
623 big_negative_stop=(
|
amine@194
|
624 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@194
|
625 slice(None, -1500),
|
amine@194
|
626 0,
|
amine@194
|
627 b"",
|
amine@194
|
628 ),
|
amine@194
|
629 empty=(
|
amine@194
|
630 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
631 slice(0, 0),
|
amine@194
|
632 0,
|
amine@194
|
633 b"",
|
amine@194
|
634 ),
|
amine@194
|
635 empty_start_stop_reversed=(
|
amine@194
|
636 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
637 slice(200, 100),
|
amine@194
|
638 0.2,
|
amine@194
|
639 b"",
|
amine@194
|
640 ),
|
amine@194
|
641 empty_big_positive_start=(
|
amine@194
|
642 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
643 slice(2000, 3000),
|
amine@194
|
644 2,
|
amine@194
|
645 b"",
|
amine@194
|
646 ),
|
amine@194
|
647 empty_negative_reversed=(
|
amine@194
|
648 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
649 slice(-100, -200),
|
amine@194
|
650 0.9,
|
amine@194
|
651 b"",
|
amine@194
|
652 ),
|
amine@194
|
653 empty_big_negative_stop=(
|
amine@194
|
654 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
655 slice(0, -2000),
|
amine@194
|
656 0,
|
amine@194
|
657 b"",
|
amine@194
|
658 ),
|
amine@194
|
659 )
|
amine@194
|
660 def test_region_slicing(
|
amine@194
|
661 self, region, slice_, expected_start, expected_data
|
amine@194
|
662 ):
|
amine@194
|
663 sub_region = region[slice_]
|
amine@194
|
664 self.assertEqual(sub_region.start, expected_start)
|
amine@194
|
665 self.assertEqual(bytes(sub_region), expected_data)
|
amine@194
|
666
|
amine@194
|
667 @genty_dataset(
|
amine@88
|
668 simple=(8000, 1, 1),
|
amine@88
|
669 stereo_sw_2=(8000, 2, 2),
|
amine@88
|
670 arbitray_sr_multichannel=(5413, 2, 3),
|
amine@88
|
671 )
|
amine@88
|
672 def test_concatenation(self, sampling_rate, sample_width, channels):
|
amine@88
|
673
|
amine@88
|
674 region_1, region_2 = _make_random_length_regions(
|
amine@88
|
675 [b"a", b"b"], sampling_rate, sample_width, channels
|
amine@88
|
676 )
|
amine@88
|
677
|
amine@88
|
678 expected_start = region_1.start
|
amine@88
|
679 expected_duration = region_1.duration + region_2.duration
|
amine@88
|
680 expected_end = expected_start + expected_duration
|
amine@88
|
681 expected_data = bytes(region_1) + bytes(region_2)
|
amine@88
|
682 concat_region = region_1 + region_2
|
amine@88
|
683
|
amine@88
|
684 self.assertEqual(concat_region.start, expected_start)
|
amine@88
|
685 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
|
amine@88
|
686 self.assertAlmostEqual(
|
amine@88
|
687 concat_region.duration, expected_duration, places=6
|
amine@88
|
688 )
|
amine@88
|
689 self.assertEqual(bytes(concat_region), expected_data)
|
amine@88
|
690
|
amine@88
|
691 @genty_dataset(
|
amine@88
|
692 simple=(8000, 1, 1),
|
amine@88
|
693 stereo_sw_2=(8000, 2, 2),
|
amine@88
|
694 arbitray_sr_multichannel=(5413, 2, 3),
|
amine@88
|
695 )
|
amine@88
|
696 def test_concatenation_many(self, sampling_rate, sample_width, channels):
|
amine@88
|
697
|
amine@88
|
698 regions = _make_random_length_regions(
|
amine@88
|
699 [b"a", b"b", b"c"], sampling_rate, sample_width, channels
|
amine@88
|
700 )
|
amine@88
|
701 expected_start = regions[0].start
|
amine@88
|
702 expected_duration = sum(r.duration for r in regions)
|
amine@88
|
703 expected_end = expected_start + expected_duration
|
amine@88
|
704 expected_data = b"".join(bytes(r) for r in regions)
|
amine@88
|
705 concat_region = sum(regions)
|
amine@88
|
706
|
amine@88
|
707 self.assertEqual(concat_region.start, expected_start)
|
amine@88
|
708 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
|
amine@88
|
709 self.assertAlmostEqual(
|
amine@88
|
710 concat_region.duration, expected_duration, places=6
|
amine@88
|
711 )
|
amine@88
|
712 self.assertEqual(bytes(concat_region), expected_data)
|
amine@88
|
713
|
amine@88
|
714 def test_concatenation_different_sampling_rate_error(self):
|
amine@88
|
715
|
amine@88
|
716 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
|
amine@88
|
717 region_2 = AudioRegion(b"b" * 100, 0, 3000, 1, 1)
|
amine@88
|
718
|
amine@88
|
719 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
720 region_1 + region_2
|
amine@88
|
721 self.assertEqual(
|
amine@88
|
722 "Can only concatenate AudioRegions of the same "
|
amine@88
|
723 "sampling rate (8000 != 3000)",
|
amine@88
|
724 str(val_err.exception),
|
amine@88
|
725 )
|
amine@88
|
726
|
amine@88
|
727 def test_concatenation_different_sample_width_error(self):
|
amine@88
|
728
|
amine@88
|
729 region_1 = AudioRegion(b"a" * 100, 0, 8000, 2, 1)
|
amine@88
|
730 region_2 = AudioRegion(b"b" * 100, 0, 8000, 4, 1)
|
amine@88
|
731
|
amine@88
|
732 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
733 region_1 + region_2
|
amine@88
|
734 self.assertEqual(
|
amine@88
|
735 "Can only concatenate AudioRegions of the same "
|
amine@88
|
736 "sample width (2 != 4)",
|
amine@88
|
737 str(val_err.exception),
|
amine@88
|
738 )
|
amine@88
|
739
|
amine@88
|
740 def test_concatenation_different_number_of_channels_error(self):
|
amine@88
|
741
|
amine@88
|
742 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
|
amine@88
|
743 region_2 = AudioRegion(b"b" * 100, 0, 8000, 1, 2)
|
amine@88
|
744
|
amine@88
|
745 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
746 region_1 + region_2
|
amine@88
|
747 self.assertEqual(
|
amine@88
|
748 "Can only concatenate AudioRegions of the same "
|
amine@88
|
749 "number of channels (1 != 2)",
|
amine@88
|
750 str(val_err.exception),
|
amine@88
|
751 )
|
amine@196
|
752
|
amine@196
|
753 @genty_dataset(
|
amine@196
|
754 simple=(0.01, 0.03, 30),
|
amine@196
|
755 rounded_len_floor=(0.00575, 0.01725, 17),
|
amine@196
|
756 rounded_len_ceil=(0.00625, 0.01875, 19),
|
amine@196
|
757 )
|
amine@196
|
758 def test_multiplication(
|
amine@196
|
759 self, duration, expected_duration, expected_length
|
amine@196
|
760 ):
|
amine@196
|
761 sw = 2
|
amine@196
|
762 data = b"0" * int(duration * 8000 * sw)
|
amine@196
|
763 region = AudioRegion(data, 0, 8000, sw, 1)
|
amine@196
|
764 m_region = 1 * region * 3
|
amine@196
|
765 self.assertEqual(bytes(m_region), data * 3)
|
amine@196
|
766 self.assertEqual(m_region.sr, 8000)
|
amine@196
|
767 self.assertEqual(m_region.sw, 2)
|
amine@196
|
768 self.assertEqual(m_region.ch, 1)
|
amine@196
|
769 self.assertEqual(m_region.duration, expected_duration)
|
amine@196
|
770 self.assertEqual(len(m_region), expected_length)
|
amine@197
|
771
|
amine@198
|
772 @genty_dataset(_str=("x", "str"), _float=(1.4, "float"))
|
amine@197
|
773 def test_multiplication_non_int(self, factor, _type):
|
amine@197
|
774 with self.assertRaises(TypeError) as type_err:
|
amine@198
|
775 AudioRegion(b"0" * 80, 0, 8000, 1, 1) * factor
|
amine@197
|
776 err_msg = "Can't multiply AudioRegion by a non-int of type '{}'"
|
amine@197
|
777 self.assertEqual(err_msg.format(_type), str(type_err.exception))
|