~kf5jwc/garretts-plugin-google

d9e0bfed23ea7402d01dba5ba87ebba79526af2d — Kyle Jones 2 years ago
Initial
A  => .gitignore +1 -0
@@ 1,1 @@
dist/

A  => pyproject.toml +20 -0
@@ 1,20 @@
[build-system]
requires = ["flit"]
build-backend = "flit.buildapi"

[tool.flit.metadata]
module = "text_synthesizer_plugin_google"
author = "Kyle Jones"
author-email = "kyle@kf5jwc.us"
requires-python = ">=3.7"
requires = [
	"text_synthesizer",
	"google-cloud-texttospeech",
]

[tool.flit.metadata.requires-extra]
french = []
english = []

[tool.flit.entrypoints.gather]
package = "text_synthesizer_plugin_google"

A  => text_synthesizer_plugin_google/__init__.py +57 -0
@@ 1,57 @@
"""A plugin for text_synthesizer, supporting google as a synthesizer."""

__version__ = "0.1.0"

from text_synthesizer import Plugin as __plugin
from .google import synthesize_text as __synthesize
from typing import BinaryIO as _BinaryIO

# Standard
@__plugin.register(name="Google Standard A")
def standard_a(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-A")

@__plugin.register(name="Google Standard B")
def standard_b(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-B")

@__plugin.register(name="Google Standard C")
def standard_c(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-C")

@__plugin.register(name="Google Standard D")
def standard_d(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-D")

@__plugin.register(name="Google Standard E")
def standard_e(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-E")

@__plugin.register(name="Google Standard F")
def standard_f(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-F")

# Wavenet
@__plugin.register(name="Google Wavenet A")
def wavenet_a(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-A")

@__plugin.register(name="Google Wavenet B")
def wavenet_b(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-B")

@__plugin.register(name="Google Wavenet C")
def wavenet_c(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-C")

@__plugin.register(name="Google Wavenet D")
def wavenet_d(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-D")

@__plugin.register(name="Google Wavenet E")
def wavenet_e(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-E")

@__plugin.register(name="Google Wavenet F")
def wavenet_f(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-F")

A  => text_synthesizer_plugin_google/__main__.py +37 -0
@@ 1,37 @@
#!/usr/bin/env python

# Copyright 2018 Google Inc. All Rights Reserved.
#
# 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.

"""Google Cloud Text-To-Speech API sample application"""

import argparse
from .google import synthesize_text

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
        #description=__doc__,
        #formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('--voice',
                       help="The name of the voice to use.")
    parser.add_argument('text',
                       help='The text from which to synthesize speech.')

    args = parser.parse_args()

    call_args = { "text": args.text }
    if args.voice:
        call_args["name"] = args.voice

    synthesize_text(**call_args)

A  => text_synthesizer_plugin_google/google.py +59 -0
@@ 1,59 @@
from google.cloud import texttospeech
from typing import BinaryIO, List

# [START tts_synthesize_text]
def synthesize_text(text, name=None) -> BinaryIO:
    """Synthesizes speech from the input string of text."""
    client = texttospeech.TextToSpeechClient()

    input_text = texttospeech.types.SynthesisInput(text=text)

    # Note: the voice can also be specified by name.
    # Names of voices can be retrieved with client.list_voices().
    opts = {
        'language_code': 'en-US',
        'ssml_gender': texttospeech.enums.SsmlVoiceGender.FEMALE
    }

    # hackity hack hack
    if name is not None:
        opts['name'] = name

    voice = texttospeech.types.VoiceSelectionParams(**opts)

    audio_config = texttospeech.types.AudioConfig(
        audio_encoding=texttospeech.enums.AudioEncoding.MP3)

    response = client.synthesize_speech(input_text, voice, audio_config)

    return response.audio_content
# [END tts_synthesize_text]

# [START tts_list_voices]
def list_voices() -> List[str]:
    """Lists the available voices."""
    client = texttospeech.TextToSpeechClient()

    # Performs the list voices request
    voices = client.list_voices()

    for voice in voices.voices:
        # Display the voice's name. Example: tpc-vocoded
        print('Name: {}'.format(voice.name))

        # Display the supported language codes for this voice. Example: "en-US"
        for language_code in voice.language_codes:
            print('Supported language: {}'.format(language_code))

        # SSML Voice Gender values from google.cloud.texttospeech.enums
        ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE',
                              'FEMALE', 'NEUTRAL']

        # Display the SSML Voice Gender
        print('SSML Voice Gender: {}'.format(
            ssml_voice_genders[voice.ssml_gender]))

        # Display the natural sample rate hertz for this voice. Example: 24000
        print('Natural Sample Rate Hertz: {}\n'.format(
            voice.natural_sample_rate_hertz))
# [END tts_list_voices]