amine@106
|
1 import os
|
amine@106
|
2 import sys
|
amine@106
|
3 import math
|
amine@107
|
4 from array import array
|
amine@110
|
5 from tempfile import NamedTemporaryFile
|
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@110
|
10 DATA_FORMAT,
|
amine@110
|
11 AudioParameterError,
|
amine@110
|
12 check_audio_data,
|
amine@116
|
13 _array_to_bytes,
|
amine@118
|
14 _mix_audio_channels,
|
amine@119
|
15 _extract_selected_channel,
|
amine@120
|
16 from_file,
|
amine@111
|
17 _save_raw,
|
amine@110
|
18 _save_wave,
|
amine@110
|
19 )
|
amine@106
|
20
|
amine@106
|
21
|
amine@106
|
22 if sys.version_info >= (3, 0):
|
amine@106
|
23 PYTHON_3 = True
|
amine@120
|
24 from unittest.mock import patch
|
amine@106
|
25 else:
|
amine@106
|
26 PYTHON_3 = False
|
amine@120
|
27 from mock import patch
|
amine@120
|
28
|
amine@120
|
29 AUDIO_PARAMS_SHORT = {"sr": 16000, "sw": 2, "ch": 1}
|
amine@106
|
30
|
amine@106
|
31
|
amine@106
|
32 def _sample_generator(*data_buffers):
|
amine@106
|
33 """
|
amine@106
|
34 Takes a list of many mono audio data buffers and makes a sample generator
|
amine@106
|
35 of interleaved audio samples, one sample from each channel. The resulting
|
amine@106
|
36 generator can be used to build a multichannel audio buffer.
|
amine@106
|
37 >>> gen = _sample_generator("abcd", "ABCD")
|
amine@106
|
38 >>> list(gen)
|
amine@106
|
39 ["a", "A", "b", "B", "c", "C", "d", "D"]
|
amine@106
|
40 """
|
amine@106
|
41 frame_gen = zip(*data_buffers)
|
amine@106
|
42 return (sample for frame in frame_gen for sample in frame)
|
amine@106
|
43
|
amine@106
|
44
|
amine@107
|
45 def _generate_pure_tone(
|
amine@107
|
46 frequency, duration_sec=1, sampling_rate=16000, sample_width=2, volume=1e4
|
amine@107
|
47 ):
|
amine@107
|
48 """
|
amine@107
|
49 Generates a pure tone with the given frequency.
|
amine@107
|
50 """
|
amine@107
|
51 assert frequency <= sampling_rate / 2
|
amine@107
|
52 max_value = (2 ** (sample_width * 8) // 2) - 1
|
amine@107
|
53 if volume > max_value:
|
amine@107
|
54 volume = max_value
|
amine@107
|
55 fmt = DATA_FORMAT[sample_width]
|
amine@107
|
56 total_samples = int(sampling_rate * duration_sec)
|
amine@107
|
57 step = frequency / sampling_rate
|
amine@107
|
58 two_pi_step = 2 * math.pi * step
|
amine@107
|
59 data = array(
|
amine@107
|
60 fmt,
|
amine@107
|
61 (
|
amine@107
|
62 int(math.sin(two_pi_step * i) * volume)
|
amine@107
|
63 for i in range(total_samples)
|
amine@107
|
64 ),
|
amine@107
|
65 )
|
amine@107
|
66 return data
|
amine@107
|
67
|
amine@107
|
68
|
amine@107
|
69 PURE_TONE_DICT = {
|
amine@107
|
70 freq: _generate_pure_tone(freq, 1, 16000, 2) for freq in (400, 800, 1600)
|
amine@107
|
71 }
|
amine@107
|
72 PURE_TONE_DICT.update(
|
amine@107
|
73 {
|
amine@107
|
74 freq: _generate_pure_tone(freq, 0.1, 16000, 2)
|
amine@107
|
75 for freq in (600, 1150, 2400, 7220)
|
amine@107
|
76 }
|
amine@107
|
77 )
|
amine@108
|
78
|
amine@108
|
79
|
amine@108
|
80 @genty
|
amine@108
|
81 class TestIO(TestCase):
|
amine@108
|
82 @genty_dataset(
|
amine@108
|
83 valid_mono=(b"\0" * 113, 1, 1),
|
amine@108
|
84 valid_stereo=(b"\0" * 160, 1, 2),
|
amine@108
|
85 invalid_mono_sw_2=(b"\0" * 113, 2, 1, False),
|
amine@108
|
86 invalid_stereo_sw_1=(b"\0" * 113, 1, 2, False),
|
amine@108
|
87 invalid_stereo_sw_2=(b"\0" * 158, 2, 2, False),
|
amine@108
|
88 )
|
amine@108
|
89 def test_check_audio_data(self, data, sample_width, channels, valid=True):
|
amine@108
|
90
|
amine@108
|
91 if not valid:
|
amine@108
|
92 with self.assertRaises(AudioParameterError):
|
amine@108
|
93 check_audio_data(data, sample_width, channels)
|
amine@108
|
94 else:
|
amine@108
|
95 self.assertIsNone(check_audio_data(data, sample_width, channels))
|
amine@110
|
96
|
amine@110
|
97 @genty_dataset(
|
amine@118
|
98 mono_1byte=([400], 1),
|
amine@118
|
99 stereo_1byte=([400, 600], 1),
|
amine@118
|
100 three_channel_1byte=([400, 600, 2400], 1),
|
amine@118
|
101 mono_2byte=([400], 2),
|
amine@118
|
102 stereo_2byte=([400, 600], 2),
|
amine@118
|
103 three_channel_2byte=([400, 600, 1150], 2),
|
amine@118
|
104 mono_4byte=([400], 4),
|
amine@118
|
105 stereo_4byte=([400, 600], 4),
|
amine@118
|
106 four_channel_2byte=([400, 600, 1150, 7220], 4),
|
amine@118
|
107 )
|
amine@118
|
108 def test_mix_audio_channels(self, frequencies, sample_width):
|
amine@118
|
109 sampling_rate = 16000
|
amine@118
|
110 sample_width = 2
|
amine@118
|
111 channels = len(frequencies)
|
amine@118
|
112 mono_channels = [
|
amine@118
|
113 _generate_pure_tone(
|
amine@118
|
114 freq,
|
amine@118
|
115 duration_sec=0.1,
|
amine@118
|
116 sampling_rate=sampling_rate,
|
amine@118
|
117 sample_width=sample_width,
|
amine@118
|
118 )
|
amine@118
|
119 for freq in frequencies
|
amine@118
|
120 ]
|
amine@118
|
121 fmt = DATA_FORMAT[sample_width]
|
amine@118
|
122 expected = _array_to_bytes(
|
amine@118
|
123 array(
|
amine@118
|
124 fmt,
|
amine@118
|
125 (sum(samples) // channels for samples in zip(*mono_channels)),
|
amine@118
|
126 )
|
amine@118
|
127 )
|
amine@118
|
128 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
|
amine@118
|
129 mixed = _mix_audio_channels(data, channels, sample_width)
|
amine@118
|
130 self.assertEqual(mixed, expected)
|
amine@118
|
131
|
amine@118
|
132 @genty_dataset(
|
amine@119
|
133 mono_1byte=([400], 1, 0),
|
amine@119
|
134 stereo_1byte_2st_channel=([400, 600], 1, 1),
|
amine@119
|
135 mono_2byte=([400], 2, 0),
|
amine@119
|
136 stereo_2byte_1st_channel=([400, 600], 2, 0),
|
amine@119
|
137 stereo_2byte_2nd_channel=([400, 600], 2, 1),
|
amine@119
|
138 three_channel_2byte_last_negative_idx=([400, 600, 1150], 2, -1),
|
amine@119
|
139 three_channel_2byte_2nd_negative_idx=([400, 600, 1150], 2, -2),
|
amine@119
|
140 three_channel_2byte_1st_negative_idx=([400, 600, 1150], 2, -3),
|
amine@119
|
141 three_channel_4byte_1st=([400, 600, 1150], 4, 0),
|
amine@119
|
142 three_channel_4byte_last_negative_idx=([400, 600, 1150], 4, -1),
|
amine@119
|
143 )
|
amine@119
|
144 def test_extract_selected_channel(
|
amine@119
|
145 self, frequencies, sample_width, use_channel
|
amine@119
|
146 ):
|
amine@119
|
147
|
amine@119
|
148 mono_channels = [
|
amine@119
|
149 _generate_pure_tone(
|
amine@119
|
150 freq,
|
amine@119
|
151 duration_sec=0.1,
|
amine@119
|
152 sampling_rate=16000,
|
amine@119
|
153 sample_width=sample_width,
|
amine@119
|
154 )
|
amine@119
|
155 for freq in frequencies
|
amine@119
|
156 ]
|
amine@119
|
157 channels = len(frequencies)
|
amine@119
|
158 fmt = DATA_FORMAT[sample_width]
|
amine@119
|
159 expected = _array_to_bytes(mono_channels[use_channel])
|
amine@119
|
160 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
|
amine@119
|
161 selected_channel = _extract_selected_channel(
|
amine@119
|
162 data, channels, sample_width, use_channel
|
amine@119
|
163 )
|
amine@119
|
164 self.assertEqual(selected_channel, expected)
|
amine@119
|
165
|
amine@119
|
166 @genty_dataset(
|
amine@120
|
167 raw_with_audio_format=(
|
amine@120
|
168 "audio",
|
amine@120
|
169 "raw",
|
amine@120
|
170 "_load_raw",
|
amine@120
|
171 AUDIO_PARAMS_SHORT,
|
amine@120
|
172 ),
|
amine@120
|
173 raw_with_extension=(
|
amine@120
|
174 "audio.raw",
|
amine@120
|
175 None,
|
amine@120
|
176 "_load_raw",
|
amine@120
|
177 AUDIO_PARAMS_SHORT,
|
amine@120
|
178 ),
|
amine@120
|
179 wave_with_audio_format=("audio", "wave", "_load_wave"),
|
amine@120
|
180 wav_with_audio_format=("audio", "wave", "_load_wave"),
|
amine@120
|
181 wav_with_extension=("audio.wav", None, "_load_wave"),
|
amine@120
|
182 format_and_extension_both_given=("audio.dat", "wav", "_load_wave"),
|
amine@120
|
183 format_and_extension_both_given_b=("audio.raw", "wave", "_load_wave"),
|
amine@120
|
184 no_format_nor_extension=("audio", None, "_load_with_pydub"),
|
amine@120
|
185 other_formats_ogg=("audio.ogg", None, "_load_with_pydub"),
|
amine@120
|
186 other_formats_webm=("audio", "webm", "_load_with_pydub"),
|
amine@120
|
187 )
|
amine@120
|
188 def test_from_file(
|
amine@120
|
189 self, filename, audio_format, funtion_name, kwargs=None
|
amine@120
|
190 ):
|
amine@120
|
191 funtion_name = "auditok.io." + funtion_name
|
amine@120
|
192 if kwargs is None:
|
amine@120
|
193 kwargs = {}
|
amine@120
|
194 with patch(funtion_name) as patch_function:
|
amine@120
|
195 from_file(filename, audio_format, **kwargs)
|
amine@120
|
196 self.assertTrue(patch_function.called)
|
amine@120
|
197
|
amine@120
|
198 @genty_dataset(
|
amine@111
|
199 mono=("mono_400Hz.raw", (400,)),
|
amine@111
|
200 three_channel=("3channel_400-800-1600Hz.raw", (400, 800, 1600)),
|
amine@111
|
201 )
|
amine@111
|
202 def test_save_raw(self, filename, frequencies):
|
amine@111
|
203 filename = "tests/data/test_16KHZ_{}".format(filename)
|
amine@111
|
204 sample_width = 2
|
amine@111
|
205 fmt = DATA_FORMAT[sample_width]
|
amine@111
|
206 mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
|
amine@111
|
207 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
|
amine@111
|
208 tmpfile = NamedTemporaryFile()
|
amine@111
|
209 _save_raw(tmpfile.name, data)
|
amine@111
|
210 self.assertTrue(filecmp.cmp(tmpfile.name, filename, shallow=False))
|
amine@111
|
211
|
amine@111
|
212 @genty_dataset(
|
amine@110
|
213 mono=("mono_400Hz.wav", (400,)),
|
amine@110
|
214 three_channel=("3channel_400-800-1600Hz.wav", (400, 800, 1600)),
|
amine@110
|
215 )
|
amine@110
|
216 def test_save_wave(self, filename, frequencies):
|
amine@110
|
217 filename = "tests/data/test_16KHZ_{}".format(filename)
|
amine@110
|
218 sampling_rate = 16000
|
amine@110
|
219 sample_width = 2
|
amine@110
|
220 channels = len(frequencies)
|
amine@110
|
221 fmt = DATA_FORMAT[sample_width]
|
amine@110
|
222 mono_channels = [PURE_TONE_DICT[freq] for freq in frequencies]
|
amine@110
|
223 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
|
amine@110
|
224 tmpfile = NamedTemporaryFile()
|
amine@110
|
225 _save_wave(tmpfile.name, data, sampling_rate, sample_width, channels)
|
amine@110
|
226 self.assertTrue(filecmp.cmp(tmpfile.name, filename, shallow=False))
|