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