Better synchronization mechanism between push-to-talk and platypush when an event is matched and no assistant playback should follow up

This commit is contained in:
Fabio Manganiello 2019-07-12 18:11:14 +02:00
parent 2999e47a0f
commit bb3a39acb9
3 changed files with 20 additions and 15 deletions

View file

@ -22,7 +22,7 @@ class AssistantEvent(Event):
self._assistant = get_plugin('assistant.google.pushtotalk') self._assistant = get_plugin('assistant.google.pushtotalk')
if not self._assistant: if not self._assistant:
logger.warning('google.assistant not configured/initialized') logger.warning('Assistant plugin/backend not configured/initialized')
self._assistant = None self._assistant = None
class ConversationStartEvent(AssistantEvent): class ConversationStartEvent(AssistantEvent):
@ -85,13 +85,13 @@ class SpeechRecognizedEvent(AssistantEvent):
Event triggered when a speech is recognized Event triggered when a speech is recognized
""" """
def __init__(self, phrase, *args, assistant=None, **kwargs): def __init__(self, phrase, *args, **kwargs):
""" """
:param phrase: Recognized user phrase :param phrase: Recognized user phrase
:type phrase: str :type phrase: str
""" """
super().__init__(*args, phrase=phrase, assistant=assistant, **kwargs) super().__init__(*args, phrase=phrase, **kwargs)
self.recognized_phrase = phrase.strip().lower() self.recognized_phrase = phrase.strip().lower()
if not self._assistant and assistant: if not self._assistant and assistant:
@ -106,9 +106,7 @@ class SpeechRecognizedEvent(AssistantEvent):
result = super().matches_condition(condition) result = super().matches_condition(condition)
if result.is_match and self._assistant and 'phrase' in condition.args: if result.is_match and self._assistant and 'phrase' in condition.args:
if hasattr(self._assistant, 'play_response'): if self._assistant:
self._assistant.play_response = False
elif hasattr(self._assistant, 'stop_conversation'):
self._assistant.stop_conversation() self._assistant.stop_conversation()
return result return result

View file

@ -115,6 +115,7 @@ class SampleAssistant(object):
if self.on_conversation_start: if self.on_conversation_start:
self.on_conversation_start() self.on_conversation_start()
self.play_response = True
logging.info('Recording audio request.') logging.info('Recording audio request.')
def iter_log_assist_requests(): def iter_log_assist_requests():
@ -132,7 +133,6 @@ class SampleAssistant(object):
logging.info('End of audio request detected.') logging.info('End of audio request detected.')
logging.info('Stopping recording.') logging.info('Stopping recording.')
self.conversation_stream.stop_recording() self.conversation_stream.stop_recording()
self.play_response = True
if self.detected_speech and self.on_speech_recognized: if self.detected_speech and self.on_speech_recognized:
self.on_speech_recognized(self.detected_speech) self.on_speech_recognized(self.detected_speech)
@ -151,8 +151,10 @@ class SampleAssistant(object):
self.conversation_stream.start_playback() self.conversation_stream.start_playback()
logging.info('Playing assistant response.') logging.info('Playing assistant response.')
if self.play_response: if self.play_response and self.conversation_stream.playing:
self.conversation_stream.write(resp.audio_out.audio_data) self.conversation_stream.write(resp.audio_out.audio_data)
elif self.conversation_stream.playing:
self.conversation_stream.stop_playback()
if resp.dialog_state_out.conversation_state: if resp.dialog_state_out.conversation_state:
conversation_state = resp.dialog_state_out.conversation_state conversation_state = resp.dialog_state_out.conversation_state
logging.debug('Updating conversation state.') logging.debug('Updating conversation state.')

View file

@ -146,21 +146,21 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
def on_conversation_start(self): def on_conversation_start(self):
""" Conversation start handler """ """ Conversation start handler """
def handler(): def handler():
get_bus().post(ConversationStartEvent(assistant=self.assistant)) get_bus().post(ConversationStartEvent())
return handler return handler
def on_conversation_end(self): def on_conversation_end(self):
""" Conversation end handler """ """ Conversation end handler """
def handler(with_follow_on_turn): def handler(with_follow_on_turn):
get_bus().post(ConversationEndEvent(with_follow_on_turn=with_follow_on_turn, assistant=self.assistant)) get_bus().post(ConversationEndEvent(with_follow_on_turn=with_follow_on_turn))
return handler return handler
def on_speech_recognized(self): def on_speech_recognized(self):
""" Speech recognized handler """ """ Speech recognized handler """
def handler(phrase): def handler(phrase):
get_bus().post(SpeechRecognizedEvent(phrase=phrase, assistant=self.assistant)) get_bus().post(SpeechRecognizedEvent(phrase=phrase))
self.interactions.append({'request': phrase}) self.interactions.append({'request': phrase})
return handler return handler
@ -168,14 +168,14 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
def on_volume_changed(self): def on_volume_changed(self):
""" Volume changed event """ """ Volume changed event """
def handler(volume): def handler(volume):
get_bus().post(VolumeChangedEvent(volume=volume, assistant=self.assistant)) get_bus().post(VolumeChangedEvent(volume=volume))
return handler return handler
def on_response(self): def on_response(self):
""" Response handler """ """ Response handler """
def handler(response): def handler(response):
get_bus().post(ResponseEvent(response_text=response, assistant=self.assistant)) get_bus().post(ResponseEvent(response_text=response))
if not self.interactions: if not self.interactions:
self.interactions.append({'response': response}) self.interactions.append({'response': response})
@ -243,8 +243,13 @@ class AssistantGooglePushtotalkPlugin(AssistantPlugin):
def stop_conversation(self): def stop_conversation(self):
""" Stop a conversation """ """ Stop a conversation """
if self.assistant: if self.assistant:
self.conversation_stream.stop_playback() self.assistant.play_response = False
get_bus().post(ConversationEndEvent(assistant=self.assistant))
if self.conversation_stream:
self.conversation_stream.stop_playback()
self.conversation_stream.stop_recording()
get_bus().post(ConversationEndEvent())
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: