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-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
|
|
|
|
|
2016-09-08 15:31:40 +00:00
|
|
|
from mycroft.configuration import ConfigurationManager
|
2016-05-20 14:16:01 +00:00
|
|
|
from mycroft.util.log import getLogger
|
|
|
|
|
|
|
|
__author__ = 'jdorleans'
|
|
|
|
|
|
|
|
LOGGER = getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
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__()
|
|
|
|
self.lang = lang
|
|
|
|
self.voice = voice
|
2016-09-16 18:12:43 +00:00
|
|
|
self.filename = '/tmp/tts.wav'
|
|
|
|
self.validator = validator
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-09-16 18:12:43 +00:00
|
|
|
@abstractmethod
|
|
|
|
def execute(self, sentence):
|
2016-05-20 14:16:01 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
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
|