From f086bbfdeb150dd0f89d297c882a2663f71a65fb Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Tue, 6 Jan 2015 18:01:48 +0200 Subject: [PATCH] audio: use specified config --- amodem-cli | 2 +- amodem/audio.py | 37 ++++++++++++++++--------------------- amodem/config.py | 2 +- tests/test_audio.py | 4 ++-- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/amodem-cli b/amodem-cli index 657373d..240eea0 100755 --- a/amodem-cli +++ b/amodem-cli @@ -55,7 +55,7 @@ def main(): 'Fs={3:.1f} kHz') description = fmt.format(config.modem_bps / 1e3, len(config.symbols), config.Nfreq, config.Fs / 1e3) - interface = audio.Library('libportaudio.so') + interface = audio.Interface('libportaudio.so', config=config) p = argparse.ArgumentParser(description=description) subparsers = p.add_subparsers() diff --git a/amodem/audio.py b/amodem/audio.py index 55eb710..f9972b1 100644 --- a/amodem/audio.py +++ b/amodem/audio.py @@ -4,9 +4,10 @@ import logging log = logging.getLogger(__name__) -class Library(object): - def __init__(self, name): +class Interface(object): + def __init__(self, name, config): self.lib = ctypes.CDLL(name) + self.config = config self.streams = [] assert self._error_string(0) == 'Success' @@ -32,10 +33,10 @@ class Library(object): self.call('Terminate') def recorder(self): - return Stream(self, read=True) + return Stream(lib=self, config=self.config, read=True) def player(self): - return Stream(self, write=True) + return Stream(lib=self, config=self.config, write=True) class Stream(object): @@ -46,29 +47,23 @@ class Stream(object): ('channelCount', ctypes.c_int), ('sampleFormat', ctypes.c_ulong), ('suggestedLatency', ctypes.c_double), - ('hostApiSpecificStreamInfo', ctypes.POINTER(None)), + ('hostApiSpecificStreamInfo', ctypes.POINTER(None)) ] - sample_rate = 32000.0 - frames_per_buffer = 4096 - suggested_latency = 0.1 - channel_count = 1 - sample_format = 0x00000008 # 16-bit samples (paInt16) - bytes_per_sample = 2 - flags = 0 # no flags (paNoFlag) - - def __init__(self, lib, read=False, write=False): + def __init__(self, lib, config, read=False, write=False): self.lib = lib self.stream = ctypes.POINTER(ctypes.c_void_p)() self.user_data = ctypes.c_void_p(None) self.stream_callback = ctypes.c_void_p(None) + self.bytes_per_sample = config.sample_size + assert config.bits_per_sample == 16 # just to make sure :) index = lib.call('GetDefaultInputDevice', restype=ctypes.c_int) self.params = Stream.Parameters( - device=index, - channelCount=self.channel_count, - sampleFormat=self.sample_format, - suggestedLatency=self.suggested_latency, + device=index, # choose default device + channelCount=1, # mono audio + sampleFormat=0x00000008, # 16-bit samples (paInt16) + suggestedLatency=0.1, # 100ms should be good enough hostApiSpecificStreamInfo=None) self.lib.call( @@ -76,9 +71,9 @@ class Stream(object): ctypes.byref(self.stream), ctypes.byref(self.params) if read else None, ctypes.byref(self.params) if write else None, - ctypes.c_double(self.sample_rate), - ctypes.c_ulong(self.frames_per_buffer), - ctypes.c_ulong(self.flags), + ctypes.c_double(config.Fs), + ctypes.c_ulong(config.samples_per_buffer), + ctypes.c_ulong(0), # no flags (paNoFlag) self.stream_callback, self.user_data) diff --git a/amodem/config.py b/amodem/config.py index 9efbd2b..286369d 100644 --- a/amodem/config.py +++ b/amodem/config.py @@ -11,7 +11,7 @@ class Configuration(object): # audio config bits_per_sample = 16 sample_size = bits_per_sample // 8 - samples_per_buffer = 1024 + samples_per_buffer = 4096 # sender config silence_start = 1.0 diff --git a/tests/test_audio.py b/tests/test_audio.py index e072845..c0d54a1 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -1,4 +1,4 @@ -from amodem import audio +from amodem import audio, config import mock import pytest @@ -13,7 +13,7 @@ def test(): lib.Pa_GetDefaultInputDevice.return_value = 1 lib.Pa_OpenStream.return_value = 0 cdll.return_value = lib - interface = audio.Library('portaudio') + interface = audio.Interface(name='portaudio', config=config.fastest()) with interface: s = interface.player() s.stream = 1 # simulate non-zero output stream handle