File size: 11,115 Bytes
5853bf1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# Speech generation (text-to-speech)
Source: <https://ai.google.dev/gemini-api/docs/speech-generation>
---
The Gemini API can transform text input into single speaker or multi-speaker audio using native text-to-speech (TTS) generation capabilities. Text-to-speech (TTS) generation is _controllable_ , meaning you can use natural language to structure interactions and guide the _style_ , _accent_ , _pace_ , and _tone_ of the audio.
The TTS capability differs from speech generation provided through the [Live API](/gemini-api/docs/live), which is designed for interactive, unstructured audio, and multimodal inputs and outputs. While the Live API excels in dynamic conversational contexts, TTS through the Gemini API is tailored for scenarios that require exact text recitation with fine-grained control over style and sound, such as podcast or audiobook generation.
This guide shows you how to generate single-speaker and multi-speaker audio from text.
**Preview:** Native text-to-speech (TTS) is in [Preview](/gemini-api/docs/models#preview).
## Before you begin
Ensure you use a Gemini 2.5 model variant with native text-to-speech (TTS) capabilities, as listed in the [Supported models](/gemini-api/docs/speech-generation#supported-models) section. For optimal results, consider which model best fits your specific use case.
You may find it useful to [test the Gemini 2.5 TTS models in AI Studio](https://aistudio.google.com/generate-speech) before you start building.
**Note:** TTS models accept text-only inputs and produce audio-only outputs. For a complete list of restrictions specific to TTS models, review the [Limitations](/gemini-api/docs/speech-generation#limitations) section.
## Single-speaker text-to-speech
To convert text to single-speaker audio, set the response modality to "audio", and pass a `SpeechConfig` object with `VoiceConfig` set. You'll need to choose a voice name from the prebuilt output voices.
This example saves the output audio from the model in a wave file:
from google import genai
from google.genai import types
import wave
# Set up the wave file to save the output:
def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
with wave.open(filename, "wb") as wf:
wf.setnchannels(channels)
wf.setsampwidth(sample_width)
wf.setframerate(rate)
wf.writeframes(pcm)
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash-preview-tts",
contents="Say cheerfully: Have a wonderful day!",
config=types.GenerateContentConfig(
response_modalities=["AUDIO"],
speech_config=types.SpeechConfig(
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(
voice_name='Kore',
)
)
),
)
)
data = response.candidates[0].content.parts[0].inline_data.data
file_name='out.wav'
wave_file(file_name, data) # Saves the file to current directory
For more code samples, refer to the "TTS - Get Started" file in the cookbooks repository:
[View on GitHub](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/Get_started_TTS.ipynb)
## Multi-speaker text-to-speech
For multi-speaker audio, you'll need a `MultiSpeakerVoiceConfig` object with each speaker (up to 2) configured as a `SpeakerVoiceConfig`. You'll need to define each `speaker` with the same names used in the prompt:
from google import genai
from google.genai import types
import wave
# Set up the wave file to save the output:
def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
with wave.open(filename, "wb") as wf:
wf.setnchannels(channels)
wf.setsampwidth(sample_width)
wf.setframerate(rate)
wf.writeframes(pcm)
client = genai.Client()
prompt = """TTS the following conversation between Joe and Jane:
Joe: How's it going today Jane?
Jane: Not too bad, how about you?"""
response = client.models.generate_content(
model="gemini-2.5-flash-preview-tts",
contents=prompt,
config=types.GenerateContentConfig(
response_modalities=["AUDIO"],
speech_config=types.SpeechConfig(
multi_speaker_voice_config=types.MultiSpeakerVoiceConfig(
speaker_voice_configs=[
types.SpeakerVoiceConfig(
speaker='Joe',
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(
voice_name='Kore',
)
)
),
types.SpeakerVoiceConfig(
speaker='Jane',
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(
voice_name='Puck',
)
)
),
]
)
)
)
)
data = response.candidates[0].content.parts[0].inline_data.data
file_name='out.wav'
wave_file(file_name, data) # Saves the file to current directory
## Controlling speech style with prompts
You can control style, tone, accent, and pace using natural language prompts for both single- and multi-speaker TTS. For example, in a single-speaker prompt, you can say:
Say in an spooky whisper:
"By the pricking of my thumbs...
Something wicked this way comes"
In a multi-speaker prompt, provide the model with each speaker's name and corresponding transcript. You can also provide guidance for each speaker individually:
Make Speaker1 sound tired and bored, and Speaker2 sound excited and happy:
Speaker1: So... what's on the agenda today?
Speaker2: You're never going to guess!
Try using a voice option that corresponds to the style or emotion you want to convey, to emphasize it even more. In the previous prompt, for example, _Enceladus_ 's breathiness might emphasize "tired" and "bored", while _Puck_ 's upbeat tone could complement "excited" and "happy".
## Generating a prompt to convert to audio
The TTS models only output audio, but you can use [other models](/gemini-api/docs/models) to generate a transcript first, then pass that transcript to the TTS model to read aloud.
from google import genai
from google.genai import types
client = genai.Client()
transcript = client.models.generate_content(
model="gemini-2.0-flash",
contents="""Generate a short transcript around 100 words that reads
like it was clipped from a podcast by excited herpetologists.
The hosts names are Dr. Anya and Liam.""").text
response = client.models.generate_content(
model="gemini-2.5-flash-preview-tts",
contents=transcript,
config=types.GenerateContentConfig(
response_modalities=["AUDIO"],
speech_config=types.SpeechConfig(
multi_speaker_voice_config=types.MultiSpeakerVoiceConfig(
speaker_voice_configs=[
types.SpeakerVoiceConfig(
speaker='Dr. Anya',
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(
voice_name='Kore',
)
)
),
types.SpeakerVoiceConfig(
speaker='Liam',
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(
voice_name='Puck',
)
)
),
]
)
)
)
)
# ...Code to stream or save the output
## Voice options
TTS models support the following 30 voice options in the `voice_name` field:
**Zephyr** \-- _Bright_ | **Puck** \-- _Upbeat_ | **Charon** \-- _Informative_
---|---|---
**Kore** \-- _Firm_ | **Fenrir** \-- _Excitable_ | **Leda** \-- _Youthful_
**Orus** \-- _Firm_ | **Aoede** \-- _Breezy_ | **Callirrhoe** \-- _Easy-going_
**Autonoe** \-- _Bright_ | **Enceladus** \-- _Breathy_ | **Iapetus** \-- _Clear_
**Umbriel** \-- _Easy-going_ | **Algieba** \-- _Smooth_ | **Despina** \-- _Smooth_
**Erinome** \-- _Clear_ | **Algenib** \-- _Gravelly_ | **Rasalgethi** \-- _Informative_
**Laomedeia** \-- _Upbeat_ | **Achernar** \-- _Soft_ | **Alnilam** \-- _Firm_
**Schedar** \-- _Even_ | **Gacrux** \-- _Mature_ | **Pulcherrima** \-- _Forward_
**Achird** \-- _Friendly_ | **Zubenelgenubi** \-- _Casual_ | **Vindemiatrix** \-- _Gentle_
**Sadachbia** \-- _Lively_ | **Sadaltager** \-- _Knowledgeable_ | **Sulafat** \-- _Warm_
You can hear all the voice options in [AI Studio](https://aistudio.google.com/generate-speech).
## Supported languages
The TTS models detect the input language automatically. They support the following 24 languages:
Language | BCP-47 Code | Language | BCP-47 Code
---|---|---|---
Arabic (Egyptian) | `ar-EG` | German (Germany) | `de-DE`
English (US) | `en-US` | Spanish (US) | `es-US`
French (France) | `fr-FR` | Hindi (India) | `hi-IN`
Indonesian (Indonesia) | `id-ID` | Italian (Italy) | `it-IT`
Japanese (Japan) | `ja-JP` | Korean (Korea) | `ko-KR`
Portuguese (Brazil) | `pt-BR` | Russian (Russia) | `ru-RU`
Dutch (Netherlands) | `nl-NL` | Polish (Poland) | `pl-PL`
Thai (Thailand) | `th-TH` | Turkish (Turkey) | `tr-TR`
Vietnamese (Vietnam) | `vi-VN` | Romanian (Romania) | `ro-RO`
Ukrainian (Ukraine) | `uk-UA` | Bengali (Bangladesh) | `bn-BD`
English (India) | `en-IN` & `hi-IN` bundle | Marathi (India) | `mr-IN`
Tamil (India) | `ta-IN` | Telugu (India) | `te-IN`
## Supported models
Model | Single speaker | Multispeaker
---|---|---
[Gemini 2.5 Flash Preview TTS](/gemini-api/docs/models#gemini-2.5-flash-preview-tts) | ✔️ | ✔️
[Gemini 2.5 Pro Preview TTS](/gemini-api/docs/models#gemini-2.5-pro-preview-tts) | ✔️ | ✔️
## Limitations
* TTS models can only receive text inputs and generate audio outputs.
* A TTS session has a [context window](/gemini-api/docs/long-context) limit of 32k tokens.
* Review [Languages](/gemini-api/docs/speech-generation#languages) section for language support.
## What's next
* Try the [audio generation cookbook](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/Get_started_TTS.ipynb).
* Gemini's [Live API](/gemini-api/docs/live) offers interactive audio generation options you can interleave with other modalities.
* For working with audio _inputs_ , visit the [Audio understanding](/gemini-api/docs/audio) guide.
|