changeset 157:682bf4477fae

Move helper test functions to a new file
author Amine Sehili <amine.sehili@gmail.com>
date Sun, 24 Feb 2019 17:16:53 +0100
parents eda276f5d871
children 6ed3a1eea98d
files tests/test_io.py tests/test_util.py
diffstat 2 files changed, 52 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- 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(
--- /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)
+    }
+)