Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 696a1cf

Browse filesBrowse files
crowdusnnegrey
authored andcommitted
Python Text-to-Speech "Speaking addresses with SSML" tutorial (GoogleCloudPlatform#2255)
* initial tts ssml commit. TODO readme, tests * added formatting comments to src * working on tutorial files * fixed readme * fixed block comment syntax error * made 4-space tabs * more stylistic spaces and newline at end of file * more spaces * fixed block comments * fixed ambiguous variable name * still fixing style issues * more formatting changes re: PR review * renamed tests file to align with GCP convention * refactored tests to unit tests * removing obsolete testing files * fixing Linter bugs * fixing spacing Linter bugs * per Linter, removed excessive blank lines * nailed down final two blank line linter bugs * fixed all too-long lines except for tests * fixed newline typo in special character audio test case * Linter should be good to go. Fixed final lines-too-long error * more Linter errors :) * line continuation indent fix for Linter * experimenting with line continuation indent to make Linter happy * fun with line continuation indentation * removing outdated example.mp3 file * organizing repo. adding resources directory * removing pesky example.mp3 file * adding both tagged and un-tagged examples * fixing copyright spacing * auto-generated readme * whoops. forgot to add new readme. * generalizing HTML ampersand replacement * standardizing main * consolidating example directories * cleaning up repo & refactored tests * fixed indentation in tests * cleaning up resources * cleaning up * updated ampersand in resource files * added html python3 support * removed trailing whitespace * Refactored tests for pytest * Refactored tests for pytest * Lint * removing unneccessary test * removed test docstrings * consolidated test ssml * deleted test mp3 file * actually consolidating example ssml * more consolidation * consistency check for example ssmls * removing tagged examples * removed None checker in ssml_to_audio
1 parent 55bb04f commit 696a1cf
Copy full SHA for 696a1cf

File tree

Expand file treeCollapse file tree

8 files changed

+294
-0
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+294
-0
lines changed
+97Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Text-to-Speech API 'Speaking Addresses with SSML' Tutorial 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 'Speaking Addresses with SSML' Tutorial. Google Cloud Text-To-Speech API 'Speaking Addresses with SSML Tutorial'_ enables you to use Speech Synthesis Markup Language (SSML) to speak a text file of addresses. You can embed SSML commands in a string of text to personalize synthetic audio from Cloud Text-to-Speech API.
11+
12+
13+
14+
15+
.. _Google Cloud Text-to-Speech API 'Speaking Addresses with SSML' Tutorial: https://cloud.google.com/text-to-speech/docs/ssml-tutorial
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+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. 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.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
Samples
62+
-------------------------------------------------------------------------------
63+
64+
Speaking addresses with SSML Tutorial
65+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66+
67+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
68+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/tts.py,/README.rst
69+
70+
71+
72+
73+
To run this sample:
74+
75+
.. code-block:: bash
76+
77+
$ python tts.py
78+
79+
80+
81+
82+
The client library
83+
-------------------------------------------------------------------------------
84+
85+
This sample uses the `Google Cloud Client Library for Python`_.
86+
You can read the documentation for more details on API usage and use GitHub
87+
to `browse the source`_ and `report issues`_.
88+
89+
.. _Google Cloud Client Library for Python:
90+
https://googlecloudplatform.github.io/google-cloud-python/
91+
.. _browse the source:
92+
https://github.com/GoogleCloudPlatform/google-cloud-python
93+
.. _report issues:
94+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
95+
96+
97+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
# This file is used to generate README.rst
3+
4+
product:
5+
name: Google Cloud Text-to-Speech API 'Speaking Addresses with SSML' Tutorial
6+
short_name: Cloud TTS API SSML Addresses Tutorial
7+
url: https://cloud.google.com/text-to-speech/docs/ssml-tutorial
8+
description: >
9+
Google Cloud Text-To-Speech API 'Speaking Addresses with SSML Tutorial'_ enables you to use Speech Synthesis Markup Language (SSML) to speak a text file of addresses. You can embed SSML commands in a string of text to personalize synthetic audio from Cloud Text-to-Speech API.
10+
11+
setup:
12+
- auth
13+
- install_deps
14+
15+
samples:
16+
- name: Speaking addresses with SSML Tutorial
17+
file: tts.py
18+
19+
cloud_client_library: true
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-texttospeech==0.4.0
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<speak>123 Street Ln, Small Town, IL 12345 USA
2+
<break time="2s"/>1 Jenny St &amp; Number St, Tutone City, CA 86753
3+
<break time="2s"/>1 Piazza del Fibonacci, 12358 Pisa, Italy
4+
<break time="2s"/></speak>
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
123 Street Ln, Small Town, IL 12345 USA
2+
1 Jenny St & Number St, Tutone City, CA 86753
3+
1 Piazza del Fibonacci, 12358 Pisa, Italy
Binary file not shown.

‎texttospeech/ssml_addresses/tts.py

