# Copyright 2016 Mycroft AI, Inc. # # This file is part of Mycroft Core. # # Mycroft Core is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Mycroft Core is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Mycroft Core. If not, see . import re import sys from threading import Thread, Lock from mycroft.client.enclosure.api import EnclosureAPI from mycroft.client.speech.listener import RecognizerLoop from mycroft.configuration import ConfigurationManager from mycroft.identity import IdentityManager from mycroft.messagebus.client.ws import WebsocketClient from mycroft.messagebus.message import Message from mycroft.tts import TTSFactory from mycroft.util import kill, play_wav, resolve_resource_file, create_signal from mycroft.util.log import getLogger logger = getLogger("SpeechClient") ws = None tts = TTSFactory.create() lock = Lock() loop = None config = ConfigurationManager.get() def handle_record_begin(): logger.info("Begin Recording...") # If enabled, play a wave file with a short sound to audibly # indicate recording has begun. if config.get('confirm_listening'): file = resolve_resource_file( config.get('sounds').get('start_listening')) if file: play_wav(file) ws.emit(Message('recognizer_loop:record_begin')) def handle_record_end(): logger.info("End Recording...") ws.emit(Message('recognizer_loop:record_end')) def handle_wakeword(event): logger.info("Wakeword Detected: " + event['utterance']) ws.emit(Message('recognizer_loop:wakeword', event)) def handle_utterance(event): logger.info("Utterance: " + str(event['utterances'])) ws.emit(Message('recognizer_loop:utterance', event)) def mute_and_speak(utterance): lock.acquire() ws.emit(Message("recognizer_loop:audio_output_start")) try: logger.info("Speak: " + utterance) loop.mute() tts.execute(utterance) finally: loop.unmute() lock.release() ws.emit(Message("recognizer_loop:audio_output_end")) def handle_multi_utterance_intent_failure(event): logger.info("Failed to find intent on multiple intents.") # TODO: Localize mute_and_speak("Sorry, I didn't catch that. Please rephrase your request.") def handle_speak(event): utterance = event.data['utterance'] expect_response = event.data.get('expect_response', False) # This is a bit of a hack for Picroft. The analog audio on a Pi blocks # for 30 seconds fairly often, so we don't want to break on periods # (decreasing the chance of encountering the block). But we will # keep the split for non-Picroft installs since it give user feedback # faster on longer phrases. # # TODO: Remove or make an option? This is really a hack, anyway, # so we likely will want to get rid of this when not running on Mimic if not config.get('enclosure', {}).get('platform') == "picroft": chunks = re.split(r'(?