Gemini API は、Gemini のテキスト読み上げ(TTS)生成機能を使用して、テキスト入力を単一話者または複数話者の音声に変換できます。テキスト読み上げ(TTS)生成は制御可能です。つまり、自然言語を使用してインタラクションを構造化し、音声のスタイル、アクセント、ペース、トーンをガイドできます。
TTS 機能は、インタラクティブな非構造化音声とマルチモーダルな入力と出力用に設計された Live API を介して提供される音声生成とは異なります。Live API は動的な会話コンテキストに優れていますが、Gemini API を介した TTS は、ポッドキャストやオーディオブックの生成など、スタイルやサウンドを細かく制御して正確なテキスト朗読が必要なシナリオ向けに調整されています。
このガイドでは、テキストから単一話者と複数話者の音声を生成する方法について説明します。
始める前に
サポートされているモデルのセクションに記載されているように、Gemini テキスト読み上げ(TTS)機能を備えた Gemini 2.5 モデル バリアントを使用してください。最適な結果を得るには、特定のユースケースに最適なモデルを検討してください。
構築を開始する前に、AI Studio で Gemini TTS モデルをテストすることをおすすめします。
単一話者 TTS
テキストを単一話者の音声に変換するには、レスポンス モダリティを「audio」に設定し、音声名を含む speech_config オブジェクトを渡します。事前構築された出力音声から音声名を選択する必要があります。
この例では、モデルからの出力音声を wave ファイルに保存します。
Python
from google import genai
import wave
import base64
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()
interaction = client.interactions.create(
model="gemini-3.1-flash-tts-preview",
input="Say cheerfully: Have a wonderful day!",
response_format={"type": "audio"},
generation_config={
"speech_config": [
{"voice": "Kore"}
]
}
)
wave_file('out.wav', base64.b64decode(interaction.output_audio.data))
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 client = new GoogleGenAI({});
const interaction = await client.interactions.create({
model: "gemini-3.1-flash-tts-preview",
input: "Say cheerfully: Have a wonderful day!",
response_format: { type: 'audio' },
generation_config: {
speech_config: [
{ voice: 'Kore' }
]
},
});
const audioBuffer = Buffer.from(interaction.output_audio.data, 'base64');
await saveWaveFile('out.wav', audioBuffer);
}
await main();
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-tts-preview",
"input": "Say cheerfully: Have a wonderful day!",
"response_format": {
"type": "audio"
},
"generation_config": {
"speech_config": [
{ "voice": "Kore" }
]
}
}'
生成された音声データは、最後に生成された音声ブロックを返す interaction.output_audio プロパティを使用して取得できます。便宜的なプロパティの詳細については、インタラクションの概要をご覧ください。
マルチスピーカー TTS
マルチスピーカー オーディオの場合は、各スピーカー(最大 2 つ)が speaker_voice_config として構成された multi_speaker_voice_config オブジェクトが必要です。各 speaker は、プロンプトで使用されている名前と同じ名前で定義する必要があります。
Python
from google import genai
import wave
import base64
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?"""
interaction = client.interactions.create(
model="gemini-3.1-flash-tts-preview",
input=prompt,
response_format={"type": "audio"},
generation_config={
"speech_config": [
{"speaker": "Joe", "voice": "Kore"},
{"speaker": "Jane", "voice": "Puck"}
]
}
)
wave_file('out.wav', base64.b64decode(interaction.output_audio.data))
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) =>