Added support for extra mplayer arguments for TTS plugins

This commit is contained in:
Fabio Manganiello 2020-03-16 18:40:17 +01:00
parent f2b3000922
commit d938017bf8
2 changed files with 54 additions and 47 deletions

View file

@ -1,5 +1,6 @@
import subprocess import subprocess
import urllib.parse import urllib.parse
from typing import Optional, List
from platypush.plugins import Plugin, action from platypush.plugins import Plugin, action
@ -13,30 +14,38 @@ class TtsPlugin(Plugin):
* **mplayer** - see your distribution docs on how to install the mplayer package * **mplayer** - see your distribution docs on how to install the mplayer package
""" """
def __init__(self, lang='en-gb'): def __init__(self, language='en-gb', player_args: Optional[List[str]] = None):
"""
:param language: Language code (default: ``en-gb``).
:param player_args: Extra options to be passed to the audio player (default: ``mplayer``).
"""
super().__init__() super().__init__()
self.lang=lang self.language = language
self.player_args = player_args or []
@action @action
def say(self, text, language=None): def say(self, text: str, language: Optional[str] = None, player_args: Optional[List[str]] = None):
""" """
Say a phrase Say some text.
:param text: Phrase to say :param text: Text to say.
:type text: str :param language: Language code override.
:param player_args: ``player_args`` override.
:param language: Language code
:type language: str
""" """
if language is None: language=self.lang language = language or self.language
cmd = ['mplayer -ao alsa -really-quiet -noconsolecontrols ' + player_args = player_args or self.player_args
'"http://translate.google.com/translate_tts?{}"' cmd = [
.format(urllib.parse.urlencode({ 'mplayer -ao alsa -really-quiet -noconsolecontrols ' +
' '.join(player_args) + ' ' +
'"http://translate.google.com/translate_tts?{}"'.format(
urllib.parse.urlencode({
'ie': 'UTF-8', 'ie': 'UTF-8',
'client': 'tw-ob', 'client': 'tw-ob',
'tl': language, 'tl': language,
'q': text, 'q': text,
}))] })
)
]
try: try:
return subprocess.check_output( return subprocess.check_output(
@ -44,6 +53,4 @@ class TtsPlugin(Plugin):
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
raise RuntimeError(e.output.decode('utf-8')) raise RuntimeError(e.output.decode('utf-8'))
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -1,11 +1,13 @@
import os import os
import subprocess import subprocess
import tempfile import tempfile
from typing import Optional, List
from platypush.plugins import Plugin, action from platypush.plugins import action
from platypush.plugins.tts import TtsPlugin
class TtsGooglePlugin(Plugin): class TtsGooglePlugin(TtsPlugin):
""" """
Advanced text-to-speech engine that leverages the Google Cloud TTS API. 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 See https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries#client-libraries-install-python
@ -17,27 +19,25 @@ class TtsGooglePlugin(Plugin):
* **mplayer** - see your distribution docs on how to install the mplayer package * **mplayer** - see your distribution docs on how to install the mplayer package
""" """
def __init__(self, language='en-US', voice=None, def __init__(self,
gender='FEMALE', credentials_file='~/.credentials/platypush/google/platypush-tts.json'): language: str ='en-US',
voice: Optional[str] = None,
gender: str = 'FEMALE',
credentials_file: str = '~/.credentials/platypush/google/platypush-tts.json',
player_args: Optional[List[str]] = None):
""" """
:param language: Language code, see https://cloud.google.com/text-to-speech/docs/basics for supported languages :param language: Language code, see https://cloud.google.com/text-to-speech/docs/basics for supported languages
:type language: str
:param voice: Voice type, see https://cloud.google.com/text-to-speech/docs/basics for supported voices :param voice: Voice type, see https://cloud.google.com/text-to-speech/docs/basics for supported voices
:type voice: str
:param gender: Voice gender (MALE, FEMALE or NEUTRAL) :param gender: Voice gender (MALE, FEMALE or NEUTRAL)
:type gender: str
:param credentials_file: Where your GCloud credentials for TTS are stored, see https://cloud.google.com/text-to-speech/docs/basics :param credentials_file: Where your GCloud credentials for TTS are stored, see https://cloud.google.com/text-to-speech/docs/basics
:type credentials_file: str :param player_args: Extra options to be passed to the audio player (default: ``mplayer``).
""" """
from google.cloud import texttospeech from google.cloud import texttospeech
super().__init__() super().__init__()
self.language = language self.language = language
self.voice = voice self.voice = voice
self.player_args = player_args or []
self.language = self._parse_language(language) self.language = self._parse_language(language)
self.voice = self._parse_voice(self.language, voice) self.voice = self._parse_voice(self.language, voice)
@ -67,21 +67,20 @@ class TtsGooglePlugin(Plugin):
return language + '-Wavenet-A' return language + '-Wavenet-A'
@action @action
def say(self, text, language=None, voice=None, gender=None): def say(self,
text: str,
language: Optional[str] = None,
voice: Optional[str] = None,
gender: Optional[str] = None,
player_args: Optional[List[str]] = None):
""" """
Say a phrase Say a phrase.
:param text: Text to say :param text: Text to say.
:type text: str :param language: Language code override.
:param voice: Voice type override.
:param language: Language code override :param gender: Gender override.
:type language: str :param player_args: Player args override.
:param voice: Voice type override
:type voice: str
:param gender: Gender override
:type gender: str
""" """
from google.cloud import texttospeech from google.cloud import texttospeech
@ -96,6 +95,7 @@ class TtsGooglePlugin(Plugin):
else: else:
gender = getattr(texttospeech.enums.SsmlVoiceGender, gender.upper()) gender = getattr(texttospeech.enums.SsmlVoiceGender, gender.upper())
player_args = player_args or self.player_args
voice = texttospeech.types.VoiceSelectionParams( voice = texttospeech.types.VoiceSelectionParams(
language_code=language, ssml_gender=gender, language_code=language, ssml_gender=gender,
name=voice) name=voice)
@ -107,8 +107,8 @@ class TtsGooglePlugin(Plugin):
with tempfile.NamedTemporaryFile() as f: with tempfile.NamedTemporaryFile() as f:
f.write(response.audio_content) f.write(response.audio_content)
cmd = ['mplayer -ao alsa -really-quiet -noconsolecontrols {}' cmd = ['mplayer -ao alsa -really-quiet -noconsolecontrols {} "{}"'.format(
.format(f.name)] ' '.join(player_args), f.name)]
try: try:
return subprocess.check_output( return subprocess.check_output(