This tutorial demonstrates how to use Speech Synthesis Markup Language (SSML) to speak a text file of addresses. You can mark up a string of text with SSML tags to personalize synthetic audio from Text-to-Speech.
Plaintext | SSML rendering of plaintext |
---|---|
123 Street Ln |
<speak>123 Street Ln</speak> |
1 Number St |
<speak>1 Number St</speak> |
1 Piazza del Fibonacci |
<speak>1 Piazza del Fibonacci</speak> |
Objective
Send a synthetic speech request to Text-to-Speech using SSML and Text-to-Speech client libraries.
Costs
Refer to the Text-to-Speech pricing page for cost information.
Before you begin
- Make sure that you have a Text-to-Speech project in Google Cloud console.
- This tutorial allows you to use Java, Node.js, or Python. If you plan to use Java, download and install Maven. If you plan to use Node.js, download npm.
Download the code samples
To download the code samples, clone the Google Cloud GitHub samples for the programming language that you intend to use.
Java
This tutorial uses code in the texttospeech/cloud-client/src/main/java/com/example/texttospeech/
directory of the
Google Cloud Platform Java samples repository.
To download and navigate to the code for this tutorial, run the following commands
from the terminal.
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git cd java-docs-samples/texttospeech/cloud-client/src/main/java/com/example/texttospeech/
Node.js
This tutorial uses code in the texttospeech
directory of the
Google Cloud Platform Node.js samples repository.
To download and navigate to the code for this tutorial, run the
following commands
from the terminal.
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git cd texttospeech/
Python
This tutorial uses code in the texttospeech/snippets
directory of the
Google Cloud Platform Python samples repository.
To download and navigate to the code for this tutorial, run the following commands
from the terminal.
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd samples/snippets
Install the client library
This tutorial uses the Text-to-Speech client library.
Java
This tutorial uses the following dependencies.
Node.js
From terminal, run the follow command.
npm install @google-cloud/text-to-speech
Python
From terminal, run the follow command.
pip install --upgrade google-cloud-texttospeech
Set up your Google Cloud Platform credentials
Provide authentication credentials to your application code by setting the
environment variable GOOGLE_APPLICATION_CREDENTIALS
. This
variable applies only to your current shell session. If you want the variable
to apply to future shell sessions, set the variable in your shell startup file,
for example in the ~/.bashrc
or ~/.profile
file.
Linux or macOS
export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH
"
Replace KEY_PATH
with the path of the JSON file that contains your credentials.
For example:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
Windows
For PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH
"
Replace KEY_PATH
with the path of the JSON file that contains your credentials.
For example:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
For command prompt:
set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH
Replace KEY_PATH
with the path of the JSON file that contains your credentials.
Import libraries
This tutorial uses the following system and client libraries.
Java
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Java API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Node.js API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Python API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Use the Text-to-Speech API
The following function takes a string of text tagged with SSML and the name of an MP3 file. The function uses the text tagged with SSML to generate synthetic audio. The function saves the synthetic audio to the MP3 filename designated as a parameter.
The entire SSML input can only be read by a single voice.
You can set the voice in the
VoiceSelectionParams
object.
Java
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Java API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Node.js API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Python API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Personalize synthetic audio
The following function takes in the name of a text file and converts the contents of the file into a string of text tagged with SSML.
Java
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Java API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Node.js API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To learn how to install and use the client library for Text-to-Speech, see Text-to-Speech client libraries. For more information, see the Text-to-Speech Python API reference documentation.
To authenticate to Text-to-Speech, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Put it all together
This program uses the following input.
123 Street Ln, Small Town, IL 12345 USA 1 Jenny St & Number St, Tutone City, CA 86753 1 Piazza del Fibonacci, 12358 Pisa, Italy
Passing the above text to text_to_ssml()
generates the following tagged text.
<speak>123 Street Ln, Small Town, IL 12345 USA <break time="2s"/>1 Jenny St & Number St, Tutone City, CA 86753 <break time="2s"/>1 Piazza del Fibonacci, 12358 Pisa, Italy <break time="2s"/></speak>
Run the code
To generate an audio file of synthetic speech, run the following code from the command line.
Java
Linux or MacOS
From the java-docs-samples/texttospeech/cloud-client/
directory,
execute the following command on the command line.
$ mvn clean package
Windows
From the java-docs-samples/texttospeech/cloud-client/
directory,
execute the following command on the command line.
$ mvn clean package
Node.js
Linux or MacOS
In the hybridGlossaries.js
file, uncomment the TODO (developer)
commented-out variables.
In the following command, replace projectId with your Google Cloud project ID.
From the nodejs-docs-samples/texttospeech
directory,
execute the following command on the command line.
$ node ssmlAddresses.js projectId
Windows
In the hybridGlossaries.js
file, uncomment the TODO (developer)
commented-out variables.
In the following command, replace projectId with your Google Cloud project ID.
From the nodejs-docs-samples/texttospeech
directory,
execute the following command on the command line.
$env: C:/Node.js/node.exe C: ssmlAddresses.js projectId
Python
Linux or MacOS
From the python-docs-samples/texttospeech/snippets
directory,
execute the following command on the command line.
$ python ssml_addresses.py
Windows
From the python-docs-samples/texttospeech/snippets
directory,
execute the following command on the command line.
$env: C:/Python3/python.exe C: ssml_addresses.py
Check your output
This program outputs an example.mp3 audio file of synthetic speech.
Java
Navigate into java-docs-samples/texttospeech/cloud-client/resources/
directory.
Check the resources
directory for an example.mp3 file.
Node.js
Navigate into nodejs-docs-samples/texttospeech/resources/
directory.
Check the resources
directory for an example.mp3 file.
Python
Navigate into python-docs-samples/texttospeech/snippets/resources
.
Check the resources
directory for an example.mp3 file.
Listen to the following audio clip to check that your example.mp3 file sounds the same.
Troubleshoot
Forgetting to set the GOOGLE_APPLICATION_CREDENTIALS environment variable on the command line generates the error message:
The Application Default Credentials are not available.
Passing
text_to_ssml()
the name of a non-existent file generates the error message:IOError: [Errno 2] No such file or directory
Passing
ssml_to_audio()
a ssml_text parameter which contains None generates the error message:InvalidArgument: 400 Invalid input type. Type has to be text or SSML
Make sure that you are running the code from the correct directory.
What's next
- Explore other SSML tags.
- Learn how to use SSML with Translation and Vision
Clean up
To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial, use the Google Cloud console to delete your project if you do not need it.
Delete your project
- In the Google Cloud console, go to the Projects page.
- In the project list, select the project you want to delete and click Delete.
- In the dialog box, type the project ID, and click Shut down to delete the project.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-12-19 UTC.