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