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