Mercurial > hg > auditok
annotate tests/test_io.py @ 106:9505b35ef8ea
Create test_io.py with a few helper functions
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Sat, 12 Jan 2019 17:50:24 +0100 |
parents | |
children | faf3bdd69251 |
rev | line source |
---|---|
amine@106 | 1 import os |
amine@106 | 2 import sys |
amine@106 | 3 import math |
amine@106 | 4 |
amine@106 | 5 |
amine@106 | 6 if sys.version_info >= (3, 0): |
amine@106 | 7 PYTHON_3 = True |
amine@106 | 8 else: |
amine@106 | 9 PYTHON_3 = False |
amine@106 | 10 |
amine@106 | 11 |
amine@106 | 12 def _sample_generator(*data_buffers): |
amine@106 | 13 """ |
amine@106 | 14 Takes a list of many mono audio data buffers and makes a sample generator |
amine@106 | 15 of interleaved audio samples, one sample from each channel. The resulting |
amine@106 | 16 generator can be used to build a multichannel audio buffer. |
amine@106 | 17 >>> gen = _sample_generator("abcd", "ABCD") |
amine@106 | 18 >>> list(gen) |
amine@106 | 19 ["a", "A", "b", "B", "c", "C", "d", "D"] |
amine@106 | 20 """ |
amine@106 | 21 frame_gen = zip(*data_buffers) |
amine@106 | 22 return (sample for frame in frame_gen for sample in frame) |
amine@106 | 23 |
amine@106 | 24 |
amine@106 | 25 def _array_to_bytes(a): |
amine@106 | 26 """ |
amine@106 | 27 Converts an `array.array` to `bytes`. |
amine@106 | 28 """ |
amine@106 | 29 if PYTHON_3: |
amine@106 | 30 return a.tobytes() |
amine@106 | 31 else: |
amine@106 | 32 return a.tostring() |