Skip to content

Commit 0c24763

Browse files
committed
Merge remote-tracking branch 'migration/main' into python-texttospeech-migration
2 parents 8501411 + 97a1bf9 commit 0c24763

19 files changed

+993
-0
lines changed

texttospeech/snippets/README.rst

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
2+
.. This file is automatically generated. Do not edit this file directly.
3+
4+
Google Cloud Text-to-Speech API Python Samples
5+
===============================================================================
6+
7+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
8+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/README.rst
9+
10+
11+
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.
12+
13+
14+
15+
16+
.. _Google Cloud Text-to-Speech API: https://cloud.google.com/text-to-speech/docs/
17+
18+
19+
Setup
20+
-------------------------------------------------------------------------------
21+
22+
23+
24+
Authentication
25+
++++++++++++++
26+
27+
This sample requires you to have authentication setup. Refer to the
28+
`Authentication Getting Started Guide`_ for instructions on setting up
29+
credentials for applications.
30+
31+
.. _Authentication Getting Started Guide:
32+
https://cloud.google.com/docs/authentication/getting-started
33+
34+
35+
36+
37+
Install Dependencies
38+
++++++++++++++++++++
39+
40+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
41+
42+
.. code-block:: bash
43+
44+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
45+
46+
#. 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.
47+
48+
.. _Python Development Environment Setup Guide:
49+
https://cloud.google.com/python/setup
50+
51+
#. Create a virtualenv. Samples are compatible with Python 3.6+.
52+
53+
.. code-block:: bash
54+
55+
$ virtualenv env
56+
$ source env/bin/activate
57+
58+
#. Install the dependencies needed to run the samples.
59+
60+
.. code-block:: bash
61+
62+
$ pip install -r requirements.txt
63+
64+
.. _pip: https://pip.pypa.io/
65+
.. _virtualenv: https://virtualenv.pypa.io/
66+
67+
68+
69+
70+
71+
72+
Samples
73+
-------------------------------------------------------------------------------
74+
75+
76+
Quickstart
77+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
78+
79+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
80+
: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
81+
82+
83+
84+
85+
To run this sample:
86+
87+
.. code-block:: bash
88+
89+
$ python quickstart.py
90+
91+
92+
93+
94+
List voices
95+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
96+
97+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
98+
: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
99+
100+
101+
102+
103+
To run this sample:
104+
105+
.. code-block:: bash
106+
107+
$ python list_voices.py
108+
109+
110+
111+
112+
Synthesize text
113+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
114+
115+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
116+
: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
117+
118+
119+
120+
121+
To run this sample:
122+
123+
.. code-block:: bash
124+
125+
$ python synthesize_text.py
126+
127+
128+
usage: synthesize_text.py [-h] (--text TEXT | --ssml SSML)
129+
130+
Google Cloud Text-To-Speech API sample application .
131+
132+
Example usage:
133+
python synthesize_text.py --text "hello"
134+
python synthesize_text.py --ssml "<speak>Hello there.</speak>"
135+
136+
optional arguments:
137+
-h, --help show this help message and exit
138+
--text TEXT The text from which to synthesize speech.
139+
--ssml SSML The ssml string from which to synthesize speech.
140+
141+
142+
143+
144+
145+
Synthesize file
146+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
147+
148+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
149+
: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
150+
151+
152+
153+
154+
To run this sample:
155+
156+
.. code-block:: bash
157+
158+
$ python synthesize_file.py
159+
160+
161+
usage: synthesize_file.py [-h] (--text TEXT | --ssml SSML)
162+
163+
Google Cloud Text-To-Speech API sample application .
164+
165+
Example usage:
166+
python synthesize_file.py --text resources/hello.txt
167+
python synthesize_file.py --ssml resources/hello.ssml
168+
169+
optional arguments:
170+
-h, --help show this help message and exit
171+
--text TEXT The text file from which to synthesize speech.
172+
--ssml SSML The ssml file from which to synthesize speech.
173+
174+
175+
176+
177+
178+
Audio profile
179+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
180+
181+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
182+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/audio_profile.py,/README.rst
183+
184+
185+
186+
187+
To run this sample:
188+
189+
.. code-block:: bash
190+
191+
$ python audio_profile.py
192+
193+
194+
usage: audio_profile.py [-h] [--output OUTPUT] [--text TEXT]
195+
[--effects_profile_id EFFECTS_PROFILE_ID]
196+
197+
Google Cloud Text-To-Speech API sample application for audio profile.
198+
199+
Example usage:
200+
python audio_profile.py --text "hello" --effects_profile_id
201+
"telephony-class-application" --output "output.mp3"
202+
203+
optional arguments:
204+
-h, --help show this help message and exit
205+
--output OUTPUT The output mp3 file.
206+
--text TEXT The text from which to synthesize speech.
207+
--effects_profile_id EFFECTS_PROFILE_ID
208+
The audio effects profile id to be applied.
209+
210+
211+
212+
213+
214+
215+
216+
217+
218+
The client library
219+
-------------------------------------------------------------------------------
220+
221+
This sample uses the `Google Cloud Client Library for Python`_.
222+
You can read the documentation for more details on API usage and use GitHub
223+
to `browse the source`_ and `report issues`_.
224+
225+
.. _Google Cloud Client Library for Python:
226+
https://googlecloudplatform.github.io/google-cloud-python/
227+
.. _browse the source:
228+
https://github.com/GoogleCloudPlatform/google-cloud-python
229+
.. _report issues:
230+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
231+
232+
233+
234+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

