- Cloud Text-to-Speech

- Documentation

- Guides

Was this helpful?

Cloud Text-to-Speech basics

Text-to-Speech allows developers to create natural-sounding, synthetic human speech as playable audio. You can use the audio data files you create using Text-to-Speech to power your applications or augment media like videos or audio recordings (in compliance with the Google Cloud Platform Terms of Service including compliance with all applicable law).

Text-to-Speech converts text or Speech Synthesis Markup Language (SSML) input into audio data like MP3 or LINEAR16 (the encoding used in WAV files).

This document is a guide to the fundamental concepts of using Text-to-Speech. Before diving into the API itself, review the quickstarts.

Basic example

Text-to-Speech is ideal for any application that plays audio of human speech to users. It allows you to convert arbitrary strings, words, and sentences into the sound of a person speaking the same things.

Imagine that you have a voice assistant app that provides natural language feedback to your users as playable audio files. Your app might take an action and then provide human speech as feedback to the user.

For example, your app may want to report that it successfully added an event to the user's calendar. Your app constructs a response string to report the success to the user, something like "I've added the event to your calendar."

With Text-to-Speech, you can convert that response string to actual human speech to play back to the user, similar to the example provided below.

Example 1. Audio file generated from Text-to-Speech

To create an audio file like example 1, you send a request to Text-to-Speech like the following code snippet.

Note: The following sample uses the gcloud auth application-default print-access-token command to retrieve an authorization token for the request. You must already have gcloud installed to run the code sample.

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -H "Content-Type: application/json; charset=utf-8" --data "{

'input':{

'text':'I\'ve added the event to your calendar.'

},

'voice':{

'languageCode':'en-gb',

'name':'en-GB-Standard-A',

'ssmlGender':'FEMALE'

},

'audioConfig':{

'audioEncoding':'MP3'

}

}" "https://texttospeech.googleapis.com/v1/text:synthesize"

Speech synthesis

The process of translating text input into audio data is called synthesis and the output of synthesis is called synthetic speech. Text-to-Speech takes two types of input: raw text or SSML-formatted data (discussed below). To create a new audio file, you call the synthesize endpoint of the API.

The speech synthesis process generates raw audio data as a base64-encoded string. You must decode the base64-encoded string into an audio file before an application can play it. Most platforms and operating systems have tools for decoding base64 text into playable media files.

Note: You must decode the base64 string returned from Text-to-Speech before you can play it. For more information about how to decode base64 data, see Decoding Base64-Encoded Audio Content

To learn more about synthesis, review the quickstarts or the Creating Voice Audio Files page.

Voices

Text-to-Speech creates raw audio data of natural, human speech. That is, it creates audio that sounds like a person talking. When you send a synthesis request to Text-to-Speech, you must specify a voice that 'speaks' the words.

Text-to-Speech has a wide selection of custom voices available for you to use. The voices differ by language, gender, and accent (for some languages). For example, you can create audio that mimics the sound of a female English speaker with a British accent like example 1, above. You can also convert the same text into a different voice, say a male English speaker with an Australian accent.

Example 2. Audio file generated with en-AU speaker

To see the complete list of the available voices, see Supported Voices.

WaveNet voices

Along with other, traditional synthetic voices, Text-to-Speech also provides premium, WaveNet-generated voices. Users find the Wavenet-generated voices to be more warm and human-like than other synthetic voices.

The key difference to a WaveNet voice is the WaveNet model used to generate the voice. WaveNet models have been trained using raw audio samples of actual humans speaking. As a result, these models generate synthetic speech with more human-like emphasis and inflection on syllables, phonemes, and words.

Compare the following two samples of synthetic speech.

Example 3. Audio file generated with a standard voice

Example 4. Audio file generated with a WaveNet voice

To learn more about the benefits of WaveNet-generated voices, see WaveNet and Other Synthetic Voices.

Other audio output settings

