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