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