texttospeech/snippets/README.rst.in

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
- name: Audio profile
26+
file: audio_profile.py
27+
show_help: True
28+
29+
cloud_client_library: true
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2018 Google LLC. 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 for audio profile.
18+
19+
Example usage:
20+
python audio_profile.py --text "hello" --effects_profile_id
21+
"telephony-class-application" --output "output.mp3"
22+
"""
23+
24+
import argparse
25+
26+
27+
# [START tts_synthesize_text_audio_profile]
28+
# [START tts_synthesize_text_audio_profile_beta]
29+
def synthesize_text_with_audio_profile(text, output, effects_profile_id):
30+
"""Synthesizes speech from the input string of text."""
31+
from google.cloud import texttospeech
32+
33+
client = texttospeech.TextToSpeechClient()
34+
35+
input_text = texttospeech.SynthesisInput(text=text)
36+
37+
# Note: the voice can also be specified by name.
38+
# Names of voices can be retrieved with client.list_voices().
39+
voice = texttospeech.VoiceSelectionParams(language_code="en-US")
40+
41+
# Note: you can pass in multiple effects_profile_id. They will be applied
42+
# in the same order they are provided.
43+
audio_config = texttospeech.AudioConfig(
44+
audio_encoding=texttospeech.AudioEncoding.MP3,
45+
effects_profile_id=[effects_profile_id],
46+
)
47+
48+
response = client.synthesize_speech(
49+
input=input_text, voice=voice, audio_config=audio_config
50+
)
51+
52+
# The response's audio_content is binary.
53+
with open(output, "wb") as out:
54+
out.write(response.audio_content)
55+
print('Audio content written to file "%s"' % output)
56+
57+
58+
# [END tts_synthesize_text_audio_profile_beta]
59+
# [END tts_synthesize_text_audio_profile]
60+
61+
62+
if __name__ == "__main__":
63+
parser = argparse.ArgumentParser(
64+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
65+
)
66+
parser.add_argument("--output", help="The output mp3 file.")
67+
parser.add_argument("--text", help="The text from which to synthesize speech.")
68+
parser.add_argument(
69+
"--effects_profile_id", help="The audio effects profile id to be applied."
70+
)
71+
72+
args = parser.parse_args()
73+
74+
synthesize_text_with_audio_profile(args.text, args.output, args.effects_profile_id)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2018, Google, LLC.
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 os
15+
import os.path
16+
17+
import audio_profile
18+
19+
TEXT = "hello"
20+
OUTPUT = "output.mp3"
21+
EFFECTS_PROFILE_ID = "telephony-class-application"
22+
23+
24+
def test_audio_profile(capsys):
25+
if os.path.exists(OUTPUT):
26+
os.remove(OUTPUT)
27+
assert not os.path.exists(OUTPUT)
28+
audio_profile.synthesize_text_with_audio_profile(TEXT, OUTPUT, EFFECTS_PROFILE_ID)
29+
out, err = capsys.readouterr()
30+
31+
assert ('Audio content written to file "%s"' % OUTPUT) in out
32+
assert os.path.exists(OUTPUT)
33+
os.remove(OUTPUT)

0 commit comments

Comments
 (0)