diff --git a/amodem/calib.py b/amodem/calib.py index 299f5fb..44fd784 100644 --- a/amodem/calib.py +++ b/amodem/calib.py @@ -8,6 +8,7 @@ log = logging.getLogger(__name__) from . import common from . import dsp from . import sampling +from . import stream def volume_controller(cmd): @@ -110,7 +111,7 @@ def iter_window(iterable, size): yield block -def recv(config, src, verbose=False, volume_cmd=None): +def recv(config, src, verbose=False, volume_cmd=None, dump_audio=None): fmt = '{0.freq:6.0f} Hz: {0.msg:20s}' if verbose: fields = ['total', 'rms', 'coherency', 'peak'] @@ -118,6 +119,8 @@ def recv(config, src, verbose=False, volume_cmd=None): volume_ctl = volume_controller(volume_cmd) + if dump_audio: + src = stream.Dumper(src, dump_audio) result_iterator = detector(config=config, src=src) result_iterator = volume_calibration(result_iterator, volume_ctl) result_iterator = iter_window(result_iterator, size=3) diff --git a/amodem/recv.py b/amodem/recv.py index 5815b34..71f5afb 100644 --- a/amodem/recv.py +++ b/amodem/recv.py @@ -194,20 +194,9 @@ class Receiver(object): self.plt.title(title) -class Dumper(object): - def __init__(self, src, dst): - self.src = src - self.dst = dst - - def read(self, size): - data = self.src.read(size) - self.dst.write(data) - return data - - def main(config, src, dst, dump_audio=None, pylab=None): if dump_audio: - src = Dumper(src, dump_audio) + src = stream.Dumper(src, dump_audio) reader = stream.Reader(src, data_type=common.loads) signal = itertools.chain.from_iterable(reader) diff --git a/amodem/stream.py b/amodem/stream.py index ecadd0f..f6e6d14 100644 --- a/amodem/stream.py +++ b/amodem/stream.py @@ -43,3 +43,14 @@ class Reader(object): raise IOError('timeout') __next__ = next + + +class Dumper(object): + def __init__(self, src, dst): + self.src = src + self.dst = dst + + def read(self, size): + data = self.src.read(size) + self.dst.write(data) + return data diff --git a/tests/test_calib.py b/tests/test_calib.py index 31a9649..f64b42c 100644 --- a/tests/test_calib.py +++ b/tests/test_calib.py @@ -144,8 +144,10 @@ def test_recv_binary_search(): calib.send(config, buf, gain=gain, limit=2) buf.seek(0) + dump = BytesIO() with mock.patch('subprocess.check_call') as check_call: - calib.recv(config, src=buf, volume_cmd='ctl') + calib.recv(config, src=buf, volume_cmd='ctl', dump_audio=dump) + assert dump.getvalue() == buf.getvalue() gains.append(gains[-1]) fmt = 'ctl {0:.0f}%'