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