语音生成(文本转语音)

Gemini API 可以使用原生文字转语音 (TTS) 生成功能将文本输入转换为单人或多人音频。文字转语音 (TTS) 生成是可控的,这意味着您可以使用自然语言来构建互动,并引导音频的风格口音节奏音调

TTS 功能不同于通过 Live API 提供的语音生成功能,后者专为交互式、非结构化音频以及多模态输入和输出而设计。虽然 Live API 在动态对话情境中表现出色,但通过 Gemini API 实现的 TTS 专门针对需要精确朗读文本并对风格和声音进行精细控制的场景,例如播客或有声读物生成。

本指南介绍了如何根据文本生成单发言人和多发言人音频。

准备工作

请确保您使用的是具有原生文字转语音 (TTS) 功能的 Gemini 2.5 模型变体,如支持的模型部分中所列。为获得最佳结果,请考虑哪种模型最适合您的特定使用情形。

在开始构建之前,您可能会发现在 AI Studio 中测试 Gemini 2.5 TTS 模型很有用。

单说话者文字转语音

如需将文本转换为单人音频,请将响应模态设置为“音频”,并传递一个设置了 VoiceConfigSpeechConfig 对象。您需要从预建的输出语音中选择一个语音名称。

此示例将模型生成的输出音频保存到 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

多说话人文字转语音

对于多扬声器音频,您需要一个 MultiSpeakerVoiceConfig 对象,其中每个扬声器(最多 2 个)都配置为 SpeakerVoiceConfig。您需要使用与提示中相同的名称来定义每个 speaker

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

通过提示控制说话风格

您可以使用自然语言提示来控制单人或多人 TTS 的风格、语气、口音和语速。 例如,在单音箱提示中,您可以说:

Say in an spooky whisper:
"By the pricking of my thumbs...
Something wicked this way comes"

在多位发言者的提示中,为模型提供每位发言者的姓名和相应的转写内容。您还可以单独为每个音箱提供指导:

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!

不妨尝试使用与您想要传达的风格或情感相对应的语音选项,以进一步强调。例如,在前面的提示中,恩克拉多斯的喘息声可能强调“疲惫”和“无聊”,而 Puck 的欢快语气可能与“兴奋”和“快乐”相得益彰。

生成用于转换为音频的提示

TTS 模型仅输出音频,但您可以先使用其他模型生成转写内容,然后将该转写内容传递给 TTS 模型以进行朗读。

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();

语音选项

TTS 模型在 voice_name 字段中支持以下 30 种语音选项:

Zephyr - 明亮 Puck - 欢快 Charon - 信息丰富
Kore -- 坚定 Fenrir - Excitable Leda - 青春
Orus - 公司 Aoede - Breezy Callirrhoe - 轻松
Autonoe - 明亮 Enceladus - 气声 Iapetus - 清晰
Umbriel - 随和 Algieba - 平滑 Despina - 平滑
Erinome - 清除 Algenib - Gravelly Rasalgethi - 信息丰富
Laomedeia - 欢快 Achernar - Alnilam - Firm
Schedar -- 均匀 Gacrux - 成人 Pulcherrima - 直率
Achird - 友好 Zubenelgenubi -- 随意 Vindemiatrix -- 温和
Sadachbia - 活泼 Sadaltager - 知识渊博 Sulafat - 暖色

您可以在 AI Studio 中试听所有语音选项。

支持的语言

TTS 模型会自动检测输入语言。支持以下 24 种语言:

语言 BCP-47 代码 语言 BCP-47 代码
阿拉伯语(埃及语) ar-EG 德语(德国) de-DE
英语(美国) en-US 西班牙语(美国) es-US
法语(法国) fr-FR 印地语(印度) hi-IN
印度尼西亚语(印度尼西亚) id-ID 意大利语(意大利) it-IT
日语(日本) ja-JP 韩语(韩国) ko-KR
葡萄牙语(巴西) pt-BR 俄语(俄罗斯) ru-RU
荷兰语(荷兰) nl-NL 波兰语(波兰) pl-PL
泰语(泰国) th-TH 土耳其语(土耳其) tr-TR
越南语(越南) vi-VN 罗马尼亚语(罗马尼亚) ro-RO
乌克兰语(乌克兰) uk-UA 孟加拉语(孟加拉) bn-BD
英语(印度) en-INhi-IN套装 马拉地语(印度) mr-IN
泰米尔语(印度) ta-IN 泰卢固语(印度) te-IN

支持的模型

型号 一位说话者 多扬声器
Gemini 2.5 Flash 预览版 TTS ✔️ ✔️
Gemini 2.5 Pro 预览版 TTS ✔️ ✔️

限制

  • TTS 模型只能接收文本输入并生成音频输出。
  • TTS 会话的上下文窗口限制为 3.2 万个 token。
  • 如需了解语言支持,请参阅语言部分。

后续步骤