tts plugins should leverage sound`, not an external media player.

This commit is contained in:
Fabio Manganiello 2023-10-28 16:50:55 +02:00
parent 72b2625425
commit 0cbd0a94d6
2 changed files with 31 additions and 52 deletions

View file

@ -1,79 +1,59 @@
import urllib.parse
from typing import Optional
from platypush.config import Config
from platypush.context import get_plugin
from platypush.plugins import Plugin, action
from platypush.plugins.media import MediaPlugin
class TtsPlugin(Plugin):
"""
Default Text-to-Speech plugin. It leverages Google Translate.
Requires:
* At least a *media plugin* (see :class:`platypush.plugins.media.MediaPlugin`) enabled/configured - used for
speech playback.
Default Text-to-Speech plugin. It leverages Google Translate's unofficial
frontend API.
"""
_supported_media_plugins = [
'media.gstreamer',
'media.omxplayer',
'media.mplayer',
'media.mpv',
'media.vlc',
]
def __init__(self, language='en-gb', media_plugin: Optional[str] = None, player_args: Optional[dict] = None):
def __init__(self, language='en-US', **player_args):
"""
:param language: Language code (default: ``en-gb``).
:param media_plugin: Media plugin to be used for audio playback. Supported:
- ``media.gstreamer``
- ``media.omxplayer``
- ``media.mplayer``
- ``media.mpv``
- ``media.vlc``
:param player_args: Optional arguments that should be passed to the player plugin's
:meth:`platypush.plugins.media.MediaPlugin.play` method.
:param language: Language code (default: ``en-US``).
:param player_args: Additional arguments to be passed to
:meth:`platypush.plugins.sound.SoundPlugin.play` (like volume,
duration, channels etc.).
"""
super().__init__()
self.language = language
self.player_args = player_args or {}
self.media_plugin = get_plugin(media_plugin) if media_plugin else self._get_media_plugin()
assert self.media_plugin, 'No media playback plugin configured. Supported plugins: [{}]'.format(
', '.join(self._supported_media_plugins))
@classmethod
def _get_media_plugin(cls) -> Optional[MediaPlugin]:
for plugin in cls._supported_media_plugins:
if plugin in Config.get():
return get_plugin(plugin)
def _playback(self, resource: str, **kwargs):
audio = get_plugin('sound')
assert audio
audio.play(resource, **{**self.player_args, **kwargs})
@action
def say(self, text: str, language: Optional[str] = None, player_args: Optional[dict] = None):
def say(
self,
text: str,
language: Optional[str] = None,
**player_args,
):
"""
Say some text.
:param text: Text to say.
:param language: Language code override.
:param player_args: Optional arguments that should be passed to the player plugin's
:meth:`platypush.plugins.media.MediaPlugin.play` method.
:param player_args: Extends the additional arguments to be passed to
:meth:`platypush.plugins.sound.SoundPlugin.play` (like volume,
duration, channels etc.).
"""
language = language or self.language
player_args = player_args or self.player_args
url = 'https://translate.google.com/translate_tts?{}'.format(
urllib.parse.urlencode({
url = 'https://translate.google.com/translate_tts?' + urllib.parse.urlencode(
{
'ie': 'UTF-8',
'client': 'tw-ob',
'tl': language,
'q': text,
}))
}
)
self.media_plugin.play(url, **player_args)
self._playback(url, **player_args)
# vim:sw=4:ts=4:et:

View file

@ -19,7 +19,7 @@ class TtsGooglePlugin(TtsPlugin):
voice: Optional[str] = None,
gender: str = 'FEMALE',
credentials_file: str = '~/.credentials/platypush/google/platypush-tts.json',
**kwargs
**kwargs,
):
"""
:param language: Language code, see
@ -124,7 +124,7 @@ class TtsGooglePlugin(TtsPlugin):
language: Optional[str] = None,
voice: Optional[str] = None,
gender: Optional[str] = None,
player_args: Optional[dict] = None,
**player_args,
):
"""
Say a phrase.
@ -133,9 +133,9 @@ class TtsGooglePlugin(TtsPlugin):
:param language: Language code override.
:param voice: Voice type override.
:param gender: Gender override.
:param player_args: Optional arguments that should be passed to the
player plugin's :meth:`platypush.plugins.media.MediaPlugin.play`
method.
:param player_args: Extends the additional arguments to be passed to
:meth:`platypush.plugins.sound.SoundPlugin.play` (like volume,
duration, channels etc.).
"""
from google.cloud import texttospeech
@ -159,11 +159,10 @@ class TtsGooglePlugin(TtsPlugin):
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
player_args = player_args or {}
with tempfile.NamedTemporaryFile() as f:
f.write(response.audio_content)
self.media_plugin.play(f.name, **player_args)
self._playback(f.name, **player_args)
# vim:sw=4:ts=4:et: