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

View file

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