amine@106
|
1 import os
|
amine@106
|
2 import sys
|
amine@106
|
3 import math
|
amine@107
|
4 from array import array
|
amine@133
|
5 from tempfile import NamedTemporaryFile, TemporaryDirectory
|
amine@110
|
6 import filecmp
|
amine@108
|
7 from unittest import TestCase
|
amine@108
|
8 from genty import genty, genty_dataset
|
amine@110
|
9 from auditok.io import (
|
amine@126
|
10 DATA_FORMAT,
|
amine@121
|
11 AudioIOError,
|
amine@110
|
12 AudioParameterError,
|
amine@126
|
13 BufferAudioSource,
|
amine@110
|
14 check_audio_data,
|
amine@143
|
15 _guess_audio_format,
|
amine@144
|
16 _normalize_use_channel,
|
amine@128
|
17 _get_audio_parameters,
|
amine@116
|
18 _array_to_bytes,
|
amine@118
|
19 _mix_audio_channels,
|
amine@119
|
20 _extract_selected_channel,
|
amine@126
|
21 _load_raw,
|
amine@129
|
22 _load_wave,
|
amine@131
|
23 _load_with_pydub,
|
amine@120
|
24 from_file,
|
amine@111
|
25 _save_raw,
|
amine@110
|
26 _save_wave,
|
amine@141
|
27 _save_with_pydub,
|
amine@135
|
28 to_file,
|
amine@110
|
29 )
|
amine@106
|
30
|
amine@106
|
31
|
amine@106
|
32 if sys.version_info >= (3, 0):
|
amine@106
|
33 PYTHON_3 = True
|
amine@124
|
34 from unittest.mock import patch, Mock
|
amine@106
|
35 else:
|
amine@106
|
36 PYTHON_3 = False
|
amine@124
|
37 from mock import patch, Mock
|
amine@120
|
38
|
amine@120
|
39 AUDIO_PARAMS_SHORT = {"sr": 16000, "sw": 2, "ch": 1}
|
amine@106
|
40
|
amine@106
|
41
|
amine@106
|
42 def _sample_generator(*data_buffers):
|
amine@106
|
43 """
|
amine@106
|
44 Takes a list of many mono audio data buffers and makes a sample generator
|
amine@106
|
45 of interleaved audio samples, one sample from each channel. The resulting
|
amine@106
|
46 generator can be used to build a multichannel audio buffer.
|
amine@106
|
47 >>> gen = _sample_generator("abcd", "ABCD")
|
amine@106
|
48 >>> list(gen)
|
amine@106
|
49 ["a", "A", "b", "B", "c", "C", "d", "D"]
|
amine@106
|
50 """
|
amine@106
|
51 frame_gen = zip(*data_buffers)
|
amine@106
|
52 return (sample for frame in frame_gen for sample in frame)
|
amine@106
|
53
|
amine@106
|
54
|
amine@107
|
55 def _generate_pure_tone(
|
amine@107
|
56 frequency, duration_sec=1, sampling_rate=16000, sample_width=2, volume=1e4
|
amine@107
|
57 ):
|
amine@107
|
58 """
|
amine@107
|
59 Generates a pure tone with the given frequency.
|
amine@107
|
60 """
|
amine@107
|
61 assert frequency <= sampling_rate / 2
|
amine@107
|
62 max_value = (2 ** (sample_width * 8) // 2) - 1
|
amine@107
|
63 if volume > max_value:
|
amine@107
|
64 volume = max_value
|
amine@107
|
65 fmt = DATA_FORMAT[sample_width]
|
amine@107
|
66 total_samples = int(sampling_rate * duration_sec)
|
amine@107
|
67 step = frequency / sampling_rate
|
amine@107
|
68 two_pi_step = 2 * math.pi * step
|
amine@107
|
69 data = array(
|
amine@107
|
70 fmt,
|
amine@107
|
71 (
|
amine@107
|
72 int(math.sin(two_pi_step * i) * volume)
|
amine@107
|
73 for i in range(total_samples)
|
amine@107
|
74 ),
|
amine@107
|
75 )
|
amine@107
|
76 return data
|
amine@107
|
77
|
amine@107
|
78
|
amine@107
|
79 PURE_TONE_DICT = {
|
amine@107
|
80 freq: _generate_pure_tone(freq, 1, 16000, 2) for freq in (400, 800, 1600)
|
amine@107
|
81 }
|
amine@107
|
82 PURE_TONE_DICT.update(
|
amine@107
|
83 {
|
amine@107
|
84 freq: _generate_pure_tone(freq, 0.1, 16000, 2)
|
amine@107
|
85 for freq in (600, 1150, 2400, 7220)
|
amine@107
|
86 }
|
amine@107
|
87 )
|
amine@108
|
88
|
amine@108
|
89
|
amine@108
|
90 @genty
|
amine@108
|
91 class TestIO(TestCase):
|
amine@108
|
92 @genty_dataset(
|
amine@108
|
93 valid_mono=(b"\0" * 113, 1, 1),
|
amine@108
|
94 valid_stereo=(b"\0" * 160, 1, 2),
|
amine@108
|
95 invalid_mono_sw_2=(b"\0" * 113, 2, 1, False),
|
amine@108
|
96 invalid_stereo_sw_1=(b"\0" * 113, 1, 2, False),
|
amine@108
|
97 invalid_stereo_sw_2=(b"\0" * 158, 2, 2, False),
|
amine@108
|
98 )
|
amine@108
|
99 def test_check_audio_data(self, data, sample_width, channels, valid=True):
|
amine@108
|
100
|
amine@108
|
101 if not valid:
|
amine@108
|
102 with self.assertRaises(AudioParameterError):
|
amine@108
|
103 check_audio_data(data, sample_width, channels)
|
amine@108
|
104 else:
|
amine@108
|
105 self.assertIsNone(check_audio_data(data, sample_width, channels))
|
amine@110
|
106
|
amine@110
|
107 @genty_dataset(
|
amine@143
|
108 extention_and_format_same=("wav", "filename.wav", "wav"),
|
amine@143
|
109 extention_and_format_different=("wav", "filename.mp3", "wav"),
|
amine@143
|
110 extention_no_format=(None, "filename.wav", "wav"),
|
amine@143
|
111 format_no_extension=("wav", "filename", "wav"),
|
amine@143
|
112 no_format_no_extension=(None, "filename", None),
|
amine@143
|
113 )
|
amine@143
|
114 def test_guess_audio_format(self, fmt, filename, expected):
|
amine@143
|
115 result = _guess_audio_format(fmt, filename)
|
amine@143
|
116 self.assertEqual(result, expected)
|
amine@143
|
117
|
amine@143
|
118 @genty_dataset(
|
amine@144
|
119 none=(None, 0),
|
amine@144
|
120 positive_int=(1, 1),
|
amine@144
|
121 negative_int=(-1, -1),
|
amine@144
|
122 left=("left", 0),
|
amine@144
|
123 right=("right", 1),
|
amine@144
|
124 mix=("mix", "mix"),
|
amine@144
|
125 )
|
amine@144
|
126 def test_normalize_use_channel(self, use_channel, expected):
|
amine@144
|
127 result = _normalize_use_channel(use_channel)
|
amine@144
|
128 self.assertEqual(result, expected)
|
amine@144
|
129
|
amine@144
|
130 @genty_dataset(
|
amine@145
|
131 simple=((8000, 2, 1, 0), (8000, 2, 1, 0)),
|
amine@145
|
132 use_channel_left=((8000, 2, 1, "left"), (8000, 2, 1, 0)),
|
amine@145
|
133 use_channel_right=((8000, 2, 1, "right"), (8000, 2, 1, 1)),
|
amine@145
|
134 use_channel_mix=((8000, 2, 1, "mix"), (8000, 2, 1, "mix")),
|
amine@145
|
135 use_channel_None=((8000, 2, 2, None), (8000, 2, 2, 0)),
|
amine@145
|
136 no_use_channel=((8000, 2, 2), (8000, 2, 2, 0)),
|
amine@145
|
137 )
|
amine@145
|
138 def test_get_audio_parameters_short_params(self, values, expected):
|
amine@145
|
139 params = {k: v for k, v in zip(("sr", "sw", "ch", "uc"), values)}
|
amine@145
|
140 result = _get_audio_parameters(params)
|
amine@145
|
141 self.assertEqual(result, expected)
|
amine@145
|
142
|
amine@145
|
143 @genty_dataset(
|
amine@145
|
144 simple=((8000, 2, 1, 0), (8000, 2, 1, 0)),
|
amine@145
|
145 use_channel_left=((8000, 2, 1, "left"), (8000, 2, 1, 0)),
|
amine@145
|
146 use_channel_right=((8000, 2, 1, "right"), (8000, 2, 1, 1)),
|
amine@145
|
147 use_channel_mix=((8000, 2, 1, "mix"), (8000, 2, 1, "mix")),
|
amine@145
|
148 use_channel_None=((8000, 2, 2, None), (8000, 2, 2, 0)),
|
amine@145
|
149 no_use_channel=((8000, 2, 2), (8000, 2, 2, 0)),
|
amine@145
|
150 )
|
amine@145
|
151 def test_get_audio_parameters_long_params(self, values, expected):
|
amine@145
|
152 params = {
|
amine@145
|
153 k: v
|
amine@145
|
154 for k, v in zip(
|
amine@145
|
155 ("sampling_rate", "sample_width", "channels", "use_channel"),
|
amine@145
|
156 values,
|
amine@145
|
157 )
|
amine@145
|
158 }
|
amine@145
|
159 result = _get_audio_parameters(params)
|
amine@145
|
160 self.assertEqual(result, expected)
|
amine@145
|
161
|
amine@145
|
162 @genty_dataset(simple=((8000, 2, 1, 0), (8000, 2, 1, 0)))
|
amine@145
|
163 def test_get_audio_parameters_short_and_long_params(
|
amine@145
|
164 self, values, expected
|
amine@145
|
165 ):
|
amine@145
|
166 params = {
|
amine@145
|
167 k: v
|
amine@145
|
168 for k, v in zip(
|
amine@145
|
169 ("sampling_rate", "sample_width", "channels", "use_channel"),
|
amine@145
|
170 values,
|
amine@145
|
171 )
|
amine@145
|
172 }
|
amine@145
|
173
|
amine@145
|
174 params.update({k: v for k, v in zip(("sr", "sw", "ch", "uc"), "xxxx")})
|
amine@145
|
175 result = _get_audio_parameters(params)
|
amine@145
|
176 self.assertEqual(result, expected)
|
amine@145
|
177
|
amine@145
|
178 @genty_dataset(
|
amine@118
|
179 mono_1byte=([400], 1),
|
amine@118
|
180 stereo_1byte=([400, 600], 1),
|
amine@118
|
181 three_channel_1byte=([400, 600, 2400], 1),
|
amine@118
|
182 mono_2byte=([400], 2),
|
amine@118
|
183 stereo_2byte=([400, 600], 2),
|
amine@118
|
184 three_channel_2byte=([400, 600, 1150], 2),
|
amine@118
|
185 mono_4byte=([400], 4),
|
amine@118
|
186 stereo_4byte=([400, 600], 4),
|
amine@118
|
187 four_channel_2byte=([400, 600, 1150, 7220], 4),
|
amine@118
|
188 )
|
amine@118
|
189 def test_mix_audio_channels(self, frequencies, sample_width):
|
amine@118
|
190 sampling_rate = 16000
|
amine@118
|
191 sample_width = 2
|
amine@118
|
192 channels = len(frequencies)
|
amine@118
|
193 mono_channels = [
|
amine@118
|
194 _generate_pure_tone(
|
amine@118
|
195 freq,
|
amine@118
|
196 duration_sec=0.1,
|
amine@118
|
197 sampling_rate=sampling_rate,
|
amine@118
|
198 sample_width=sample_width,
|
amine@118
|
199 )
|
amine@118
|
200 for freq in frequencies
|
amine@118
|
201 ]
|
amine@118
|
202 fmt = DATA_FORMAT[sample_width]
|
amine@118
|
203 expected = _array_to_bytes(
|
amine@118
|
204 array(
|
amine@118
|
205 fmt,
|
amine@118
|
206 (sum(samples) // channels for samples in zip(*mono_channels)),
|
amine@118
|
207 )
|
amine@118
|
208 )
|
amine@118
|
209 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
|
amine@118
|
210 mixed = _mix_audio_channels(data, channels, sample_width)
|
amine@118
|
211 self.assertEqual(mixed, expected)
|
amine@118
|
212
|
amine@118
|
213 @genty_dataset(
|
amine@119
|
214 mono_1byte=([400], 1, 0),
|
amine@119
|
215 stereo_1byte_2st_channel=([400, 600], 1, 1),
|
amine@119
|
216 mono_2byte=([400], 2, 0),
|
amine@119
|
217 stereo_2byte_1st_channel=([400, 600], 2, 0),
|
amine@119
|
218 stereo_2byte_2nd_channel=([400, 600], 2, 1),
|
amine@119
|
219 three_channel_2byte_last_negative_idx=([400, 600, 1150], 2, -1),
|
amine@119
|
220 three_channel_2byte_2nd_negative_idx=([400, 600, 1150], 2, -2),
|
amine@119
|
221 three_channel_2byte_1st_negative_idx=([400, 600, 1150], 2, -3),
|
amine@119
|
222 three_channel_4byte_1st=([400, 600, 1150], 4, 0),
|
amine@119
|
223 three_channel_4byte_last_negative_idx=([400, 600, 1150], 4, -1),
|
amine@119
|
224 )
|
amine@119
|
225 def test_extract_selected_channel(
|
amine@119
|
226 self, frequencies, sample_width, use_channel
|
amine@119
|
227 ):
|
amine@119
|
228
|
amine@119
|
229 mono_channels = [
|
amine@119
|
230 _generate_pure_tone(
|
amine@119
|
231 freq,
|
amine@119
|
232 duration_sec=0.1,
|
amine@119
|
233 sampling_rate=16000,
|
amine@119
|
234 sample_width=sample_width,
|
amine@119
|
235 )
|
amine@119
|
236 for freq in frequencies
|
amine@119
|
237 ]
|
amine@119
|
238 channels = len(frequencies)
|
amine@119
|
239 fmt = DATA_FORMAT[sample_width]
|
amine@119
|
240 expected = _array_to_bytes(mono_channels[use_channel])
|
amine@119
|
241 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
|
amine@119
|
242 selected_channel = _extract_selected_channel(
|
amine@119
|
243 data, channels, sample_width, use_channel
|
amine@119
|
244 )
|
amine@119
|
245 self.assertEqual(selected_channel, expected)
|
amine@119
|
246
|
amine@119
|
247 @genty_dataset(
|
amine@120
|
248 raw_with_audio_format=(
|
amine@120
|
249 "audio",
|
amine@120
|
250 "raw",
|
amine@120
|
251 "_load_raw",
|
amine@120
|
252 AUDIO_PARAMS_SHORT,
|
amine@120
|
253 ),
|
amine@120
|
254 raw_with_extension=(
|
amine@120
|
255 "audio.raw",
|
amine@120
|
256 None,
|
amine@120
|
257 "_load_raw",
|
amine@120
|
258 AUDIO_PARAMS_SHORT,
|
amine@120
|
259 ),
|
amine@120
|
260 wave_with_audio_format=("audio", "wave", "_load_wave"),
|
amine@120
|
261 wav_with_audio_format=("audio", "wave", "_load_wave"),
|
amine@120
|
262 wav_with_extension=("audio.wav", None, "_load_wave"),
|
amine@120
|
263 format_and_extension_both_given=("audio.dat", "wav", "_load_wave"),
|
amine@120
|
264 format_and_extension_both_given_b=("audio.raw", "wave", "_load_wave"),
|
amine@120
|
265 no_format_nor_extension=("audio", None, "_load_with_pydub"),
|
amine@120
|
266 other_formats_ogg=("audio.ogg", None, "_load_with_pydub"),
|
amine@120
|
267 other_formats_webm=("audio", "webm", "_load_with_pydub"),
|
amine@120
|
268 )
|
amine@120
|
269 def test_from_file(
|
amine@120
|
270 self, filename, audio_format, funtion_name, kwargs=None
|
amine@120
|
271 ):
|
amine@120
|
272 funtion_name = "auditok.io." + funtion_name
|
amine@120
|
273 if kwargs is None:
|
amine@120
|
274 kwargs = {}
|
amine@120
|
275 with patch(funtion_name) as patch_function:
|
amine@120
|
276 from_file(filename, audio_format, **kwargs)
|
amine@120
|
277 self.assertTrue(patch_function.called)
|
amine@120
|
278
|
amine@137
|
279 @genty_dataset(
|
amine@137
|
280 missing_sampling_rate=("sr",),
|
amine@137
|
281 missing_sample_width=("sw",),
|
amine@137
|
282 missing_channels=("ch",),
|
amine@137
|
283 )
|
amine@137
|
284 def test_from_file_missing_audio_param(self, missing_param):
|
amine@137
|
285 with self.assertRaises(AudioParameterError):
|
amine@137
|
286 params = AUDIO_PARAMS_SHORT.copy()
|
amine@137
|
287 del params[missing_param]
|
amine@137
|
288 from_file("audio", audio_format="raw", **params)
|
amine@137
|
289
|
amine@121
|
290 def test_from_file_no_pydub(self):
|
amine@121
|
291 with patch("auditok.io._WITH_PYDUB", False):
|
amine@121
|
292 with self.assertRaises(AudioIOError):
|
amine@121
|
293 from_file("audio", "mp3")
|
amine@121
|
294
|
amine@111
|
295 @genty_dataset(
|
amine@122
|
296 raw_first_channel=("raw", 0, 400),
|
amine@122
|
297 raw_second_channel=("raw", 1, 800),
|
amine@122
|
298 raw_third_channel=("raw", 2, 1600),
|
amine@122
|
299 raw_left_channel=("raw", "left", 400),
|
amine@122
|
300 raw_right_channel=("raw", "right", 800),
|
amine@122
|
301 wav_first_channel=("wav", 0, 400),
|
amine@122
|
302 wav_second_channel=("wav", 1, 800),
|
amine@122
|
303 wav_third_channel=("wav", 2, 1600),
|
amine@122
|
304 wav_left_channel=("wav", "left", 400),
|
amine@122
|
305 wav_right_channel=("wav", "right", 800),
|
amine@122
|
306 )
|
amine@122
|
307 def test_from_file_multichannel_audio(
|
amine@122
|
308 self, audio_format, use_channel, frequency
|
amine@122
|
309 ):
|
amine@122
|
310 expected = PURE_TONE_DICT[frequency]
|
amine@122
|
311 filename = "tests/data/test_16KHZ_3channel_400-800-1600Hz.{}".format(
|
amine@122
|
312 audio_format
|
amine@122
|
313 )
|
amine@122
|
314 sample_width = 2
|
amine@122
|
315 audio_source = from_file(
|
amine@122
|
316 filename,
|
amine@122
|
317 sampling_rate=16000,
|
amine@122
|
318 sample_width=sample_width,
|
amine@122
|
319 channels=3,
|
amine@122
|
320 use_channel=use_channel,
|
amine@122
|
321 )
|
amine@122
|
322 fmt = DATA_FORMAT[sample_width]
|
amine@122
|
323 data = array(fmt, audio_source._buffer)
|
amine@122
|
324 self.assertEqual(data, expected)
|
amine@122
|
325
|
amine@122
|
326 @genty_dataset(
|
amine@123
|
327 raw_mono=("raw", "mono_400Hz", (400,)),
|
amine@123
|
328 raw_3channel=("raw", "3channel_400-800-1600Hz", (400, 800, 1600)),
|
amine@123
|
329 wav_mono=("wav", "mono_400Hz", (400,)),
|
amine@123
|
330 wav_3channel=("wav", "3channel_400-800-1600Hz", (400, 800, 1600)),
|
amine@123
|
331 )
|
amine@123
|
332 def test_from_file_multichannel_audio_mix(
|
amine@123
|
333 self, audio_format, filename_suffix, frequencies
|
amine@123
|
334 ):
|
amine@123
|
335 sampling_rate = 16000
|
amine@123
|
336 sample_width = 2
|
amine@123
|
337 channels = len(frequencies)
|
amine@123
|
338 mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
|
amine@123
|
339 channels = len(frequencies)
|
amine@123
|
340 fmt = DATA_FORMAT[sample_width]
|
amine@123
|
341 expected = _array_to_bytes(
|
amine@123
|
342 array(
|
amine@123
|
343 fmt,
|
amine@123
|
344 (sum(samples) // channels for samples in zip(*mono_channels)),
|
amine@123
|
345 )
|
amine@123
|
346 )
|
amine@123
|
347 filename = "tests/data/test_16KHZ_{}.{}".format(
|
amine@123
|
348 filename_suffix, audio_format
|
amine@123
|
349 )
|
amine@123
|
350 audio_source = from_file(
|
amine@123
|
351 filename,
|
amine@123
|
352 use_channel="mix",
|
amine@123
|
353 sampling_rate=sampling_rate,
|
amine@123
|
354 sample_width=2,
|
amine@123
|
355 channels=channels,
|
amine@123
|
356 )
|
amine@123
|
357 mixed = audio_source._buffer
|
amine@123
|
358 self.assertEqual((mixed), expected)
|
amine@123
|
359
|
amine@124
|
360 @patch("auditok.io._WITH_PYDUB", True)
|
amine@124
|
361 @patch("auditok.io.BufferAudioSource")
|
amine@124
|
362 @genty_dataset(
|
amine@124
|
363 ogg_first_channel=("ogg", 0, "from_ogg"),
|
amine@124
|
364 ogg_second_channel=("ogg", 1, "from_ogg"),
|
amine@124
|
365 ogg_mix=("ogg", "mix", "from_ogg"),
|
amine@124
|
366 ogg_default=("ogg", None, "from_ogg"),
|
amine@124
|
367 mp3_left_channel=("mp3", "left", "from_mp3"),
|
amine@124
|
368 mp3_right_channel=("mp3", "right", "from_mp3"),
|
amine@124
|
369 flac_first_channel=("flac", 0, "from_file"),
|
amine@124
|
370 flac_second_channel=("flac", 1, "from_file"),
|
amine@124
|
371 flv_left_channel=("flv", "left", "from_flv"),
|
amine@124
|
372 webm_right_channel=("webm", "right", "from_file"),
|
amine@124
|
373 )
|
amine@124
|
374 def test_from_file_multichannel_audio_compressed(
|
amine@124
|
375 self, audio_format, use_channel, function, *mocks
|
amine@124
|
376 ):
|
amine@124
|
377 filename = "audio.{}".format(audio_format)
|
amine@124
|
378 segment_mock = Mock()
|
amine@124
|
379 segment_mock.sample_width = 2
|
amine@124
|
380 segment_mock.channels = 2
|
amine@124
|
381 segment_mock._data = b"abcd"
|
amine@124
|
382 with patch("auditok.io._extract_selected_channel") as ext_mock:
|
amine@124
|
383 with patch(
|
amine@124
|
384 "auditok.io.AudioSegment.{}".format(function)
|
amine@124
|
385 ) as open_func:
|
amine@124
|
386 open_func.return_value = segment_mock
|
amine@124
|
387 from_file(filename, use_channel=use_channel)
|
amine@124
|
388 self.assertTrue(open_func.called)
|
amine@124
|
389 self.assertTrue(ext_mock.called)
|
amine@124
|
390
|
amine@124
|
391 use_channel = {"left": 0, "right": 1, None: 0}.get(
|
amine@124
|
392 use_channel, use_channel
|
amine@124
|
393 )
|
amine@124
|
394 ext_mock.assert_called_with(
|
amine@124
|
395 segment_mock._data,
|
amine@124
|
396 segment_mock.channels,
|
amine@124
|
397 segment_mock.sample_width,
|
amine@124
|
398 use_channel,
|
amine@124
|
399 )
|
amine@124
|
400
|
amine@124
|
401 with patch("auditok.io._extract_selected_channel") as ext_mock:
|
amine@124
|
402 with patch(
|
amine@124
|
403 "auditok.io.AudioSegment.{}".format(function)
|
amine@124
|
404 ) as open_func:
|
amine@124
|
405 segment_mock.channels = 1
|
amine@124
|
406 open_func.return_value = segment_mock
|
amine@124
|
407 from_file(filename, use_channel=use_channel)
|
amine@124
|
408 self.assertTrue(open_func.called)
|
amine@124
|
409 self.assertFalse(ext_mock.called)
|
amine@124
|
410
|
amine@125
|
411 @patch("auditok.io._WITH_PYDUB", True)
|
amine@125
|
412 @patch("auditok.io.BufferAudioSource")
|
amine@125
|
413 @genty_dataset(
|
amine@125
|
414 ogg=("ogg", "from_ogg"),
|
amine@125
|
415 mp3=("mp3", "from_mp3"),
|
amine@125
|
416 flac=("flac", "from_file"),
|
amine@125
|
417 )
|
amine@125
|
418 def test_from_file_multichannel_audio_mix_compressed(
|
amine@125
|
419 self, audio_format, function, *mocks
|
amine@125
|
420 ):
|
amine@125
|
421 filename = "audio.{}".format(audio_format)
|
amine@125
|
422 segment_mock = Mock()
|
amine@125
|
423 segment_mock.sample_width = 2
|
amine@125
|
424 segment_mock.channels = 2
|
amine@125
|
425 segment_mock._data = b"abcd"
|
amine@125
|
426 with patch("auditok.io._mix_audio_channels") as mix_mock:
|
amine@125
|
427 with patch(
|
amine@125
|
428 "auditok.io.AudioSegment.{}".format(function)
|
amine@125
|
429 ) as open_func:
|
amine@125
|
430 open_func.return_value = segment_mock
|
amine@125
|
431 from_file(filename, use_channel="mix")
|
amine@125
|
432 self.assertTrue(open_func.called)
|
amine@125
|
433 mix_mock.assert_called_with(
|
amine@125
|
434 segment_mock._data,
|
amine@125
|
435 segment_mock.channels,
|
amine@125
|
436 segment_mock.sample_width,
|
amine@125
|
437 )
|
amine@125
|
438
|
amine@123
|
439 @genty_dataset(
|
amine@126
|
440 dafault_first_channel=(None, 400),
|
amine@126
|
441 first_channel=(0, 400),
|
amine@126
|
442 second_channel=(1, 800),
|
amine@126
|
443 third_channel=(2, 1600),
|
amine@126
|
444 negative_first_channel=(-3, 400),
|
amine@126
|
445 negative_second_channel=(-2, 800),
|
amine@126
|
446 negative_third_channel=(-1, 1600),
|
amine@126
|
447 )
|
amine@126
|
448 def test_load_raw(self, use_channel, frequency):
|
amine@126
|
449 filename = "tests/data/test_16KHZ_3channel_400-800-1600Hz.raw"
|
amine@126
|
450 if use_channel is not None:
|
amine@126
|
451 audio_source = _load_raw(
|
amine@126
|
452 filename,
|
amine@126
|
453 sampling_rate=16000,
|
amine@126
|
454 sample_width=2,
|
amine@126
|
455 channels=3,
|
amine@126
|
456 use_channel=use_channel,
|
amine@126
|
457 )
|
amine@126
|
458 else:
|
amine@126
|
459 audio_source = _load_raw(
|
amine@126
|
460 filename, sampling_rate=16000, sample_width=2, channels=3
|
amine@126
|
461 )
|
amine@126
|
462 self.assertIsInstance(audio_source, BufferAudioSource)
|
amine@126
|
463 self.assertEqual(audio_source.sampling_rate, 16000)
|
amine@126
|
464 self.assertEqual(audio_source.sample_width, 2)
|
amine@126
|
465 self.assertEqual(audio_source.channels, 1)
|
amine@126
|
466 # generate a pure sine wave tone of the given frequency
|
amine@126
|
467 expected = PURE_TONE_DICT[frequency]
|
amine@126
|
468 # compre with data read from file
|
amine@126
|
469 fmt = DATA_FORMAT[2]
|
amine@126
|
470 data = array(fmt, audio_source._buffer)
|
amine@126
|
471 self.assertEqual(data, expected)
|
amine@126
|
472
|
amine@126
|
473 @genty_dataset(
|
amine@127
|
474 mono=("mono_400Hz", (400,)),
|
amine@127
|
475 three_channel=("3channel_400-800-1600Hz", (400, 800, 1600)),
|
amine@127
|
476 )
|
amine@127
|
477 def test_load_raw_mix(self, filename_suffix, frequencies):
|
amine@127
|
478 sampling_rate = 16000
|
amine@127
|
479 sample_width = 2
|
amine@127
|
480 channels = len(frequencies)
|
amine@127
|
481 mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
|
amine@127
|
482
|
amine@127
|
483 fmt = DATA_FORMAT[sample_width]
|
amine@127
|
484 expected = _array_to_bytes(
|
amine@127
|
485 array(
|
amine@127
|
486 fmt,
|
amine@127
|
487 (sum(samples) // channels for samples in zip(*mono_channels)),
|
amine@127
|
488 )
|
amine@127
|
489 )
|
amine@127
|
490 filename = "tests/data/test_16KHZ_{}.raw".format(filename_suffix)
|
amine@127
|
491 audio_source = _load_raw(
|
amine@127
|
492 filename,
|
amine@127
|
493 use_channel="mix",
|
amine@127
|
494 sampling_rate=sampling_rate,
|
amine@127
|
495 sample_width=2,
|
amine@127
|
496 channels=channels,
|
amine@127
|
497 )
|
amine@127
|
498 mixed = audio_source._buffer
|
amine@127
|
499 self.assertEqual(mixed, expected)
|
amine@127
|
500 self.assertIsInstance(audio_source, BufferAudioSource)
|
amine@127
|
501 self.assertEqual(audio_source.sampling_rate, sampling_rate)
|
amine@127
|
502 self.assertEqual(audio_source.sample_width, sample_width)
|
amine@127
|
503 self.assertEqual(audio_source.channels, 1)
|
amine@127
|
504
|
amine@127
|
505 @genty_dataset(
|
amine@128
|
506 missing_sampling_rate=("sr",),
|
amine@128
|
507 missing_sample_width=("sw",),
|
amine@128
|
508 missing_channels=("ch",),
|
amine@128
|
509 )
|
amine@128
|
510 def test_load_raw_missing_audio_param(self, missing_param):
|
amine@128
|
511 with self.assertRaises(AudioParameterError):
|
amine@128
|
512 params = AUDIO_PARAMS_SHORT.copy()
|
amine@128
|
513 del params[missing_param]
|
amine@128
|
514 srate, swidth, channels, _ = _get_audio_parameters(params)
|
amine@128
|
515 _load_raw("audio", srate, swidth, channels)
|
amine@128
|
516
|
amine@128
|
517 @genty_dataset(
|
amine@129
|
518 dafault_first_channel=(None, 400),
|
amine@129
|
519 first_channel=(0, 400),
|
amine@129
|
520 second_channel=(1, 800),
|
amine@129
|
521 third_channel=(2, 1600),
|
amine@129
|
522 negative_first_channel=(-3, 400),
|
amine@129
|
523 negative_second_channel=(-2, 800),
|
amine@129
|
524 negative_third_channel=(-1, 1600),
|
amine@129
|
525 )
|
amine@129
|
526 def test_load_wave(self, use_channel, frequency):
|
amine@129
|
527 filename = "tests/data/test_16KHZ_3channel_400-800-1600Hz.wav"
|
amine@129
|
528 if use_channel is not None:
|
amine@129
|
529 audio_source = _load_wave(filename, use_channel=use_channel)
|
amine@129
|
530 else:
|
amine@129
|
531 audio_source = _load_wave(filename)
|
amine@129
|
532 self.assertIsInstance(audio_source, BufferAudioSource)
|
amine@129
|
533 self.assertEqual(audio_source.sampling_rate, 16000)
|
amine@129
|
534 self.assertEqual(audio_source.sample_width, 2)
|
amine@129
|
535 self.assertEqual(audio_source.channels, 1)
|
amine@129
|
536 # generate a pure sine wave tone of the given frequency
|
amine@129
|
537 expected = PURE_TONE_DICT[frequency]
|
amine@129
|
538 # compre with data read from file
|
amine@129
|
539 fmt = DATA_FORMAT[2]
|
amine@129
|
540 data = array(fmt, audio_source._buffer)
|
amine@129
|
541 self.assertEqual(data, expected)
|
amine@129
|
542
|
amine@129
|
543 @genty_dataset(
|
amine@130
|
544 mono=("mono_400Hz", (400,)),
|
amine@130
|
545 three_channel=("3channel_400-800-1600Hz", (400, 800, 1600)),
|
amine@130
|
546 )
|
amine@130
|
547 def test_load_wave_mix(self, filename_suffix, frequencies):
|
amine@130
|
548 sampling_rate = 16000
|
amine@130
|
549 sample_width = 2
|
amine@130
|
550 channels = len(frequencies)
|
amine@130
|
551 mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
|
amine@130
|
552 fmt = DATA_FORMAT[sample_width]
|
amine@130
|
553 expected = _array_to_bytes(
|
amine@130
|
554 array(
|
amine@130
|
555 fmt,
|
amine@130
|
556 (sum(samples) // channels for samples in zip(*mono_channels)),
|
amine@130
|
557 )
|
amine@130
|
558 )
|
amine@130
|
559 filename = "tests/data/test_16KHZ_{}.wav".format(filename_suffix)
|
amine@130
|
560 audio_source = _load_wave(filename, use_channel="mix")
|
amine@130
|
561 mixed = audio_source._buffer
|
amine@130
|
562 self.assertEqual(mixed, expected)
|
amine@130
|
563 self.assertIsInstance(audio_source, BufferAudioSource)
|
amine@130
|
564 self.assertEqual(audio_source.sampling_rate, sampling_rate)
|
amine@130
|
565 self.assertEqual(audio_source.sample_width, sample_width)
|
amine@130
|
566 self.assertEqual(audio_source.channels, 1)
|
amine@130
|
567
|
amine@131
|
568 @patch("auditok.io._WITH_PYDUB", True)
|
amine@131
|
569 @patch("auditok.io.BufferAudioSource")
|
amine@131
|
570 @genty_dataset(
|
amine@131
|
571 ogg_default_first_channel=("ogg", 2, None, "from_ogg"),
|
amine@131
|
572 ogg_first_channel=("ogg", 1, 0, "from_ogg"),
|
amine@131
|
573 ogg_second_channel=("ogg", 2, 1, "from_ogg"),
|
amine@131
|
574 ogg_mix_channels=("ogg", 3, "mix", "from_ogg"),
|
amine@131
|
575 mp3_left_channel=("mp3", 1, "left", "from_mp3"),
|
amine@131
|
576 mp3_right_channel=("mp3", 2, "right", "from_mp3"),
|
amine@131
|
577 mp3_mix_channels=("mp3", 3, "mix", "from_mp3"),
|
amine@131
|
578 flac_first_channel=("flac", 2, 0, "from_file"),
|
amine@131
|
579 flac_second_channel=("flac", 2, 1, "from_file"),
|
amine@131
|
580 flv_left_channel=("flv", 1, "left", "from_flv"),
|
amine@131
|
581 webm_right_channel=("webm", 2, "right", "from_file"),
|
amine@131
|
582 webm_mix_channels=("webm", 4, "mix", "from_file"),
|
amine@131
|
583 )
|
amine@131
|
584 def test_load_with_pydub(
|
amine@131
|
585 self, audio_format, channels, use_channel, function, *mocks
|
amine@131
|
586 ):
|
amine@131
|
587 filename = "audio.{}".format(audio_format)
|
amine@131
|
588 segment_mock = Mock()
|
amine@131
|
589 segment_mock.sample_width = 2
|
amine@131
|
590 segment_mock.channels = channels
|
amine@131
|
591 segment_mock._data = b"abcdefgh"
|
amine@131
|
592 with patch("auditok.io._extract_selected_channel") as ext_mock:
|
amine@131
|
593 with patch(
|
amine@131
|
594 "auditok.io.AudioSegment.{}".format(function)
|
amine@131
|
595 ) as open_func:
|
amine@131
|
596 open_func.return_value = segment_mock
|
amine@131
|
597 use_channel = {"left": 0, "right": 1, None: 0}.get(
|
amine@131
|
598 use_channel, use_channel
|
amine@131
|
599 )
|
amine@131
|
600 _load_with_pydub(filename, audio_format, use_channel)
|
amine@131
|
601 self.assertTrue(open_func.called)
|
amine@131
|
602 if channels > 1:
|
amine@131
|
603 self.assertTrue(ext_mock.called)
|
amine@131
|
604 ext_mock.assert_called_with(
|
amine@131
|
605 segment_mock._data,
|
amine@131
|
606 segment_mock.channels,
|
amine@131
|
607 segment_mock.sample_width,
|
amine@131
|
608 use_channel,
|
amine@131
|
609 )
|
amine@131
|
610 else:
|
amine@131
|
611 self.assertFalse(ext_mock.called)
|
amine@131
|
612
|
amine@130
|
613 @genty_dataset(
|
amine@132
|
614 mono=("mono_400Hz.raw", (400,)),
|
amine@132
|
615 three_channel=("3channel_400-800-1600Hz.raw", (400, 800, 1600)),
|
amine@132
|
616 )
|
amine@132
|
617 def test_save_raw(self, filename, frequencies):
|
amine@132
|
618 filename = "tests/data/test_16KHZ_{}".format(filename)
|
amine@132
|
619 sample_width = 2
|
amine@132
|
620 fmt = DATA_FORMAT[sample_width]
|
amine@132
|
621 mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
|
amine@132
|
622 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
|
amine@132
|
623 tmpfile = NamedTemporaryFile()
|
amine@136
|
624 _save_raw(data, tmpfile.name)
|
amine@132
|
625 self.assertTrue(filecmp.cmp(tmpfile.name, filename, shallow=False))
|
amine@132
|
626
|
amine@132
|
627 @genty_dataset(
|
amine@110
|
628 mono=("mono_400Hz.wav", (400,)),
|
amine@110
|
629 three_channel=("3channel_400-800-1600Hz.wav", (400, 800, 1600)),
|
amine@110
|
630 )
|
amine@110
|
631 def test_save_wave(self, filename, frequencies):
|
amine@110
|
632 filename = "tests/data/test_16KHZ_{}".format(filename)
|
amine@110
|
633 sampling_rate = 16000
|
amine@110
|
634 sample_width = 2
|
amine@110
|
635 channels = len(frequencies)
|
amine@110
|
636 fmt = DATA_FORMAT[sample_width]
|
amine@110
|
637 mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
|
amine@110
|
638 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
|
amine@110
|
639 tmpfile = NamedTemporaryFile()
|
amine@136
|
640 _save_wave(data, tmpfile.name, sampling_rate, sample_width, channels)
|
amine@110
|
641 self.assertTrue(filecmp.cmp(tmpfile.name, filename, shallow=False))
|
amine@132
|
642
|
amine@132
|
643 @genty_dataset(
|
amine@132
|
644 missing_sampling_rate=("sr",),
|
amine@132
|
645 missing_sample_width=("sw",),
|
amine@132
|
646 missing_channels=("ch",),
|
amine@132
|
647 )
|
amine@132
|
648 def test_save_wave_missing_audio_param(self, missing_param):
|
amine@132
|
649 with self.assertRaises(AudioParameterError):
|
amine@132
|
650 params = AUDIO_PARAMS_SHORT.copy()
|
amine@132
|
651 del params[missing_param]
|
amine@132
|
652 srate, swidth, channels, _ = _get_audio_parameters(params)
|
amine@136
|
653 _save_wave(b"\0\0", "audio", srate, swidth, channels)
|
amine@133
|
654
|
amine@141
|
655 def test_save_with_pydub(self):
|
amine@141
|
656 with patch("auditok.io.AudioSegment.export") as export:
|
amine@142
|
657 tmpdir = TemporaryDirectory()
|
amine@142
|
658 filename = os.path.join(tmpdir.name, "audio.ogg")
|
amine@142
|
659 _save_with_pydub(b"\0\0", filename, "ogg", 16000, 2, 1)
|
amine@141
|
660 self.assertTrue(export.called)
|
amine@142
|
661 tmpdir.cleanup()
|
amine@141
|
662
|
amine@133
|
663 @genty_dataset(
|
amine@133
|
664 raw_with_audio_format=("audio", "raw"),
|
amine@133
|
665 raw_with_extension=("audio.raw", None),
|
amine@133
|
666 raw_with_audio_format_and_extension=("audio.mp3", "raw"),
|
amine@133
|
667 raw_no_audio_format_nor_extension=("audio", None),
|
amine@133
|
668 )
|
amine@133
|
669 def test_to_file_raw(self, filename, audio_format):
|
amine@133
|
670 exp_filename = "tests/data/test_16KHZ_mono_400Hz.raw"
|
amine@133
|
671 tmpdir = TemporaryDirectory()
|
amine@133
|
672 filename = os.path.join(tmpdir.name, filename)
|
amine@133
|
673 data = _array_to_bytes(PURE_TONE_DICT[400])
|
amine@135
|
674 to_file(data, filename, audio_format=audio_format)
|
amine@133
|
675 self.assertTrue(filecmp.cmp(filename, exp_filename, shallow=False))
|
amine@133
|
676 tmpdir.cleanup()
|
amine@134
|
677
|
amine@134
|
678 @genty_dataset(
|
amine@134
|
679 wav_with_audio_format=("audio", "wav"),
|
amine@134
|
680 wav_with_extension=("audio.wav", None),
|
amine@134
|
681 wav_with_audio_format_and_extension=("audio.mp3", "wav"),
|
amine@134
|
682 wave_with_audio_format=("audio", "wave"),
|
amine@134
|
683 wave_with_extension=("audio.wave", None),
|
amine@134
|
684 wave_with_audio_format_and_extension=("audio.mp3", "wave"),
|
amine@134
|
685 )
|
amine@135
|
686 def test_to_file_wave(self, filename, audio_format):
|
amine@134
|
687 exp_filename = "tests/data/test_16KHZ_mono_400Hz.wav"
|
amine@134
|
688 tmpdir = TemporaryDirectory()
|
amine@134
|
689 filename = os.path.join(tmpdir.name, filename)
|
amine@134
|
690 data = _array_to_bytes(PURE_TONE_DICT[400])
|
amine@135
|
691 to_file(
|
amine@135
|
692 data,
|
amine@135
|
693 filename,
|
amine@135
|
694 audio_format=audio_format,
|
amine@135
|
695 sampling_rate=16000,
|
amine@135
|
696 sample_width=2,
|
amine@135
|
697 channels=1,
|
amine@134
|
698 )
|
amine@134
|
699 self.assertTrue(filecmp.cmp(filename, exp_filename, shallow=False))
|
amine@134
|
700 tmpdir.cleanup()
|
amine@138
|
701
|
amine@138
|
702 @genty_dataset(
|
amine@138
|
703 missing_sampling_rate=("sr",),
|
amine@138
|
704 missing_sample_width=("sw",),
|
amine@138
|
705 missing_channels=("ch",),
|
amine@138
|
706 )
|
amine@138
|
707 def test_to_file_missing_audio_param(self, missing_param):
|
amine@138
|
708 params = AUDIO_PARAMS_SHORT.copy()
|
amine@138
|
709 del params[missing_param]
|
amine@138
|
710 with self.assertRaises(AudioParameterError):
|
amine@138
|
711 to_file(b"\0\0", "audio", audio_format="wav", **params)
|
amine@138
|
712 with self.assertRaises(AudioParameterError):
|
amine@138
|
713 to_file(b"\0\0", "audio", audio_format="mp3", **params)
|
amine@139
|
714
|
amine@139
|
715 def test_to_file_no_pydub(self):
|
amine@139
|
716 with patch("auditok.io._WITH_PYDUB", False):
|
amine@139
|
717 with self.assertRaises(AudioIOError):
|
amine@139
|
718 to_file("audio", b"", "mp3")
|
amine@140
|
719
|
amine@140
|
720 @patch("auditok.io._WITH_PYDUB", True)
|
amine@140
|
721 @genty_dataset(
|
amine@140
|
722 ogg_with_extension=("audio.ogg", None),
|
amine@140
|
723 ogg_with_audio_format=("audio", "ogg"),
|
amine@140
|
724 ogg_format_with_wrong_extension=("audio.wav", "ogg"),
|
amine@140
|
725 )
|
amine@140
|
726 def test_to_file_compressed(self, filename, audio_format, *mocks):
|
amine@140
|
727 with patch("auditok.io.AudioSegment.export") as export:
|
amine@142
|
728 tmpdir = TemporaryDirectory()
|
amine@142
|
729 filename = os.path.join(tmpdir.name, filename)
|
amine@140
|
730 to_file(b"\0\0", filename, audio_format, **AUDIO_PARAMS_SHORT)
|
amine@140
|
731 self.assertTrue(export.called)
|
amine@142
|
732 tmpdir.cleanup()
|