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-05-20 14:16:01 +00:00
|
|
|
import abc
|
|
|
|
from os.path import dirname, exists, isdir
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, lang, voice, filename='/tmp/tts.wav'):
|
|
|
|
super(TTS, self).__init__()
|
|
|
|
self.lang = lang
|
|
|
|
self.voice = voice
|
|
|
|
self.filename = filename
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2016-09-05 21:27:09 +00:00
|
|
|
def execute(self, sentence, client):
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def validate(self, tts):
|
|
|
|
self.__validate_instance(tts)
|
|
|
|
self.__validate_filename(tts.filename)
|
|
|
|
self.validate_lang(tts.lang)
|
|
|
|
self.validate_connection(tts)
|
|
|
|
|
|
|
|
def __validate_instance(self, tts):
|
|
|
|
instance = self.get_instance()
|
|
|
|
if not isinstance(tts, instance):
|
2016-05-20 22:15:53 +00:00
|
|
|
raise AttributeError(
|
|
|
|
'tts must be instance of ' + instance.__name__)
|
2016-05-20 14:16:01 +00:00
|
|
|
LOGGER.debug('TTS: ' + str(instance))
|
|
|
|
|
|
|
|
def __validate_filename(self, filename):
|
|
|
|
if not (filename and filename.endswith('.wav')):
|
2016-05-20 22:15:53 +00:00
|
|
|
raise AttributeError(
|
|
|
|
'filename: ' + filename + ' must be a .wav file!')
|
2016-05-20 14:16:01 +00:00
|
|
|
dir_path = dirname(filename)
|
|
|
|
|
|
|
|
if not (exists(dir_path) and isdir(dir_path)):
|
2016-05-20 22:15:53 +00:00
|
|
|
raise AttributeError(
|
|
|
|
'filename: ' + filename + ' is not a valid file path!')
|
2016-05-20 14:16:01 +00:00
|
|
|
LOGGER.debug('Filename: ' + filename)
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def validate_lang(self, lang):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def validate_connection(self, tts):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def get_instance(self):
|
|
|
|
pass
|