语音生成(文本转语音)

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 -- Firm Fenrir - Excitable Leda - 青春
Orus - 公司 Aoede - Breezy Callirrhoe - 轻松
Autonoe - 明亮 Enceladus - 气声 Iapetus - 清晰
Umbriel - 轻松自在 Algieba - 平滑 Despina - 平滑
Erinome - 清除 Algenib - Gravelly Rasalgethi - 信息丰富
Laomedeia - 欢快 Achernar - Alnilam - Firm
Schedar -- Even 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。
  • 如需了解语言支持,请参阅语言部分。

提示指南

Gemini 原生音频生成文字转语音 (TTS) 模型与传统 TTS 模型的不同之处在于,它使用的大语言模型不仅知道要说什么,还知道怎么说

若要解锁此功能,用户可以把自己想象成导演,为虚拟配音演员设置表演场景。为了精心打造提示,我们建议您考虑以下组成部分:定义角色核心身份和原型的音频配置文件;确定实体环境和情感“氛围”的场景说明;以及提供有关风格、口音和节奏控制的更精确表演指导的导演注释

通过提供细致的指令,例如精确的地区口音、特定的副语言特征(例如气声)或语速,用户可以利用模型的上下文感知能力生成高度动态、自然且富有表现力的音频表演。为获得最佳效果,我们建议脚本和导演提示保持一致,以便“谁在说”“说了什么”“怎么说”保持一致。

本指南旨在为您提供基本指导,并在您使用 Gemini TTS 音频生成功能开发音频体验时激发您的灵感。我们非常期待看到您的创作成果!

提示结构

一个出色的提示应包含以下元素,这些元素共同构成出色的表演:

  • 音频配置文件 - 为语音建立角色,定义角色身份、原型和任何其他特征,例如年龄、背景等。
  • 场景 - 设置舞台。描述了实体环境和“氛围”。
  • 导演笔记 - 效果指南,您可以在其中细分哪些指令对虚拟人才来说需要注意。例如,风格、呼吸、节奏、发音和口音。
  • 示例上下文 - 为模型提供上下文起点,以便虚拟演员自然地进入您设置的场景。
  • 转写内容 - 模型将朗读的文本。为获得最佳效果,请注意转写内容的主题和写作风格应与您给出的指令相关。

完整提示示例:

# AUDIO PROFILE: Jaz R.
## "The Morning Hype"

## THE SCENE: The London Studio
It is 10:00 PM in a glass-walled studio overlooking the moonlit London skyline,
but inside, it is blindingly bright. The red "ON AIR" tally light is blazing.
Jaz is standing up, not sitting, bouncing on the balls of their heels to the
rhythm of a thumping backing track. Their hands fly across the faders on a
massive mixing desk. It is a chaotic, caffeine-fueled cockpit designed to wake
up an entire nation.

### DIRECTOR'S NOTES
Style:
* The "Vocal Smile": You must hear the grin in the audio. The soft palate is
always raised to keep the tone bright, sunny, and explicitly inviting.
* Dynamics: High projection without shouting. Punchy consonants and elongated
vowels on excitement words (e.g., "Beauuutiful morning").

Pace: Speaks at an energetic pace, keeping up with the fast music.  Speaks
with A "bouncing" cadence. High-speed delivery with fluid transitions — no dead
air, no gaps.

Accent: Jaz is from Brixton, London

### SAMPLE CONTEXT
Jaz is the industry standard for Top 40 radio, high-octane event promos, or any
script that requires a charismatic Estuary accent and 11/10 infectious energy.

#### TRANSCRIPT
Yes, massive vibes in the studio! You are locked in and it is absolutely
popping off in London right now. If you're stuck on the tube, or just sat
there pretending to work... stop it. Seriously, I see you. Turn this up!
We've got the project roadmap landing in three, two... let's go!

详细的提示策略

下面我们来详细了解提示的各个要素。

音频配置

简要描述角色的性格。

  • 名称:为角色命名有助于将模型和紧凑的表演联系起来,在设置场景和上下文时,请按名称提及角色
  • 角色。场景中正在上演的角色的核心身份和原型。例如,电台 DJ、播客主播、新闻记者等

示例:

# AUDIO PROFILE: Jaz R.
## "The Morning Hype"


