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