~kf5jwc/garretts-plugin-google

ref: 9b0537b41a1155ed919fae5d63e458c8b6f4a7fa garretts-plugin-google/garretts_plugin_google/google.py -rw-r--r-- 1.9 KiB View raw
9b0537b4 — Kyle Jones Rename to GarreTTS 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from google.cloud import texttospeech
from typing import BinaryIO, List


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


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))