# AUDIO PROFILE: Monica A.
## "The Beauty Influencer"

场景

设置场景的背景信息,包括地点、氛围和环境细节,以确定基调和氛围。描述角色周围发生的事情以及这些事情对角色的影响。场景为整个互动提供了环境背景,并以一种微妙而自然的方式引导表演。

示例:

## THE SCENE: The London Studio
It is 10:00 PM in a glass-walled studio overlooking the moonlit London skyline,
but inside, it is blindingly bright. The red "ON AIR" tally light is blazing.
Jaz is standing up, not sitting, bouncing on the balls of their heels to the
rhythm of a thumping backing track. Their hands fly across the faders on a
massive mixing desk. It is a chaotic, caffeine-fueled cockpit designed to
wake up an entire nation.


## THE SCENE: Homegrown Studio
A meticulously sound-treated bedroom in a suburban home. The space is
deadened by plush velvet curtains and a heavy rug, but there is a
distinct "proximity effect."

导演备注

此关键部分包含具体的性能指导。您可以跳过所有其他元素,但我们建议您添加此元素。

仅定义对效果有重要影响的因素,并注意不要过度指定。过于严格的规则会限制模型的创造力,并可能导致效果不佳。平衡角色和场景说明与具体的表演规则。

最常见的指令是风格、节奏和口音,但模型不限于这些指令,也不要求必须使用这些指令。您可以随意添加自定义说明,以涵盖对广告效果至关重要的任何其他详细信息,并根据需要提供尽可能详细或尽可能简略的说明。

例如:

### DIRECTOR'S NOTES

Style: Enthusiastic and Sassy GenZ beauty YouTuber

Pacing: Speaks at an energetic pace, keeping up with the extremely fast, rapid
delivery influencers use in short form videos.

Accent: Southern california valley girl from Laguna Beach |

样式

设置生成的语音的语气和风格。包括欢快、活力、放松、无聊等,以指导表演。请提供尽可能详细的说明:“富有感染力的热情。听众应该感觉自己正在参与一场盛大而精彩的社区活动。”比简单地说“充满活力和热情”效果更好。

您甚至可以尝试配音行业中常用的术语,例如“声音微笑”。您可以根据需要叠加任意数量的样式特征。

示例:

Simple Emotion

DIRECTORS NOTES
...
Style: Frustrated and angry developer who can't get the build to run.
...

更深入

DIRECTORS NOTES
...
Style: Sassy GenZ beauty YouTuber, who mostly creates content for YouTube Shorts.
...

复杂

DIRECTORS NOTES
Style:
* The "Vocal Smile": You must hear the grin in the audio. The soft palate is
always raised to keep the tone bright, sunny, and explicitly inviting.
*Dynamics: High projection without shouting. Punchy consonants and
elongated vowels on excitement words (e.g., "Beauuutiful morning").

Accent

描述所需的口音。描述越具体,结果就越好。例如,使用“英国克罗伊登的英式英语口音”而不是“英式口音”。

示例:

### DIRECTORS NOTES
...
Accent: Southern california valley girl from Laguna Beach
...


### DIRECTORS NOTES
...
Accent: Jaz is a from Brixton, London
...

预算花费进度

整个作品的总体节奏和节奏变化。

示例:

简单

### DIRECTORS NOTES
...
Pacing: Speak as fast as possible
...

更深入

### DIRECTORS NOTES
...
Pacing: Speaks at a faster, energetic pace, keeping up with fast paced music.
...

复杂

### DIRECTORS NOTES
...
Pacing: The "Drift": The tempo is incredibly slow and liquid. Words bleed into each other. There is zero urgency.
...

快来试试吧

AI Studio 中亲自尝试这些示例,体验我们的 TTS 应用,让 Gemini 助您成为导演。请谨记以下提示,以便录制出精彩的人声表演:

  • 请务必确保整个提示连贯一致,因为脚本和指令是打造出色表演的关键。
  • 您不必描述所有内容,有时给模型留出填补空白的空间有助于提高自然度。(就像一位才华横溢的演员)
  • 如果您遇到瓶颈,不妨让 Gemini 助您一臂之力,帮助您撰写剧本或表演。

后续步骤