platypush/platypush/plugins/tts/google.py

139 lines
5.0 KiB
Python
Raw Permalink Normal View History

import os
2018-10-14 10:29:02 +02:00
import tempfile
2021-02-19 20:47:29 +01:00
from typing import Optional
2018-10-14 10:29:02 +02:00
from platypush.plugins import action
from platypush.plugins.tts import TtsPlugin
2018-10-14 10:29:02 +02:00
2019-07-01 22:26:04 +02:00
class TtsGooglePlugin(TtsPlugin):
2018-10-14 10:29:02 +02:00
"""
Advanced text-to-speech engine that leverages the Google Cloud TTS API.
See https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-install-python
for how to enable the API on your account and get your credentials.
Requires:
* **google-cloud-texttospeech** - ``pip install google-cloud-texttospeech``
* **mplayer** - see your distribution docs on how to install the mplayer package
2021-02-19 20:47:29 +01:00
2018-10-14 10:29:02 +02:00
"""
def __init__(self,
2021-02-19 20:47:29 +01:00
language: str = 'en-US',
voice: Optional[str] = None,
gender: str = 'FEMALE',
credentials_file: str = '~/.credentials/platypush/google/platypush-tts.json',
2021-02-19 20:47:29 +01:00
**kwargs):
2018-10-14 10:29:02 +02:00
"""
:param language: Language code, see https://cloud.google.com/text-to-speech/docs/basics for supported languages
:param voice: Voice type, see https://cloud.google.com/text-to-speech/docs/basics for supported voices
:param gender: Voice gender (MALE, FEMALE or NEUTRAL)
:param credentials_file: Where your GCloud credentials for TTS are stored, see https://cloud.google.com/text-to-speech/docs/basics
2021-02-19 20:47:29 +01:00
:param kwargs: Extra arguments to be passed to the :class:`platypush.plugins.tts.TtsPlugin` constructor.
2018-10-14 10:29:02 +02:00
"""
2021-02-19 20:47:29 +01:00
super().__init__(**kwargs)
2018-10-14 10:29:02 +02:00
self.language = language
self.voice = voice
2019-06-11 11:53:15 +02:00
self.language = self._parse_language(language)
self.voice = self._parse_voice(self.language, voice)
2021-02-19 20:47:29 +01:00
self.gender = getattr(self._gender, gender.upper())
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.expanduser(credentials_file)
2018-10-14 10:29:02 +02:00
2019-06-11 11:53:15 +02:00
def _parse_language(self, language):
if language is None:
language = self.language or 'en-US'
if len(language) == 2:
language = language.lower()
if language == 'en':
language = 'en-US'
else:
language += '-' + language.upper()
return language
@staticmethod
def _parse_voice(language, voice):
if voice is not None:
return voice
if language == 'en-US':
return language + '-Wavenet-C'
return language + '-Wavenet-A'
2021-02-19 20:47:29 +01:00
@property
def _gender(self):
from google.cloud import texttospeech
return texttospeech.enums.SsmlVoiceGender if hasattr(texttospeech, 'enums') else \
texttospeech.SsmlVoiceGender
@property
def _voice_selection_params(self):
from google.cloud import texttospeech
return texttospeech.types.VoiceSelectionParams if hasattr(texttospeech, 'types') else \
texttospeech.VoiceSelectionParams
@property
def _synthesis_input(self):
from google.cloud import texttospeech
return texttospeech.types.SynthesisInput if hasattr(texttospeech, 'types') else \
texttospeech.SynthesisInput
@property
def _audio_config(self):
from google.cloud import texttospeech
return texttospeech.types.AudioConfig if hasattr(texttospeech, 'types') else \
texttospeech.AudioConfig
@property
def _audio_encoding(self):
from google.cloud import texttospeech
return texttospeech.enums.AudioEncoding if hasattr(texttospeech, 'enums') else \
texttospeech.AudioEncoding
2018-10-14 10:29:02 +02:00
@action
def say(self,
text: str,
language: Optional[str] = None,
voice: Optional[str] = None,
gender: Optional[str] = None,
2021-02-19 20:47:29 +01:00
player_args: Optional[dict] = None):
2018-10-14 10:29:02 +02:00
"""
Say a phrase.
2018-10-14 10:29:02 +02:00
:param text: Text to say.
:param language: Language code override.
:param voice: Voice type override.
:param gender: Gender override.
2021-02-19 20:47:29 +01:00
:param player_args: Optional arguments that should be passed to the player plugin's
:meth:`platypush.plugins.media.MediaPlugin.play` method.
2018-10-14 10:29:02 +02:00
"""
from google.cloud import texttospeech
2018-10-14 10:29:02 +02:00
client = texttospeech.TextToSpeechClient()
2021-02-19 20:47:29 +01:00
# noinspection PyTypeChecker
synthesis_input = self._synthesis_input(text=text)
2018-10-14 10:29:02 +02:00
2019-06-11 11:53:15 +02:00
language = self._parse_language(language)
voice = self._parse_voice(language, voice)
2018-10-14 10:29:02 +02:00
if gender is None:
gender = self.gender
else:
2021-02-19 20:47:29 +01:00
gender = getattr(self._gender, gender.upper())
2018-10-14 10:29:02 +02:00
2021-02-19 20:47:29 +01:00
voice = self._voice_selection_params(language_code=language, ssml_gender=gender, name=voice)
# noinspection PyTypeChecker
audio_config = self._audio_config(audio_encoding=self._audio_encoding.MP3)
response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config)
player_args = player_args or {}
2018-10-14 10:29:02 +02:00
with tempfile.NamedTemporaryFile() as f:
f.write(response.audio_content)
2021-02-19 20:47:29 +01:00
self.media_plugin.play(f.name, **player_args)
2018-10-14 10:29:02 +02:00
# vim:sw=4:ts=4:et: