comparison tests/test_io.py @ 119:c768fa017e21

Add tests for _extract_selected_channel
author Amine Sehili <amine.sehili@gmail.com>
date Sat, 02 Feb 2019 11:53:59 +0100
parents 1af0c6050073
children 9b117eb6ecfd
comparison
equal deleted inserted replaced
118:1af0c6050073 119:c768fa017e21
10 DATA_FORMAT, 10 DATA_FORMAT,
11 AudioParameterError, 11 AudioParameterError,
12 check_audio_data, 12 check_audio_data,
13 _array_to_bytes, 13 _array_to_bytes,
14 _mix_audio_channels, 14 _mix_audio_channels,
15 _extract_selected_channel,
15 _save_raw, 16 _save_raw,
16 _save_wave, 17 _save_wave,
17 ) 18 )
18 19
19 20
122 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels))) 123 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
123 mixed = _mix_audio_channels(data, channels, sample_width) 124 mixed = _mix_audio_channels(data, channels, sample_width)
124 self.assertEqual(mixed, expected) 125 self.assertEqual(mixed, expected)
125 126
126 @genty_dataset( 127 @genty_dataset(
128 mono_1byte=([400], 1, 0),
129 stereo_1byte_2st_channel=([400, 600], 1, 1),
130 mono_2byte=([400], 2, 0),
131 stereo_2byte_1st_channel=([400, 600], 2, 0),
132 stereo_2byte_2nd_channel=([400, 600], 2, 1),
133 three_channel_2byte_last_negative_idx=([400, 600, 1150], 2, -1),
134 three_channel_2byte_2nd_negative_idx=([400, 600, 1150], 2, -2),
135 three_channel_2byte_1st_negative_idx=([400, 600, 1150], 2, -3),
136 three_channel_4byte_1st=([400, 600, 1150], 4, 0),
137 three_channel_4byte_last_negative_idx=([400, 600, 1150], 4, -1),
138 )
139 def test_extract_selected_channel(
140 self, frequencies, sample_width, use_channel
141 ):
142
143 mono_channels = [
144 _generate_pure_tone(
145 freq,
146 duration_sec=0.1,
147 sampling_rate=16000,
148 sample_width=sample_width,
149 )
150 for freq in frequencies
151 ]
152 channels = len(frequencies)
153 fmt = DATA_FORMAT[sample_width]
154 expected = _array_to_bytes(mono_channels[use_channel])
155 data = _array_to_bytes(array(fmt, _sample_generator(*mono_channels)))
156 selected_channel = _extract_selected_channel(
157 data, channels, sample_width, use_channel
158 )
159 self.assertEqual(selected_channel, expected)
160
161 @genty_dataset(
127 mono=("mono_400Hz.raw", (400,)), 162 mono=("mono_400Hz.raw", (400,)),
128 three_channel=("3channel_400-800-1600Hz.raw", (400, 800, 1600)), 163 three_channel=("3channel_400-800-1600Hz.raw", (400, 800, 1600)),
129 ) 164 )
130 def test_save_raw(self, filename, frequencies): 165 def test_save_raw(self, filename, frequencies):
131 filename = "tests/data/test_16KHZ_{}".format(filename) 166 filename = "tests/data/test_16KHZ_{}".format(filename)