Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of the Google Cloud TTS #2895

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 103 additions & 0 deletions mycroft/tts/google_cloud_tts.py
@@ -0,0 +1,103 @@
# Copyright 2019 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.
#

from .tts import TTS, TTSValidator
from mycroft.configuration import Configuration

from google.cloud import texttospeech
from google.oauth2 import service_account

VOICE_LIST = {
"male": texttospeech.SsmlVoiceGender.MALE,
"female": texttospeech.SsmlVoiceGender.FEMALE
}

OUTPUT_FILE_FORMAT = {
"wav": texttospeech.AudioEncoding.LINEAR16,
"mp3": texttospeech.AudioEncoding.MP3
}


class GoogleCloudTTS(TTS):

def __init__(self, lang, config):
self.config = (Configuration.get().get("tts", {})
.get("google_cloud", {}))

self.type = self.config.get("file_format", "wav").lower()
super(GoogleCloudTTS, self).__init__(lang, config,
GoogleCloudTTSValidator(self),
audio_ext=self.type)
self.lang = self.config.get("lang", lang)

voice_gender = self.config.get("voice_gender", "male").lower()

service_account_info = self.config.get("service_account_info", {})

credentials = (service_account.Credentials
.from_service_account_info(service_account_info))

# Instantiates a client
self.client = texttospeech.TextToSpeechClient(credentials=credentials)

# Select the language code and the ssml voice gender
self.voice = texttospeech.VoiceSelectionParams(
language_code=self.lang, ssml_gender=VOICE_LIST.get(voice_gender)
)

# Select the type of audio file you want returned
self.audio_config = texttospeech.AudioConfig(
audio_encoding=OUTPUT_FILE_FORMAT.get(self.type)
)

def get_tts(self, sentence, audio_file):
with open(audio_file, "wb") as out:
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text=sentence)

# Perform the text-to-speech request on the text input
# with the selected voice parameters and audio file type
response = self.client.synthesize_speech(
input=synthesis_input, voice=self.voice,
audio_config=self.audio_config
)

out.write(response.audio_content)
return audio_file, None # No phonemes


class GoogleCloudTTSValidator(TTSValidator):
def __init__(self, tts):
super(GoogleCloudTTSValidator, self).__init__(tts)

def validate_lang(self):
pass

def validate_connection(self):
try:
synthesis_input = texttospeech.SynthesisInput(text="Test")

self.tts.client.synthesize_speech(
input=synthesis_input, voice=self.tts.voice,
audio_config=self.tts.audio_config
)
except Exception as ex:
raise Exception(
'Error connecting to Google Cloud TTS server. '
'Please check your internet connection '
'and configuration settings', ex)

def get_tts_class(self):
return GoogleCloudTTS
2 changes: 2 additions & 0 deletions mycroft/tts/tts.py
Expand Up @@ -537,6 +537,7 @@ class TTSFactory:
from mycroft.tts.espeak_tts import ESpeak
from mycroft.tts.fa_tts import FATTS
from mycroft.tts.google_tts import GoogleTTS
from mycroft.tts.google_cloud_tts import GoogleCloudTTS
from mycroft.tts.mary_tts import MaryTTS
from mycroft.tts.mimic_tts import Mimic
from mycroft.tts.spdsay_tts import SpdSay
Expand All @@ -553,6 +554,7 @@ class TTSFactory:
"mimic": Mimic,
"mimic2": Mimic2,
"google": GoogleTTS,
"google_cloud": GoogleCloudTTS,
"marytts": MaryTTS,
"fatts": FATTS,
"festival": Festival,
Expand Down
3 changes: 0 additions & 3 deletions mycroft/util/__init__.py
Expand Up @@ -18,9 +18,6 @@
"""
from __future__ import absolute_import

import os

import mycroft.audio
from mycroft.util.format import nice_number
from .string_utils import camel_case_split
from .audio_utils import (play_audio_file, play_wav, play_ogg, play_mp3,
Expand Down
2 changes: 0 additions & 2 deletions mycroft/util/format.py
Expand Up @@ -35,8 +35,6 @@
date_time_format, expand_options,
_translate_word)

from padatious.util import expand_parentheses


def nice_number(number, lang=None, speech=True, denominators=None):
"""Format a float to human readable functions
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements.txt
@@ -1,5 +1,6 @@
requests>=2.20.0,<2.26.0
gTTS>=2.2.2,<2.3.0
google-cloud-texttospeech==2.3.0
PyAudio==0.2.11
pyee==8.1.0
SpeechRecognition==3.8.1
Expand Down