Besides the voice, you can also configure other aspects of the audio data output created by speech synthesis. Text-to-Speech supports configuring the speaking rate, pitch, volume, and sample rate hertz.

Review the AudioConfig reference for more information.

Speech Synthesis Markup Language (SSML) support

You can enhance the synthetic speech produced by Text-to-Speech by marking up the text using Speech Synthesis Markup Language (SSML). SSML enables you to insert pauses, acronym pronunciations, or other additional details into the audio data created by Text-to-Speech. Text-to-Speech supports a subset of the available SSML elements.

Note: Text-to-Speech does not support all SSML elements for all available languages.

For example, you can ensure that the synthetic speech correctly pronounces ordinal numbers by providing Text-to-Speech with SSML input that marks ordinal numbers as such.

Example 5. Audio file generated from plain text input

Example 6. Audio file generated from SSML input

To learn more about how to synthesize speech from SSML, see Creating Voice Audio Files

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how Text-to-Speech performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Try Text-to-Speech free

Quickstart: Using the command line

This document walks you through the process of making a request to Text-to-Speech using the command line. To learn more about the fundamental concepts in Text-to-Speech, read Text-to-Speech Basics.

Prerequisites

Before you can send a request to the Text-to-Speech API, you must have completed the following actions. See the before you begin page for details.

- Enable Text-to-Speech on a GCP project.

1. Make sure billing is enabled for Text-to-Speech.

2. Create and/or assign one or more service accounts to Text-to-Speech.

3. Download a service account credential key.

- Set your authentication environment variable.

Synthesize audio from text

You can convert text to audio by making an HTTP POST request to the https://texttospeech.googleapis.com/v1/text:synthesize endpoint. In the body of your POST command, specify the type of voice to synthesize in the voice configuration section, specify the text to synthesize in the text field of the input section, and specify the type of audio to create in the audioConfig section.

1. Execute the REST request below at the command line to synthesize audio from text using Text-to-Speech. The command uses the gcloud auth application-default print-access-token command to retrieve an authorization token for the request.

HTTP method and URL:

POST https://texttospeech.googleapis.com/v1/text:synthesize

Request JSON body:

{

"input":{

"text":"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets."

},

"voice":{

"languageCode":"en-gb",

"name":"en-GB-Standard-A",

"ssmlGender":"FEMALE"

},

"audioConfig":{

"audioEncoding":"MP3"

}

}

To send your request, expand one of these options:

1. curl (Linux, macOS, or Cloud Shell)

PowerShell (Windows)

You should receive a JSON response similar to the following:

{

"audioContent": "//NExAASCCIIAAhEAGAAEMW4kAYPnwwIKw/BBTpwTvB+IAxIfghUfW.."

}

2. The JSON output for the REST command contains the synthesized audio in base64-encoded format. Copy the contents of the audioContent field into a new file named synthesize-output-base64.txt. Your new file will look something like the following:

//NExAARqoIIAAhEuWAAAGNmBGMY4EBcxvABAXBPmPIAF//yAuh9Tn5CEap3/o

...

VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

3. Decode the contents of the synthesize-output-base64.txt file into a new file named synthesized-audio.mp3. For information on decoding base64, see Decoding Base64-Encoded Audio Content.

LinuxMac OSXWindows

1. Copy only the base-64 encoded content into a text file.

2. Decode the source text file using the base64 command line tool by using the -d flag:

$ base64 SOURCE_BASE64_TEXT_FILE -d > DESTINATION_AUDIO_FILE

4. Play the contents of synthesized-audio.mp3 in an audio application or on an audio device. You can also open the synthesized-audio.mp3 in the Chrome browser to play the audio by navigating to the folder that contains the file, for example file://my_file_path/synthesized-audio.mp3

Clean up

To avoid unnecessary Google Cloud Platform charges, use the Cloud Console to delete your project if you do not need it.

What's next

- Learn more about Cloud Text-to-Speech by reading the basics.

- Review the list of available voices you can use for synthetic speech.