进阶 ai.google.dev 2026-10-15 02:43:21 · 29 阅读

第7章 Gemini 语音设计 (Voice design):通过自然语言创建自定义 AI 声音角色

Gemini 4 Argon 现已上线,欢迎试用。

首页 Gemini API 文档

Voice design

通过 Voice design,你只需用自然语言描述,就能通过 Gemini API 的 Voices 端点(POST /v1beta/voices)创建一个全新的、可持久保存的声音角色。你不必局限于预置音色,也不用录制参考音频,只需描述角色的年龄、音色、口音和基础说话方式,即可获得一个保存到项目中的可复用 voice_... ID。

设计、试听和迭代自定义声音最快的方式,是使用 Google AI Studio 中的交互式 Voice Design studio。你可以用文本提示生成自定义角色,用示例脚本试听效果,并将生成的 voice_... ID 直接复制到应用代码中。

在 Google AI Studio 中试用

Gemini 3.8 Flash TTS(gemini-3.8-flash-tts)和 Gemini 3.8 Flash-Lite TTS(gemini-3.8-flash-lite-tts)均支持 Voice design。

创建设计好的声音

使用 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 编码),方便你立即试听生成的声音:

Pythonimport 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))

JavaScriptimport * 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") ); }

RESTcurl "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

工作原理

创建提示式声音:调用 voices.create(POST /v1beta/voices),设置 type="prompted" 和 store=True。 获取持久的 voice_id 和试听音频:API 会生成声音特征并存储到你的项目中,返回一个永久 ID(如 voice_abc123...)以及 sample_audio(mime_type 为 "audio/wav",base64 编码),内含该声音的试听音频。 合成语音:在合成请求中,任何接受声音名称的位置都可以传入这个 voice_id。

用设计好的声音合成语音

创建好声音后,将它的 id(voice_...)传给 Interactions API 来生成语音:

Pythonimport 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))

JavaScriptimport * 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"));

RESTcurl "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"} ] } }' | jq -r '[.steps[] | select(.type=="model_output") | .content[] | select(.type=="audio")] | last | .data' | base64 --decode > out.wav

管理你的声音

你可以随时使用 Voices API 列出、筛选、查看和删除已存储的声音(全部筛选参数参见 Extended Voice Library and filtering)。

存储限制和 TTL:有状态声音(store=True,包括 prompted 和 replicated 声音)每个项目上限 200 个,TTL(存活时间)为 1 年。 sample_audio 的可用性:voices.create()(CreateVoice)和 voices.get()(GetVoice)会为"提示式"声音填充 sample_audio(mime_type 为 "audio/wav",base64 编码)。为保持列表接口轻量,voices.list()(ListVoices)不返回 sample_audio("replicated" 和 "prebuilt" 声音也没有这个字段)。

Pythonfrom 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)

JavaScriptimport { 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 design 提示词最佳实践

把永久性声音特征放进 Voice design,而不是 style:在 voices.create 创建声音时,定义不可变的特征,如年龄、性别、音色、声音质地和地方口音。 用 speech_metadata.style 控制情境情绪:自定义声音创建好后,使用简短的风格提示(如 "whispered urgently" 或 "cheerful and energetic")来调整每一轮的表达方式,同时不改变说话人的核心特征。 描述要具体且简洁:清晰的一两句话描述(如 "A crisp, energetic sports announcer in her 30s with a slight Midwestern accent")比自相矛盾或冗长的段落能产生更干净、更一致的效果。

后续步骤

了解如何在 Voice replication 中复制现有说话人的声音。 在 Text-to-speech 指南中探索轮次级风格控制、内联标签和多说话人对话。

评论 (0)