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