Skip to content

Commit e134962

Browse files
feat(tts): add streaming synthesis sample (#12642)
* Create streaming_tts_quickstart.py Add quickstart documentation for new streaming synthesize API * Update streaming_tts_quickstart.py * Update streaming_tts_quickstart.py * Update streaming_tts_quickstart.py * Update streaming_tts_quickstart.py * Update streaming_tts_quickstart.py * Update texttospeech/snippets/streaming_tts_quickstart.py Co-authored-by: Katie McLaughlin <[email protected]> * Update texttospeech/snippets/streaming_tts_quickstart.py Co-authored-by: Katie McLaughlin <[email protected]> * Update texttospeech/snippets/streaming_tts_quickstart.py Co-authored-by: Katie McLaughlin <[email protected]> * Update texttospeech/snippets/streaming_tts_quickstart.py Co-authored-by: Katie McLaughlin <[email protected]> * Update streaming_tts_quickstart.py resolving some comments from PR * Update streaming_tts_quickstart.py responding to comments in PR * Update streaming_tts_quickstart.py remove whitespace * Create streaming_tts_quickstart_test.py add test * Update streaming_tts_quickstart_test.py * Update streaming_tts_quickstart_test.py * Update requirements.txt * Update streaming_tts_quickstart.py * Update streaming_tts_quickstart.py --------- Co-authored-by: Katie McLaughlin <[email protected]>
1 parent 457dfe3 commit e134962

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
future==1.0.0
2-
google-cloud-texttospeech==2.14.1
2+
google-cloud-texttospeech==2.17.2
33
google-cloud-storage==2.9.0
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
# Copyright 2024 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 streaming sample application .
18+
19+
Example usage:
20+
python streaming_tts_quickstart.py
21+
"""
22+
23+
24+
def run_streaming_tts_quickstart():
25+
# [START tts_synthezise_streaming]
26+
"""Synthesizes speech from a stream of input text.
27+
"""
28+
from google.cloud import texttospeech
29+
import itertools
30+
31+
client = texttospeech.TextToSpeechClient()
32+
33+
# 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"))
35+
36+
# 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)
38+
39+
# Request generator. Consider using Gemini or another LLM with output streaming as a generator.
40+
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."))
45+
46+
streaming_responses = client.streaming_synthesize(itertools.chain([config_request], request_generator()))
47+
for response in streaming_responses:
48+
print(f"Audio content size in bytes is: {len(response.audio_content)}")
49+
# [END tts_synthezise_streaming]
50+
51+
52+
if __name__ == "__main__":
53+
run_streaming_tts_quickstart()
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
# Copyright 2024 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+
import streaming_tts_quickstart
17+
18+
19+
def test_streaming_synthesize(capsys):
20+
streaming_tts_quickstart.run_streaming_tts_quickstart()
21+
22+
out, err = capsys.readouterr()
23+
assert "Audio content size in bytes is" in out

0 commit comments

Comments
 (0)