amine@192
|
1 import os
|
amine@221
|
2 import math
|
amine@88
|
3 from random import random
|
amine@192
|
4 from tempfile import TemporaryDirectory
|
amine@221
|
5 from unittest import TestCase
|
amine@86
|
6 from genty import genty, genty_dataset
|
amine@207
|
7 from auditok import split, AudioRegion, AudioParameterError
|
amine@215
|
8 from auditok.core import _duration_to_nb_windows
|
amine@212
|
9 from auditok.util import AudioDataSource
|
amine@210
|
10 from auditok.io import (
|
amine@210
|
11 _normalize_use_channel,
|
amine@210
|
12 _extract_selected_channel,
|
amine@210
|
13 get_audio_source,
|
amine@210
|
14 )
|
amine@86
|
15
|
amine@86
|
16
|
amine@88
|
17 def _make_random_length_regions(
|
amine@88
|
18 byte_seq, sampling_rate, sample_width, channels
|
amine@88
|
19 ):
|
amine@88
|
20 regions = []
|
amine@88
|
21 for b in byte_seq:
|
amine@88
|
22 duration = round(random() * 10, 6)
|
amine@95
|
23 data = b * int(duration * sampling_rate) * sample_width * channels
|
amine@88
|
24 start = round(random() * 13, 3)
|
amine@88
|
25 region = AudioRegion(
|
amine@88
|
26 data, start, sampling_rate, sample_width, channels
|
amine@88
|
27 )
|
amine@88
|
28 regions.append(region)
|
amine@88
|
29 return regions
|
amine@88
|
30
|
amine@88
|
31
|
amine@86
|
32 @genty
|
amine@215
|
33 class TestFunctions(TestCase):
|
amine@215
|
34 @genty_dataset(
|
amine@221
|
35 zero_duration=(0, 1, None, 0),
|
amine@221
|
36 multiple=(0.3, 0.1, round, 3),
|
amine@221
|
37 not_multiple_ceil=(0.35, 0.1, math.ceil, 4),
|
amine@221
|
38 not_multiple_floor=(0.35, 0.1, math.floor, 3),
|
amine@221
|
39 small_duration=(0.05, 0.1, round, 0),
|
amine@221
|
40 small_duration_ceil=(0.05, 0.1, math.ceil, 1),
|
amine@232
|
41 with_round_error=(0.3, 0.1, math.floor, 3, {"epsilon":1e-6}),
|
amine@221
|
42 negative_duration=(-0.5, 0.1, math.ceil, ValueError),
|
amine@221
|
43 negative_analysis_window=(0.5, -0.1, math.ceil, ValueError),
|
amine@215
|
44 )
|
amine@221
|
45 def test_duration_to_nb_windows(
|
amine@232
|
46 self, duration, analysis_window, round_fn, expected, kwargs=None
|
amine@221
|
47 ):
|
amine@221
|
48 if expected == ValueError:
|
amine@215
|
49 with self.assertRaises(expected):
|
amine@221
|
50 _duration_to_nb_windows(duration, analysis_window, round_fn)
|
amine@215
|
51 else:
|
amine@232
|
52 if kwargs is None:
|
amine@232
|
53 kwargs = {}
|
amine@221
|
54 result = _duration_to_nb_windows(
|
amine@232
|
55 duration, analysis_window, round_fn, **kwargs
|
amine@221
|
56 )
|
amine@215
|
57 self.assertEqual(result, expected)
|
amine@215
|
58
|
amine@215
|
59
|
amine@215
|
60 @genty
|
amine@207
|
61 class TestSplit(TestCase):
|
amine@207
|
62 @genty_dataset(
|
amine@207
|
63 simple=(
|
amine@207
|
64 0.2,
|
amine@207
|
65 5,
|
amine@207
|
66 0.2,
|
amine@207
|
67 False,
|
amine@207
|
68 False,
|
amine@207
|
69 {"eth": 50},
|
amine@207
|
70 [(2, 16), (17, 31), (34, 76)],
|
amine@207
|
71 ),
|
amine@214
|
72 short_max_dur=(
|
amine@214
|
73 0.3,
|
amine@214
|
74 2,
|
amine@214
|
75 0.2,
|
amine@214
|
76 False,
|
amine@214
|
77 False,
|
amine@214
|
78 {"eth": 50},
|
amine@214
|
79 [(2, 16), (17, 31), (34, 54), (54, 74), (74, 76)],
|
amine@214
|
80 ),
|
amine@214
|
81 long_min_dur=(3, 5, 0.2, False, False, {"eth": 50}, [(34, 76)]),
|
amine@214
|
82 long_max_silence=(0.2, 80, 10, False, False, {"eth": 50}, [(2, 76)]),
|
amine@214
|
83 zero_max_silence=(
|
amine@214
|
84 0.2,
|
amine@214
|
85 5,
|
amine@214
|
86 0.0,
|
amine@214
|
87 False,
|
amine@214
|
88 False,
|
amine@214
|
89 {"eth": 50},
|
amine@214
|
90 [(2, 14), (17, 24), (26, 29), (34, 76)],
|
amine@214
|
91 ),
|
amine@207
|
92 low_energy_threshold=(
|
amine@207
|
93 0.2,
|
amine@207
|
94 5,
|
amine@207
|
95 0.2,
|
amine@207
|
96 False,
|
amine@207
|
97 False,
|
amine@207
|
98 {"energy_threshold": 40},
|
amine@207
|
99 [(0, 50), (50, 76)],
|
amine@207
|
100 ),
|
amine@207
|
101 high_energy_threshold=(
|
amine@207
|
102 0.2,
|
amine@207
|
103 5,
|
amine@207
|
104 0.2,
|
amine@207
|
105 False,
|
amine@207
|
106 False,
|
amine@207
|
107 {"energy_threshold": 60},
|
amine@207
|
108 [],
|
amine@207
|
109 ),
|
amine@207
|
110 trim_leading_and_trailing_silence=(
|
amine@207
|
111 0.2,
|
amine@207
|
112 10, # use long max_dur
|
amine@207
|
113 0.5, # and a max_silence longer than any inter-region silence
|
amine@207
|
114 True,
|
amine@207
|
115 False,
|
amine@207
|
116 {"eth": 50},
|
amine@207
|
117 [(2, 76)],
|
amine@207
|
118 ),
|
amine@207
|
119 drop_trailing_silence=(
|
amine@207
|
120 0.2,
|
amine@207
|
121 5,
|
amine@207
|
122 0.2,
|
amine@207
|
123 True,
|
amine@207
|
124 False,
|
amine@207
|
125 {"eth": 50},
|
amine@207
|
126 [(2, 14), (17, 29), (34, 76)],
|
amine@207
|
127 ),
|
amine@207
|
128 drop_trailing_silence_2=(
|
amine@207
|
129 1.5,
|
amine@207
|
130 5,
|
amine@207
|
131 0.2,
|
amine@207
|
132 True,
|
amine@207
|
133 False,
|
amine@207
|
134 {"eth": 50},
|
amine@207
|
135 [(34, 76)],
|
amine@207
|
136 ),
|
amine@207
|
137 strict_min_dur=(
|
amine@207
|
138 0.3,
|
amine@207
|
139 2,
|
amine@207
|
140 0.2,
|
amine@207
|
141 False,
|
amine@207
|
142 True,
|
amine@207
|
143 {"eth": 50},
|
amine@207
|
144 [(2, 16), (17, 31), (34, 54), (54, 74)],
|
amine@207
|
145 ),
|
amine@207
|
146 )
|
amine@207
|
147 def test_split_params(
|
amine@207
|
148 self,
|
amine@207
|
149 min_dur,
|
amine@207
|
150 max_dur,
|
amine@207
|
151 max_silence,
|
amine@207
|
152 drop_trailing_silence,
|
amine@207
|
153 strict_min_dur,
|
amine@207
|
154 kwargs,
|
amine@207
|
155 expected,
|
amine@207
|
156 ):
|
amine@207
|
157 with open("tests/data/test_split_10HZ_mono.raw", "rb") as fp:
|
amine@207
|
158 data = fp.read()
|
amine@207
|
159
|
amine@207
|
160 regions = split(
|
amine@207
|
161 data,
|
amine@207
|
162 min_dur,
|
amine@207
|
163 max_dur,
|
amine@207
|
164 max_silence,
|
amine@207
|
165 drop_trailing_silence,
|
amine@207
|
166 strict_min_dur,
|
amine@207
|
167 analysis_window=0.1,
|
amine@207
|
168 sr=10,
|
amine@207
|
169 sw=2,
|
amine@207
|
170 ch=1,
|
amine@207
|
171 **kwargs
|
amine@207
|
172 )
|
amine@207
|
173 regions = list(regions)
|
amine@207
|
174 err_msg = "Wrong number of regions after split, expected: "
|
amine@210
|
175 err_msg += "{}, found: {}".format(len(expected), len(regions))
|
amine@207
|
176 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@207
|
177
|
amine@207
|
178 sample_width = 2
|
amine@207
|
179 for reg, exp in zip(regions, expected):
|
amine@207
|
180 onset, offset = exp
|
amine@207
|
181 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@207
|
182 self.assertEqual(bytes(reg), exp_data)
|
amine@207
|
183
|
amine@211
|
184 @genty_dataset(
|
amine@211
|
185 stereo_all_default=(2, {}, [(2, 16), (17, 31), (34, 76)]),
|
amine@213
|
186 mono_max_read=(1, {"max_read": 5}, [(2, 16), (17, 31), (34, 50)]),
|
amine@213
|
187 mono_max_read_short_name=(1, {"mr": 5}, [(2, 16), (17, 31), (34, 50)]),
|
amine@211
|
188 mono_use_channel_1=(
|
amine@211
|
189 1,
|
amine@211
|
190 {"eth": 50, "use_channel": 1},
|
amine@211
|
191 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
192 ),
|
amine@211
|
193 mono_uc_1=(1, {"eth": 50, "uc": 1}, [(2, 16), (17, 31), (34, 76)]),
|
amine@211
|
194 mono_use_channel_left=(
|
amine@211
|
195 1,
|
amine@211
|
196 {"eth": 50, "use_channel": "left"},
|
amine@211
|
197 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
198 ),
|
amine@211
|
199 mono_uc_left=(
|
amine@211
|
200 1,
|
amine@211
|
201 {"eth": 50, "uc": "left"},
|
amine@211
|
202 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
203 ),
|
amine@211
|
204 mono_use_channel_None=(
|
amine@211
|
205 1,
|
amine@211
|
206 {"eth": 50, "use_channel": None},
|
amine@211
|
207 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
208 ),
|
amine@211
|
209 stereo_use_channel_1=(
|
amine@211
|
210 2,
|
amine@211
|
211 {"eth": 50, "use_channel": 1},
|
amine@211
|
212 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
213 ),
|
amine@211
|
214 stereo_use_channel_left=(
|
amine@211
|
215 2,
|
amine@211
|
216 {"eth": 50, "use_channel": "left"},
|
amine@211
|
217 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
218 ),
|
amine@211
|
219 stereo_use_channel_no_use_channel_given=(
|
amine@211
|
220 2,
|
amine@211
|
221 {"eth": 50},
|
amine@211
|
222 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
223 ),
|
amine@211
|
224 stereo_use_channel_minus_2=(
|
amine@211
|
225 2,
|
amine@211
|
226 {"eth": 50, "use_channel": -2},
|
amine@211
|
227 [(2, 16), (17, 31), (34, 76)],
|
amine@211
|
228 ),
|
amine@211
|
229 stereo_uc_2=(2, {"eth": 50, "uc": 2}, [(10, 32), (36, 76)]),
|
amine@211
|
230 stereo_use_channel_right=(
|
amine@211
|
231 2,
|
amine@211
|
232 {"eth": 50, "use_channel": "right"},
|
amine@211
|
233 [(10, 32), (36, 76)],
|
amine@211
|
234 ),
|
amine@211
|
235 stereo_uc_minus_1=(2, {"eth": 50, "uc": -1}, [(10, 32), (36, 76)]),
|
amine@213
|
236 mono_uc_mix=(
|
amine@213
|
237 1,
|
amine@213
|
238 {"eth": 50, "uc": "mix"},
|
amine@213
|
239 [(2, 16), (17, 31), (34, 76)],
|
amine@213
|
240 ),
|
amine@213
|
241 stereo_use_channel_mix=(
|
amine@213
|
242 2,
|
amine@213
|
243 {"energy_threshold": 53.5, "use_channel": "mix"},
|
amine@213
|
244 [(54, 76)],
|
amine@213
|
245 ),
|
amine@213
|
246 stereo_uc_mix=(2, {"eth": 52, "uc": "mix"}, [(17, 26), (54, 76)]),
|
amine@213
|
247 stereo_uc_mix_default_eth=(
|
amine@213
|
248 2,
|
amine@213
|
249 {"uc": "mix"},
|
amine@213
|
250 [(10, 16), (17, 31), (36, 76)],
|
amine@213
|
251 ),
|
amine@211
|
252 )
|
amine@211
|
253 def test_split_kwargs(self, channels, kwargs, expected):
|
amine@211
|
254
|
amine@211
|
255 mono_or_stereo = "mono" if channels == 1 else "stereo"
|
amine@211
|
256 filename = "tests/data/test_split_10HZ_{}.raw".format(mono_or_stereo)
|
amine@211
|
257 with open(filename, "rb") as fp:
|
amine@211
|
258 data = fp.read()
|
amine@211
|
259
|
amine@211
|
260 regions = split(
|
amine@211
|
261 data,
|
amine@211
|
262 min_dur=0.2,
|
amine@211
|
263 max_dur=5,
|
amine@211
|
264 max_silence=0.2,
|
amine@211
|
265 drop_trailing_silence=False,
|
amine@211
|
266 strict_min_dur=False,
|
amine@211
|
267 analysis_window=0.1,
|
amine@211
|
268 sr=10,
|
amine@211
|
269 sw=2,
|
amine@211
|
270 ch=channels,
|
amine@211
|
271 **kwargs
|
amine@211
|
272 )
|
amine@212
|
273 regions = list(regions)
|
amine@211
|
274 sample_width = 2
|
amine@211
|
275 import numpy as np
|
amine@211
|
276
|
amine@211
|
277 use_channel = kwargs.get("use_channel", kwargs.get("uc"))
|
amine@211
|
278 # extrat channel of interest
|
amine@211
|
279 if channels != 1:
|
amine@211
|
280 use_channel = kwargs.get("use_channel", kwargs.get("uc"))
|
amine@211
|
281 use_channel = _normalize_use_channel(use_channel)
|
amine@211
|
282 data = _extract_selected_channel(
|
amine@211
|
283 data, channels, sample_width, use_channel=use_channel
|
amine@211
|
284 )
|
amine@211
|
285 err_msg = "Wrong number of regions after split, expected: "
|
amine@211
|
286 err_msg += "{}, found: {}".format(expected, regions)
|
amine@211
|
287 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@212
|
288 for reg, exp in zip(regions, expected):
|
amine@212
|
289 onset, offset = exp
|
amine@212
|
290 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@212
|
291 self.assertEqual(bytes(reg), exp_data)
|
amine@211
|
292
|
amine@212
|
293 @genty_dataset(
|
amine@220
|
294 mono_aw_0_2_max_silence_0_2=(
|
amine@220
|
295 0.2,
|
amine@220
|
296 5,
|
amine@220
|
297 0.2,
|
amine@220
|
298 1,
|
amine@220
|
299 {"uc": 1, "aw": 0.2},
|
amine@220
|
300 [(2, 30), (34, 76)],
|
amine@220
|
301 ),
|
amine@220
|
302 mono_aw_0_2_max_silence_0_3=(
|
amine@220
|
303 0.2,
|
amine@220
|
304 5,
|
amine@220
|
305 0.3,
|
amine@220
|
306 1,
|
amine@220
|
307 {"uc": 1, "aw": 0.2},
|
amine@227
|
308 [(2, 30), (34, 76)],
|
amine@220
|
309 ),
|
amine@220
|
310 mono_aw_0_2_max_silence_0_4=(
|
amine@220
|
311 0.2,
|
amine@220
|
312 5,
|
amine@227
|
313 0.4,
|
amine@220
|
314 1,
|
amine@220
|
315 {"uc": 1, "aw": 0.2},
|
amine@220
|
316 [(2, 32), (34, 76)],
|
amine@220
|
317 ),
|
amine@231
|
318 mono_aw_0_2_max_silence_0=(
|
amine@231
|
319 0.2,
|
amine@231
|
320 5,
|
amine@231
|
321 0,
|
amine@231
|
322 1,
|
amine@231
|
323 {"uc": 1, "aw": 0.2},
|
amine@231
|
324 [(2, 14), (16, 24), (26, 28), (34, 76)],
|
amine@231
|
325 ),
|
amine@220
|
326 mono_aw_0_2=(
|
amine@220
|
327 0.2,
|
amine@220
|
328 5,
|
amine@220
|
329 0.2,
|
amine@220
|
330 1,
|
amine@220
|
331 {"uc": 1, "aw": 0.2},
|
amine@220
|
332 [(2, 30), (34, 76)],
|
amine@220
|
333 ),
|
amine@231
|
334 mono_aw_0_3_max_silence_0=(
|
amine@231
|
335 0.3,
|
amine@231
|
336 5,
|
amine@231
|
337 0,
|
amine@231
|
338 1,
|
amine@231
|
339 {"uc": 1, "aw": 0.3},
|
amine@231
|
340 [(3, 12), (15, 24), (36, 76)],
|
amine@231
|
341 ),
|
amine@231
|
342 mono_aw_0_3_max_silence_0_3=(
|
amine@231
|
343 0.3,
|
amine@231
|
344 5,
|
amine@231
|
345 0.3,
|
amine@231
|
346 1,
|
amine@231
|
347 {"uc": 1, "aw": 0.3},
|
amine@231
|
348 [(3, 27), (36, 76)],
|
amine@231
|
349 ),
|
amine@231
|
350 mono_aw_0_3_max_silence_0_5=(
|
amine@231
|
351 0.3,
|
amine@231
|
352 5,
|
amine@231
|
353 0.5,
|
amine@231
|
354 1,
|
amine@231
|
355 {"uc": 1, "aw": 0.3},
|
amine@231
|
356 [(3, 27), (36, 76)],
|
amine@231
|
357 ),
|
amine@231
|
358 mono_aw_0_3_max_silence_0_6=(
|
amine@231
|
359 0.3,
|
amine@231
|
360 5,
|
amine@231
|
361 0.6,
|
amine@231
|
362 1,
|
amine@231
|
363 {"uc": 1, "aw": 0.3},
|
amine@231
|
364 [(3, 30), (36, 76)],
|
amine@231
|
365 ),
|
amine@231
|
366 mono_aw_0_4_max_silence_0=(
|
amine@231
|
367 0.2,
|
amine@231
|
368 5,
|
amine@232
|
369 0,
|
amine@231
|
370 1,
|
amine@231
|
371 {"uc": 1, "aw": 0.4},
|
amine@231
|
372 [(4, 12), (16, 24), (36, 76)],
|
amine@231
|
373 ),
|
amine@231
|
374 mono_aw_0_4_max_silence_0_3=(
|
amine@231
|
375 0.2,
|
amine@231
|
376 5,
|
amine@231
|
377 0.3,
|
amine@231
|
378 1,
|
amine@231
|
379 {"uc": 1, "aw": 0.4},
|
amine@231
|
380 [(4, 12), (16, 24), (36, 76)],
|
amine@231
|
381 ),
|
amine@231
|
382 mono_aw_0_4_max_silence_0_4=(
|
amine@231
|
383 0.2,
|
amine@231
|
384 5,
|
amine@231
|
385 0.4,
|
amine@231
|
386 1,
|
amine@231
|
387 {"uc": 1, "aw": 0.4},
|
amine@231
|
388 [(4, 28), (36, 76)],
|
amine@231
|
389 ),
|
amine@220
|
390 stereo_uc_1_analysis_window_0_2=(
|
amine@220
|
391 0.2,
|
amine@220
|
392 5,
|
amine@220
|
393 0.2,
|
amine@220
|
394 2,
|
amine@220
|
395 {"uc": 1, "analysis_window": 0.2},
|
amine@220
|
396 [(2, 30), (34, 76)],
|
amine@220
|
397 ),
|
amine@231
|
398 stereo_uc_2_analysis_window_0_2=(
|
amine@231
|
399 0.2,
|
amine@231
|
400 5,
|
amine@231
|
401 0.2,
|
amine@231
|
402 2,
|
amine@231
|
403 {"uc": 2, "analysis_window": 0.2},
|
amine@231
|
404 [(10, 32), (36, 76)],
|
amine@231
|
405 ),
|
amine@220
|
406 )
|
amine@220
|
407 def test_split_analysis_window(
|
amine@220
|
408 self, min_dur, max_dur, max_silence, channels, kwargs, expected
|
amine@220
|
409 ):
|
amine@220
|
410
|
amine@220
|
411 mono_or_stereo = "mono" if channels == 1 else "stereo"
|
amine@220
|
412 filename = "tests/data/test_split_10HZ_{}.raw".format(mono_or_stereo)
|
amine@220
|
413 with open(filename, "rb") as fp:
|
amine@220
|
414 data = fp.read()
|
amine@220
|
415
|
amine@220
|
416 regions = split(
|
amine@220
|
417 data,
|
amine@220
|
418 min_dur=min_dur,
|
amine@220
|
419 max_dur=max_dur,
|
amine@220
|
420 max_silence=max_silence,
|
amine@220
|
421 drop_trailing_silence=False,
|
amine@220
|
422 strict_min_dur=False,
|
amine@220
|
423 sr=10,
|
amine@220
|
424 sw=2,
|
amine@220
|
425 ch=channels,
|
amine@220
|
426 **kwargs
|
amine@220
|
427 )
|
amine@220
|
428 regions = list(regions)
|
amine@220
|
429 sample_width = 2
|
amine@220
|
430 import numpy as np
|
amine@220
|
431
|
amine@220
|
432 use_channel = kwargs.get("use_channel", kwargs.get("uc"))
|
amine@220
|
433 # extrat channel of interest
|
amine@220
|
434 if channels != 1:
|
amine@220
|
435 use_channel = kwargs.get("use_channel", kwargs.get("uc"))
|
amine@220
|
436 use_channel = _normalize_use_channel(use_channel)
|
amine@220
|
437 data = _extract_selected_channel(
|
amine@220
|
438 data, channels, sample_width, use_channel=use_channel
|
amine@220
|
439 )
|
amine@220
|
440 err_msg = "Wrong number of regions after split, expected: "
|
amine@220
|
441 err_msg += "{}, found: {}".format(expected, regions)
|
amine@220
|
442 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@220
|
443 for reg, exp in zip(regions, expected):
|
amine@220
|
444 onset, offset = exp
|
amine@220
|
445 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@220
|
446 self.assertEqual(bytes(reg), exp_data)
|
amine@220
|
447
|
amine@220
|
448 @genty_dataset(
|
amine@212
|
449 filename_audio_format=(
|
amine@212
|
450 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
451 {"audio_format": "raw", "sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
452 ),
|
amine@212
|
453 filename_audio_format_short_name=(
|
amine@212
|
454 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
455 {"fmt": "raw", "sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
456 ),
|
amine@212
|
457 filename_no_audio_format=(
|
amine@212
|
458 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
459 {"sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
460 ),
|
amine@212
|
461 filename_no_long_audio_params=(
|
amine@212
|
462 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
463 {"sampling_rate": 10, "sample_width": 2, "channels": 2},
|
amine@212
|
464 ),
|
amine@212
|
465 bytes_=(
|
amine@212
|
466 open("tests/data/test_split_10HZ_stereo.raw", "rb").read(),
|
amine@212
|
467 {"sr": 10, "sw": 2, "ch": 2},
|
amine@212
|
468 ),
|
amine@212
|
469 audio_reader=(
|
amine@212
|
470 AudioDataSource(
|
amine@212
|
471 "tests/data/test_split_10HZ_stereo.raw",
|
amine@212
|
472 sr=10,
|
amine@212
|
473 sw=2,
|
amine@212
|
474 ch=2,
|
amine@212
|
475 block_dur=0.1,
|
amine@212
|
476 ),
|
amine@212
|
477 {},
|
amine@212
|
478 ),
|
amine@212
|
479 audio_region=(
|
amine@212
|
480 AudioRegion(
|
amine@212
|
481 open("tests/data/test_split_10HZ_stereo.raw", "rb").read(),
|
amine@212
|
482 0,
|
amine@212
|
483 10,
|
amine@212
|
484 2,
|
amine@212
|
485 2,
|
amine@212
|
486 ),
|
amine@212
|
487 {},
|
amine@212
|
488 ),
|
amine@212
|
489 audio_source=(
|
amine@212
|
490 get_audio_source(
|
amine@212
|
491 "tests/data/test_split_10HZ_stereo.raw", sr=10, sw=2, ch=2
|
amine@212
|
492 ),
|
amine@212
|
493 {},
|
amine@212
|
494 ),
|
amine@212
|
495 )
|
amine@212
|
496 def test_split_input_type(self, input, kwargs):
|
amine@212
|
497
|
amine@212
|
498 with open("tests/data/test_split_10HZ_mono.raw", "rb") as fp:
|
amine@212
|
499 data = fp.read()
|
amine@212
|
500
|
amine@212
|
501 regions = split(
|
amine@212
|
502 input,
|
amine@212
|
503 min_dur=0.2,
|
amine@212
|
504 max_dur=5,
|
amine@212
|
505 max_silence=0.2,
|
amine@212
|
506 drop_trailing_silence=False,
|
amine@212
|
507 strict_min_dur=False,
|
amine@212
|
508 analysis_window=0.1,
|
amine@212
|
509 **kwargs
|
amine@212
|
510 )
|
amine@212
|
511 regions = list(regions)
|
amine@212
|
512 expected = [(2, 16), (17, 31), (34, 76)]
|
amine@212
|
513 sample_width = 2
|
amine@212
|
514 err_msg = "Wrong number of regions after split, expected: "
|
amine@212
|
515 err_msg += "{}, found: {}".format(expected, regions)
|
amine@212
|
516 self.assertEqual(len(regions), len(expected), err_msg)
|
amine@211
|
517 for reg, exp in zip(regions, expected):
|
amine@211
|
518 onset, offset = exp
|
amine@211
|
519 exp_data = data[onset * sample_width : offset * sample_width]
|
amine@211
|
520 self.assertEqual(bytes(reg), exp_data)
|
amine@211
|
521
|
amine@223
|
522 @genty_dataset(
|
amine@223
|
523 min_dur_greater_than_max_dur=(0.5, 0.4, 0.1),
|
amine@223
|
524 durations_OK_but_wrong_number_of_analysis_windows=(0.44, 0.49, 0.1),
|
amine@223
|
525 )
|
amine@223
|
526 def test_split_wrong_min_max_dur(self, min_dur, max_dur, analysis_window):
|
amine@223
|
527
|
amine@223
|
528 with self.assertRaises(ValueError) as val_err:
|
amine@223
|
529 split(
|
amine@223
|
530 b"0" * 16,
|
amine@223
|
531 min_dur=min_dur,
|
amine@223
|
532 max_dur=max_dur,
|
amine@223
|
533 max_silence=0.2,
|
amine@223
|
534 sr=16000,
|
amine@223
|
535 sw=1,
|
amine@223
|
536 ch=1,
|
amine@223
|
537 analysis_window=analysis_window,
|
amine@223
|
538 )
|
amine@223
|
539
|
amine@223
|
540 err_msg = "'min_dur' ({0} sec.) results in {1} analysis "
|
amine@223
|
541 err_msg += "window(s) ({1} == ceil({0} / {2})) which is "
|
amine@223
|
542 err_msg += "higher than the number of analysis window(s) for "
|
amine@223
|
543 err_msg += "'max_dur' ({3} == floor({4} / {2}))"
|
amine@223
|
544
|
amine@223
|
545 err_msg = err_msg.format(
|
amine@223
|
546 min_dur,
|
amine@223
|
547 math.ceil(min_dur / analysis_window),
|
amine@223
|
548 analysis_window,
|
amine@223
|
549 math.floor(max_dur / analysis_window),
|
amine@223
|
550 max_dur,
|
amine@223
|
551 )
|
amine@223
|
552 self.assertEqual(err_msg, str(val_err.exception))
|
amine@223
|
553
|
amine@224
|
554 @genty_dataset(
|
amine@224
|
555 max_silence_equals_max_dur=(0.5, 0.5, 0.1),
|
amine@224
|
556 max_silence_greater_than_max_dur=(0.5, 0.4, 0.1),
|
amine@224
|
557 durations_OK_but_wrong_number_of_analysis_windows=(0.44, 0.49, 0.1),
|
amine@224
|
558 )
|
amine@224
|
559 def test_split_wrong_max_silence_max_dur(
|
amine@224
|
560 self, max_silence, max_dur, analysis_window
|
amine@224
|
561 ):
|
amine@224
|
562
|
amine@224
|
563 with self.assertRaises(ValueError) as val_err:
|
amine@224
|
564 split(
|
amine@224
|
565 b"0" * 16,
|
amine@224
|
566 min_dur=0.2,
|
amine@224
|
567 max_dur=max_dur,
|
amine@224
|
568 max_silence=max_silence,
|
amine@224
|
569 sr=16000,
|
amine@224
|
570 sw=1,
|
amine@224
|
571 ch=1,
|
amine@224
|
572 analysis_window=analysis_window,
|
amine@224
|
573 )
|
amine@224
|
574
|
amine@224
|
575 err_msg = "'max_silence' ({0} sec.) results in {1} analysis "
|
amine@224
|
576 err_msg += "window(s) ({1} == floor({0} / {2})) which is "
|
amine@224
|
577 err_msg += "higher or equal to the number of analysis window(s) for "
|
amine@224
|
578 err_msg += "'max_dur' ({3} == floor({4} / {2}))"
|
amine@224
|
579
|
amine@224
|
580 err_msg = err_msg.format(
|
amine@224
|
581 max_silence,
|
amine@224
|
582 math.floor(max_silence / analysis_window),
|
amine@224
|
583 analysis_window,
|
amine@224
|
584 math.floor(max_dur / analysis_window),
|
amine@224
|
585 max_dur,
|
amine@224
|
586 )
|
amine@224
|
587 self.assertEqual(err_msg, str(val_err.exception))
|
amine@224
|
588
|
amine@226
|
589 @genty_dataset(
|
amine@226
|
590 negative_min_dur=({"min_dur": -1},),
|
amine@226
|
591 zero_min_dur=({"min_dur": 0},),
|
amine@226
|
592 negative_max_dur=({"max_dur": -1},),
|
amine@226
|
593 zero_max_dur=({"max_dur": 0},),
|
amine@226
|
594 negative_max_silence=({"max_silence": -1},),
|
amine@226
|
595 )
|
amine@226
|
596 def test_split_negative_temporal_params(self, wrong_param):
|
amine@226
|
597
|
amine@226
|
598 params = {"min_dur": 0.2, "max_dur": 0.5, "max_silence": 0.1}
|
amine@226
|
599 params.update(wrong_param)
|
amine@226
|
600 with self.assertRaises(ValueError) as val_err:
|
amine@226
|
601 split(None, **params)
|
amine@226
|
602
|
amine@226
|
603 name = set(wrong_param).pop()
|
amine@226
|
604 value = wrong_param[name]
|
amine@226
|
605 err_msg = "'{}' ({}) must be >{} 0".format(
|
amine@226
|
606 name, value, "=" if name == "max_silence" else ""
|
amine@226
|
607 )
|
amine@226
|
608 self.assertEqual(err_msg, str(val_err.exception))
|
amine@226
|
609
|
amine@207
|
610
|
amine@207
|
611 @genty
|
amine@207
|
612 class TestAudioRegion(TestCase):
|
amine@86
|
613 @genty_dataset(
|
amine@86
|
614 simple=(b"\0" * 8000, 0, 8000, 1, 1, 1, 1, 1000),
|
amine@86
|
615 one_ms_less_than_1_sec=(
|
amine@86
|
616 b"\0" * 7992,
|
amine@86
|
617 0,
|
amine@86
|
618 8000,
|
amine@86
|
619 1,
|
amine@86
|
620 1,
|
amine@86
|
621 0.999,
|
amine@86
|
622 0.999,
|
amine@86
|
623 999,
|
amine@86
|
624 ),
|
amine@86
|
625 tree_quarter_ms_less_than_1_sec=(
|
amine@86
|
626 b"\0" * 7994,
|
amine@86
|
627 0,
|
amine@86
|
628 8000,
|
amine@86
|
629 1,
|
amine@86
|
630 1,
|
amine@86
|
631 0.99925,
|
amine@86
|
632 0.99925,
|
amine@86
|
633 999,
|
amine@86
|
634 ),
|
amine@86
|
635 half_ms_less_than_1_sec=(
|
amine@86
|
636 b"\0" * 7996,
|
amine@86
|
637 0,
|
amine@86
|
638 8000,
|
amine@86
|
639 1,
|
amine@86
|
640 1,
|
amine@86
|
641 0.9995,
|
amine@86
|
642 0.9995,
|
amine@86
|
643 1000,
|
amine@86
|
644 ),
|
amine@86
|
645 quarter_ms_less_than_1_sec=(
|
amine@86
|
646 b"\0" * 7998,
|
amine@86
|
647 0,
|
amine@86
|
648 8000,
|
amine@86
|
649 1,
|
amine@86
|
650 1,
|
amine@86
|
651 0.99975,
|
amine@86
|
652 0.99975,
|
amine@86
|
653 1000,
|
amine@86
|
654 ),
|
amine@86
|
655 simple_sample_width_2=(b"\0" * 8000 * 2, 0, 8000, 2, 1, 1, 1, 1000),
|
amine@86
|
656 simple_stereo=(b"\0" * 8000 * 2, 0, 8000, 1, 2, 1, 1, 1000),
|
amine@86
|
657 simple_multichannel=(b"\0" * 8000 * 5, 0, 8000, 1, 5, 1, 1, 1000),
|
amine@86
|
658 simple_sample_width_2_multichannel=(
|
amine@86
|
659 b"\0" * 8000 * 2 * 5,
|
amine@86
|
660 0,
|
amine@86
|
661 8000,
|
amine@86
|
662 2,
|
amine@86
|
663 5,
|
amine@86
|
664 1,
|
amine@86
|
665 1,
|
amine@86
|
666 1000,
|
amine@86
|
667 ),
|
amine@86
|
668 one_ms_less_than_1s_sw_2_multichannel=(
|
amine@86
|
669 b"\0" * 7992 * 2 * 5,
|
amine@86
|
670 0,
|
amine@86
|
671 8000,
|
amine@86
|
672 2,
|
amine@86
|
673 5,
|
amine@86
|
674 0.999,
|
amine@86
|
675 0.999,
|
amine@86
|
676 999,
|
amine@86
|
677 ),
|
amine@86
|
678 tree_qrt_ms_lt_1_s_sw_2_multichannel=(
|
amine@86
|
679 b"\0" * 7994 * 2 * 5,
|
amine@86
|
680 0,
|
amine@86
|
681 8000,
|
amine@86
|
682 2,
|
amine@86
|
683 5,
|
amine@86
|
684 0.99925,
|
amine@86
|
685 0.99925,
|
amine@86
|
686 999,
|
amine@86
|
687 ),
|
amine@86
|
688 half_ms_lt_1s_sw_2_multichannel=(
|
amine@86
|
689 b"\0" * 7996 * 2 * 5,
|
amine@86
|
690 0,
|
amine@86
|
691 8000,
|
amine@86
|
692 2,
|
amine@86
|
693 5,
|
amine@86
|
694 0.9995,
|
amine@86
|
695 0.9995,
|
amine@86
|
696 1000,
|
amine@86
|
697 ),
|
amine@86
|
698 quarter_ms_lt_1s_sw_2_multichannel=(
|
amine@86
|
699 b"\0" * 7998 * 2 * 5,
|
amine@86
|
700 0,
|
amine@86
|
701 8000,
|
amine@86
|
702 2,
|
amine@86
|
703 5,
|
amine@86
|
704 0.99975,
|
amine@86
|
705 0.99975,
|
amine@86
|
706 1000,
|
amine@86
|
707 ),
|
amine@86
|
708 arbitrary_length_1=(
|
amine@86
|
709 b"\0" * int(8000 * 1.33),
|
amine@86
|
710 2.7,
|
amine@86
|
711 8000,
|
amine@86
|
712 1,
|
amine@86
|
713 1,
|
amine@86
|
714 4.03,
|
amine@86
|
715 1.33,
|
amine@86
|
716 1330,
|
amine@86
|
717 ),
|
amine@86
|
718 arbitrary_length_2=(
|
amine@86
|
719 b"\0" * int(8000 * 0.476),
|
amine@86
|
720 11.568,
|
amine@86
|
721 8000,
|
amine@86
|
722 1,
|
amine@86
|
723 1,
|
amine@86
|
724 12.044,
|
amine@86
|
725 0.476,
|
amine@86
|
726 476,
|
amine@86
|
727 ),
|
amine@86
|
728 arbitrary_length_sw_2_multichannel=(
|
amine@86
|
729 b"\0" * int(8000 * 1.711) * 2 * 3,
|
amine@86
|
730 9.415,
|
amine@86
|
731 8000,
|
amine@86
|
732 2,
|
amine@86
|
733 3,
|
amine@86
|
734 11.126,
|
amine@86
|
735 1.711,
|
amine@86
|
736 1711,
|
amine@86
|
737 ),
|
amine@86
|
738 arbitrary_samplig_rate=(
|
amine@86
|
739 b"\0" * int(3172 * 1.318),
|
amine@86
|
740 17.236,
|
amine@86
|
741 3172,
|
amine@86
|
742 1,
|
amine@86
|
743 1,
|
amine@86
|
744 17.236 + int(3172 * 1.318) / 3172,
|
amine@86
|
745 int(3172 * 1.318) / 3172,
|
amine@86
|
746 1318,
|
amine@86
|
747 ),
|
amine@86
|
748 arbitrary_sr_sw_2_multichannel=(
|
amine@86
|
749 b"\0" * int(11317 * 0.716) * 2 * 3,
|
amine@86
|
750 18.811,
|
amine@86
|
751 11317,
|
amine@86
|
752 2,
|
amine@86
|
753 3,
|
amine@86
|
754 18.811 + int(11317 * 0.716) / 11317,
|
amine@86
|
755 int(11317 * 0.716) / 11317,
|
amine@86
|
756 716,
|
amine@86
|
757 ),
|
amine@86
|
758 )
|
amine@86
|
759 def test_creation(
|
amine@86
|
760 self,
|
amine@86
|
761 data,
|
amine@86
|
762 start,
|
amine@86
|
763 sampling_rate,
|
amine@86
|
764 sample_width,
|
amine@86
|
765 channels,
|
amine@86
|
766 expected_end,
|
amine@86
|
767 expected_duration_s,
|
amine@86
|
768 expected_duration_ms,
|
amine@86
|
769 ):
|
amine@86
|
770 region = AudioRegion(
|
amine@86
|
771 data, start, sampling_rate, sample_width, channels
|
amine@86
|
772 )
|
amine@86
|
773 self.assertEqual(region.sampling_rate, sampling_rate)
|
amine@86
|
774 self.assertEqual(region.sr, sampling_rate)
|
amine@86
|
775 self.assertEqual(region.sample_width, sample_width)
|
amine@86
|
776 self.assertEqual(region.sw, sample_width)
|
amine@86
|
777 self.assertEqual(region.channels, channels)
|
amine@86
|
778 self.assertEqual(region.ch, channels)
|
amine@86
|
779 self.assertEqual(region.start, start)
|
amine@86
|
780 self.assertEqual(region.end, expected_end)
|
amine@86
|
781 self.assertEqual(region.duration, expected_duration_s)
|
amine@86
|
782 self.assertEqual(len(region), expected_duration_ms)
|
amine@86
|
783 self.assertEqual(bytes(region), data)
|
amine@88
|
784
|
amine@97
|
785 def test_creation_invalid_data_exception(self):
|
amine@97
|
786 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@97
|
787 _ = AudioRegion(
|
amine@97
|
788 data=b"ABCDEFGHI",
|
amine@97
|
789 start=0,
|
amine@97
|
790 sampling_rate=8,
|
amine@97
|
791 sample_width=2,
|
amine@97
|
792 channels=1,
|
amine@97
|
793 )
|
amine@97
|
794 self.assertEqual(
|
amine@97
|
795 "The length of audio data must be an integer "
|
amine@97
|
796 "multiple of `sample_width * channels`",
|
amine@97
|
797 str(audio_param_err.exception),
|
amine@97
|
798 )
|
amine@97
|
799
|
amine@88
|
800 @genty_dataset(
|
amine@192
|
801 simple=("output.wav", 1.230, "output.wav"),
|
amine@192
|
802 start=("output_{start}.wav", 1.230, "output_1.23.wav"),
|
amine@192
|
803 start_2=("output_{start}.wav", 1.233712, "output_1.233712.wav"),
|
amine@192
|
804 start_3=("output_{start}.wav", 1.2300001, "output_1.23.wav"),
|
amine@192
|
805 start_4=("output_{start:.3f}.wav", 1.233712, "output_1.234.wav"),
|
amine@192
|
806 start_5=(
|
amine@192
|
807 "output_{start:.8f}.wav",
|
amine@192
|
808 1.233712345,
|
amine@192
|
809 "output_1.23371200.wav",
|
amine@192
|
810 ),
|
amine@192
|
811 start_end_duration=(
|
amine@192
|
812 "output_{start}_{end}_{duration}.wav",
|
amine@192
|
813 1.455,
|
amine@192
|
814 "output_1.455_2.455_1.0.wav",
|
amine@192
|
815 ),
|
amine@192
|
816 start_end_duration_2=(
|
amine@192
|
817 "output_{start}_{end}_{duration}.wav",
|
amine@192
|
818 1.455321,
|
amine@192
|
819 "output_1.455321_2.455321_1.0.wav",
|
amine@192
|
820 ),
|
amine@192
|
821 )
|
amine@192
|
822 def test_save(self, format, start, expected):
|
amine@192
|
823 with TemporaryDirectory() as tmpdir:
|
amine@192
|
824 region = AudioRegion(b"0" * 160, start, 160, 1, 1)
|
amine@192
|
825 format = os.path.join(tmpdir, format)
|
amine@192
|
826 filename = region.save(format)[len(tmpdir) + 1 :]
|
amine@192
|
827 self.assertEqual(filename, expected)
|
amine@192
|
828
|
amine@193
|
829 def test_save_file_exists_exception(self):
|
amine@193
|
830 with TemporaryDirectory() as tmpdir:
|
amine@193
|
831 filename = os.path.join(tmpdir, "output.wav")
|
amine@193
|
832 open(filename, "w").close()
|
amine@193
|
833 region = AudioRegion(b"0" * 160, 0, 160, 1, 1)
|
amine@193
|
834 with self.assertRaises(FileExistsError):
|
amine@193
|
835 region.save(filename, exists_ok=False)
|
amine@193
|
836
|
amine@192
|
837 @genty_dataset(
|
amine@194
|
838 first_half=(
|
amine@194
|
839 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
840 slice(0, 500),
|
amine@194
|
841 0,
|
amine@194
|
842 b"a" * 80,
|
amine@194
|
843 ),
|
amine@194
|
844 second_half=(
|
amine@194
|
845 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
846 slice(500, None),
|
amine@194
|
847 0.5,
|
amine@194
|
848 b"b" * 80,
|
amine@194
|
849 ),
|
amine@194
|
850 second_half_negative=(
|
amine@194
|
851 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
852 slice(-500, None),
|
amine@194
|
853 0.5,
|
amine@194
|
854 b"b" * 80,
|
amine@194
|
855 ),
|
amine@194
|
856 middle=(
|
amine@194
|
857 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
858 slice(200, 750),
|
amine@194
|
859 0.2,
|
amine@194
|
860 b"a" * 48 + b"b" * 40,
|
amine@194
|
861 ),
|
amine@194
|
862 middle_negative=(
|
amine@194
|
863 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
864 slice(-800, -250),
|
amine@194
|
865 0.2,
|
amine@194
|
866 b"a" * 48 + b"b" * 40,
|
amine@194
|
867 ),
|
amine@194
|
868 middle_sw2=(
|
amine@194
|
869 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 2, 1),
|
amine@194
|
870 slice(200, 750),
|
amine@194
|
871 0.2,
|
amine@194
|
872 b"a" * 96 + b"b" * 80,
|
amine@194
|
873 ),
|
amine@194
|
874 middle_ch2=(
|
amine@194
|
875 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 1, 2),
|
amine@194
|
876 slice(200, 750),
|
amine@194
|
877 0.2,
|
amine@194
|
878 b"a" * 96 + b"b" * 80,
|
amine@194
|
879 ),
|
amine@194
|
880 middle_sw2_ch2=(
|
amine@194
|
881 AudioRegion(b"a" * 320 + b"b" * 320, 0, 160, 2, 2),
|
amine@194
|
882 slice(200, 750),
|
amine@194
|
883 0.2,
|
amine@194
|
884 b"a" * 192 + b"b" * 160,
|
amine@194
|
885 ),
|
amine@194
|
886 but_first_sample=(
|
amine@194
|
887 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
888 slice(1, None),
|
amine@194
|
889 0.001,
|
amine@194
|
890 b"a" * (4000 - 8) + b"b" * 4000,
|
amine@194
|
891 ),
|
amine@194
|
892 but_first_sample_negative=(
|
amine@194
|
893 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
894 slice(-999, None),
|
amine@194
|
895 0.001,
|
amine@194
|
896 b"a" * (4000 - 8) + b"b" * 4000,
|
amine@194
|
897 ),
|
amine@194
|
898 but_last_sample=(
|
amine@194
|
899 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
900 slice(0, 999),
|
amine@194
|
901 0,
|
amine@194
|
902 b"a" * 4000 + b"b" * (4000 - 8),
|
amine@194
|
903 ),
|
amine@194
|
904 but_last_sample_negative=(
|
amine@194
|
905 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
906 slice(0, -1),
|
amine@194
|
907 0,
|
amine@194
|
908 b"a" * 4000 + b"b" * (4000 - 8),
|
amine@194
|
909 ),
|
amine@194
|
910 big_negative_start=(
|
amine@194
|
911 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@194
|
912 slice(-5000, None),
|
amine@194
|
913 0,
|
amine@194
|
914 b"a" * 160,
|
amine@194
|
915 ),
|
amine@194
|
916 big_negative_stop=(
|
amine@194
|
917 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@194
|
918 slice(None, -1500),
|
amine@194
|
919 0,
|
amine@194
|
920 b"",
|
amine@194
|
921 ),
|
amine@194
|
922 empty=(
|
amine@194
|
923 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
924 slice(0, 0),
|
amine@194
|
925 0,
|
amine@194
|
926 b"",
|
amine@194
|
927 ),
|
amine@194
|
928 empty_start_stop_reversed=(
|
amine@194
|
929 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
930 slice(200, 100),
|
amine@194
|
931 0.2,
|
amine@194
|
932 b"",
|
amine@194
|
933 ),
|
amine@194
|
934 empty_big_positive_start=(
|
amine@194
|
935 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
936 slice(2000, 3000),
|
amine@194
|
937 2,
|
amine@194
|
938 b"",
|
amine@194
|
939 ),
|
amine@194
|
940 empty_negative_reversed=(
|
amine@194
|
941 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
942 slice(-100, -200),
|
amine@194
|
943 0.9,
|
amine@194
|
944 b"",
|
amine@194
|
945 ),
|
amine@194
|
946 empty_big_negative_stop=(
|
amine@194
|
947 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
948 slice(0, -2000),
|
amine@194
|
949 0,
|
amine@194
|
950 b"",
|
amine@194
|
951 ),
|
amine@230
|
952 arbitrary_sampling_rate=(
|
amine@230
|
953 AudioRegion(b"a" * 124 + b"b" * 376, 0, 1234, 1, 1),
|
amine@230
|
954 slice(100, 200),
|
amine@230
|
955 123 / 1234,
|
amine@230
|
956 b"a" + b"b" * 123,
|
amine@230
|
957 ),
|
amine@194
|
958 )
|
amine@231
|
959 def test_region_temporal_slicing(
|
amine@194
|
960 self, region, slice_, expected_start, expected_data
|
amine@194
|
961 ):
|
amine@229
|
962 sub_region = region.millis[slice_]
|
amine@194
|
963 self.assertEqual(sub_region.start, expected_start)
|
amine@194
|
964 self.assertEqual(bytes(sub_region), expected_data)
|
amine@194
|
965
|
amine@229
|
966 start_sec = slice_.start / 1000 if slice_.start is not None else None
|
amine@229
|
967 stop_sec = slice_.stop / 1000 if slice_.stop is not None else None
|
amine@229
|
968
|
amine@229
|
969 sub_region = region.sec[start_sec:stop_sec]
|
amine@229
|
970 self.assertEqual(sub_region.start, expected_start)
|
amine@229
|
971 self.assertEqual(bytes(sub_region), expected_data)
|
amine@229
|
972
|
amine@194
|
973 @genty_dataset(
|
amine@231
|
974 first_half=(
|
amine@231
|
975 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
976 slice(0, 80),
|
amine@231
|
977 0,
|
amine@231
|
978 b"a" * 80,
|
amine@231
|
979 ),
|
amine@231
|
980 second_half=(
|
amine@231
|
981 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
982 slice(80, None),
|
amine@231
|
983 0.5,
|
amine@231
|
984 b"b" * 80,
|
amine@231
|
985 ),
|
amine@231
|
986 second_half_negative=(
|
amine@231
|
987 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
988 slice(-80, None),
|
amine@231
|
989 0.5,
|
amine@231
|
990 b"b" * 80,
|
amine@231
|
991 ),
|
amine@231
|
992 middle=(
|
amine@231
|
993 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
994 slice(160 // 5, 160 // 4 * 3),
|
amine@231
|
995 0.2,
|
amine@231
|
996 b"a" * 48 + b"b" * 40,
|
amine@231
|
997 ),
|
amine@231
|
998 middle_negative=(
|
amine@231
|
999 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
1000 slice(-160 // 5 * 4, -160 // 4),
|
amine@231
|
1001 0.2,
|
amine@231
|
1002 b"a" * 48 + b"b" * 40,
|
amine@231
|
1003 ),
|
amine@231
|
1004 middle_sw2=(
|
amine@231
|
1005 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 2, 1),
|
amine@231
|
1006 slice(160 // 5, 160 // 4 * 3),
|
amine@231
|
1007 0.2,
|
amine@231
|
1008 b"a" * 96 + b"b" * 80,
|
amine@231
|
1009 ),
|
amine@231
|
1010 middle_ch2=(
|
amine@231
|
1011 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 1, 2),
|
amine@231
|
1012 slice(160 // 5, 160 // 4 * 3),
|
amine@231
|
1013 0.2,
|
amine@231
|
1014 b"a" * 96 + b"b" * 80,
|
amine@231
|
1015 ),
|
amine@231
|
1016 middle_sw2_ch2=(
|
amine@231
|
1017 AudioRegion(b"a" * 320 + b"b" * 320, 0, 160, 2, 2),
|
amine@231
|
1018 slice(160 // 5, 160 // 4 * 3),
|
amine@231
|
1019 0.2,
|
amine@231
|
1020 b"a" * 192 + b"b" * 160,
|
amine@231
|
1021 ),
|
amine@231
|
1022 but_first_sample=(
|
amine@231
|
1023 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@231
|
1024 slice(1, None),
|
amine@231
|
1025 1 / 8000,
|
amine@231
|
1026 b"a" * (4000 - 1) + b"b" * 4000,
|
amine@231
|
1027 ),
|
amine@231
|
1028 but_first_sample_negative=(
|
amine@231
|
1029 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@231
|
1030 slice(-7999, None),
|
amine@231
|
1031 1 / 8000,
|
amine@231
|
1032 b"a" * (4000 - 1) + b"b" * 4000,
|
amine@231
|
1033 ),
|
amine@231
|
1034 but_last_sample=(
|
amine@231
|
1035 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@231
|
1036 slice(0, 7999),
|
amine@231
|
1037 0,
|
amine@231
|
1038 b"a" * 4000 + b"b" * (4000 - 1),
|
amine@231
|
1039 ),
|
amine@231
|
1040 but_last_sample_negative=(
|
amine@231
|
1041 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@231
|
1042 slice(0, -1),
|
amine@231
|
1043 0,
|
amine@231
|
1044 b"a" * 4000 + b"b" * (4000 - 1),
|
amine@231
|
1045 ),
|
amine@231
|
1046 big_negative_start=(
|
amine@231
|
1047 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@231
|
1048 slice(-1600, None),
|
amine@231
|
1049 0,
|
amine@231
|
1050 b"a" * 160,
|
amine@231
|
1051 ),
|
amine@231
|
1052 big_negative_stop=(
|
amine@231
|
1053 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@231
|
1054 slice(None, -1600),
|
amine@231
|
1055 0,
|
amine@231
|
1056 b"",
|
amine@231
|
1057 ),
|
amine@231
|
1058 empty=(
|
amine@231
|
1059 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
1060 slice(0, 0),
|
amine@231
|
1061 0,
|
amine@231
|
1062 b"",
|
amine@231
|
1063 ),
|
amine@231
|
1064 empty_start_stop_reversed=(
|
amine@231
|
1065 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
1066 slice(80, 40),
|
amine@231
|
1067 0.5,
|
amine@231
|
1068 b"",
|
amine@231
|
1069 ),
|
amine@231
|
1070 empty_big_positive_start=(
|
amine@231
|
1071 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
1072 slice(1600, 3000),
|
amine@231
|
1073 10,
|
amine@231
|
1074 b"",
|
amine@231
|
1075 ),
|
amine@231
|
1076 empty_negative_reversed=(
|
amine@231
|
1077 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
1078 slice(-16, -32),
|
amine@231
|
1079 0.9,
|
amine@231
|
1080 b"",
|
amine@231
|
1081 ),
|
amine@231
|
1082 empty_big_negative_stop=(
|
amine@231
|
1083 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@231
|
1084 slice(0, -2000),
|
amine@231
|
1085 0,
|
amine@231
|
1086 b"",
|
amine@231
|
1087 ),
|
amine@231
|
1088 arbitrary_sampling_rate=(
|
amine@231
|
1089 AudioRegion(b"a" * 124 + b"b" * 376, 0, 1235, 1, 1),
|
amine@231
|
1090 slice(100, 200),
|
amine@231
|
1091 100 / 1235,
|
amine@231
|
1092 b"a" * 24 + b"b" * 76,
|
amine@231
|
1093 ),
|
amine@231
|
1094 arbitrary_sampling_rate_middle_sw2_ch2=(
|
amine@231
|
1095 AudioRegion(b"a" * 124 + b"b" * 376, 0, 1235, 2, 2),
|
amine@231
|
1096 slice(25, 50),
|
amine@231
|
1097 25 / 1235,
|
amine@231
|
1098 b"a" * 24 + b"b" * 76,
|
amine@231
|
1099 ),
|
amine@231
|
1100 )
|
amine@231
|
1101 def test_region_sample_slicing(
|
amine@231
|
1102 self, region, slice_, expected_start, expected_data
|
amine@231
|
1103 ):
|
amine@231
|
1104 sub_region = region[slice_]
|
amine@231
|
1105 self.assertEqual(sub_region.start, expected_start)
|
amine@231
|
1106 self.assertEqual(bytes(sub_region), expected_data)
|
amine@231
|
1107
|
amine@231
|
1108 @genty_dataset(
|
amine@88
|
1109 simple=(8000, 1, 1),
|
amine@88
|
1110 stereo_sw_2=(8000, 2, 2),
|
amine@229
|
1111 arbitrary_sr_multichannel=(5413, 2, 3),
|
amine@88
|
1112 )
|
amine@88
|
1113 def test_concatenation(self, sampling_rate, sample_width, channels):
|
amine@88
|
1114
|
amine@88
|
1115 region_1, region_2 = _make_random_length_regions(
|
amine@88
|
1116 [b"a", b"b"], sampling_rate, sample_width, channels
|
amine@88
|
1117 )
|
amine@88
|
1118
|
amine@88
|
1119 expected_start = region_1.start
|
amine@88
|
1120 expected_duration = region_1.duration + region_2.duration
|
amine@88
|
1121 expected_end = expected_start + expected_duration
|
amine@88
|
1122 expected_data = bytes(region_1) + bytes(region_2)
|
amine@88
|
1123 concat_region = region_1 + region_2
|
amine@88
|
1124
|
amine@88
|
1125 self.assertEqual(concat_region.start, expected_start)
|
amine@88
|
1126 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
|
amine@88
|
1127 self.assertAlmostEqual(
|
amine@88
|
1128 concat_region.duration, expected_duration, places=6
|
amine@88
|
1129 )
|
amine@88
|
1130 self.assertEqual(bytes(concat_region), expected_data)
|
amine@88
|
1131
|
amine@88
|
1132 @genty_dataset(
|
amine@88
|
1133 simple=(8000, 1, 1),
|
amine@88
|
1134 stereo_sw_2=(8000, 2, 2),
|
amine@229
|
1135 arbitrary_sr_multichannel=(5413, 2, 3),
|
amine@88
|
1136 )
|
amine@88
|
1137 def test_concatenation_many(self, sampling_rate, sample_width, channels):
|
amine@88
|
1138
|
amine@88
|
1139 regions = _make_random_length_regions(
|
amine@88
|
1140 [b"a", b"b", b"c"], sampling_rate, sample_width, channels
|
amine@88
|
1141 )
|
amine@88
|
1142 expected_start = regions[0].start
|
amine@88
|
1143 expected_duration = sum(r.duration for r in regions)
|
amine@88
|
1144 expected_end = expected_start + expected_duration
|
amine@88
|
1145 expected_data = b"".join(bytes(r) for r in regions)
|
amine@88
|
1146 concat_region = sum(regions)
|
amine@88
|
1147
|
amine@88
|
1148 self.assertEqual(concat_region.start, expected_start)
|
amine@88
|
1149 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
|
amine@88
|
1150 self.assertAlmostEqual(
|
amine@88
|
1151 concat_region.duration, expected_duration, places=6
|
amine@88
|
1152 )
|
amine@88
|
1153 self.assertEqual(bytes(concat_region), expected_data)
|
amine@88
|
1154
|
amine@88
|
1155 def test_concatenation_different_sampling_rate_error(self):
|
amine@88
|
1156
|
amine@88
|
1157 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
|
amine@88
|
1158 region_2 = AudioRegion(b"b" * 100, 0, 3000, 1, 1)
|
amine@88
|
1159
|
amine@88
|
1160 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
1161 region_1 + region_2
|
amine@88
|
1162 self.assertEqual(
|
amine@88
|
1163 "Can only concatenate AudioRegions of the same "
|
amine@88
|
1164 "sampling rate (8000 != 3000)",
|
amine@88
|
1165 str(val_err.exception),
|
amine@88
|
1166 )
|
amine@88
|
1167
|
amine@88
|
1168 def test_concatenation_different_sample_width_error(self):
|
amine@88
|
1169
|
amine@88
|
1170 region_1 = AudioRegion(b"a" * 100, 0, 8000, 2, 1)
|
amine@88
|
1171 region_2 = AudioRegion(b"b" * 100, 0, 8000, 4, 1)
|
amine@88
|
1172
|
amine@88
|
1173 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
1174 region_1 + region_2
|
amine@88
|
1175 self.assertEqual(
|
amine@88
|
1176 "Can only concatenate AudioRegions of the same "
|
amine@88
|
1177 "sample width (2 != 4)",
|
amine@88
|
1178 str(val_err.exception),
|
amine@88
|
1179 )
|
amine@88
|
1180
|
amine@88
|
1181 def test_concatenation_different_number_of_channels_error(self):
|
amine@88
|
1182
|
amine@88
|
1183 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
|
amine@88
|
1184 region_2 = AudioRegion(b"b" * 100, 0, 8000, 1, 2)
|
amine@88
|
1185
|
amine@88
|
1186 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
1187 region_1 + region_2
|
amine@88
|
1188 self.assertEqual(
|
amine@88
|
1189 "Can only concatenate AudioRegions of the same "
|
amine@88
|
1190 "number of channels (1 != 2)",
|
amine@88
|
1191 str(val_err.exception),
|
amine@88
|
1192 )
|
amine@196
|
1193
|
amine@196
|
1194 @genty_dataset(
|
amine@196
|
1195 simple=(0.01, 0.03, 30),
|
amine@196
|
1196 rounded_len_floor=(0.00575, 0.01725, 17),
|
amine@196
|
1197 rounded_len_ceil=(0.00625, 0.01875, 19),
|
amine@196
|
1198 )
|
amine@196
|
1199 def test_multiplication(
|
amine@196
|
1200 self, duration, expected_duration, expected_length
|
amine@196
|
1201 ):
|
amine@196
|
1202 sw = 2
|
amine@196
|
1203 data = b"0" * int(duration * 8000 * sw)
|
amine@196
|
1204 region = AudioRegion(data, 0, 8000, sw, 1)
|
amine@196
|
1205 m_region = 1 * region * 3
|
amine@196
|
1206 self.assertEqual(bytes(m_region), data * 3)
|
amine@196
|
1207 self.assertEqual(m_region.sr, 8000)
|
amine@196
|
1208 self.assertEqual(m_region.sw, 2)
|
amine@196
|
1209 self.assertEqual(m_region.ch, 1)
|
amine@196
|
1210 self.assertEqual(m_region.duration, expected_duration)
|
amine@196
|
1211 self.assertEqual(len(m_region), expected_length)
|
amine@197
|
1212
|
amine@198
|
1213 @genty_dataset(_str=("x", "str"), _float=(1.4, "float"))
|
amine@197
|
1214 def test_multiplication_non_int(self, factor, _type):
|
amine@197
|
1215 with self.assertRaises(TypeError) as type_err:
|
amine@198
|
1216 AudioRegion(b"0" * 80, 0, 8000, 1, 1) * factor
|
amine@197
|
1217 err_msg = "Can't multiply AudioRegion by a non-int of type '{}'"
|
amine@197
|
1218 self.assertEqual(err_msg.format(_type), str(type_err.exception))
|