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

Commit b53c5f8

Browse files
happyhumanbusunkim96
authored andcommitted
[DO NOT MERGE] Added Audio Profile sample [(#1538)](GoogleCloudPlatform/python-docs-samples#1538)
* Added Audio Profile sample * Adjusted the row lengths * Adjusted the row length * Fixed Import orders * Fixed print statement * Debugging the unit test * Fixed the unit test * Some fixes per Noah's suggestions. * Renamed the function name in the test. * Multilined the long line. * Fixed the misspelling * Fixed the long line * Forcing the CicleCi to build again * Changing Inc to LLC * Updated library version. * Generated README.rst
1 parent 8c0eb3a commit b53c5f8

File tree

5 files changed

+143
-3
lines changed

5 files changed

+143
-3
lines changed

samples/README.rst

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Google Cloud Text-to-Speech API Python Samples
77
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/README.rst
88

99

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.
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.
1111

1212

1313

@@ -153,6 +153,39 @@ To run this sample:
153153
154154
155155
156+
Audio profile
157+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
158+
159+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
160+
: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
161+
162+
163+
164+
165+
To run this sample:
166+
167+
.. code-block:: bash
168+
169+
$ python audio_profile.py
170+
171+
usage: audio_profile.py [-h] [--output OUTPUT] [--text TEXT]
172+
[--effects_profile_id EFFECTS_PROFILE_ID]
173+
174+
Google Cloud Text-To-Speech API sample application for audio profile.
175+
176+
Example usage:
177+
python audio_profile.py --text "hello" --effects_profile_id
178+
"telephony-class-application"
179+
180+
optional arguments:
181+
-h, --help show this help message and exit
182+
--output OUTPUT The output mp3 file.
183+
--text TEXT The text from which to synthesize speech.
184+
--effects_profile_id EFFECTS_PROFILE_ID
185+
The audio effects profile id to be applied.
186+
187+
188+
156189
157190
158191
The client library
@@ -170,4 +203,4 @@ to `browse the source`_ and `report issues`_.
170203
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
171204

172205

173-
.. _Google Cloud SDK: https://cloud.google.com/sdk/
206+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

samples/README.rst.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ samples:
2222
- name: Synthesize file
2323
file: synthesize_file.py
2424
show_help: True
25+
- name: Audio profile
26+
file: audio_profile.py
27+
show_help: True
2528

2629
cloud_client_library: true

samples/audio_profile.py

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

samples/audio_profile_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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,
29+
EFFECTS_PROFILE_ID)
30+
out, err = capsys.readouterr()
31+
32+
assert ('Audio content written to file "%s"' % OUTPUT) in out
33+
assert os.path.exists(OUTPUT)
34+
os.remove(OUTPUT)

samples/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-cloud-texttospeech==0.1.0
1+
google-cloud-texttospeech==0.2.0

0 commit comments

Comments
 (0)