2017-10-04 06:28:44 +00:00
|
|
|
# Copyright 2017 Mycroft AI Inc.
|
2016-05-26 16:16:13 +00:00
|
|
|
#
|
2017-10-04 06:28:44 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2016-05-26 16:16:13 +00:00
|
|
|
#
|
2017-10-04 06:28:44 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-26 16:16:13 +00:00
|
|
|
#
|
2017-10-04 06:28:44 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2016-05-26 16:16:13 +00:00
|
|
|
#
|
2017-09-18 19:14:21 +00:00
|
|
|
import hashlib
|
2016-09-17 02:08:53 +00:00
|
|
|
import random
|
2017-07-07 14:08:28 +00:00
|
|
|
from Queue import Queue, Empty
|
2017-09-18 19:14:21 +00:00
|
|
|
from threading import Thread
|
2017-06-04 06:07:37 +00:00
|
|
|
from time import time, sleep
|
2017-09-18 19:14:21 +00:00
|
|
|
|
2017-06-05 08:31:31 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
2017-09-18 19:14:21 +00:00
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
from os.path import dirname, exists, isdir
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2017-09-18 19:14:21 +00:00
|
|
|
import mycroft.util
|
2016-09-17 02:08:53 +00:00
|
|
|
from mycroft.client.enclosure.api import EnclosureAPI
|
2017-09-23 12:13:50 +00:00
|
|
|
from mycroft.configuration import Configuration
|
2017-07-06 21:02:40 +00:00
|
|
|
from mycroft.messagebus.message import Message
|
2017-09-12 09:45:04 +00:00
|
|
|
from mycroft.util import play_wav, play_mp3, check_for_signal, create_signal
|
2017-09-18 19:14:21 +00:00
|
|
|
from mycroft.util.log import LOG
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
|
2017-06-04 06:07:37 +00:00
|
|
|
class PlaybackThread(Thread):
|
2017-06-05 08:31:31 +00:00
|
|
|
"""
|
|
|
|
Thread class for playing back tts audio and sending
|
|
|
|
visime data to enclosure.
|
|
|
|
"""
|
|
|
|
|
2017-06-04 06:07:37 +00:00
|
|
|
def __init__(self, queue):
|
|
|
|
super(PlaybackThread, self).__init__()
|
|
|
|
self.queue = queue
|
|
|
|
self._terminated = False
|
2017-07-07 14:08:28 +00:00
|
|
|
self._processing_queue = False
|
2017-08-03 08:57:14 +00:00
|
|
|
self._clear_visimes = False
|
2017-06-04 06:07:37 +00:00
|
|
|
|
2017-07-06 21:02:40 +00:00
|
|
|
def init(self, tts):
|
|
|
|
self.tts = tts
|
|
|
|
|
2017-06-05 20:23:17 +00:00
|
|
|
def clear_queue(self):
|
|
|
|
"""
|
|
|
|
Remove all pending playbacks.
|
|
|
|
"""
|
|
|
|
while not self.queue.empty():
|
|
|
|
self.queue.get()
|
2017-07-06 09:49:47 +00:00
|
|
|
try:
|
|
|
|
self.p.terminate()
|
|
|
|
except:
|
|
|
|
pass
|
2017-06-05 20:23:17 +00:00
|
|
|
|
2017-06-04 06:07:37 +00:00
|
|
|
def run(self):
|
2017-06-05 08:31:31 +00:00
|
|
|
"""
|
|
|
|
Thread main loop. get audio and visime data from queue
|
|
|
|
and play.
|
|
|
|
"""
|
2017-06-04 06:07:37 +00:00
|
|
|
while not self._terminated:
|
|
|
|
try:
|
|
|
|
snd_type, data, visimes = self.queue.get(timeout=2)
|
|
|
|
self.blink(0.5)
|
2017-07-07 14:08:28 +00:00
|
|
|
if not self._processing_queue:
|
|
|
|
self._processing_queue = True
|
|
|
|
self.tts.begin_audio()
|
|
|
|
|
2017-06-04 06:07:37 +00:00
|
|
|
if snd_type == 'wav':
|
2017-07-06 09:49:47 +00:00
|
|
|
self.p = play_wav(data)
|
2017-06-04 06:07:37 +00:00
|
|
|
elif snd_type == 'mp3':
|
2017-07-06 09:49:47 +00:00
|
|
|
self.p = play_mp3(data)
|
2017-06-04 06:07:37 +00:00
|
|
|
|
|
|
|
if visimes:
|
2017-06-05 20:23:17 +00:00
|
|
|
if self.show_visimes(visimes):
|
|
|
|
self.clear_queue()
|
2017-07-06 09:49:47 +00:00
|
|
|
else:
|
|
|
|
self.p.communicate()
|
2017-07-06 21:02:40 +00:00
|
|
|
self.p.wait()
|
2017-07-07 14:08:28 +00:00
|
|
|
|
|
|
|
if self.queue.empty():
|
2017-07-06 22:17:25 +00:00
|
|
|
self.tts.end_audio()
|
2017-07-07 14:08:28 +00:00
|
|
|
self._processing_queue = False
|
2017-06-04 06:07:37 +00:00
|
|
|
self.blink(0.2)
|
2017-07-07 14:08:28 +00:00
|
|
|
except Empty:
|
2017-06-04 06:07:37 +00:00
|
|
|
pass
|
2017-07-07 14:08:28 +00:00
|
|
|
except Exception, e:
|
2017-09-18 18:55:58 +00:00
|
|
|
LOG.exception(e)
|
2017-07-07 14:08:28 +00:00
|
|
|
if self._processing_queue:
|
|
|
|
self.tts.end_audio()
|
|
|
|
self._processing_queue = False
|
2017-06-04 06:07:37 +00:00
|
|
|
|
|
|
|
def show_visimes(self, pairs):
|
2017-06-05 08:31:31 +00:00
|
|
|
"""
|
|
|
|
Send visime data to enclosure
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pairs(list): Visime and timing pair
|
2017-06-05 20:23:17 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if button has been pressed.
|
2017-06-05 08:31:31 +00:00
|
|
|
"""
|
2017-06-04 06:07:37 +00:00
|
|
|
start = time()
|
|
|
|
for code, duration in pairs:
|
2017-08-03 08:57:14 +00:00
|
|
|
if self._clear_visimes:
|
|
|
|
self._clear_visimes = False
|
2017-06-05 20:23:17 +00:00
|
|
|
return True
|
2017-06-04 06:07:37 +00:00
|
|
|
if self.enclosure:
|
2017-11-21 03:22:48 +00:00
|
|
|
# Include time stamp to assist with animation timing
|
|
|
|
self.enclosure.mouth_viseme(code, start+duration)
|
2017-06-04 06:07:37 +00:00
|
|
|
delta = time() - start
|
|
|
|
if delta < duration:
|
|
|
|
sleep(duration - delta)
|
2017-06-05 20:23:17 +00:00
|
|
|
return False
|
2017-06-04 06:07:37 +00:00
|
|
|
|
2017-08-03 08:57:14 +00:00
|
|
|
def clear_visimes(self):
|
|
|
|
self._clear_visimes = True
|
|
|
|
|
2017-06-04 06:07:37 +00:00
|
|
|
def blink(self, rate=1.0):
|
2017-06-05 08:31:31 +00:00
|
|
|
""" Blink mycroft's eyes """
|
2017-06-04 06:07:37 +00:00
|
|
|
if self.enclosure and random.random() < rate:
|
|
|
|
self.enclosure.eyes_blink("b")
|
|
|
|
|
|
|
|
def stop(self):
|
2017-06-05 08:31:31 +00:00
|
|
|
""" Stop thread """
|
2017-06-04 06:07:37 +00:00
|
|
|
self._terminated = True
|
2017-06-05 20:23:17 +00:00
|
|
|
self.clear_queue()
|
2017-06-04 06:07:37 +00:00
|
|
|
|
2017-06-05 08:31:31 +00:00
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
class TTS(object):
|
|
|
|
"""
|
|
|
|
TTS abstract class to be implemented by all TTS engines.
|
|
|
|
|
2016-05-20 22:15:53 +00:00
|
|
|
It aggregates the minimum required parameters and exposes
|
|
|
|
``execute(sentence)`` function.
|
2016-05-20 14:16:01 +00:00
|
|
|
"""
|
2016-09-16 18:12:43 +00:00
|
|
|
__metaclass__ = ABCMeta
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
def __init__(self, lang, voice, validator):
|
2016-05-20 14:16:01 +00:00
|
|
|
super(TTS, self).__init__()
|
2017-01-20 21:21:03 +00:00
|
|
|
self.lang = lang or 'en-us'
|
2016-05-20 14:16:01 +00:00
|
|
|
self.voice = voice
|
2016-09-16 18:12:43 +00:00
|
|
|
self.filename = '/tmp/tts.wav'
|
|
|
|
self.validator = validator
|
2017-03-30 07:40:56 +00:00
|
|
|
self.enclosure = None
|
2016-09-17 02:08:53 +00:00
|
|
|
random.seed()
|
2017-06-04 06:07:37 +00:00
|
|
|
self.queue = Queue()
|
|
|
|
self.playback = PlaybackThread(self.queue)
|
|
|
|
self.playback.start()
|
2017-06-05 08:31:31 +00:00
|
|
|
self.clear_cache()
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2017-07-06 21:02:40 +00:00
|
|
|
def begin_audio(self):
|
|
|
|
"""Helper function for child classes to call in execute()"""
|
2017-10-13 06:40:35 +00:00
|
|
|
# Create signals informing start of speech
|
2017-07-06 21:02:40 +00:00
|
|
|
self.ws.emit(Message("recognizer_loop:audio_output_start"))
|
2017-09-12 09:45:04 +00:00
|
|
|
create_signal("isSpeaking")
|
2017-07-06 21:02:40 +00:00
|
|
|
|
|
|
|
def end_audio(self):
|
2017-10-13 06:40:35 +00:00
|
|
|
"""
|
|
|
|
Helper function for child classes to call in execute().
|
|
|
|
|
|
|
|
Sends the recognizer_loop:audio_output_end message, indicating
|
|
|
|
that speaking is done for the moment. It also checks if cache
|
|
|
|
directory needs cleaning to free up disk space.
|
|
|
|
"""
|
|
|
|
|
2017-07-06 21:02:40 +00:00
|
|
|
self.ws.emit(Message("recognizer_loop:audio_output_end"))
|
2017-10-13 06:40:35 +00:00
|
|
|
# Clean the cache as needed
|
|
|
|
cache_dir = mycroft.util.get_cache_directory("tts")
|
|
|
|
mycroft.util.curate_cache(cache_dir, min_free_percent=100)
|
2017-07-06 21:02:40 +00:00
|
|
|
|
2017-09-12 09:45:04 +00:00
|
|
|
# This check will clear the "signal"
|
|
|
|
check_for_signal("isSpeaking")
|
|
|
|
|
2016-12-21 05:15:51 +00:00
|
|
|
def init(self, ws):
|
|
|
|
self.ws = ws
|
2017-07-06 21:02:40 +00:00
|
|
|
self.playback.init(self)
|
2016-12-21 05:15:51 +00:00
|
|
|
self.enclosure = EnclosureAPI(self.ws)
|
2017-06-04 06:07:37 +00:00
|
|
|
self.playback.enclosure = self.enclosure
|
2016-12-21 05:15:51 +00:00
|
|
|
|
2017-06-05 08:31:31 +00:00
|
|
|
def get_tts(self, sentence, wav_file):
|
|
|
|
"""
|
|
|
|
Abstract method that a tts implementation needs to implement.
|
|
|
|
Should get data from tts.
|
2017-03-30 07:40:56 +00:00
|
|
|
|
2017-06-05 08:31:31 +00:00
|
|
|
Args:
|
|
|
|
sentence(str): Sentence to synthesize
|
|
|
|
wav_file(str): output file
|
2017-03-30 07:40:56 +00:00
|
|
|
|
2017-06-05 08:31:31 +00:00
|
|
|
Returns: (wav_file, phoneme) tuple
|
|
|
|
"""
|
2016-05-20 14:16:01 +00:00
|
|
|
pass
|
|
|
|
|
2017-06-05 08:31:31 +00:00
|
|
|
def execute(self, sentence):
|
|
|
|
"""
|
|
|
|
Convert sentence to speech.
|
|
|
|
|
|
|
|
The method caches results if possible using the hash of the
|
|
|
|
sentence.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sentence: Sentence to be spoken
|
|
|
|
"""
|
|
|
|
key = str(hashlib.md5(sentence.encode('utf-8', 'ignore')).hexdigest())
|
|
|
|
wav_file = os.path.join(mycroft.util.get_cache_directory("tts"),
|
2017-07-19 15:21:24 +00:00
|
|
|
key + '.' + self.type)
|
2017-06-05 08:31:31 +00:00
|
|
|
|
|
|
|
if os.path.exists(wav_file):
|
2017-09-18 18:55:58 +00:00
|
|
|
LOG.debug("TTS cache hit")
|
2017-06-05 08:31:31 +00:00
|
|
|
phonemes = self.load_phonemes(key)
|
|
|
|
else:
|
|
|
|
wav_file, phonemes = self.get_tts(sentence, wav_file)
|
|
|
|
if phonemes:
|
|
|
|
self.save_phonemes(key, phonemes)
|
|
|
|
|
|
|
|
self.queue.put((self.type, wav_file, self.visime(phonemes)))
|
|
|
|
|
|
|
|
def visime(self, phonemes):
|
|
|
|
"""
|
|
|
|
Create visimes from phonemes. Needs to be implemented for all
|
|
|
|
tts backend
|
|
|
|
|
|
|
|
Args:
|
|
|
|
phonemes(str): String with phoneme data
|
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
|
|
|
def clear_cache(self):
|
|
|
|
""" Remove all cached files. """
|
|
|
|
if not os.path.exists(mycroft.util.get_cache_directory('tts')):
|
|
|
|
return
|
|
|
|
for f in os.listdir(mycroft.util.get_cache_directory("tts")):
|
|
|
|
file_path = os.path.join(mycroft.util.get_cache_directory("tts"),
|
|
|
|
f)
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
os.unlink(file_path)
|
|
|
|
|
|
|
|
def save_phonemes(self, key, phonemes):
|
|
|
|
"""
|
|
|
|
Cache phonemes
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key: Hash key for the sentence
|
|
|
|
phonemes: phoneme string to save
|
|
|
|
"""
|
|
|
|
|
2017-10-13 06:40:35 +00:00
|
|
|
cache_dir = mycroft.util.get_cache_directory("tts")
|
2017-06-05 08:31:31 +00:00
|
|
|
pho_file = os.path.join(cache_dir, key + ".pho")
|
|
|
|
try:
|
|
|
|
with open(pho_file, "w") as cachefile:
|
|
|
|
cachefile.write(phonemes)
|
|
|
|
except:
|
2017-09-18 18:55:58 +00:00
|
|
|
LOG.debug("Failed to write .PHO to cache")
|
2017-06-05 08:31:31 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def load_phonemes(self, key):
|
|
|
|
"""
|
|
|
|
Load phonemes from cache file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Key: Key identifying phoneme cache
|
|
|
|
"""
|
|
|
|
pho_file = os.path.join(mycroft.util.get_cache_directory("tts"),
|
2017-09-18 19:14:21 +00:00
|
|
|
key + ".pho")
|
2017-06-05 08:31:31 +00:00
|
|
|
if os.path.exists(pho_file):
|
|
|
|
try:
|
|
|
|
with open(pho_file, "r") as cachefile:
|
|
|
|
phonemes = cachefile.read().strip()
|
|
|
|
return phonemes
|
|
|
|
except:
|
2017-09-18 18:55:58 +00:00
|
|
|
LOG.debug("Failed to read .PHO from cache")
|
2017-06-05 08:31:31 +00:00
|
|
|
return None
|
|
|
|
|
2017-06-04 06:07:37 +00:00
|
|
|
def __del__(self):
|
|
|
|
self.playback.stop()
|
|
|
|
self.playback.join()
|
2016-09-17 02:08:53 +00:00
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
class TTSValidator(object):
|
|
|
|
"""
|
|
|
|
TTS Validator abstract class to be implemented by all TTS engines.
|
|
|
|
|
2016-05-20 22:15:53 +00:00
|
|
|
It exposes and implements ``validate(tts)`` function as a template to
|
|
|
|
validate the TTS engines.
|
2016-05-20 14:16:01 +00:00
|
|
|
"""
|
2016-09-16 18:12:43 +00:00
|
|
|
__metaclass__ = ABCMeta
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
def __init__(self, tts):
|
|
|
|
self.tts = tts
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
def validate(self):
|
|
|
|
self.validate_instance()
|
|
|
|
self.validate_filename()
|
|
|
|
self.validate_lang()
|
|
|
|
self.validate_connection()
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
def validate_instance(self):
|
|
|
|
clazz = self.get_tts_class()
|
|
|
|
if not isinstance(self.tts, clazz):
|
|
|
|
raise AttributeError('tts must be instance of ' + clazz.__name__)
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
def validate_filename(self):
|
|
|
|
filename = self.tts.filename
|
2016-05-20 14:16:01 +00:00
|
|
|
if not (filename and filename.endswith('.wav')):
|
2016-09-16 18:12:43 +00:00
|
|
|
raise AttributeError('file: %s must be in .wav format!' % filename)
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
dir_path = dirname(filename)
|
2016-05-20 14:16:01 +00:00
|
|
|
if not (exists(dir_path) and isdir(dir_path)):
|
2016-09-16 18:12:43 +00:00
|
|
|
raise AttributeError('filename: %s is not valid!' % filename)
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
@abstractmethod
|
|
|
|
def validate_lang(self):
|
2016-05-20 14:16:01 +00:00
|
|
|
pass
|
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
@abstractmethod
|
|
|
|
def validate_connection(self):
|
2016-05-20 14:16:01 +00:00
|
|
|
pass
|
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
@abstractmethod
|
|
|
|
def get_tts_class(self):
|
2016-05-20 14:16:01 +00:00
|
|
|
pass
|
2016-09-08 15:31:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TTSFactory(object):
|
2016-09-16 18:12:43 +00:00
|
|
|
from mycroft.tts.espeak_tts import ESpeak
|
|
|
|
from mycroft.tts.fa_tts import FATTS
|
|
|
|
from mycroft.tts.google_tts import GoogleTTS
|
|
|
|
from mycroft.tts.mary_tts import MaryTTS
|
|
|
|
from mycroft.tts.mimic_tts import Mimic
|
|
|
|
from mycroft.tts.spdsay_tts import SpdSay
|
|
|
|
|
|
|
|
CLASSES = {
|
|
|
|
"mimic": Mimic,
|
|
|
|
"google": GoogleTTS,
|
|
|
|
"marytts": MaryTTS,
|
|
|
|
"fatts": FATTS,
|
|
|
|
"espeak": ESpeak,
|
|
|
|
"spdsay": SpdSay
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:31:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def create():
|
|
|
|
"""
|
|
|
|
Factory method to create a TTS engine based on configuration.
|
|
|
|
|
|
|
|
The configuration file ``mycroft.conf`` contains a ``tts`` section with
|
|
|
|
the name of a TTS module to be read by this method.
|
|
|
|
|
|
|
|
"tts": {
|
|
|
|
"module": <engine_name>
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
from mycroft.tts.remote_tts import RemoteTTS
|
2017-09-23 12:13:50 +00:00
|
|
|
config = Configuration.get().get('tts', {})
|
2016-09-16 18:12:43 +00:00
|
|
|
module = config.get('module', 'mimic')
|
|
|
|
lang = config.get(module).get('lang')
|
|
|
|
voice = config.get(module).get('voice')
|
|
|
|
clazz = TTSFactory.CLASSES.get(module)
|
|
|
|
|
|
|
|
if issubclass(clazz, RemoteTTS):
|
|
|
|
url = config.get(module).get('url')
|
|
|
|
tts = clazz(lang, voice, url)
|
2016-09-08 15:31:40 +00:00
|
|
|
else:
|
2016-09-16 18:12:43 +00:00
|
|
|
tts = clazz(lang, voice)
|
|
|
|
|
|
|
|
tts.validator.validate()
|
2016-09-08 15:31:40 +00:00
|
|
|
return tts
|