Copy file name to clipboard
+116Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START tts_ssml_address_imports]
17+
from google.cloud import texttospeech
18+
19+
# For Python 3, instead use:
20+
# import html
21+
import cgi
22+
# [END tts_ssml_address_imports]
23+
24+
25+
# [START tts_ssml_address_audio]
26+
def ssml_to_audio(ssml_text, outfile):
27+
# Generates SSML text from plaintext.
28+
#
29+
# Given a string of SSML text and an output file name, this function
30+
# calls the Text-to-Speech API. The API returns a synthetic audio
31+
# version of the text, formatted according to the SSML commands. This
32+
# function saves the synthetic audio to the designated output file.
33+
#
34+
# Args:
35+
# ssml_text: string of SSML text
36+
# outfile: string name of file under which to save audio output
37+
#
38+
# Returns:
39+
# nothing
40+
41+
# Instantiates a client
42+
client = texttospeech.TextToSpeechClient()
43+
44+
# Sets the text input to be synthesized
45+
synthesis_input = texttospeech.types.SynthesisInput(ssml=ssml_text)
46+
47+
# Builds the voice request, selects the language code ("en-US") and
48+
# the SSML voice gender ("MALE")
49+
voice = texttospeech.types.VoiceSelectionParams(
50+
language_code='en-US',
51+
ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE)
52+
53+
# Selects the type of audio file to return
54+
audio_config = texttospeech.types.AudioConfig(
55+
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
56+
57+
# Performs the text-to-speech request on the text input with the selected
58+
# voice parameters and audio file type
59+
response = client.synthesize_speech(synthesis_input, voice, audio_config)
60+
61+
# Writes the synthetic audio to the output file.
62+
with open(outfile, 'wb') as out:
63+
out.write(response.audio_content)
64+
print('Audio content written to file ' + outfile)
65+
# [END tts_ssml_address_audio]
66+
67+
68+
# [START tts_ssml_address_ssml]
69+
def text_to_ssml(inputfile):
70+
# Generates SSML text from plaintext.
71+
# Given an input filename, this function converts the contents of the text
72+
# file into a string of formatted SSML text. This function formats the SSML
73+
# string so that, when synthesized, the synthetic audio will pause for two
74+
# seconds between each line of the text file. This function also handles
75+
# special text characters which might interfere with SSML commands.
76+
#
77+
# Args:
78+
# inputfile: string name of plaintext file
79+
#
80+
# Returns:
81+
# A string of SSML text based on plaintext input
82+
83+
# Parses lines of input file
84+
with open(inputfile, 'r') as f:
85+
raw_lines = f.read()
86+
87+
# Replace special characters with HTML Ampersand Character Codes
88+
# These Codes prevent the API from confusing text with
89+
# SSML commands
90+
# For example, '<' --> '&lt;' and '&' --> '&amp;'
91+
92+
# For Python 3, instead use:
93+
# escaped_lines = html.escape(raw_lines)
94+
escaped_lines = cgi.escape(raw_lines)
95+
96+
# Convert plaintext to SSML
97+
# Wait two seconds between each address
98+
ssml = '<speak>{}</speak>'.format(
99+
escaped_lines.replace('\n', '\n<break time="2s"/>'))
100+
101+
# Return the concatenated string of ssml script
102+
return ssml
103+
# [END tts_ssml_address_ssml]
104+
105+
106+
# [START tts_ssml_address_test]
107+
def main():
108+
# test example address file
109+
plaintext = 'resources/example.txt'
110+
ssml_text = text_to_ssml(plaintext)
111+
ssml_to_audio(ssml_text, 'resources/example.mp3')
112+
# [END tts_ssml_address_test]
113+
114+
115+
if __name__ == '__main__':
116+
main()
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from tts import text_to_ssml
16+
from tts import ssml_to_audio
17+
18+
import filecmp
19+
import os
20+
21+
22+
def test_text_to_ssml(capsys):
23+
24+
# Read expected SSML output from resources
25+
with open('resources/example.ssml', 'r') as f:
26+
expected_ssml = f.read()
27+
28+
# Assert plaintext converted to SSML
29+
input_text = 'resources/example.txt'
30+
tested_ssml = text_to_ssml(input_text)
31+
assert expected_ssml == tested_ssml
32+
33+
34+
def test_ssml_to_audio(capsys):
35+
36+
# Read SSML input from resources
37+
with open('resources/example.ssml', 'r') as f:
38+
input_ssml = f.read()
39+
40+
# Assert audio file generated
41+
ssml_to_audio(input_ssml, 'test_example.mp3')
42+
assert os.path.isfile('test_example.mp3')
43+
44+
# Assert audio file generated correctly
45+
assert filecmp.cmp('test_example.mp3',
46+
'resources/expected_example.mp3',
47+
shallow=True)
48+
out, err = capsys.readouterr()
49+
50+
# Delete test file
51+
os.remove("test_example.mp3")
52+
53+
# Assert success message printed
54+
assert "Audio content written to file test_example.mp3" in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.