From 2999e47a0fe65d7fcfc6c243aafdc9e4ddef5886 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 12 Jul 2019 17:44:59 +0200 Subject: [PATCH] Run the hotword detected sound in another thread, or it's likely to mess up with the timing of the hotword-assistant interaction --- .../backend/assistant/snowboy/__init__.py | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/platypush/backend/assistant/snowboy/__init__.py b/platypush/backend/assistant/snowboy/__init__.py index 894f52648..1645d331a 100644 --- a/platypush/backend/assistant/snowboy/__init__.py +++ b/platypush/backend/assistant/snowboy/__init__.py @@ -6,6 +6,7 @@ import json import os import subprocess +import threading import time from platypush.backend import Backend @@ -110,9 +111,11 @@ class AssistantSnowboyBackend(Backend): raise AttributeError('No voice_model_file specified for model {}'.format(name)) model_file = os.path.abspath(os.path.expanduser(model_file)) - detect_sound = os.path.abspath(os.path.expanduser(detect_sound)) assistant_plugin_name = conf.get('assistant_plugin') + if detect_sound: + detect_sound = os.path.abspath(os.path.expanduser(detect_sound)) + if not os.path.isfile(model_file): raise FileNotFoundError('Voice model file {} does not exist or it not a regular file'.format(model_file)) @@ -128,13 +131,16 @@ class AssistantSnowboyBackend(Backend): """ Callback called on hotword detection """ + try: + import snowboydecoder + except ImportError: + import snowboy.snowboydecoder as snowboydecoder + + + def sound_thread(sound): + snowboydecoder.play_audio_file(sound) def callback(): - try: - import snowboydecoder - except ImportError: - import snowboy.snowboydecoder as snowboydecoder - self.bus.post(HotwordDetectedEvent(hotword=hotword)) model = self.models[hotword] @@ -143,7 +149,7 @@ class AssistantSnowboyBackend(Backend): assistant_language = model.get('assistant_language') if detect_sound: - snowboydecoder.play_audio_file(detect_sound) + threading.Thread(target=sound_thread, args=(detect_sound,)).start() if assistant_plugin: assistant_plugin.start_conversation(language=assistant_language) @@ -158,7 +164,7 @@ class AssistantSnowboyBackend(Backend): def run(self): super().run() self.detector.start(detected_callback=[ - lambda: self.hotword_detected(hotword) + self.hotword_detected(hotword) for hotword in self.models.keys() ])