From 2bb4956439cdc2625aa0c0952e3609ac6e5fc557 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 10 Jan 2015 11:55:12 +0200 Subject: [PATCH] audio: fix GetDefault???Device API call --- amodem/audio.py | 8 +++++++- tests/test_audio.py | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/amodem/audio.py b/amodem/audio.py index bf91aa5..76c03f0 100644 --- a/amodem/audio.py +++ b/amodem/audio.py @@ -62,7 +62,13 @@ class Stream(object): 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) + read = bool(read) + write = bool(write) + assert read != write + + direction = 'Input' if read else 'Output' + api_name = 'GetDefault{0}Device'.format(direction) + index = lib.call(api_name, restype=ctypes.c_int) self.params = Stream.Parameters( device=index, # choose default device channelCount=1, # mono audio diff --git a/tests/test_audio.py b/tests/test_audio.py index 1372365..b546409 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -10,7 +10,8 @@ def test(): with mock.patch('ctypes.CDLL') as cdll: lib = mock.Mock() lib.Pa_GetErrorText = lambda code: 'Error' if code else 'Success' - lib.Pa_GetDefaultInputDevice.return_value = 1 + lib.Pa_GetDefaultOutputDevice.return_value = 1 + lib.Pa_GetDefaultInputDevice.return_value = 2 lib.Pa_OpenStream.return_value = 0 cdll.return_value = lib interface = audio.Interface( @@ -18,12 +19,14 @@ def test(): ) with interface: s = interface.player() + assert s.params.device == 1 s.stream = 1 # simulate non-zero output stream handle s.write(data=data) s.close() with interface: s = interface.recorder() + assert s.params.device == 2 s.stream = 2 # simulate non-zero input stream handle s.read(len(data)) s.close()