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