您可以使用语音设计功能,根据自然语言描述创建自定义语音。您无需选择预构建的语音或录制参考音频,而是通过描述角色的年龄、音色、口音和基准表达方式来创建语音。Voices API 会将语音存储在您的项目中,并返回一个可重复使用的 voice_... ID,您可以在语音生成请求中传递该 ID。
设计的语音可与 Gemini 3.8 Flash TTS (gemini-3.8-flash-tts) 和 Gemini 3.8 Flash-Lite TTS (gemini-3.8-flash-lite-tts) 搭配使用。
语音设计的工作原理
- 创建提示语音:调用 Voices API
create方法,并将voice.type设置为VOICE_TYPE_PROMPTED,将store设置为true。 - 接收声音 ID 和样本:Voices API 会生成声音,将其存储在您的项目中,并在
id字段中返回其 ID(例如voice_abc123...),在sample_audio字段中返回声音样本。 - 生成语音:在调用
generateContent或streamGenerateContent时,在speechConfig.voiceConfig.voice中传递语音 ID。
准备工作
完成 Gemini TTS 页面上准备工作中的步骤。Voices API 可在 global 位置通过 v1beta1 API 版本使用。
创建设计的声音
在请求中设置以下字段:
store:必须为true。设计的语音始终存储在您的项目中。voice.type:VOICE_TYPE_PROMPTED。voice.prompted.input:对声音的自然语言描述。voice.display_name、voice.gender、voice.language_code:语音的可选元数据。
请勿设置 voice.model。同时设置模型和 store: true 的请求会失败,并显示 INVALID_ARGUMENT 错误。
设计声音可能需要几秒钟的时间。Python 示例将请求超时设置为 60 秒。
响应包含以下字段:
id:新声音的 ID。sample_audio:语音样本。data字段包含采用 base64 编码的音频,mime_type字段指定其编码。默认情况下,示例是一个带有 RIFF 标头的完整 WAV 文件 (audio/wav):16 位 PCM、24 kHz、单声道。某些语音可以返回无标头的 16 位 PCM (audio/l16; rate=24000; channels=1),因此请在保存音频之前检查mime_type。usage:请求使用的 token 数量。如需了解详情,请参阅价格。
Python
import base64
import wave
from google import genai
client = genai.Client(enterprise=True, project="PROJECT_ID", location="global")
voice = client.voices.create(
store=True,
voice={
"type": "VOICE_TYPE_PROMPTED",
"display_name": "Warm British Astronomer",
"gender": "male",
"language_code": "en-GB",
"prompted": {
"input": (
"A warm, thoughtful astronomer in his late 60s with a gentle"
" British accent, speaking with quiet wonder."
)
},
},
timeout=60,
)
print(f"Created voice ID: {voice.id}")
# Save the voice sample. The SDK returns sample_audio.data as a base64
# string. By default, it's a complete WAV file (audio/wav). For
# compatibility, add a WAV header if the sample is raw 16-bit PCM
# (audio/l16).
sample = base64.b64decode(voice.sample_audio.data)
if voice.sample_audio.mime_type.lower().startswith("audio/l16"):
with wave.open("voice_sample.wav", "wb") as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(24000)
wf.writeframes(sample)
else:
with open("voice_sample.wav", "wb") as f:
f.write(sample)
REST
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/voices \
-d '{
"store": true,
"voice": {
"type": "VOICE_TYPE_PROMPTED",
"displayName": "Warm British Astronomer",
"gender": "male",
"languageCode": "en-GB",
"prompted": {
"input": "A warm, thoughtful astronomer in his late 60s with a gentle British accent, speaking with quiet wonder."
}
}
}' > created_voice.json
# Print the voice ID and the encoding of the voice sample.
jq -r '.id, .sample_audio.mime_type' created_voice.json
# Decode the voice sample.
jq -r '.sample_audio.data' created_voice.json | base64 --decode > voice_sample.wav
默认情况下,sample_audio.mime_type 为 audio/wav,voice_sample.wav 文件是完整的 WAV 文件。如果 mime_type 为 audio/l16,则文件包含无标头的 16 位 PCM 音频,采样率为 24 kHz,单声道,需要添加 WAV 标头后,大多数播放器才能打开。
如需使用您自己的文本来听取语音,请按照下一部分中的说明使用该语音生成语音。如需在不重新描述的情况下更改声音的某些特征,请使用声音改换风格功能。
使用设计的语音生成语音
在 speechConfig.voiceConfig.voice 中传递语音 ID:
Python
from google import genai
client = genai.Client(enterprise=True, project="PROJECT_ID", location="global")
response = client.models.generate_content(
model="gemini-3.8-flash-tts",
contents=[{
"role": "user",
"parts": [{
"text": (
"Look out past the rings of Saturn. Those faint photons left"
" their source millions of years ago."
),
"speech_metadata": {"style": "reflective and awe-inspired"},
}],
}],
config={
"response_modalities": ["AUDIO"],
"speech_config": {"voice_config": {"voice": "VOICE_ID"}},
},
)
# The SDK has already decoded the base64 audio, so inline_data.data is a
# complete WAV file by default.
with open("designed_voice.wav", "wb") as f:
f.write(response.candidates[0].content.parts[0].inline_data.data)
REST
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/google/models/gemini-3.8-flash-tts:generateContent \
-d '{
"contents": [{
"role": "user",
"parts": [{
"text": "Look out past the rings of Saturn. Those faint photons left their source millions of years ago.",
"speechMetadata": {"style": "reflective and awe-inspired"}
}]
}],
"generationConfig": {
"responseModalities": ["AUDIO"],
"speechConfig": {
"voiceConfig": {"voice": "VOICE_ID"}
}
}
}' | jq -r '.candidates[0].content.parts[0].inlineData.data' | base64 --decode > designed_voice.wav
将 VOICE_ID 替换为 create 方法返回的语音 ID。
管理存储的声音
您可以列出、获取和删除项目中存储的声音,包括设计的声音、改换风格的声音和存储的复制声音。list 方法会返回存储在项目中的语音,后跟预构建和扩展语音库语音。如需仅列出存储在项目中的声音,请将 type 过滤条件设置为 prompted 和 replicated,如以下示例所示。prompted 类型包括设计的声音和改换风格的声音。get 方法还会在 sample_audio 字段中返回设计或改换风格后的语音样本。
Python
from google import genai
client = genai.Client(enterprise=True, project="PROJECT_ID", location="global")
# List the voices stored in your project.
for voice in client.voices.list(type_=["prompted", "replicated"]).voices or []:
print(voice.id, voice.display_name, voice.type)
# Get a voice by ID.
voice = client.voices.get(id="VOICE_ID")
# Delete a voice.
client.voices.delete(id="VOICE_ID")
REST
# List the voices stored in your project.
curl -G \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/voices \
--data-urlencode "type=prompted" \
--data-urlencode "type=replicated"
# Get a voice by ID.
curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/voices/VOICE_ID
# Delete a voice.
curl -X DELETE \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/voices/VOICE_ID
限制
- 存储的声音会在上次使用一年后过期。使用某个声音生成语音,或将其用作改换风格的基础声音,会重新开始计算此期限,因此您经常使用的声音不会过期。删除不再需要的语音。
- 语音创建请求受每个项目每分钟配额的限制。如果超出此限制,
create方法会返回RESOURCE_EXHAUSTED错误。请稍后重试。如需了解详情,请参阅配额和系统限制。
价格
创建设计的声音时,系统会按照 Gemini 3.8 Flash TTS 费率,将文本输入 token 和音频输出 token 计费。音频输出 token 涵盖 Voices API 生成的语音样本。create 响应中的 usage 字段会报告这两个计数。例如,创建示例中的一句话描述使用了大约 220 个输入 token 和大约 1,100 个输出 token。
get、list 和 delete 方法不计费。您使用设计的声音生成的语音将按照您调用的模型的费率计费。
如需了解 token 费率,请参阅 Gemini Enterprise 价格。
提示撰写最佳实践
- 在声音描述中添加永久特征:在创建声音时定义年龄、性别、音色、声音质感和地区口音,而不是在
speech_metadata.style中定义。 - 使用
speech_metadata.style表达情境情绪:创建语音后,使用简短的style值(例如"whispered urgently"或"cheerful and energetic")来引导每个回合,而无需更改说话者的身份。 - 具体且简洁:一两句清晰的描述(例如“一位 30 多岁、声音清脆、充满活力的体育播音员,带有轻微的美国中西部口音”)比冗长或自相矛盾的段落能产生更一致的结果。
后续步骤
- 使用声音混音修改设计的声音或预构建的声音。
- 使用语音复刻功能复刻现有说话者的声音。
- 如需了解轮次级样式、内嵌标签和多说话者对话,请参阅使用 Gemini TTS 生成语音。