借助语音设计,您可以使用 Gemini API Voices 端点 (POST /v1beta/voices) 通过自然语言描述来创建全新的持久性声音角色。您无需局限于预建的语音或录制参考音频,只需描述角色的年龄、音色、口音和基准发音,即可获得保存到项目中的可重复使用的 voice_... ID。
在 Google AI Studio 中使用交互式语音设计工作室,可以快速设计、试听和迭代自定义语音。您可以根据文本提示生成自定义角色,使用示例脚本对其进行测试,然后将生成的 voice_... ID 直接复制到应用代码中。
Gemini 3.8 Flash TTS (gemini-3.8-flash-tts) 和 Gemini 3.8 Flash-Lite TTS (gemini-3.8-flash-lite-tts) 均支持语音设计。
创建设计的语音
使用 Google GenAI SDK(google-genai 2.25.0+ / @google/genai 2.24.0+)或 REST API,根据文本说明创建自定义语音。对于 "prompted" 语音,voices.create (CreateVoice) 和 voices.get (GetVoice) 都会返回仅输出的 sample_audio 字段(mime_type: "audio/wav",以 base64 编码的 data),以便您可以立即试听生成的语音:
Python
import base64
from google import genai
client = genai.Client()
# 1. Design a custom voice persona from natural language
created_voice = client.voices.create(
store=True,
voice={
"model": "gemini-3.8-flash-tts",
"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."
)
},
},
)
print(f"Created voice ID: {created_voice.id}")
# Save the generated sample_audio preview (audio/wav) returned by CreateVoice
if created_voice.sample_audio and created_voice.sample_audio.data:
with open("voice_preview.wav", "wb") as f:
f.write(base64.b64decode(created_voice.sample_audio.data))
JavaScript
import * as fs from "node:fs";
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI();
// 1. Design a custom voice persona from natural language
const createdVoice = await ai.voices.create({
store: true,
voice: {
model: "gemini-3.8-flash-tts",
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.",
},
},
});
console.log(`Created voice ID: ${createdVoice.id}`);
// Save the generated sample_audio preview (audio/wav) returned by CreateVoice
if (createdVoice.sample_audio?.data) {
fs.writeFileSync(
"voice_preview.wav",
Buffer.from(createdVoice.sample_audio.data, "base64")
);
}
REST
curl "https://generativelanguage.googleapis.com/v1beta/voices" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"store": true,
"voice": {
"model": "gemini-3.8-flash-tts",
"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."
}
}
}' | tee created_voice.json | jq -r '.sample_audio.data' | base64 --decode > voice_preview.wav
语音设计的工作原理
- 创建提示语音:使用
type="prompted"和store=True调用voices.create(POST /v1beta/voices)。 - 接收持久性
voice_id和sample_audio预览:API 会生成声音身份,将其存储在您的项目中,并返回永久性 ID(例如voice_abc123...)以及sample_audio(mime_type: "audio/wav",base64 编码的data),其中包含为该声音生成的预览音频。 - 合成语音:在合成请求中,将
voice_id传递到接受语音名称的任何位置。
使用您设计的语音合成语音
创建语音后,将该语音的 id (voice_...) 传递给 Interactions API 以生成语音:
Python
import base64
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.8-flash-tts",
input=[{
"type": "user_input",
"content": [{
"type": "text",
"text": (
"Look out past the rings of Saturn. Those faint photons left"
" their source millions of years ago."
),
"annotations": [{
"type": "speech_metadata",
"style": "reflective and awe-inspired",
}],
}],
}],
response_format={"type": "audio"},
generation_config={
"speech_config": [
{"voice": created_voice.id},
]
},
)
with open("designed_voice.wav", "wb") as f:
f.write(base64.b64decode(interaction.output_audio.data))
JavaScript
import * as fs from "node:fs";
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI();
const interaction = await ai.interactions.create({
model: "gemini-3.8-flash-tts",
input: [{
type: "user_input",
content: [{
type: "text",
text: "Look out past the rings of Saturn. Those faint photons left their source millions of years ago.",
annotations: [{
type: "speech_metadata",
style: "reflective and awe-inspired",
}],
}],
}],
response_format: { type: "audio" },
generation_config: {
speech_config: [
{ voice: createdVoice.id },
],
},
});
fs.writeFileSync("designed_voice.wav", Buffer.from(interaction.output_audio.data, "base64"));
REST
curl "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"model": "gemini-3.8-flash-tts",
"input": [{
"type": "user_input",
"content": [{
"type": "text",
"text": "Look out past the rings of Saturn. Those faint photons left their source millions of years ago.",
"annotations": [{
"type": "speech_metadata",
"style": "reflective and awe-inspired"
}]
}]
}],
"response_format": {"type": "audio"},
"generation_config": {
"speech_config": [
{"voice": "voice_YOUR_DESIGNED_VOICE_ID"}
]
}
}'
管理声音
您可以随时使用 Voices API 列出、过滤、检查和删除已存储的声音(有关所有过滤参数,请参阅扩展语音库和过滤)。
- 存储空间限制和 TTL:有状态的声音(
store=True,在提示的声音和复制的声音之间共享)的限制为每个项目 200 个声音,并且存留时间 (TTL) 为 1 年。 sample_audio可用性:voices.create()(CreateVoice) 和voices.get()(GetVoice) 会为"prompted"语音填充sample_audio(mime_type: "audio/wav",base64 编码的data)。为了保持列表的轻量级,voices.list()(ListVoices) 会省略sample_audio(对于"replicated"和"prebuilt"语音,sample_audio未设置)。
Python
from google import genai
client = genai.Client()
# List stored prompted voices in your project filtered by language
response = client.voices.list(
type_=["prompted"],
language_code=["en-US", "en-GB"],
)
for voice in response.voices or []:
print(voice.id, voice.display_name, voice.type)
# Retrieve a specific voice by ID
voice_details = client.voices.get(id=created_voice.id)
# Delete a stored custom voice
client.voices.delete(id=created_voice.id)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI();
// List stored prompted voices in your project filtered by language
const response = await ai.voices.list({
type: ["prompted"],
language_code: ["en-US", "en-GB"],
});
for (const voice of response.voices ?? []) {
console.log(voice.id, voice.display_name, voice.type);
}
// Retrieve a specific voice by ID
const voiceDetails = await ai.voices.get(createdVoice.id);
// Delete a stored custom voice
await ai.voices.delete(createdVoice.id);
REST
# List stored prompted voices filtered by language
curl -G "https://generativelanguage.googleapis.com/v1beta/voices" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
--data-urlencode "type=prompted" \
--data-urlencode "language_code=en-US" \
--data-urlencode "language_code=en-GB"
# Retrieve a specific voice by ID
curl "https://generativelanguage.googleapis.com/v1beta/voices/voice_YOUR_DESIGNED_VOICE_ID" \
-H "x-goog-api-key: $GEMINI_API_KEY"
# Delete a stored custom voice
curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/voices/voice_YOUR_DESIGNED_VOICE_ID" \
-H "x-goog-api-key: $GEMINI_API_KEY"
语音设计的提示最佳实践
- 将永久性声音特征放在 Voice 设计中,而不是
style中:在voices.create中创建声音时,定义不可变的特征,例如年龄、性别、音色、声音质感和地区口音。 - 预留
speech_metadata.style用于表达情境情绪:创建自定义声音后,使用简短的style提示(例如"whispered urgently"或"cheerful and energetic")来引导逐轮表演,而不会改变说话者的核心身份。 - 内容要具体且简洁:清晰的 1-2 句话描述(例如“一位 30 多岁的体育播报员,声音清脆有力,略带中西部口音”)比矛盾或过长的段落能生成更清晰、更一致的结果。