Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Commit ce3a43d

Browse files
nnegreyandrewsg
authored andcommitted
Add text-to-speech beta samples [(#1421)](GoogleCloudPlatform/python-docs-samples#1421)
0 parents  commit ce3a43d

12 files changed

+613
-0
lines changed

samples/snippets/README.rst

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Text-to-Speech API Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/README.rst
8+
9+
10+
This directory contains samples for Google Cloud Text-to-Speech API. The `Google Cloud Text To Speech API`_ enables you to generate and customize synthesized speech from text or SSML.
11+
12+
13+
14+
15+
.. _Google Cloud Text-to-Speech API: https://cloud.google.com/text-to-speech/docs/
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
35+
36+
.. _Python Development Environment Setup Guide:
37+
https://cloud.google.com/python/setup
38+
39+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
40+
41+
.. code-block:: bash
42+
43+
$ virtualenv env
44+
$ source env/bin/activate
45+
46+
#. Install the dependencies needed to run the samples.
47+
48+
.. code-block:: bash
49+
50+
$ pip install -r requirements.txt
51+
52+
.. _pip: https://pip.pypa.io/
53+
.. _virtualenv: https://virtualenv.pypa.io/
54+
55+
Samples
56+
-------------------------------------------------------------------------------
57+
58+
Quickstart
59+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
60+
61+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
62+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/quickstart.py;/README.rst
63+
64+
65+
66+
67+
To run this sample:
68+
69+
.. code-block:: bash
70+
71+
$ python quickstart.py
72+
73+
74+
List voices
75+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
76+
77+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
78+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/list_voices.py;/README.rst
79+
80+
81+
82+
83+
To run this sample:
84+
85+
.. code-block:: bash
86+
87+
$ python list_voices.py
88+
89+
90+
Synthesize text
91+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
92+
93+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
94+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/synthesize_text.py;/README.rst
95+
96+
97+
98+
99+
To run this sample:
100+
101+
.. code-block:: bash
102+
103+
$ python synthesize_text.py
104+
105+
usage: synthesize_text.py [-h] (--text TEXT | --ssml SSML)
106+
107+
Google Cloud Text-To-Speech API sample application .
108+
109+
Example usage:
110+
python synthesize_text.py --text "hello"
111+
python synthesize_text.py --ssml "<speak>Hello there.</speak>"
112+
113+
optional arguments:
114+
-h, --help show this help message and exit
115+
--text TEXT The text from which to synthesize speech.
116+
--ssml SSML The ssml string from which to synthesize speech.
117+
118+
119+
120+
Synthesize file
121+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
122+
123+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
124+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/synthesize_file.py;/README.rst
125+
126+
127+
128+
129+
To run this sample:
130+
131+
.. code-block:: bash
132+
133+
$ python synthesize_file.py
134+
135+
usage: synthesize_file.py [-h] (--text TEXT | --ssml SSML)
136+
137+
Google Cloud Text-To-Speech API sample application .
138+
139+
Example usage:
140+
python synthesize_file.py --text resources/hello.txt
141+
python synthesize_file.py --ssml resources/hello.ssml
142+
143+
optional arguments:
144+
-h, --help show this help message and exit
145+
--text TEXT The text file from which to synthesize speech.
146+
--ssml SSML The ssml file from which to synthesize speech.
147+
148+
149+
150+
151+
152+
The client library
153+
-------------------------------------------------------------------------------
154+
155+
This sample uses the `Google Cloud Client Library for Python`_.
156+
You can read the documentation for more details on API usage and use GitHub
157+
to `browse the source`_ and `report issues`_.
158+
159+
.. _Google Cloud Client Library for Python:
160+
https://googlecloudplatform.github.io/google-cloud-python/
161+
.. _browse the source:
162+
https://github.com/GoogleCloudPlatform/google-cloud-python
163+
.. _report issues:
164+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
165+
166+
167+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

samples/snippets/README.rst.in

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud Text-to-Speech API
5+
short_name: Cloud TTS API
6+
url: https://cloud.google.com/text-to-speech/docs/
7+
description: >
8+
The `Google Cloud Text To Speech API`_ enables you to generate and customize synthesized speech from text or SSML.
9+
10+
setup:
11+
- auth
12+
- install_deps
13+
14+
samples:
15+
- name: Quickstart
16+
file: quickstart.py
17+
- name: List voices
18+
file: list_voices.py
19+
- name: Synthesize text
20+
file: synthesize_text.py
21+
show_help: True
22+
- name: Synthesize file
23+
file: synthesize_file.py
24+
show_help: True
25+
26+
cloud_client_library: true

samples/snippets/list_voices.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Text-To-Speech API sample application.
18+
19+
Example usage:
20+
python list_voices.py
21+
"""
22+
23+
24+
# [START tts_list_voices]
25+
def list_voices():
26+
"""Lists the available voices."""
27+
from google.cloud import texttospeech
28+
client = texttospeech.TextToSpeechClient()
29+
30+
# Performs the list voices request
31+
voices = client.list_voices()
32+
33+
for voice in voices.voices:
34+
# Display the voice's name. Example: tpc-vocoded
35+
print('Name: {}'.format(voice.name))
36+
37+
# Display the supported language codes for this voice. Example: "en-US"
38+
for language_code in voice.language_codes:
39+
print('Supported language: {}'.format(language_code))
40+
41+
# SSML Voice Gender values from google.cloud.texttospeech.enums
42+
ssml_voice_genders = ['SSML_VOICE_GENDER_UNSPECIFIED', 'MALE',
43+
'FEMALE', 'NEUTRAL']
44+
45+
# Display the SSML Voice Gender
46+
print('SSML Voice Gender: {}'.format(
47+
ssml_voice_genders[voice.ssml_gender]))
48+
49+
# Display the natural sample rate hertz for this voice. Example: 24000
50+
print('Natural Sample Rate Hertz: {}\n'.format(
51+
voice.natural_sample_rate_hertz))
52+
# [END tts_list_voices]
53+
54+
55+
if __name__ == '__main__':
56+
list_voices()

samples/snippets/list_voices_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2018, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import list_voices
15+
16+
17+
def test_list_voices(capsys):
18+
list_voices.list_voices()
19+
out, err = capsys.readouterr()
20+
21+
assert 'en-US' in out
22+
assert 'SSML Voice Gender: MALE' in out
23+
assert 'SSML Voice Gender: FEMALE' in out

samples/snippets/quickstart.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Text-To-Speech API sample application .
18+
19+
Example usage:
20+
python quickstart.py
21+
"""
22+
23+
24+
def run_quickstart():
25+
# [START tts_quickstart]
26+
"""Synthesizes speech from the input string of text or ssml.
27+
28+
Note: ssml must be well-formed according to:
29+
https://www.w3.org/TR/speech-synthesis/
30+
"""
31+
from google.cloud import texttospeech
32+
33+
# Instantiates a client
34+
client = texttospeech.TextToSpeechClient()
35+
36+
# Set the text input to be synthesized
37+
synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!")
38+
39+
# Build the voice request, select the language code ("en-US") and the ssml
40+
# voice gender ("neutral")
41+
voice = texttospeech.types.VoiceSelectionParams(
42+
language_code='en-US',
43+
ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)
44+
45+
# Select the type of audio file you want returned
46+
audio_config = texttospeech.types.AudioConfig(
47+
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
48+
49+
# Perform the text-to-speech request on the text input with the selected
50+
# voice parameters and audio file type
51+
response = client.synthesize_speech(synthesis_input, voice, audio_config)
52+
53+
# The response's audio_content is binary.
54+
with open('output.mp3', 'wb') as out:
55+
# Write the response to the output file.
56+
out.write(response.audio_content)
57+
print('Audio content written to file "output.mp3"')
58+
# [END tts_quickstart]
59+
60+
61+
if __name__ == '__main__':
62+
run_quickstart()

samples/snippets/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-texttospeech==0.1.0

samples/snippets/resources/hello.ssml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<speak>Hello there.</speak>

samples/snippets/resources/hello.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello there!

0 commit comments

Comments
 (0)