comparison tests/test_core.py @ 88:a10fc63118a8

Add tests for AudioRegion concatenation
author Amine Sehili <amine.sehili@gmail.com>
date Fri, 04 Jan 2019 22:13:45 +0100
parents 2b38e5824e13
children a2ad7085c262
comparison
equal deleted inserted replaced
87:629d8f15b490 88:a10fc63118a8
1 import unittest 1 import unittest
2 from random import random
2 from genty import genty, genty_dataset 3 from genty import genty, genty_dataset
3 from auditok import AudioRegion 4 from auditok import AudioRegion
5
6
7 def _make_random_length_regions(
8 byte_seq, sampling_rate, sample_width, channels
9 ):
10 regions = []
11 for b in byte_seq:
12 duration = round(random() * 10, 6)
13 data = b * int(duration * sampling_rate * sample_width * channels)
14 start = round(random() * 13, 3)
15 region = AudioRegion(
16 data, start, sampling_rate, sample_width, channels
17 )
18 regions.append(region)
19 return regions
4 20
5 21
6 @genty 22 @genty
7 class TestAudioRegion(unittest.TestCase): 23 class TestAudioRegion(unittest.TestCase):
8 @genty_dataset( 24 @genty_dataset(
174 self.assertEqual(region.start, start) 190 self.assertEqual(region.start, start)
175 self.assertEqual(region.end, expected_end) 191 self.assertEqual(region.end, expected_end)
176 self.assertEqual(region.duration, expected_duration_s) 192 self.assertEqual(region.duration, expected_duration_s)
177 self.assertEqual(len(region), expected_duration_ms) 193 self.assertEqual(len(region), expected_duration_ms)
178 self.assertEqual(bytes(region), data) 194 self.assertEqual(bytes(region), data)
195
196 @genty_dataset(
197 simple=(8000, 1, 1),
198 stereo_sw_2=(8000, 2, 2),
199 arbitray_sr_multichannel=(5413, 2, 3),
200 )
201 def test_concatenation(self, sampling_rate, sample_width, channels):
202
203 region_1, region_2 = _make_random_length_regions(
204 [b"a", b"b"], sampling_rate, sample_width, channels
205 )
206
207 expected_start = region_1.start
208 expected_duration = region_1.duration + region_2.duration
209 expected_end = expected_start + expected_duration
210 expected_data = bytes(region_1) + bytes(region_2)
211 concat_region = region_1 + region_2
212
213 self.assertEqual(concat_region.start, expected_start)
214 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
215 self.assertAlmostEqual(
216 concat_region.duration, expected_duration, places=6
217 )
218 self.assertEqual(bytes(concat_region), expected_data)
219 # due to the behavior of `round` len(concat_region) does not always
220 # equal len(region_1) + len(region_2)
221 # Exmaple if both regions are 1.0005 seconds long, then:
222 # len(region_1) == len(region_2) == round(1.0005) == 1000
223 # and:
224 # region_1.duration + region_2.duration == 1.0005 * 2 = 2.001
225 # and:
226 # len(region_3) == round(2.001 * 1000) = 2001
227 # != len(region_1) + len(region_2)
228 self.assertEqual(len(concat_region), round(expected_duration * 1000))
229
230 @genty_dataset(
231 simple=(8000, 1, 1),
232 stereo_sw_2=(8000, 2, 2),
233 arbitray_sr_multichannel=(5413, 2, 3),
234 )
235 def test_concatenation_many(self, sampling_rate, sample_width, channels):
236
237 regions = _make_random_length_regions(
238 [b"a", b"b", b"c"], sampling_rate, sample_width, channels
239 )
240 expected_start = regions[0].start
241 expected_duration = sum(r.duration for r in regions)
242 expected_end = expected_start + expected_duration
243 expected_data = b"".join(bytes(r) for r in regions)
244 concat_region = sum(regions)
245
246 self.assertEqual(concat_region.start, expected_start)
247 self.assertAlmostEqual(concat_region.end, expected_end, places=6)
248 self.assertAlmostEqual(
249 concat_region.duration, expected_duration, places=6
250 )
251 self.assertEqual(bytes(concat_region), expected_data)
252 # see test_concatenation
253 self.assertEqual(len(concat_region), round(expected_duration * 1000))
254
255 def test_concatenation_different_sampling_rate_error(self):
256
257 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
258 region_2 = AudioRegion(b"b" * 100, 0, 3000, 1, 1)
259
260 with self.assertRaises(ValueError) as val_err:
261 region_1 + region_2
262 self.assertEqual(
263 "Can only concatenate AudioRegions of the same "
264 "sampling rate (8000 != 3000)",
265 str(val_err.exception),
266 )
267
268 def test_concatenation_different_sample_width_error(self):
269
270 region_1 = AudioRegion(b"a" * 100, 0, 8000, 2, 1)
271 region_2 = AudioRegion(b"b" * 100, 0, 8000, 4, 1)
272
273 with self.assertRaises(ValueError) as val_err:
274 region_1 + region_2
275 self.assertEqual(
276 "Can only concatenate AudioRegions of the same "
277 "sample width (2 != 4)",
278 str(val_err.exception),
279 )
280
281 def test_concatenation_different_number_of_channels_error(self):
282
283 region_1 = AudioRegion(b"a" * 100, 0, 8000, 1, 1)
284 region_2 = AudioRegion(b"b" * 100, 0, 8000, 1, 2)
285
286 with self.assertRaises(ValueError) as val_err:
287 region_1 + region_2
288 self.assertEqual(
289 "Can only concatenate AudioRegions of the same "
290 "number of channels (1 != 2)",
291 str(val_err.exception),
292 )