Run the hotword detected sound in another thread, or it's likely to mess up with the timing of the hotword-assistant interaction

This commit is contained in:
Fabio Manganiello 2019-07-12 17:44:59 +02:00
parent fe96940744
commit 2999e47a0f

View file

@ -6,6 +6,7 @@
import json import json
import os import os
import subprocess import subprocess
import threading
import time import time
from platypush.backend import Backend from platypush.backend import Backend
@ -110,9 +111,11 @@ class AssistantSnowboyBackend(Backend):
raise AttributeError('No voice_model_file specified for model {}'.format(name)) raise AttributeError('No voice_model_file specified for model {}'.format(name))
model_file = os.path.abspath(os.path.expanduser(model_file)) 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') 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): if not os.path.isfile(model_file):
raise FileNotFoundError('Voice model file {} does not exist or it not a regular file'.format(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 Callback called on hotword detection
""" """
def callback():
try: try:
import snowboydecoder import snowboydecoder
except ImportError: except ImportError:
import snowboy.snowboydecoder as snowboydecoder import snowboy.snowboydecoder as snowboydecoder
def sound_thread(sound):
snowboydecoder.play_audio_file(sound)
def callback():
self.bus.post(HotwordDetectedEvent(hotword=hotword)) self.bus.post(HotwordDetectedEvent(hotword=hotword))
model = self.models[hotword] model = self.models[hotword]
@ -143,7 +149,7 @@ class AssistantSnowboyBackend(Backend):
assistant_language = model.get('assistant_language') assistant_language = model.get('assistant_language')
if detect_sound: if detect_sound:
snowboydecoder.play_audio_file(detect_sound) threading.Thread(target=sound_thread, args=(detect_sound,)).start()
if assistant_plugin: if assistant_plugin:
assistant_plugin.start_conversation(language=assistant_language) assistant_plugin.start_conversation(language=assistant_language)
@ -158,7 +164,7 @@ class AssistantSnowboyBackend(Backend):
def run(self): def run(self):
super().run() super().run()
self.detector.start(detected_callback=[ self.detector.start(detected_callback=[
lambda: self.hotword_detected(hotword) self.hotword_detected(hotword)
for hotword in self.models.keys() for hotword in self.models.keys()
]) ])