2016-05-26 16:16:13 +00:00
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
2016-09-17 02:08:53 +00:00
|
|
|
import random
|
2016-09-16 18:12:43 +00:00
|
|
|
from abc import ABCMeta, abstractmethod
|
2016-05-20 14:16:01 +00:00
|
|
|
from os.path import dirname, exists, isdir
|
2017-06-04 06:07:37 +00:00
|
|
|
from threading import Thread
|
|
|
|
from Queue import Queue
|
|
|
|
from time import time, sleep
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-17 02:08:53 +00:00
|
|
|
from mycroft.client.enclosure.api import EnclosureAPI
|
2016-09-08 15:31:40 +00:00
|
|
|
from mycroft.configuration import ConfigurationManager
|
2016-09-17 02:08:53 +00:00
|
|
|
from mycroft.messagebus.client.ws import WebsocketClient
|
2016-05-20 14:16:01 +00:00
|
|
|
from mycroft.util.log import getLogger
|
2017-06-04 06:07:37 +00:00
|
|
|
from mycroft.util import play_wav, play_mp3, check_for_signal
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
__author__ = 'jdorleans'
|
|
|
|
|
|
|
|
LOGGER = getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-06-04 06:07:37 +00:00
|
|
|
class PlaybackThread(Thread):
|
|
|
|
def __init__(self, queue):
|
|
|
|
super(PlaybackThread, self).__init__()
|
|
|
|
self.queue = queue
|
|
|
|
self._terminated = False
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not self._terminated:
|
|
|
|
try:
|
|
|
|
snd_type, data, visimes = self.queue.get(timeout=2)
|
|
|
|
print 'Data received!'
|
|
|
|
print (snd_type, data, visimes)
|
|
|
|
self.blink(0.5)
|
|
|
|
if snd_type == 'wav':
|
|
|
|
print 'playing wav'
|
|
|
|
p = play_wav(data)
|
|
|
|
elif snd_type == 'mp3':
|
|
|
|
p = play_mp3(data)
|
|
|
|
|
|
|
|
if visimes:
|
|
|
|
self.show_visimes(visimes)
|
|
|
|
p.communicate()
|
|
|
|
self.blink(0.2)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def show_visimes(self, pairs):
|
|
|
|
start = time()
|
|
|
|
print "VISIMES!"
|
|
|
|
for code, duration in pairs:
|
|
|
|
print code, duration
|
|
|
|
print "checking for signal"
|
|
|
|
if mycroft.util.check_for_signal('buttonPress'):
|
|
|
|
return
|
|
|
|
if check_for_signal('stoppingTTS', -1):
|
|
|
|
return
|
|
|
|
print "writing to enclosure"
|
|
|
|
if self.enclosure:
|
|
|
|
self.enclosure.mouth_viseme(code)
|
|
|
|
print "waiting"
|
|
|
|
delta = time() - start
|
|
|
|
if delta < duration:
|
|
|
|
sleep(duration - delta)
|
|
|
|
|
|
|
|
def blink(self, rate=1.0):
|
|
|
|
if self.enclosure and random.random() < rate:
|
|
|
|
self.enclosure.eyes_blink("b")
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._terminated = True
|
|
|
|
while not self.queue.empty():
|
|
|
|
queue.get()
|
|
|
|
|
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()
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-12-21 05:15:51 +00:00
|
|
|
def init(self, ws):
|
|
|
|
self.ws = ws
|
|
|
|
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
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
@abstractmethod
|
|
|
|
def execute(self, sentence):
|
2017-03-30 07:40:56 +00:00
|
|
|
''' This performs TTS, blocking until audio completes
|
|
|
|
|
|
|
|
This performs the TTS sequence. Upon completion, the sentence will
|
|
|
|
have been spoken. Optionally, the TTS engine may have sent visemes
|
|
|
|
to the enclosure by the TTS engine.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sentence (str): Words to be spoken
|
|
|
|
'''
|
|
|
|
# TODO: Move caching support from mimic_tts to here for all TTS
|
2016-05-20 14:16:01 +00:00
|
|
|
pass
|
|
|
|
|
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
|
|
|
|
config = ConfigurationManager.get().get('tts', {})
|
|
|
|
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
|