Skip to content

Commit 6194578

Browse files
authored
docs(texttospeech): Update TTS Samples to use Chirp 3 Model (#13253)
1 parent a0d6595 commit 6194578

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

texttospeech/snippets/streaming_tts_quickstart.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,41 @@
2323

2424
def run_streaming_tts_quickstart():
2525
# [START tts_synthezise_streaming]
26-
"""Synthesizes speech from a stream of input text.
27-
"""
26+
"""Synthesizes speech from a stream of input text."""
2827
from google.cloud import texttospeech
29-
import itertools
3028

3129
client = texttospeech.TextToSpeechClient()
3230

3331
# See https://cloud.google.com/text-to-speech/docs/voices for all voices.
34-
streaming_config = texttospeech.StreamingSynthesizeConfig(voice=texttospeech.VoiceSelectionParams(name="en-US-Journey-D", language_code="en-US"))
32+
streaming_config = texttospeech.StreamingSynthesizeConfig(
33+
voice=texttospeech.VoiceSelectionParams(
34+
name="en-US-Chirp3-HD-Charon",
35+
language_code="en-US",
36+
)
37+
)
3538

3639
# Set the config for your stream. The first request must contain your config, and then each subsequent request must contain text.
37-
config_request = texttospeech.StreamingSynthesizeRequest(streaming_config=streaming_config)
40+
config_request = texttospeech.StreamingSynthesizeRequest(
41+
streaming_config=streaming_config
42+
)
43+
44+
text_iterator = [
45+
"Hello there. ",
46+
"How are you ",
47+
"today? It's ",
48+
"such nice weather outside.",
49+
]
3850

3951
# Request generator. Consider using Gemini or another LLM with output streaming as a generator.
4052
def request_generator():
41-
yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="Hello there. "))
42-
yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="How are you "))
43-
yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="today? It's "))
44-
yield texttospeech.StreamingSynthesizeRequest(input=texttospeech.StreamingSynthesisInput(text="such nice weather outside."))
53+
yield config_request
54+
for text in text_iterator:
55+
yield texttospeech.StreamingSynthesizeRequest(
56+
input=texttospeech.StreamingSynthesisInput(text=text)
57+
)
58+
59+
streaming_responses = client.streaming_synthesize(request_generator())
4560

46-
streaming_responses = client.streaming_synthesize(itertools.chain([config_request], request_generator()))
4761
for response in streaming_responses:
4862
print(f"Audio content size in bytes is: {len(response.audio_content)}")
4963
# [END tts_synthezise_streaming]

texttospeech/snippets/synthesize_text.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@ def synthesize_text():
3838
# Names of voices can be retrieved with client.list_voices().
3939
voice = texttospeech.VoiceSelectionParams(
4040
language_code="en-US",
41-
name="en-US-Standard-C",
42-
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE,
41+
name="en-US-Chirp3-HD-Charon",
4342
)
4443

4544
audio_config = texttospeech.AudioConfig(
4645
audio_encoding=texttospeech.AudioEncoding.MP3
4746
)
4847

4948
response = client.synthesize_speech(
50-
request={"input": input_text, "voice": voice, "audio_config": audio_config}
49+
input=input_text,
50+
voice=voice,
51+
audio_config=audio_config,
5152
)
5253

5354
# The response's audio_content is binary.

0 commit comments

Comments
 (0)