Skip to content

Commit bf9f6f7

Browse files
authored
Merge pull request #8483 from GoogleCloudPlatform/python-texttospeech-migration
migrate code from googleapis/python-texttospeech
2 parents 4827801 + 3168389 commit bf9f6f7

22 files changed

+1029
-2
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
/secretmanager/**/* @GoogleCloudPlatform/aap-dpes @GoogleCloudPlatform/python-samples-reviewers
7070
/storage/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
7171
/storagetransfer/**/* @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-reviewers
72+
/texttospeech/**/* @GoogleCloudPlatform/dee-platform-ops @GoogleCloudPlatform/python-samples-reviewers
7273
/trace/**/* @ymotongpoo @GoogleCloudPlatform/python-samples-reviewers
7374
/translate/**/* @nicain @GoogleCloudPlatform/python-samples-reviewers
7475
/talent/**/* @GoogleCloudPlatform/python-samples-reviewers

.github/blunderbuss.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,15 @@ assign_issues_by:
125125
- 'api: datalabeling'
126126
to:
127127
- GoogleCloudPlatform/python-samples-reviewers
128-
- ivanmkc
128+
- ivanmkc
129129
- labels:
130130
- 'api: monitoring'
131-
to:
131+
to:
132132
- GoogleCloudPlatform/dee-observability
133+
- labels:
134+
- 'api: texttospeech'
135+
to:
136+
- GoogleCloudPlatform/dee-platform-ops
133137
- labels:
134138
- 'api: datacatalog'
135139
to:

.github/header-checker-lint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ ignoreFiles:
1616
- "**/constraints-test.txt"
1717
- "**/apt.txt"
1818
- "**/ghcnd-stations.txt"
19+
- "texttospeech/snippets/resources/example.txt"
20+
- "texttospeech/snippets/resources/hello.txt"
21+
1922

2023
ignoreLicenseYear: true
2124

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

0 commit comments

Comments
 (0)