diff --git a/amodem-cli b/amodem-cli index ed3a0d7..1cad6ae 100755 --- a/amodem-cli +++ b/amodem-cli @@ -96,7 +96,7 @@ def main(): 'Fs={3:.1f} kHz') description = fmt.format(config.modem_bps / 1e3, len(config.symbols), config.Nfreq, config.Fs / 1e3) - interface = audio.Interface('libportaudio.so', config=config) + interface = audio.Interface(config=config) p = argparse.ArgumentParser(description=description) subparsers = p.add_subparsers() @@ -158,6 +158,8 @@ def main(): for sub in subparsers.choices.values(): sub.add_argument('-c', '--calibrate', nargs='?', default=False, metavar='SYSTEM', help=calibration_help) + sub.add_argument('-l', '--audio-library', default='libportaudio.so', + help='File name of PortAudio shared library.') sub.add_argument('-z', '--zip', default=False, action='store_true', help='Use ZIP to compress data.') g = sub.add_mutually_exclusive_group() @@ -167,29 +169,28 @@ def main(): if argcomplete: argcomplete.autocomplete(p) - with interface: - args = p.parse_args() - if args.verbose == 0: - level, format = 'INFO', '%(message)s' - elif args.verbose == 1: - level, format = 'DEBUG', '%(message)s' - elif args.verbose >= 2: - level, format = ('DEBUG', '%(asctime)s %(levelname)-10s ' - '%(message)-100s ' - '%(filename)s:%(lineno)d') - if args.quiet: - level, format = 'WARNING', '%(message)s' - logging.basicConfig(level=level, format=format) - log.info('Interface "%s" loaded succesfully', interface.version) + args = p.parse_args() + if args.verbose == 0: + level, format = 'INFO', '%(message)s' + elif args.verbose == 1: + level, format = 'DEBUG', '%(message)s' + elif args.verbose >= 2: + level, format = ('DEBUG', '%(asctime)s %(levelname)-10s ' + '%(message)-100s ' + '%(filename)s:%(lineno)d') + if args.quiet: + level, format = 'WARNING', '%(message)s' + logging.basicConfig(level=level, format=format) - # Parsing and execution - log.debug(description) + # Parsing and execution + log.debug(description) - args.pylab = None - if getattr(args, 'plot', False): - import pylab - args.pylab = pylab + args.pylab = None + if getattr(args, 'plot', False): + import pylab + args.pylab = pylab + with interface.load(args.audio_library): args.src = args.input_type(args.input) args.dst = args.output_type(args.output) if args.calibrate is False: diff --git a/amodem/audio.py b/amodem/audio.py index 742aff2..895e6f1 100644 --- a/amodem/audio.py +++ b/amodem/audio.py @@ -5,18 +5,24 @@ log = logging.getLogger(__name__) class Interface(object): - def __init__(self, name, config, debug=False): + def __init__(self, config, debug=False): self.debug = bool(debug) - self.lib = ctypes.CDLL(name) self.config = config self.streams = [] + self.lib = None + + def load(self, name): + self.lib = ctypes.CDLL(name) assert self._error_string(0) == b'Success' - self.version = self.call('GetVersionText', restype=ctypes.c_char_p) + version = self.call('GetVersionText', restype=ctypes.c_char_p) + log.info('%s loaded', version) + return self def _error_string(self, code): return self.call('GetErrorText', code, restype=ctypes.c_char_p) def call(self, name, *args, **kwargs): + assert self.lib is not None func_name = 'Pa_{0}'.format(name) if self.debug: log.debug('API: %s%s', name, args) diff --git a/tests/test_audio.py b/tests/test_audio.py index b546409..9666cbf 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -14,10 +14,8 @@ def test(): lib.Pa_GetDefaultInputDevice.return_value = 2 lib.Pa_OpenStream.return_value = 0 cdll.return_value = lib - interface = audio.Interface( - name='portaudio', config=config.fastest(), debug=True - ) - with interface: + interface = audio.Interface(config=config.fastest(), debug=True) + with interface.load(name='portaudio'): s = interface.player() assert s.params.device == 1 s.stream = 1 # simulate non-zero output stream handle