# HG changeset patch # User Amine Sehili # Date 1551025013 -3600 # Node ID 682bf4477fae7f6c0015d447d8e4fe40b2d4f469 # Parent eda276f5d871ff537a8a2c57f5ea3bfcaf116cd4 Move helper test functions to a new file diff -r eda276f5d871 -r 682bf4477fae tests/test_io.py --- a/tests/test_io.py Sun Feb 24 17:16:53 2019 +0100 +++ b/tests/test_io.py Sun Feb 24 17:16:53 2019 +0100 @@ -6,6 +6,7 @@ import filecmp from unittest import TestCase from genty import genty, genty_dataset +from test_util import _sample_generator, _generate_pure_tone, PURE_TONE_DICT from auditok.io import ( DATA_FORMAT, AudioIOError, @@ -39,54 +40,6 @@ AUDIO_PARAMS_SHORT = {"sr": 16000, "sw": 2, "ch": 1} -def _sample_generator(*data_buffers): - """ - Takes a list of many mono audio data buffers and makes a sample generator - of interleaved audio samples, one sample from each channel. The resulting - generator can be used to build a multichannel audio buffer. - >>> gen = _sample_generator("abcd", "ABCD") - >>> list(gen) - ["a", "A", "b", "B", "c", "C", "d", "D"] - """ - frame_gen = zip(*data_buffers) - return (sample for frame in frame_gen for sample in frame) - - -def _generate_pure_tone( - frequency, duration_sec=1, sampling_rate=16000, sample_width=2, volume=1e4 -): - """ - Generates a pure tone with the given frequency. - """ - assert frequency <= sampling_rate / 2 - max_value = (2 ** (sample_width * 8) // 2) - 1 - if volume > max_value: - volume = max_value - fmt = DATA_FORMAT[sample_width] - total_samples = int(sampling_rate * duration_sec) - step = frequency / sampling_rate - two_pi_step = 2 * math.pi * step - data = array( - fmt, - ( - int(math.sin(two_pi_step * i) * volume) - for i in range(total_samples) - ), - ) - return data - - -PURE_TONE_DICT = { - freq: _generate_pure_tone(freq, 1, 16000, 2) for freq in (400, 800, 1600) -} -PURE_TONE_DICT.update( - { - freq: _generate_pure_tone(freq, 0.1, 16000, 2) - for freq in (600, 1150, 2400, 7220) - } -) - - @genty class TestIO(TestCase): @genty_dataset( diff -r eda276f5d871 -r 682bf4477fae tests/test_util.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_util.py Sun Feb 24 17:16:53 2019 +0100 @@ -0,0 +1,51 @@ +import math +from array import array +from auditok.io import DATA_FORMAT + + +def _sample_generator(*data_buffers): + """ + Takes a list of many mono audio data buffers and makes a sample generator + of interleaved audio samples, one sample from each channel. The resulting + generator can be used to build a multichannel audio buffer. + >>> gen = _sample_generator("abcd", "ABCD") + >>> list(gen) + ["a", "A", "b", "B", "c", "C", "d", "D"] + """ + frame_gen = zip(*data_buffers) + return (sample for frame in frame_gen for sample in frame) + + +def _generate_pure_tone( + frequency, duration_sec=1, sampling_rate=16000, sample_width=2, volume=1e4 +): + """ + Generates a pure tone with the given frequency. + """ + assert frequency <= sampling_rate / 2 + max_value = (2 ** (sample_width * 8) // 2) - 1 + if volume > max_value: + volume = max_value + fmt = DATA_FORMAT[sample_width] + total_samples = int(sampling_rate * duration_sec) + step = frequency / sampling_rate + two_pi_step = 2 * math.pi * step + data = array( + fmt, + ( + int(math.sin(two_pi_step * i) * volume) + for i in range(total_samples) + ), + ) + return data + + +PURE_TONE_DICT = { + freq: _generate_pure_tone(freq, 1, 16000, 2) for freq in (400, 800, 1600) +} +PURE_TONE_DICT.update( + { + freq: _generate_pure_tone(freq, 0.1, 16000, 2) + for freq in (600, 1150, 2400, 7220) + } +)