Skip to content

Commit f65f91c

Browse files
committed
docs(texttospeech): Create sample for Synthesize speech multiple speakers
- https://cloud.google.com/text-to-speech/docs/create-dialogue-with-multispeakers
1 parent 6597598 commit f65f91c

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
future==1.0.0
2-
google-cloud-texttospeech==2.17.2
3-
google-cloud-storage==2.9.0
2+
google-cloud-texttospeech==2.21.1
3+
google-cloud-storage==2.18.2
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python
2+
# Copyright 2014 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
"""Google Cloud Text-To-Speech API sample application .
18+
19+
Example usage:
20+
python synthesize_speech_multiple_speakers.py
21+
"""
22+
23+
24+
def synthesize_speech_multiple_speakers():
25+
# [START tts_synthesize_speech_multiple_speakers]
26+
"""Synthesizes speech for multiple speakers.
27+
Make sure to be working in a virtual environment.
28+
"""
29+
from google.cloud import texttospeech_v1beta1 as texttospeech
30+
31+
# Instantiates a client
32+
client = texttospeech.TextToSpeechClient()
33+
34+
multi_speaker_markup = texttospeech.MultiSpeakerMarkup(
35+
turns=[
36+
texttospeech.MultiSpeakerMarkup.Turn(
37+
text="I've heard that the Google Cloud multi-speaker audio generation sounds amazing!",
38+
speaker="R",
39+
),
40+
texttospeech.MultiSpeakerMarkup.Turn(
41+
text="Oh? What's so good about it?", speaker="S"
42+
),
43+
texttospeech.MultiSpeakerMarkup.Turn(text="Well..", speaker="R"),
44+
texttospeech.MultiSpeakerMarkup.Turn(text="Well what?", speaker="S"),
45+
texttospeech.MultiSpeakerMarkup.Turn(
46+
text="Well, you should find it out by yourself!", speaker="R"
47+
),
48+
texttospeech.MultiSpeakerMarkup.Turn(
49+
text="Alright alright, let's try it out!", speaker="S"
50+
),
51+
]
52+
)
53+
54+
# Set the text input to be synthesized
55+
synthesis_input = texttospeech.SynthesisInput(
56+
multi_speaker_markup=multi_speaker_markup
57+
)
58+
59+
# Build the voice request, select the language code ('en-US') and the voice
60+
voice = texttospeech.VoiceSelectionParams(
61+
language_code="en-US", name="en-US-Studio-MultiSpeaker"
62+
)
63+
64+
# Select the type of audio file you want returned
65+
audio_config = texttospeech.AudioConfig(
66+
audio_encoding=texttospeech.AudioEncoding.MP3
67+
)
68+
69+
# Perform the text-to-speech request on the text input with the selected
70+
# voice parameters and audio file type
71+
response = client.synthesize_speech(
72+
input=synthesis_input, voice=voice, audio_config=audio_config
73+
)
74+
75+
# The response's audio_content is binary.
76+
with open("output.mp3", "wb") as out:
77+
# Write the response to the output file.
78+
out.write(response.audio_content)
79+
print('Audio content written to file "output.mp3"')
80+
# [END tts_synthesize_speech_multiple_speakers]
81+
82+
83+
if __name__ == "__main__":
84+
synthesize_speech_multiple_speakers()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# Copyright 2018 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import os
18+
19+
import synthesize_speech_multiple_speakers
20+
21+
22+
def synthesize_speech_multiple_speakers_text(capsys):
23+
synthesize_speech_multiple_speakers.synthesize_speech_multiple_speakers()
24+
out, err = capsys.readouterr()
25+
26+
assert "Audio content written to file" in out
27+
statinfo = os.stat("output.mp3")
28+
assert statinfo.st_size > 0

0 commit comments

Comments
 (0)