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