Better sound release logic

This commit is contained in:
Fabio Manganiello 2018-12-27 01:10:45 +01:00
parent c445763979
commit 56ff58d06b

View file

@ -155,7 +155,7 @@ class SoundPlugin(Plugin):
data = q.get_nowait() data = q.get_nowait()
except queue.Empty: except queue.Empty:
self.logger.warning('Buffer is empty: increase buffersize?') self.logger.warning('Buffer is empty: increase buffersize?')
raise sd.CallbackAbort return
if len(data) < len(outdata): if len(data) < len(outdata):
outdata[:len(data)] = data outdata[:len(data)] = data
@ -252,12 +252,13 @@ class SoundPlugin(Plugin):
if not channels: if not channels:
channels = f.channels if f else 1 channels = f.channels if f else 1
self.logger.info('Starting playback of {} to sound device [{}]'.
format(file or sound, device))
if is_new_stream: if is_new_stream:
stream_index = self._allocate_stream_index() stream_index = self._allocate_stream_index()
self.logger.info(('Starting playback of {} to sound device [{}] ' +
'on stream [{}]').format(
file or sound, device, stream_index))
if sound: if sound:
mix = self.stream_mixes[stream_index] mix = self.stream_mixes[stream_index]
mix.add(sound) mix.add(sound)
@ -707,18 +708,20 @@ class SoundPlugin(Plugin):
self.recording_paused_changed.set() self.recording_paused_changed.set()
@action @action
def release(self, stream, index=None, midi_note=None, frequency=None): def release(self, stream_index=None,
sound_index=None, midi_note=None, frequency=None):
""" """
Remove a sound from an active stream, either by sound index (use Remove a sound from an active stream, either by sound index (use
:method:`platypush.sound.plugin.SoundPlugin.query_streams` to get :method:`platypush.sound.plugin.SoundPlugin.query_streams` to get
the sounds playing on the active streams), midi_note, frequency the sounds playing on the active streams), midi_note, frequency
or absolute file path. or absolute file path.
:param stream: Stream index :param stream_index: Stream index (default: sound removed from all the
:type stream: int active streams)
:type stream_index: int
:param index: Sound index :param sound_index: Sound index
:type index: int :type sound_index: int
:param midi_note: MIDI note :param midi_note: MIDI note
:type midi_note: int :type midi_note: int
@ -727,25 +730,33 @@ class SoundPlugin(Plugin):
:type frequency: float :type frequency: float
""" """
if index is None and midi_note is None and frequency is None: if sound_index is None and midi_note is None and frequency is None:
raise RuntimeError('Please specify either a sound index, ' + raise RuntimeError('Please specify either a sound index, ' +
'midi_note or frequency to release') 'midi_note or frequency to release')
mix = self.stream_mixes.get(stream) mixes = {
if not mix: i: mix for i, mix in self.stream_mixes.items()
raise RuntimeError('No such stream: {}'.format(stream)) } if stream_index is None else {
stream_index: self.stream_mixes[stream_index]
}
for i, sound in enumerate(mix): streams_to_stop = []
if (index is not None and i == index) or \
(midi_note is not None for i, mix in mixes.items():
and sound.get('midi_note') == midi_note) or \ for j, sound in enumerate(mix):
(frequency is not None if (sound_index is not None and j == sound_index) or \
and sound.get('frequency') == frequency): (midi_note is not None
if len(list(mix)) == 1: and sound.get('midi_note') == midi_note) or \
# Last sound in the mix (frequency is not None
self.stop_playback([stream]) and sound.get('frequency') == frequency):
else: if len(list(mix)) == 1:
mix.remove(i) # Last sound in the mix
streams_to_stop.append(i)
else:
mix.remove(j)
if streams_to_stop:
self.stop_playback(streams_to_stop)
def _get_playback_state(self, stream_index): def _get_playback_state(self, stream_index):
with self.playback_state_lock: with self.playback_state_lock: