Gemini API dapat mengubah input teks menjadi audio satu penutur atau multi-penutur menggunakan kemampuan pembuatan text-to-speech (TTS) bawaan. Pembuatan text-to-speech (TTS) dapat dikontrol, artinya Anda dapat menggunakan bahasa alami untuk menyusun interaksi dan memandu gaya, aksen, kecepatan, dan nada audio.
Kemampuan TTS berbeda dengan pembuatan ucapan yang disediakan melalui Live API, yang dirancang untuk audio interaktif dan tidak terstruktur, serta input dan output multimodal. Meskipun Live API unggul dalam konteks percakapan dinamis, TTS melalui Gemini API disesuaikan untuk skenario yang memerlukan pembacaan teks yang tepat dengan kontrol gaya dan suara yang cermat, seperti pembuatan podcast atau buku audio.
Panduan ini menunjukkan cara membuat audio satu penutur dan multi-penutur dari teks.
Sebelum memulai
Pastikan Anda menggunakan varian model Gemini 2.5 dengan kemampuan text-to-speech (TTS) bawaan, seperti yang tercantum di bagian Model yang didukung. Untuk hasil yang optimal, pertimbangkan model mana yang paling sesuai dengan kasus penggunaan spesifik Anda.
Anda mungkin merasa berguna untuk menguji model TTS Gemini 2.5 di AI Studio sebelum mulai membangun.
Text-to-speech satu pembicara
Untuk mengonversi teks menjadi audio satu penutur, tetapkan modalitas respons ke "audio",
dan teruskan objek SpeechConfig
dengan VoiceConfig
yang ditetapkan.
Anda harus memilih nama suara dari suara output bawaan.
Contoh ini menyimpan audio output dari model dalam file wave:
Python
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
JavaScript
import {GoogleGenAI} from '@google/genai';
import wav from 'wav';
async function saveWaveFile(
filename,
pcmData,
channels = 1,
rate = 24000,
sampleWidth = 2,
) {
return new Promise((resolve, reject) => {
const writer = new wav.FileWriter(filename, {
channels,
sampleRate: rate,
bitDepth: sampleWidth * 8,
});
writer.on('finish', resolve);
writer.on('error', reject);
writer.write(pcmData);
writer.end();
});
}
async function main() {
const ai = new GoogleGenAI({});
const response = await ai.models.generateContent({
model: "gemini-2.5-flash-preview-tts",
contents: [{ parts: [{ text: 'Say cheerfully: Have a wonderful day!' }] }],
config: {
responseModalities: ['AUDIO'],
speechConfig: {
voiceConfig: {
prebuiltVoiceConfig: { voiceName: 'Kore' },
},
},
},
});
const data = response.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data;
const audioBuffer = Buffer.from(data, 'base64');
const fileName = 'out.wav';
await saveWaveFile(fileName, audioBuffer);
}
await main();
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-tts:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-X POST \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts":[{
"text": "Say cheerfully: Have a wonderful day!"
}]
}],
"generationConfig": {
"responseModalities": ["AUDIO"],
"speechConfig": {
"voiceConfig": {
"prebuiltVoiceConfig": {
"voiceName": "Kore"
}
}
}
},
"model": "gemini-2.5-flash-preview-tts",
}' | jq -r '.candidates[0].content.parts[0].inlineData.data' | \
base64 --decode >out.pcm
# You may need to install ffmpeg.
ffmpeg -f s16le -ar 24000 -ac 1 -i out.pcm out.wav
Text-to-speech multi-penutur
Untuk audio multi-pembicara, Anda memerlukan objek MultiSpeakerVoiceConfig
dengan
setiap pembicara (hingga 2) yang dikonfigurasi sebagai SpeakerVoiceConfig
.
Anda harus menentukan setiap speaker
dengan nama yang sama yang digunakan dalam
prompt:
Python
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
JavaScript
import {GoogleGenAI} from '@google/genai';
import wav from 'wav';
async function saveWaveFile(
filename,
pcmData,
channels = 1,
rate = 24000,
sampleWidth = 2,
) {
return new Promise((resolve, reject) => {
const writer = new wav.FileWriter(filename, {
channels,
sampleRate: rate,
bitDepth: sampleWidth * 8,
});
writer.on('finish', resolve);
writer.on('error', reject);
writer.write(pcmData);
writer.end();
});
}
async function main() {
const ai = new GoogleGenAI({});
const prompt = `TTS the following conversation between Joe and Jane:
Joe: How's it going today Jane?
Jane: Not too bad, how about you?`;
const response = await ai.models.generateContent({
model: "gemini-2.5-flash-preview-tts",
contents: [{ parts: [{ text: prompt }] }],
config: {
responseModalities: ['AUDIO'],
speechConfig: {
multiSpeakerVoiceConfig: {
speakerVoiceConfigs: [
{
speaker: 'Joe',
voiceConfig: {
prebuiltVoiceConfig: { voiceName: 'Kore' }
}
},
{
speaker: 'Jane',
voiceConfig: {
prebuiltVoiceConfig: { voiceName: 'Puck' }
}
}
]
}
}
}
});
const data = response.candidates?.[0]?.content?.parts?.[0]?.inlineData?.data;
const audioBuffer = Buffer.from(data, 'base64');
const fileName = 'out.wav';
await saveWaveFile(fileName, audioBuffer);
}
await main();
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-tts:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-X POST \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts":[{
"text": "TTS the following conversation between Joe and Jane:
Joe: Hows it going today Jane?
Jane: Not too bad, how about you?"
}]
}],
"generationConfig": {
"responseModalities": ["AUDIO"],
"speechConfig": {
"multiSpeakerVoiceConfig": {
"speakerVoiceConfigs": [{
"speaker": "Joe",
"voiceConfig": {
"prebuiltVoiceConfig": {
"voiceName": "Kore"
}
}
}, {
"speaker": "Jane",
"voiceConfig": {
"prebuiltVoiceConfig": {
"voiceName": "Puck"
}
}
}]
}
}
},
"model": "gemini-2.5-flash-preview-tts",
}' | jq -r '.candidates[0].content.parts[0].inlineData.data' | \
base64 --decode > out.pcm
# You may need to install ffmpeg.
ffmpeg -f s16le -ar 24000 -ac 1 -i out.pcm out.wav
Mengontrol gaya ucapan dengan perintah
Anda dapat mengontrol gaya, nada, aksen, dan kecepatan menggunakan perintah bahasa natural untuk TTS satu dan multi-pembicara. Misalnya, dalam perintah satu penutur, Anda dapat mengucapkan:
Say in an spooky whisper:
"By the pricking of my thumbs...
Something wicked this way comes"
Dalam perintah multi-pembicara, berikan nama setiap pembicara dan transkrip yang sesuai kepada model. Anda juga dapat memberikan panduan untuk setiap penutur secara terpisah:
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!
Coba gunakan opsi suara yang sesuai dengan gaya atau emosi yang ingin Anda sampaikan, untuk lebih menekankannya. Misalnya, dalam perintah sebelumnya, napas Enceladus yang tersengal-sengal dapat menekankan "lelah" dan "bosan", sementara nada riang Puck dapat melengkapi "bersemangat" dan "bahagia".
Membuat perintah untuk mengonversi ke audio
Model TTS hanya menghasilkan output audio, tetapi Anda dapat menggunakan model lain untuk membuat transkrip terlebih dahulu, lalu meneruskan transkrip tersebut ke model TTS untuk dibacakan.
Python
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
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
async function main() {
const transcript = await ai.models.generateContent({
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.",
})
const response = await ai.models.generateContent({
model: "gemini-2.5-flash-preview-tts",
contents: transcript,
config: {
responseModalities: ['AUDIO'],
speechConfig: {
multiSpeakerVoiceConfig: {
speakerVoiceConfigs: [
{
speaker: "Dr. Anya",
voiceConfig: {
prebuiltVoiceConfig: {voiceName: "Kore"},
}
},
{
speaker: "Liam",
voiceConfig: {
prebuiltVoiceConfig: {voiceName: "Puck"},
}
}
]
}
}
}
});
}
// ..JavaScript code for exporting .wav file for output audio
await main();
Opsi suara
Model TTS mendukung 30 opsi suara berikut di kolom voice_name
:
Zephyr -- Terang (Bright) | Puck -- Ceria | Charon -- Informatif |
Kore -- Firm | Fenrir -- Mudah Terangsang (Excitable) | Leda -- Awet Muda |
Orus -- Firm | Aoede -- Breezy | Callirrhoe -- Santai |
Autonoe -- Bright | Enceladus -- Berbisik | Iapetus -- Hapus |
Umbriel -- Mudah bergaul | Algieba -- Smooth | Despina -- Halus (Smooth) |
Erinome -- Clear | Algenib -- Berbatu | Rasalgethi -- Informatif |
Laomedeia -- Upbeat | Achernar -- Lembut | Alnilam -- Firm |
Schedar -- Even | Gacrux -- Dewasa | Pulcherrima -- Forward |
Achird -- Ramah | Zubenelgenubi -- Kasual | Vindemiatrix -- Lembut (Gentle) |
Sadachbia -- Lively | Sadaltager -- Berpengetahuan | Sulafat -- Hangat |
Anda dapat mendengar semua opsi suara di AI Studio.
Bahasa yang didukung
Model TTS mendeteksi bahasa input secara otomatis. Fitur ini mendukung 24 bahasa berikut:
Language | Kode BCP-47 | Language | Kode BCP-47 |
---|---|---|---|
Arab (Mesir) | ar-EG |
Jerman (Jerman) | de-DE |
Inggris (AS) | en-US |
Spanyol (Amerika Serikat) | es-US |
Prancis (Prancis) | fr-FR |
Hindi (India) | hi-IN |
Indonesia (Indonesia) | id-ID |
Italia (Italia) | it-IT |
Jepang (Jepang) | ja-JP |
Korea (Korea) | ko-KR |
Portugis (Brasil) | pt-BR |
Rusia (Rusia) | ru-RU |
Belanda (Belanda) | nl-NL |
Polandia (Polandia) | pl-PL |
Thai (Thailand) | th-TH |
Turki (Turki) | tr-TR |
Vietnam (Vietnam) | vi-VN |
Rumania (Rumania) | ro-RO |
Ukraina (Ukraina) | uk-UA |
Bengali (Bangladesh) | bn-BD |
Inggris (India) | Paket en-IN & hi-IN |
Marathi (India) | mr-IN |
Tamil (India) | ta-IN |
Telugu (India) | te-IN |
Model yang didukung
Model | Satu pembicara | Multi-pembicara |
---|---|---|
TTS Pratinjau Gemini 2.5 Flash | ✔️ | ✔️ |
TTS Pratinjau Gemini 2.5 Pro | ✔️ | ✔️ |
Batasan
- Model TTS hanya dapat menerima input teks dan menghasilkan output audio.
- Sesi TTS memiliki batas jendela konteks 32 ribu token.
- Tinjau bagian Bahasa untuk mengetahui dukungan bahasa.
Langkah berikutnya
- Coba cookbook pembuatan audio.
- Live API Gemini menawarkan opsi pembuatan audio interaktif yang dapat Anda selingi dengan modalitas lain.
- Untuk mempelajari cara menggunakan input audio, buka panduan Pemahaman audio.