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@220
|
305 [(2, 32), (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@220
|
310 0.3,
|
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@207
|
514
|
amine@207
|
515 @genty
|
amine@207
|
516 class TestAudioRegion(TestCase):
|
amine@86
|
517 @genty_dataset(
|
amine@86
|
518 simple=(b"\0" * 8000, 0, 8000, 1, 1, 1, 1, 1000),
|
amine@86
|
519 one_ms_less_than_1_sec=(
|
amine@86
|
520 b"\0" * 7992,
|
amine@86
|
521 0,
|
amine@86
|
522 8000,
|
amine@86
|
523 1,
|
amine@86
|
524 1,
|
amine@86
|
525 0.999,
|
amine@86
|
526 0.999,
|
amine@86
|
527 999,
|
amine@86
|
528 ),
|
amine@86
|
529 tree_quarter_ms_less_than_1_sec=(
|
amine@86
|
530 b"\0" * 7994,
|
amine@86
|
531 0,
|
amine@86
|
532 8000,
|
amine@86
|
533 1,
|
amine@86
|
534 1,
|
amine@86
|
535 0.99925,
|
amine@86
|
536 0.99925,
|
amine@86
|
537 999,
|
amine@86
|
538 ),
|
amine@86
|
539 half_ms_less_than_1_sec=(
|
amine@86
|
540 b"\0" * 7996,
|
amine@86
|
541 0,
|
amine@86
|
542 8000,
|
amine@86
|
543 1,
|
amine@86
|
544 1,
|
amine@86
|
545 0.9995,
|
amine@86
|
546 0.9995,
|
amine@86
|
547 1000,
|
amine@86
|
548 ),
|
amine@86
|
549 quarter_ms_less_than_1_sec=(
|
amine@86
|
550 b"\0" * 7998,
|
amine@86
|
551 0,
|
amine@86
|
552 8000,
|
amine@86
|
553 1,
|
amine@86
|
554 1,
|
amine@86
|
555 0.99975,
|
amine@86
|
556 0.99975,
|
amine@86
|
557 1000,
|
amine@86
|
558 ),
|
amine@86
|
559 simple_sample_width_2=(b"\0" * 8000 * 2, 0, 8000, 2, 1, 1, 1, 1000),
|
amine@86
|
560 simple_stereo=(b"\0" * 8000 * 2, 0, 8000, 1, 2, 1, 1, 1000),
|
amine@86
|
561 simple_multichannel=(b"\0" * 8000 * 5, 0, 8000, 1, 5, 1, 1, 1000),
|
amine@86
|
562 simple_sample_width_2_multichannel=(
|
amine@86
|
563 b"\0" * 8000 * 2 * 5,
|
amine@86
|
564 0,
|
amine@86
|
565 8000,
|
amine@86
|
566 2,
|
amine@86
|
567 5,
|
amine@86
|
568 1,
|
amine@86
|
569 1,
|
amine@86
|
570 1000,
|
amine@86
|
571 ),
|
amine@86
|
572 one_ms_less_than_1s_sw_2_multichannel=(
|
amine@86
|
573 b"\0" * 7992 * 2 * 5,
|
amine@86
|
574 0,
|
amine@86
|
575 8000,
|
amine@86
|
576 2,
|
amine@86
|
577 5,
|
amine@86
|
578 0.999,
|
amine@86
|
579 0.999,
|
amine@86
|
580 999,
|
amine@86
|
581 ),
|
amine@86
|
582 tree_qrt_ms_lt_1_s_sw_2_multichannel=(
|
amine@86
|
583 b"\0" * 7994 * 2 * 5,
|
amine@86
|
584 0,
|
amine@86
|
585 8000,
|
amine@86
|
586 2,
|
amine@86
|
587 5,
|
amine@86
|
588 0.99925,
|
amine@86
|
589 0.99925,
|
amine@86
|
590 999,
|
amine@86
|
591 ),
|
amine@86
|
592 half_ms_lt_1s_sw_2_multichannel=(
|
amine@86
|
593 b"\0" * 7996 * 2 * 5,
|
amine@86
|
594 0,
|
amine@86
|
595 8000,
|
amine@86
|
596 2,
|
amine@86
|
597 5,
|
amine@86
|
598 0.9995,
|
amine@86
|
599 0.9995,
|
amine@86
|
600 1000,
|
amine@86
|
601 ),
|
amine@86
|
602 quarter_ms_lt_1s_sw_2_multichannel=(
|
amine@86
|
603 b"\0" * 7998 * 2 * 5,
|
amine@86
|
604 0,
|
amine@86
|
605 8000,
|
amine@86
|
606 2,
|
amine@86
|
607 5,
|
amine@86
|
608 0.99975,
|
amine@86
|
609 0.99975,
|
amine@86
|
610 1000,
|
amine@86
|
611 ),
|
amine@86
|
612 arbitrary_length_1=(
|
amine@86
|
613 b"\0" * int(8000 * 1.33),
|
amine@86
|
614 2.7,
|
amine@86
|
615 8000,
|
amine@86
|
616 1,
|
amine@86
|
617 1,
|
amine@86
|
618 4.03,
|
amine@86
|
619 1.33,
|
amine@86
|
620 1330,
|
amine@86
|
621 ),
|
amine@86
|
622 arbitrary_length_2=(
|
amine@86
|
623 b"\0" * int(8000 * 0.476),
|
amine@86
|
624 11.568,
|
amine@86
|
625 8000,
|
amine@86
|
626 1,
|
amine@86
|
627 1,
|
amine@86
|
628 12.044,
|
amine@86
|
629 0.476,
|
amine@86
|
630 476,
|
amine@86
|
631 ),
|
amine@86
|
632 arbitrary_length_sw_2_multichannel=(
|
amine@86
|
633 b"\0" * int(8000 * 1.711) * 2 * 3,
|
amine@86
|
634 9.415,
|
amine@86
|
635 8000,
|
amine@86
|
636 2,
|
amine@86
|
637 3,
|
amine@86
|
638 11.126,
|
amine@86
|
639 1.711,
|
amine@86
|
640 1711,
|
amine@86
|
641 ),
|
amine@86
|
642 arbitrary_samplig_rate=(
|
amine@86
|
643 b"\0" * int(3172 * 1.318),
|
amine@86
|
644 17.236,
|
amine@86
|
645 3172,
|
amine@86
|
646 1,
|
amine@86
|
647 1,
|
amine@86
|
648 17.236 + int(3172 * 1.318) / 3172,
|
amine@86
|
649 int(3172 * 1.318) / 3172,
|
amine@86
|
650 1318,
|
amine@86
|
651 ),
|
amine@86
|
652 arbitrary_sr_sw_2_multichannel=(
|
amine@86
|
653 b"\0" * int(11317 * 0.716) * 2 * 3,
|
amine@86
|
654 18.811,
|
amine@86
|
655 11317,
|
amine@86
|
656 2,
|
amine@86
|
657 3,
|
amine@86
|
658 18.811 + int(11317 * 0.716) / 11317,
|
amine@86
|
659 int(11317 * 0.716) / 11317,
|
amine@86
|
660 716,
|
amine@86
|
661 ),
|
amine@86
|
662 )
|
amine@86
|
663 def test_creation(
|
amine@86
|
664 self,
|
amine@86
|
665 data,
|
amine@86
|
666 start,
|
amine@86
|
667 sampling_rate,
|
amine@86
|
668 sample_width,
|
amine@86
|
669 channels,
|
amine@86
|
670 expected_end,
|
amine@86
|
671 expected_duration_s,
|
amine@86
|
672 expected_duration_ms,
|
amine@86
|
673 ):
|
amine@86
|
674 region = AudioRegion(
|
amine@86
|
675 data, start, sampling_rate, sample_width, channels
|
amine@86
|
676 )
|
amine@86
|
677 self.assertEqual(region.sampling_rate, sampling_rate)
|
amine@86
|
678 self.assertEqual(region.sr, sampling_rate)
|
amine@86
|
679 self.assertEqual(region.sample_width, sample_width)
|
amine@86
|
680 self.assertEqual(region.sw, sample_width)
|
amine@86
|
681 self.assertEqual(region.channels, channels)
|
amine@86
|
682 self.assertEqual(region.ch, channels)
|
amine@86
|
683 self.assertEqual(region.start, start)
|
amine@86
|
684 self.assertEqual(region.end, expected_end)
|
amine@86
|
685 self.assertEqual(region.duration, expected_duration_s)
|
amine@86
|
686 self.assertEqual(len(region), expected_duration_ms)
|
amine@86
|
687 self.assertEqual(bytes(region), data)
|
amine@88
|
688
|
amine@97
|
689 def test_creation_invalid_data_exception(self):
|
amine@97
|
690 with self.assertRaises(AudioParameterError) as audio_param_err:
|
amine@97
|
691 _ = AudioRegion(
|
amine@97
|
692 data=b"ABCDEFGHI",
|
amine@97
|
693 start=0,
|
amine@97
|
694 sampling_rate=8,
|
amine@97
|
695 sample_width=2,
|
amine@97
|
696 channels=1,
|
amine@97
|
697 )
|
amine@97
|
698 self.assertEqual(
|
amine@97
|
699 "The length of audio data must be an integer "
|
amine@97
|
700 "multiple of `sample_width * channels`",
|
amine@97
|
701 str(audio_param_err.exception),
|
amine@97
|
702 )
|
amine@97
|
703
|
amine@88
|
704 @genty_dataset(
|
amine@192
|
705 simple=("output.wav", 1.230, "output.wav"),
|
amine@192
|
706 start=("output_{start}.wav", 1.230, "output_1.23.wav"),
|
amine@192
|
707 start_2=("output_{start}.wav", 1.233712, "output_1.233712.wav"),
|
amine@192
|
708 start_3=("output_{start}.wav", 1.2300001, "output_1.23.wav"),
|
amine@192
|
709 start_4=("output_{start:.3f}.wav", 1.233712, "output_1.234.wav"),
|
amine@192
|
710 start_5=(
|
amine@192
|
711 "output_{start:.8f}.wav",
|
amine@192
|
712 1.233712345,
|
amine@192
|
713 "output_1.23371200.wav",
|
amine@192
|
714 ),
|
amine@192
|
715 start_end_duration=(
|
amine@192
|
716 "output_{start}_{end}_{duration}.wav",
|
amine@192
|
717 1.455,
|
amine@192
|
718 "output_1.455_2.455_1.0.wav",
|
amine@192
|
719 ),
|
amine@192
|
720 start_end_duration_2=(
|
amine@192
|
721 "output_{start}_{end}_{duration}.wav",
|
amine@192
|
722 1.455321,
|
amine@192
|
723 "output_1.455321_2.455321_1.0.wav",
|
amine@192
|
724 ),
|
amine@192
|
725 )
|
amine@192
|
726 def test_save(self, format, start, expected):
|
amine@192
|
727 with TemporaryDirectory() as tmpdir:
|
amine@192
|
728 region = AudioRegion(b"0" * 160, start, 160, 1, 1)
|
amine@192
|
729 format = os.path.join(tmpdir, format)
|
amine@192
|
730 filename = region.save(format)[len(tmpdir) + 1 :]
|
amine@192
|
731 self.assertEqual(filename, expected)
|
amine@192
|
732
|
amine@193
|
733 def test_save_file_exists_exception(self):
|
amine@193
|
734 with TemporaryDirectory() as tmpdir:
|
amine@193
|
735 filename = os.path.join(tmpdir, "output.wav")
|
amine@193
|
736 open(filename, "w").close()
|
amine@193
|
737 region = AudioRegion(b"0" * 160, 0, 160, 1, 1)
|
amine@193
|
738 with self.assertRaises(FileExistsError):
|
amine@193
|
739 region.save(filename, exists_ok=False)
|
amine@193
|
740
|
amine@192
|
741 @genty_dataset(
|
amine@194
|
742 first_half=(
|
amine@194
|
743 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
744 slice(0, 500),
|
amine@194
|
745 0,
|
amine@194
|
746 b"a" * 80,
|
amine@194
|
747 ),
|
amine@194
|
748 second_half=(
|
amine@194
|
749 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
750 slice(500, None),
|
amine@194
|
751 0.5,
|
amine@194
|
752 b"b" * 80,
|
amine@194
|
753 ),
|
amine@194
|
754 second_half_negative=(
|
amine@194
|
755 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
756 slice(-500, None),
|
amine@194
|
757 0.5,
|
amine@194
|
758 b"b" * 80,
|
amine@194
|
759 ),
|
amine@194
|
760 middle=(
|
amine@194
|
761 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
762 slice(200, 750),
|
amine@194
|
763 0.2,
|
amine@194
|
764 b"a" * 48 + b"b" * 40,
|
amine@194
|
765 ),
|
amine@194
|
766 middle_negative=(
|
amine@194
|
767 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
768 slice(-800, -250),
|
amine@194
|
769 0.2,
|
amine@194
|
770 b"a" * 48 + b"b" * 40,
|
amine@194
|
771 ),
|
amine@194
|
772 middle_sw2=(
|
amine@194
|
773 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 2, 1),
|
amine@194
|
774 slice(200, 750),
|
amine@194
|
775 0.2,
|
amine@194
|
776 b"a" * 96 + b"b" * 80,
|
amine@194
|
777 ),
|
amine@194
|
778 middle_ch2=(
|
amine@194
|
779 AudioRegion(b"a" * 160 + b"b" * 160, 0, 160, 1, 2),
|
amine@194
|
780 slice(200, 750),
|
amine@194
|
781 0.2,
|
amine@194
|
782 b"a" * 96 + b"b" * 80,
|
amine@194
|
783 ),
|
amine@194
|
784 middle_sw2_ch2=(
|
amine@194
|
785 AudioRegion(b"a" * 320 + b"b" * 320, 0, 160, 2, 2),
|
amine@194
|
786 slice(200, 750),
|
amine@194
|
787 0.2,
|
amine@194
|
788 b"a" * 192 + b"b" * 160,
|
amine@194
|
789 ),
|
amine@194
|
790 but_first_sample=(
|
amine@194
|
791 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
792 slice(1, None),
|
amine@194
|
793 0.001,
|
amine@194
|
794 b"a" * (4000 - 8) + b"b" * 4000,
|
amine@194
|
795 ),
|
amine@194
|
796 but_first_sample_negative=(
|
amine@194
|
797 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
798 slice(-999, None),
|
amine@194
|
799 0.001,
|
amine@194
|
800 b"a" * (4000 - 8) + b"b" * 4000,
|
amine@194
|
801 ),
|
amine@194
|
802 but_last_sample=(
|
amine@194
|
803 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
804 slice(0, 999),
|
amine@194
|
805 0,
|
amine@194
|
806 b"a" * 4000 + b"b" * (4000 - 8),
|
amine@194
|
807 ),
|
amine@194
|
808 but_last_sample_negative=(
|
amine@194
|
809 AudioRegion(b"a" * 4000 + b"b" * 4000, 0, 8000, 1, 1),
|
amine@194
|
810 slice(0, -1),
|
amine@194
|
811 0,
|
amine@194
|
812 b"a" * 4000 + b"b" * (4000 - 8),
|
amine@194
|
813 ),
|
amine@194
|
814 big_negative_start=(
|
amine@194
|
815 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@194
|
816 slice(-5000, None),
|
amine@194
|
817 0,
|
amine@194
|
818 b"a" * 160,
|
amine@194
|
819 ),
|
amine@194
|
820 big_negative_stop=(
|
amine@194
|
821 AudioRegion(b"a" * 160, 0, 160, 1, 1),
|
amine@194
|
822 slice(None, -1500),
|
amine@194
|
823 0,
|
amine@194
|
824 b"",
|
amine@194
|
825 ),
|
amine@194
|
826 empty=(
|
amine@194
|
827 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
828 slice(0, 0),
|
amine@194
|
829 0,
|
amine@194
|
830 b"",
|
amine@194
|
831 ),
|
amine@194
|
832 empty_start_stop_reversed=(
|
amine@194
|
833 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
834 slice(200, 100),
|
amine@194
|
835 0.2,
|
amine@194
|
836 b"",
|
amine@194
|
837 ),
|
amine@194
|
838 empty_big_positive_start=(
|
amine@194
|
839 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
840 slice(2000, 3000),
|
amine@194
|
841 2,
|
amine@194
|
842 b"",
|
amine@194
|
843 ),
|
amine@194
|
844 empty_negative_reversed=(
|
amine@194
|
845 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
846 slice(-100, -200),
|
amine@194
|
847 0.9,
|
amine@194
|
848 b"",
|
amine@194
|
849 ),
|
amine@194
|
850 empty_big_negative_stop=(
|
amine@194
|
851 AudioRegion(b"a" * 80 + b"b" * 80, 0, 160, 1, 1),
|
amine@194
|
852 slice(0, -2000),
|
amine@194
|
853 0,
|
amine@194
|
854 b"",
|
amine@194
|
855 ),
|
amine@194
|
856 )
|
amine@194
|
857 def test_region_slicing(
|
amine@194
|
858 self, region, slice_, expected_start, expected_data
|
amine@194
|
859 ):
|
amine@194
|
860 sub_region = region[slice_]
|
amine@194
|
861 self.assertEqual(sub_region.start, expected_start)
|
amine@194
|
862 self.assertEqual(bytes(sub_region), expected_data)
|
amine@194
|
863
|
amine@194
|
864 @genty_dataset(
|
amine@88
|
865 simple=(8000, 1, 1),
|
amine@88
|
866 stereo_sw_2=(8000, 2, 2),
|
amine@88
|
867 arbitray_sr_multichannel=(5413, 2, 3),
|
amine@88
|
868 )
|
amine@88
|
869 def test_concatenation(self, sampling_rate, sample_width, channels):
|
amine@88
|
870
|
amine@88
|
871 region_1, region_2 = _make_random_length_regions(
|
amine@88
|
872 [b"a", b"b"], sampling_rate, sample_width, channels
|
amine@88
|
873 )
|
amine@88
|
874
|
amine@88
|
875 expected_start = region_1.start
|
amine@88
|
876 expected_duration = region_1.duration + region_2.duration
|
amine@88
|
877 expected_end = expected_start + expected_duration
|
amine@88
|
878 expected_data = bytes(region_1) + bytes(region_2)
|
amine@88
|
879 concat_region = region_1 + region_2
|
amine@88
|
880
|
amine@88
|
881 self.assertEqual(concat_region.start, expected_start)
|
amine@88
|
882 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
|
amine@88
|
883 self.assertAlmostEqual(
|
amine@88
|
884 concat_region.duration, expected_duration, places=6
|
amine@88
|
885 )
|
amine@88
|
886 self.assertEqual(bytes(concat_region), expected_data)
|
amine@88
|
887
|
amine@88
|
888 @genty_dataset(
|
amine@88
|
889 simple=(8000, 1, 1),
|
amine@88
|
890 stereo_sw_2=(8000, 2, 2),
|
amine@88
|
891 arbitray_sr_multichannel=(5413, 2, 3),
|
amine@88
|
892 )
|
amine@88
|
893 def test_concatenation_many(self, sampling_rate, sample_width, channels):
|
amine@88
|
894
|
amine@88
|
895 regions = _make_random_length_regions(
|
amine@88
|
896 [b"a", b"b", b"c"], sampling_rate, sample_width, channels
|
amine@88
|
897 )
|
amine@88
|
898 expected_start = regions[0].start
|
amine@88
|
899 expected_duration = sum(r.duration for r in regions)
|
amine@88
|
900 expected_end = expected_start + expected_duration
|
amine@88
|
901 expected_data = b"".join(bytes(r) for r in regions)
|
amine@88
|
902 concat_region = sum(regions)
|
amine@88
|
903
|
amine@88
|
904 self.assertEqual(concat_region.start, expected_start)
|
amine@88
|
905 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
|
amine@88
|
906 self.assertAlmostEqual(
|
amine@88
|
907 concat_region.duration, expected_duration, places=6
|
amine@88
|
908 )
|
amine@88
|
909 self.assertEqual(bytes(concat_region), expected_data)
|
amine@88
|
910
|
amine@88
|
911 def test_concatenation_different_sampling_rate_error(self):
|
amine@88
|
912
|
amine@88
|
913 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
|
amine@88
|
914 region_2 = AudioRegion(b"b" * 100, 0, 3000, 1, 1)
|
amine@88
|
915
|
amine@88
|
916 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
917 region_1 + region_2
|
amine@88
|
918 self.assertEqual(
|
amine@88
|
919 "Can only concatenate AudioRegions of the same "
|
amine@88
|
920 "sampling rate (8000 != 3000)",
|
amine@88
|
921 str(val_err.exception),
|
amine@88
|
922 )
|
amine@88
|
923
|
amine@88
|
924 def test_concatenation_different_sample_width_error(self):
|
amine@88
|
925
|
amine@88
|
926 region_1 = AudioRegion(b"a" * 100, 0, 8000, 2, 1)
|
amine@88
|
927 region_2 = AudioRegion(b"b" * 100, 0, 8000, 4, 1)
|
amine@88
|
928
|
amine@88
|
929 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
930 region_1 + region_2
|
amine@88
|
931 self.assertEqual(
|
amine@88
|
932 "Can only concatenate AudioRegions of the same "
|
amine@88
|
933 "sample width (2 != 4)",
|
amine@88
|
934 str(val_err.exception),
|
amine@88
|
935 )
|
amine@88
|
936
|
amine@88
|
937 def test_concatenation_different_number_of_channels_error(self):
|
amine@88
|
938
|
amine@88
|
939 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
|
amine@88
|
940 region_2 = AudioRegion(b"b" * 100, 0, 8000, 1, 2)
|
amine@88
|
941
|
amine@88
|
942 with self.assertRaises(ValueError) as val_err:
|
amine@88
|
943 region_1 + region_2
|
amine@88
|
944 self.assertEqual(
|
amine@88
|
945 "Can only concatenate AudioRegions of the same "
|
amine@88
|
946 "number of channels (1 != 2)",
|
amine@88
|
947 str(val_err.exception),
|
amine@88
|
948 )
|
amine@196
|
949
|
amine@196
|
950 @genty_dataset(
|
amine@196
|
951 simple=(0.01, 0.03, 30),
|
amine@196
|
952 rounded_len_floor=(0.00575, 0.01725, 17),
|
amine@196
|
953 rounded_len_ceil=(0.00625, 0.01875, 19),
|
amine@196
|
954 )
|
amine@196
|
955 def test_multiplication(
|
amine@196
|
956 self, duration, expected_duration, expected_length
|
amine@196
|
957 ):
|
amine@196
|
958 sw = 2
|
amine@196
|
959 data = b"0" * int(duration * 8000 * sw)
|
amine@196
|
960 region = AudioRegion(data, 0, 8000, sw, 1)
|
amine@196
|
961 m_region = 1 * region * 3
|
amine@196
|
962 self.assertEqual(bytes(m_region), data * 3)
|
amine@196
|
963 self.assertEqual(m_region.sr, 8000)
|
amine@196
|
964 self.assertEqual(m_region.sw, 2)
|
amine@196
|
965 self.assertEqual(m_region.ch, 1)
|
amine@196
|
966 self.assertEqual(m_region.duration, expected_duration)
|
amine@196
|
967 self.assertEqual(len(m_region), expected_length)
|
amine@197
|
968
|
amine@198
|
969 @genty_dataset(_str=("x", "str"), _float=(1.4, "float"))
|
amine@197
|
970 def test_multiplication_non_int(self, factor, _type):
|
amine@197
|
971 with self.assertRaises(TypeError) as type_err:
|
amine@198
|
972 AudioRegion(b"0" * 80, 0, 8000, 1, 1) * factor
|
amine@197
|
973 err_msg = "Can't multiply AudioRegion by a non-int of type '{}'"
|
amine@197
|
974 self.assertEqual(err_msg.format(_type), str(type_err.exception))
|