From 5be3b54b27c9169ee8495f7825bc69ba9eb1a49a Mon Sep 17 00:00:00 2001 From: domcross Date: Thu, 1 Oct 2020 20:07:47 +0200 Subject: [PATCH] MozillaTTS Implement TTS module that works with Mozilla-TTS server. ==== Environment Notes ==== Requires a Mozilla-TTS server running preferably in your local network. --- mycroft/configuration/mycroft.conf | 8 +++- mycroft/tts/mozilla_tts.py | 73 ++++++++++++++++++++++++++++++ mycroft/tts/tts.py | 2 + 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 mycroft/tts/mozilla_tts.py diff --git a/mycroft/configuration/mycroft.conf b/mycroft/configuration/mycroft.conf index d86e52b972..a4f928a6a6 100644 --- a/mycroft/configuration/mycroft.conf +++ b/mycroft/configuration/mycroft.conf @@ -285,8 +285,8 @@ // Text to Speech parameters // Override: REMOTE "tts": { - // Engine. Options: "mimic", "google", "marytts", "fatts", "espeak", - // "spdsay", "responsive_voice", "yandex", "polly" + // Engine. Options: "mimic", "mimic2", "google", "marytts", "fatts", "espeak", + // "spdsay", "responsive_voice", "yandex", "polly", "mozilla" "pulse_duck": false, "module": "mimic", "polly": { @@ -306,6 +306,10 @@ "espeak": { "lang": "english-us", "voice": "m1" + }, + "mozilla": { + "lang": "de", + "url": "http://10.0.0.1:5002/api/tts?text=" } }, diff --git a/mycroft/tts/mozilla_tts.py b/mycroft/tts/mozilla_tts.py new file mode 100644 index 0000000000..a6da472f05 --- /dev/null +++ b/mycroft/tts/mozilla_tts.py @@ -0,0 +1,73 @@ +# Copyright 2020 Mycroft AI Inc. +# +# 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. +# + +import hashlib +import requests + +from .tts import TTS, TTSValidator +from .remote_tts import RemoteTTSException, RemoteTTSTimeoutException +from mycroft.util.log import LOG +from mycroft.tts import cache_handler +from mycroft.util import get_cache_directory +from requests_futures.sessions import FuturesSession +from requests.exceptions import ( + ReadTimeout, ConnectionError, ConnectTimeout, HTTPError +) +from urllib import parse +from .mimic_tts import VISIMES +import math +import base64 +import os +import re +import json + + +class MozillaTTS(TTS): + def __init__(self, lang="en-us", config=None): + config = config or Configuration.get().get("tts", {}).get("mozilla", {}) + super(MozillaTTS, self).__init__(lang, config, MozillaTTSValidator(self)) + self.url = config['url'] + self.type = 'wav' + + def get_tts(self, sentence, wav_file): + wav_name = hashlib.sha1(sentence.encode('utf-8')).hexdigest() + ".wav" + wav_file = "/tmp/" + wav_name + if os.path.exists(wav_file) and os.path.getsize(wav_file) > 0: + LOG.info('local response wav found.') + else: + req_route = self.url + sentence + response = requests.get(req_route) + with open(wav_file, 'wb') as f: + f.write(response.content) + return (wav_file, None) # No phonemes + + +class MozillaTTSValidator(TTSValidator): + def __init__(self, tts): + super(MozillaTTSValidator, self).__init__(tts) + + def validate_dependencies(self): + pass + + def validate_lang(self): + # TODO + pass + + def validate_connection(self): + # TODO + pass + + def get_tts_class(self): + return MozillaTTS diff --git a/mycroft/tts/tts.py b/mycroft/tts/tts.py index 6dd70f3226..a8c7a08151 100644 --- a/mycroft/tts/tts.py +++ b/mycroft/tts/tts.py @@ -479,6 +479,7 @@ class TTSFactory: from mycroft.tts.yandex_tts import YandexTTS from mycroft.tts.dummy_tts import DummyTTS from mycroft.tts.polly_tts import PollyTTS + from mycroft.tts.mozilla_tts import MozillaTTS CLASSES = { "mimic": Mimic, @@ -494,6 +495,7 @@ class TTSFactory: "responsive_voice": ResponsiveVoice, "yandex": YandexTTS, "polly": PollyTTS, + "mozilla": MozillaTTS, "dummy": DummyTTS }