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@220
|
283 mono_aw_0_2_max_silence_0_2=(
|
amine@220
|
284 0.2,
|
amine@220
|
285 5,
|
amine@220
|
286 0.2,
|
amine@220
|
287 1,
|
amine@220
|
288 {"uc": 1, "aw": 0.2},
|
amine@220
|
289 [(2, 30), (34, 76)],
|
amine@220
|
290 ),
|
amine@220
|
291 mono_aw_0_2_max_silence_0_3=(
|
amine@220
|
292 0.2,
|
amine@220
|
293 5,
|
amine@220
|
294 0.3,
|
amine@220
|
295 1,
|
amine@220
|
296 {"uc": 1, "aw": 0.2},
|
amine@220
|
297 [(2, 32), (34, 76)],
|
amine@220
|
298 ),
|
amine@220
|
299 mono_aw_0_2_max_silence_0_4=(
|
amine@220
|
300 0.2,
|
amine@220
|
301 5,
|
amine@220
|
302 0.3,
|
amine@220
|
303 1,
|
amine@220
|
304 {"uc": 1, "aw": 0.2},
|
amine@220
|
305 [(2, 32), (34, 76)],
|
amine@220
|
306 ),
|
amine@220
|
307 mono_aw_0_2=(
|
amine@220
|
308 0.2,
|
amine@220
|
309 5,
|
amine@220
|
310 0.2,
|
amine@220
|
311 1,
|
amine@220
|
312 {"uc": 1, "aw": 0.2},
|
amine@220
|
313 [(2, 30), (34, 76)],
|
amine@220
|
314 ),
|
amine@220
|
315 stereo_uc_1_analysis_window_0_2=(
|
amine@220
|
316 0.2,
|
amine@220
|
317 5,
|
amine@220
|
318 0.2,
|
amine@220
|
319 2,
|
amine@220
|
320 {"uc": 1, "analysis_window": 0.2},
|
amine@220
|
321 [(2, 30), (34, 76)],
|
amine@220
|
322 ),
|
amine@220
|
323 )
|
amine@220
|
324 def test_split_analysis_window(
|
amine@220
|
325 self, min_dur, max_dur, max_silence, channels, kwargs, expected
|
amine@220
|
326 ):
|
amine@220
|
327
|
amine@220
|
328 mono_or_stereo = "mono" if channels == 1 else "stereo"
|
amine@220
|
329 filename = "tests/data/test_split_10HZ_{}.raw".format(mono_or_stereo)
|
amine@220
|
330 with open(filename, "rb") as fp:
|
amine@220
|
331 data = fp.read()
|
amine@220
|
332
|
amine@220
|
333 regions = split(
|
amine@220
|
334 data,
|
amine@220
|
335 min_dur=min_dur,
|
amine@220
|
336 max_dur=max_dur,
|
amine@220
|
337 max_silence=max_silence,
|
amine@220
|
338 drop_trailing_silence=False,
|
amine@220
|
339 strict_min_dur=False,
|
amine@220
|
340 sr=10,
|
amine@220
|
341 sw=2,
|
amine@220
|
342 ch=channels,
|
amine@220
|
343 **kwargs
|
amine@220
|
344 )
|
amine@220
|
345 regions = list(regions)
|
amine@220
|
346 sample_width = 2
|
amine@220
|
347 import numpy as np
|
amine@220
|
348
|
amine@220
|
349 use_channel = kwargs.get("use_channel", kwargs.get("uc"))
|
amine@220
|
350 # extrat channel of interest
|
amine@220
|
351 if channels != 1:
|
amine@220
|
352 use_channel = kwargs.get("use_channel", kwargs.get("uc"))
|
amine@220
|
353 use_channel = _normalize_use_channel(use_channel)
|
amine@220
|
354 data = _extract_selected_channel(
|
amine@220
|
355 data, channels, sample_width, use_channel=use_channel
|
amine@220
|
356 )
|
amine@220
|
357 err_msg = "Wrong number of regions after split, expected: "
|
amine@220
|
358 err_msg += "{}, found: {}".format(expected, regions)
|
amine@220
|
359 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@220
|
360 for reg, exp in zip(regions, expected):
|
amine@220
|
361 onset, offset = exp
|
amine@220
|
362 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@220
|
363 self.assertEqual(bytes(reg), exp_data)
|
amine@220
|
364
|
amine@220
|
365 @genty_dataset(
|
amine@212
|
366 filename_audio_format=(
|
amine@212
|
367 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
368 {"audio_format": "raw", "sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
369 ),
|
amine@212
|
370 filename_audio_format_short_name=(
|
amine@212
|
371 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
372 {"fmt": "raw", "sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
373 ),
|
amine@212
|
374 filename_no_audio_format=(
|
amine@212
|
375 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
376 {"sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
377 ),
|
amine@212
|
378 filename_no_long_audio_params=(
|
amine@212
|
379 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
380 {"sampling_rate": 10, "sample_width": 2, "channels": 2},
|
amine@212
|
381 ),
|
amine@212
|
382 bytes_=(
|
amine@212
|
383 open("tests/data/test_split_10HZ_stereo.raw", "rb").read(),
|
amine@212
|
384 {"sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
385 ),
|
amine@212
|
386 audio_reader=(
|
amine@212
|
387 AudioDataSource(
|
amine@212
|
388 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
389 sr=10,
|
amine@212
|
390 sw=2,
|
amine@212
|
391 ch=2,
|
amine@212
|
392 block_dur=0.1,
|
amine@212
|
393 ),
|
amine@212
|
394 {},
|
amine@212
|
395 ),
|
amine@212
|
396 audio_region=(
|
amine@212
|
397 AudioRegion(
|
amine@212
|
398 open("tests/data/test_split_10HZ_stereo.raw", "rb").read(),
|
amine@212
|
399 0,
|
amine@212
|
400 10,
|
amine@212
|
401 2,
|
amine@212
|
402 2,
|
amine@212
|
403 ),
|
amine@212
|
404 {},
|
amine@212
|
405 ),
|
amine@212
|
406 audio_source=(
|
amine@212
|
407 get_audio_source(
|
amine@212
|
408 "tests/data/test_split_10HZ_stereo.raw", sr=10, sw=2, ch=2
|
amine@212
|
409 ),
|
amine@212
|
410 {},
|
amine@212
|
411 ),
|
amine@212
|
412 )
|
amine@212
|
413 def test_split_input_type(self, input, kwargs):
|
amine@212
|
414
|
amine@212
|
415 with open("tests/data/test_split_10HZ_mono.raw", "rb") as fp:
|
amine@212
|
416 data = fp.read()
|
amine@212
|
417
|
amine@212
|
418 regions = split(
|
amine@212
|
419 input,
|
amine@212
|
420 min_dur=0.2,
|
amine@212
|
421 max_dur=5,
|
amine@212
|
422 max_silence=0.2,
|
amine@212
|
423 drop_trailing_silence=False,
|
amine@212
|
424 strict_min_dur=False,
|
amine@212
|
425 analysis_window=0.1,
|
amine@212
|
426 **kwargs
|
amine@212
|
427 )
|
amine@212
|
428 regions = list(regions)
|
amine@212
|
429 expected = [(2, 16), (17, 31), (34, 76)]
|
amine@212
|
430 sample_width = 2
|
amine@212
|
431 err_msg = "Wrong number of regions after split, expected: "
|
amine@212
|
432 err_msg += "{}, found: {}".format(expected, regions)
|
amine@212
|
433 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@211
|
434 for reg, exp in zip(regions, expected):
|
amine@211
|
435 onset, offset = exp
|
amine@211
|
436 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@211
|
437 self.assertEqual(bytes(reg), exp_data)
|
amine@211
|
438
|
amine@207
|
439
|
amine@207
|
440 @genty
|
amine@207
|
441 class TestAudioRegion(TestCase):
|
amine@86
|
442 @genty_dataset(
|
amine@86
|
443 simple=(b"\0" * 8000, 0, 8000, 1, 1, 1, 1, 1000),
|
amine@86
|
444 one_ms_less_than_1_sec=(
|
amine@86
|
445 b"\0" * 7992,
|
amine@86
|
446 0,
|
amine@86
|
447 8000,
|
amine@86
|
448 1,
|
amine@86
|
449 1,
|
amine@86
|
450 0.999,
|
amine@86
|
451 0.999,
|
amine@86
|
452 999,
|
amine@86
|
453 ),
|
amine@86
|
454 tree_quarter_ms_less_than_1_sec=(
|
amine@86
|
455 b"\0" * 7994,
|
amine@86
|
456 0,
|
amine@86
|
457 8000,
|
amine@86
|
458 1,
|
amine@86
|
459 1,
|
amine@86
|
460 0.99925,
|
amine@86
|
461 0.99925,
|
amine@86
|
462 999,
|
amine@86
|
463 ),
|
amine@86
|
464 half_ms_less_than_1_sec=(
|
amine@86
|
465 b"\0" * 7996,
|
amine@86
|
466 0,
|
amine@86
|
467 8000,
|
amine@86
|
468 1,
|
amine@86
|
469 1,
|
amine@86
|
470 0.9995,
|
amine@86
|
471 0.9995,
|
amine@86
|
472 1000,
|
amine@86
|
473 ),
|
amine@86
|
474 quarter_ms_less_than_1_sec=(
|
amine@86
|
475 b"\0" * 7998,
|
amine@86
|
476 0,
|
amine@86
|
477 8000,
|
amine@86
|
478 1,
|
amine@86
|
479 1,
|
amine@86
|
480 0.99975,
|
amine@86
|
481 0.99975,
|
amine@86
|
482 1000,
|
amine@86
|
483 ),
|
amine@86
|
484 simple_sample_width_2=(b"\0" * 8000 * 2, 0, 8000, 2, 1, 1, 1, 1000),
|
amine@86
|
485 simple_stereo=(b"\0" * 8000 * 2, 0, 8000, 1, 2, 1, 1, 1000),
|
amine@86
|
486 simple_multichannel=(b"\0" * 8000 * 5, 0, 8000, 1, 5, 1, 1, 1000),
|
amine@86
|
487 simple_sample_width_2_multichannel=(
|
amine@86
|
488 b"\0" * 8000 * 2 * 5,
|
amine@86
|
489 0,
|
amine@86
|
490 8000,
|
amine@86
|
491 2,
|
amine@86
|
492 5,
|
amine@86
|
493 1,
|
amine@86
|
494 1,
|
amine@86
|
495 1000,
|
amine@86
|
496 ),
|
amine@86
|
497 one_ms_less_than_1s_sw_2_multichannel=(
|
amine@86
|
498 b"\0" * 7992 * 2 * 5,
|
amine@86
|
499 0,
|
amine@86
|
500 8000,
|
amine@86
|
501 2,
|
amine@86
|
502 5,
|
amine@86
|
503 0.999,
|
amine@86
|
504 0.999,
|
amine@86
|
505 999,
|
amine@86
|
506 ),
|
amine@86
|
507 tree_qrt_ms_lt_1_s_sw_2_multichannel=(
|
amine@86
|
508 b"\0" * 7994 * 2 * 5,
|
amine@86
|
509 0,
|
amine@86
|
510 8000,
|
amine@86
|
511 2,
|
amine@86
|
512 5,
|
amine@86
|
513 0.99925,
|
amine@86
|
514 0.99925,
|
amine@86
|
515 999,
|
amine@86
|
516 ),
|
amine@86
|
517 half_ms_lt_1s_sw_2_multichannel=(
|
amine@86
|
518 b"\0" * 7996 * 2 * 5,
|
amine@86
|
519 0,
|
amine@86
|
520 8000,
|
amine@86
|
521 2,
|
amine@86
|
522 5,
|
amine@86
|
523 0.9995,
|
amine@86
|
524 0.9995,
|
amine@86
|
525 1000,
|
amine@86
|
526 ),
|
amine@86
|
527 quarter_ms_lt_1s_sw_2_multichannel=(
|
amine@86
|
528 b"\0" * 7998 * 2 * 5,
|
amine@86
|
529 0,
|
amine@86
|
530 8000,
|
amine@86
|
531 2,
|
amine@86
|
532 5,
|
amine@86
|
533 0.99975,
|
amine@86
|
534 0.99975,
|
amine@86
|
535 1000,
|
amine@86
|
536 ),
|
amine@86
|
537 arbitrary_length_1=(
|
amine@86
|
538 b"\0" * int(8000 * 1.33),
|
amine@86
|
539 2.7,
|
amine@86
|
540 8000,
|
amine@86
|
541 1,
|
amine@86
|
542 1,
|
amine@86
|
543 4.03,
|
amine@86
|
544 1.33,
|
amine@86
|
545 1330,
|
amine@86
|
546 ),
|
amine@86
|
547 arbitrary_length_2=(
|
amine@86
|
548 b"\0" * int(8000 * 0.476),
|
amine@86
|
549 11.568,
|
amine@86
|
550 8000,
|
amine@86
|
551 1,
|
amine@86
|
552 1,
|
amine@86
|
553 12.044,
|
amine@86
|
554 0.476,
|
amine@86
|
555 476,
|
amine@86
|
556 ),
|
amine@86
|
557 arbitrary_length_sw_2_multichannel=(
|
amine@86
|
558 b"\0" * int(8000 * 1.711) * 2 * 3,
|
amine@86
|
559 9.415,
|
amine@86
|
560 8000,
|
amine@86
|
561 2,
|
amine@86
|
562 3,
|
amine@86
|
563 11.126,
|
amine@86
|
564 1.711,
|
amine@86
|
565 1711,
|
amine@86
|
566 ),
|
amine@86
|
567 arbitrary_samplig_rate=(
|
amine@86
|
568 b"\0" * int(3172 * 1.318),
|
amine@86
|
569 17.236,
|
amine@86
|
570 3172,
|
amine@86
|
571 1,
|
amine@86
|
572 1,
|
amine@86
|
573 17.236 + int(3172 * 1.318) / 3172,
|
amine@86
|
574 int(3172 * 1.318) / 3172,
|
amine@86
|
575 1318,
|
amine@86
|
576 ),
|
amine@86
|
577 arbitrary_sr_sw_2_multichannel=(
|
amine@86
|
578 b"\0" * int(11317 * 0.716) * 2 * 3,
|
amine@86
|
579 18.811,
|
amine@86
|
580 11317,
|
amine@86
|
581 2,
|
amine@86
|
582 3,
|
amine@86
|
583 18.811 + int(11317 * 0.716) / 11317,
|
amine@86
|
584 int(11317 * 0.716) / 11317,
|
amine@86
|
585 716,
|
amine@86
|
586 ),
|
amine@86
|
587 )
|
amine@86
|
588 def test_creation(
|
amine@86
|
589 self,
|
amine@86
|
590 data,
|
amine@86
|
591 start,
|
amine@86
|
592 sampling_rate,
|
amine@86
|
593 sample_width,
|
amine@86
|
594 channels,
|
amine@86
|
595 expected_end,
|
amine@86
|
596 expected_duration_s,
|
amine@86
|
597 expected_duration_ms,
|
amine@86
|
598 ):
|
amine@86
|
599 region = AudioRegion(
|
amine@86
|
600 data, start, sampling_rate, sample_width, channels
|
amine@86
|
601 )
|
amine@86
|
602 self.assertEqual(region.sampling_rate, sampling_rate)
|
amine@86
|
603 self.assertEqual(region.sr, sampling_rate)
|
amine@86
|
604 self.assertEqual(region.sample_width, sample_width)
|
amine@86
|
605 self.assertEqual(region.sw, sample_width)
|
amine@86
|
606 self.assertEqual(region.channels, channels)
|
amine@86
|
607 self.assertEqual(region.ch, channels)
|
amine@86
|
608 self.assertEqual(region.start, start)
|
amine@86
|
609 self.assertEqual(region.end, expected_end)
|
amine@86
|
610 self.assertEqual(region.duration, expected_duration_s)
|
amine@86
|
611 self.assertEqual(len(region), expected_duration_ms)
|
amine@86
|
612 self.assertEqual(bytes(region), data)
|
amine@88
|
613
|
amine@97
|
614 def test_creation_invalid_data_exception(self):
|
amine@97
|
615 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@97
|
616 _ = AudioRegion(
|
amine@97
|
617 data=b"ABCDEFGHI",
|
amine@97
|
618 start=0,
|
amine@97
|
619 sampling_rate=8,
|
amine@97
|
620 sample_width=2,
|
amine@97
|
621 channels=1,
|
amine@97
|
622 )
|
amine@97
|
623 self.assertEqual(
|
amine@97
|
624 "The length of audio data must be an integer "
|
amine@97
|
625 "multiple of `sample_width * channels`",
|
amine@97
|
626 str(audio_param_err.exception),
|
amine@97
|
627 )
|
amine@97
|
628
|
amine@88
|
629 @genty_dataset(
|
amine@192
|
630 simple=("output.wav", 1.230, "output.wav"),
|
amine@192
|
631 start=("output_{start}.wav", 1.230, "output_1.23.wav"),
|
amine@192
|
632 start_2=("output_{start}.wav", 1.233712, "output_1.233712.wav"),
|
amine@192
|
633 start_3=("output_{start}.wav", 1.2300001, "output_1.23.wav"),
|
amine@192
|
634 start_4=("output_{start:.3f}.wav", 1.233712, "output_1.234.wav"),
|
amine@192
|
635 start_5=(
|
amine@192
|
636 "output_{start:.8f}.wav",
|
amine@192
|
637 1.233712345,
|
amine@192
|
638 "output_1.23371200.wav",
|
amine@192
|
639 ),
|
amine@192
|
640 start_end_duration=(
|
amine@192
|
641 "output_{start}_{end}_{duration}.wav",
|
amine@192
|
642 1.455,
|
amine@192
|
643 "output_1.455_2.455_1.0.wav",
|
amine@192
|
644 ),
|
amine@192
|
645 start_end_duration_2=(
|
amine@192
|
646 "output_{start}_{end}_{duration}.wav",
|
amine@192
|
647 1.455321,
|
amine@192
|
648 "output_1.455321_2.455321_1.0.wav",
|
amine@192
|
649 ),
|
amine@192
|
650 )
|
amine@192
|
651 def test_save(self, format, start, expected):
|
amine@192
|
652 with TemporaryDirectory() as tmpdir:
|
amine@192
|
653 region = AudioRegion(b"0" * 160, start, 160, 1, 1)
|
amine@192
|
654 format = os.path.join(tmpdir, format)
|
amine@192
|
655 filename = region.save(format)[len(tmpdir) + 1 :]
|
amine@192
|
656 self.assertEqual(filename, expected)
|
amine@192
|
657
|
amine@193
|
658 def test_save_file_exists_exception(self):
|
amine@193
|
659 with TemporaryDirectory() as tmpdir:
|
amine@193
|
660 filename = os.path.join(tmpdir, "output.wav")
|
amine@193
|
661 open(filename, "w").close()
|
amine@193
|
662 region = AudioRegion(b"0" * 160, 0, 160, 1, 1)
|
amine@193
|
663 with self.assertRaises(FileExistsError):
|
amine@193
|
664 region.save(filename, exists_ok=False)
|
amine@193
|
665
|
amine@192
|
666 @genty_dataset(
|
amine@194
|
667 first_half=(
|
amine@194
|
668 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
669 slice(0, 500),
|
amine@194
|
670 0,
|
amine@194
|
671 b"a" * 80,
|
amine@194
|
672 ),
|
amine@194
|
673 second_half=(
|
amine@194
|
674 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
675 slice(500, None),
|
amine@194
|
676 0.5,
|
amine@194
|
677 b"b" * 80,
|
amine@194
|
678 ),
|
amine@194
|
679 second_half_negative=(
|
amine@194
|
680 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
681 slice(-500, None),
|
amine@194
|
682 0.5,
|
amine@194
|
683 b"b" * 80,
|
amine@194
|
684 ),
|
amine@194
|
685 middle=(
|
amine@194
|
686 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
687 slice(200, 750),
|
amine@194
|
688 0.2,
|
amine@194
|
689 b"a" * 48 + b"b" * 40,
|
amine@194
|
690 ),
|
amine@194
|
691 middle_negative=(
|
amine@194
|
692 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
693 slice(-800, -250),
|
amine@194
|
694 0.2,
|
amine@194
|
695 b"a" * 48 + b"b" * 40,
|
amine@194
|
696 ),
|
amine@194
|
697 middle_sw2=(
|
amine@194
|
698 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 2, 1),
|
amine@194
|
699 slice(200, 750),
|
amine@194
|
700 0.2,
|
amine@194
|
701 b"a" * 96 + b"b" * 80,
|
amine@194
|
702 ),
|
amine@194
|
703 middle_ch2=(
|
amine@194
|
704 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 1, 2),
|
amine@194
|
705 slice(200, 750),
|
amine@194
|
706 0.2,
|
amine@194
|
707 b"a" * 96 + b"b" * 80,
|
amine@194
|
708 ),
|
amine@194
|
709 middle_sw2_ch2=(
|
amine@194
|
710 AudioRegion(b"a" * 320 + b"b" * 320, 0, 160, 2, 2),
|
amine@194
|
711 slice(200, 750),
|
amine@194
|
712 0.2,
|
amine@194
|
713 b"a" * 192 + b"b" * 160,
|
amine@194
|
714 ),
|
amine@194
|
715 but_first_sample=(
|
amine@194
|
716 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
717 slice(1, None),
|
amine@194
|
718 0.001,
|
amine@194
|
719 b"a" * (4000 - 8) + b"b" * 4000,
|
amine@194
|
720 ),
|
amine@194
|
721 but_first_sample_negative=(
|
amine@194
|
722 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
723 slice(-999, None),
|
amine@194
|
724 0.001,
|
amine@194
|
725 b"a" * (4000 - 8) + b"b" * 4000,
|
amine@194
|
726 ),
|
amine@194
|
727 but_last_sample=(
|
amine@194
|
728 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
729 slice(0, 999),
|
amine@194
|
730 0,
|
amine@194
|
731 b"a" * 4000 + b"b" * (4000 - 8),
|
amine@194
|
732 ),
|
amine@194
|
733 but_last_sample_negative=(
|
amine@194
|
734 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
735 slice(0, -1),
|
amine@194
|
736 0,
|
amine@194
|
737 b"a" * 4000 + b"b" * (4000 - 8),
|
amine@194
|
738 ),
|
amine@194
|
739 big_negative_start=(
|
amine@194
|
740 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@194
|
741 slice(-5000, None),
|
amine@194
|
742 0,
|
amine@194
|
743 b"a" * 160,
|
amine@194
|
744 ),
|
amine@194
|
745 big_negative_stop=(
|
amine@194
|
746 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@194
|
747 slice(None, -1500),
|
amine@194
|
748 0,
|
amine@194
|
749 b"",
|
amine@194
|
750 ),
|
amine@194
|
751 empty=(
|
amine@194
|
752 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
753 slice(0, 0),
|
amine@194
|
754 0,
|
amine@194
|
755 b"",
|
amine@194
|
756 ),
|
amine@194
|
757 empty_start_stop_reversed=(
|
amine@194
|
758 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
759 slice(200, 100),
|
amine@194
|
760 0.2,
|
amine@194
|
761 b"",
|
amine@194
|
762 ),
|
amine@194
|
763 empty_big_positive_start=(
|
amine@194
|
764 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
765 slice(2000, 3000),
|
amine@194
|
766 2,
|
amine@194
|
767 b"",
|
amine@194
|
768 ),
|
amine@194
|
769 empty_negative_reversed=(
|
amine@194
|
770 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
771 slice(-100, -200),
|
amine@194
|
772 0.9,
|
amine@194
|
773 b"",
|
amine@194
|
774 ),
|
amine@194
|
775 empty_big_negative_stop=(
|
amine@194
|
776 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
777 slice(0, -2000),
|
amine@194
|
778 0,
|
amine@194
|
779 b"",
|
amine@194
|
780 ),
|
amine@194
|
781 )
|
amine@194
|
782 def test_region_slicing(
|
amine@194
|
783 self, region, slice_, expected_start, expected_data
|
amine@194
|
784 ):
|
amine@194
|
785 sub_region = region[slice_]
|
amine@194
|
786 self.assertEqual(sub_region.start, expected_start)
|
amine@194
|
787 self.assertEqual(bytes(sub_region), expected_data)
|
amine@194
|
788
|
amine@194
|
789 @genty_dataset(
|
amine@88
|
790 simple=(8000, 1, 1),
|
amine@88
|
791 stereo_sw_2=(8000, 2, 2),
|
amine@88
|
792 arbitray_sr_multichannel=(5413, 2, 3),
|
amine@88
|
793 )
|
amine@88
|
794 def test_concatenation(self, sampling_rate, sample_width, channels):
|
amine@88
|
795
|
amine@88
|
796 region_1, region_2 = _make_random_length_regions(
|
amine@88
|
797 [b"a", b"b"], sampling_rate, sample_width, channels
|
amine@88
|
798 )
|
amine@88
|
799
|
amine@88
|
800 expected_start = region_1.start
|
amine@88
|
801 expected_duration = region_1.duration + region_2.duration
|
amine@88
|
802 expected_end = expected_start + expected_duration
|
amine@88
|
803 expected_data = bytes(region_1) + bytes(region_2)
|
amine@88
|
804 concat_region = region_1 + region_2
|
amine@88
|
805
|
amine@88
|
806 self.assertEqual(concat_region.start, expected_start)
|
amine@88
|
807 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
|
amine@88
|
808 self.assertAlmostEqual(
|
amine@88
|
809 concat_region.duration, expected_duration, places=6
|
amine@88
|
810 )
|
amine@88
|
811 self.assertEqual(bytes(concat_region), expected_data)
|
amine@88
|
812
|
amine@88
|
813 @genty_dataset(
|
amine@88
|
814 simple=(8000, 1, 1),
|
amine@88
|
815 stereo_sw_2=(8000, 2, 2),
|
amine@88
|
816 arbitray_sr_multichannel=(5413, 2, 3),
|
amine@88
|
817 )
|
amine@88
|
818 def test_concatenation_many(self, sampling_rate, sample_width, channels):
|
amine@88
|
819
|
amine@88
|
820 regions = _make_random_length_regions(
|
amine@88
|
821 [b"a", b"b", b"c"], sampling_rate, sample_width, channels
|
amine@88
|
822 )
|
amine@88
|
823 expected_start = regions[0].start
|
amine@88
|
824 expected_duration = sum(r.duration for r in regions)
|
amine@88
|
825 expected_end = expected_start + expected_duration
|
amine@88
|
826 expected_data = b"".join(bytes(r) for r in regions)
|
amine@88
|
827 concat_region = sum(regions)
|
amine@88
|
828
|
amine@88
|
829 self.assertEqual(concat_region.start, expected_start)
|
amine@88
|
830 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
|
amine@88
|
831 self.assertAlmostEqual(
|
amine@88
|
832 concat_region.duration, expected_duration, places=6
|
amine@88
|
833 )
|
amine@88
|
834 self.assertEqual(bytes(concat_region), expected_data)
|
amine@88
|
835
|
amine@88
|
836 def test_concatenation_different_sampling_rate_error(self):
|
amine@88
|
837
|
amine@88
|
838 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
|
amine@88
|
839 region_2 = AudioRegion(b"b" * 100, 0, 3000, 1, 1)
|
amine@88
|
840
|
amine@88
|
841 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
842 region_1 + region_2
|
amine@88
|
843 self.assertEqual(
|
amine@88
|
844 "Can only concatenate AudioRegions of the same "
|
amine@88
|
845 "sampling rate (8000 != 3000)",
|
amine@88
|
846 str(val_err.exception),
|
amine@88
|
847 )
|
amine@88
|
848
|
amine@88
|
849 def test_concatenation_different_sample_width_error(self):
|
amine@88
|
850
|
amine@88
|
851 region_1 = AudioRegion(b"a" * 100, 0, 8000, 2, 1)
|
amine@88
|
852 region_2 = AudioRegion(b"b" * 100, 0, 8000, 4, 1)
|
amine@88
|
853
|
amine@88
|
854 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
855 region_1 + region_2
|
amine@88
|
856 self.assertEqual(
|
amine@88
|
857 "Can only concatenate AudioRegions of the same "
|
amine@88
|
858 "sample width (2 != 4)",
|
amine@88
|
859 str(val_err.exception),
|
amine@88
|
860 )
|
amine@88
|
861
|
amine@88
|
862 def test_concatenation_different_number_of_channels_error(self):
|
amine@88
|
863
|
amine@88
|
864 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
|
amine@88
|
865 region_2 = AudioRegion(b"b" * 100, 0, 8000, 1, 2)
|
amine@88
|
866
|
amine@88
|
867 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
868 region_1 + region_2
|
amine@88
|
869 self.assertEqual(
|
amine@88
|
870 "Can only concatenate AudioRegions of the same "
|
amine@88
|
871 "number of channels (1 != 2)",
|
amine@88
|
872 str(val_err.exception),
|
amine@88
|
873 )
|
amine@196
|
874
|
amine@196
|
875 @genty_dataset(
|
amine@196
|
876 simple=(0.01, 0.03, 30),
|
amine@196
|
877 rounded_len_floor=(0.00575, 0.01725, 17),
|
amine@196
|
878 rounded_len_ceil=(0.00625, 0.01875, 19),
|
amine@196
|
879 )
|
amine@196
|
880 def test_multiplication(
|
amine@196
|
881 self, duration, expected_duration, expected_length
|
amine@196
|
882 ):
|
amine@196
|
883 sw = 2
|
amine@196
|
884 data = b"0" * int(duration * 8000 * sw)
|
amine@196
|
885 region = AudioRegion(data, 0, 8000, sw, 1)
|
amine@196
|
886 m_region = 1 * region * 3
|
amine@196
|
887 self.assertEqual(bytes(m_region), data * 3)
|
amine@196
|
888 self.assertEqual(m_region.sr, 8000)
|
amine@196
|
889 self.assertEqual(m_region.sw, 2)
|
amine@196
|
890 self.assertEqual(m_region.ch, 1)
|
amine@196
|
891 self.assertEqual(m_region.duration, expected_duration)
|
amine@196
|
892 self.assertEqual(len(m_region), expected_length)
|
amine@197
|
893
|
amine@198
|
894 @genty_dataset(_str=("x", "str"), _float=(1.4, "float"))
|
amine@197
|
895 def test_multiplication_non_int(self, factor, _type):
|
amine@197
|
896 with self.assertRaises(TypeError) as type_err:
|
amine@198
|
897 AudioRegion(b"0" * 80, 0, 8000, 1, 1) * factor
|
amine@197
|
898 err_msg = "Can't multiply AudioRegion by a non-int of type '{}'"
|
amine@197
|
899 self.assertEqual(err_msg.format(_type), str(type_err.exception))
|