From 00dacc456c4fc50e936ae32044febdeced99521e Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 21 Dec 2018 16:52:47 +0100 Subject: [PATCH] A more robust logic for getting the default sound devices --- platypush/plugins/sound.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/platypush/plugins/sound.py b/platypush/plugins/sound.py index 928d8f50..ae65aab9 100644 --- a/platypush/plugins/sound.py +++ b/platypush/plugins/sound.py @@ -62,8 +62,8 @@ class SoundPlugin(Plugin): super().__init__(*args, **kwargs) - self.input_device = input_device or 0 - self.output_device = output_device or 0 + self.input_device = input_device + self.output_device = output_device self.input_blocksize = input_blocksize self.output_blocksize = output_blocksize self.playback_bufsize = playback_bufsize @@ -75,6 +75,17 @@ class SoundPlugin(Plugin): self.recording_state_lock = RLock() self.recording_paused_changed = Event() + def _get_default_device(self, category): + """ + Query the default audio devices. + + :param category: Device category to query. Can be either input or output + :type category: str + """ + + import sounddevice as sd + return sd.query_hostapis()[0].get('default_' + category.lower() + '_device') + @action def query_devices(self, category=None): """ @@ -158,6 +169,8 @@ class SoundPlugin(Plugin): if device is None: device = self.output_device + if device is None: + device = self._get_default_device('output') def audio_callback(outdata, frames, time, status): if self._get_playback_state() == PlaybackState.STOPPED: @@ -288,6 +301,8 @@ class SoundPlugin(Plugin): if device is None: device = self.input_device + if device is None: + device = self._get_default_device('input') if sample_rate is None: dev_info = sd.query_devices(device, 'input') @@ -394,9 +409,13 @@ class SoundPlugin(Plugin): if input_device is None: input_device = self.input_device + if input_device is None: + input_device = self._get_default_device('input') if output_device is None: output_device = self.output_device + if output_device is None: + output_device = self._get_default_device('output') if sample_rate is None: dev_info = sd.query_devices(input_device, 'input')