Thiết kế giọng nói cho phép bạn tạo một cá tính giọng nói hoàn toàn mới và bền vững từ nội dung mô tả bằng ngôn ngữ tự nhiên bằng cách sử dụng điểm cuối Giọng nói của Gemini API (POST /v1beta/voices). Thay vì bị giới hạn ở giọng nói được tạo sẵn hoặc bản ghi âm tham chiếu, bạn có thể mô tả độ tuổi, âm sắc, giọng điệu và cách truyền đạt cơ bản của nhân vật, đồng thời nhận được mã nhận dạng voice_... có thể dùng lại được lưu vào dự án của bạn.
Cách nhanh nhất để thiết kế, thử giọng và lặp lại các giọng nói tuỳ chỉnh là sử dụng phòng thu Thiết kế giọng nói tương tác trong Google AI Studio. Bạn có thể tạo các vai trò tuỳ chỉnh từ câu lệnh văn bản, kiểm thử các vai trò đó bằng tập lệnh mẫu và sao chép mã nhận dạng voice_... thu được trực tiếp vào mã xử lý ứng dụng của bạn.
Cả Gemini 3.8 Flash TTS (gemini-3.8-flash-tts) và Gemini 3.8 Flash-Lite TTS (gemini-3.8-flash-lite-tts) đều hỗ trợ Thiết kế giọng nói.
Tạo giọng nói được thiết kế
Sử dụng Google GenAI SDK (google-genai 2.25.0 trở lên / @google/genai 2.24.0 trở lên) hoặc API REST để tạo giọng nói tuỳ chỉnh từ nội dung mô tả bằng văn bản. Đối với giọng nói "prompted", cả voices.create (CreateVoice) và voices.get (GetVoice) đều trả về trường sample_audio chỉ có đầu ra (mime_type: "audio/wav", data được mã hoá base64) để bạn có thể nghe thử giọng nói được tạo ngay lập tức:
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
Cách hoạt động của thiết kế giọng nói
- Tạo giọng nói được nhắc: Gọi
voices.create(POST /v1beta/voices) bằngtype="prompted"vàstore=True. - Nhận bản xem trước
voice_idvàsample_audioliên tục: API này tạo danh tính giọng nói, lưu trữ danh tính đó trong dự án của bạn và trả về một mã nhận dạng vĩnh viễn (ví dụ:voice_abc123...) cùng vớisample_audio(mime_type: "audio/wav",datađược mã hoá base64) chứa âm thanh xem trước đã tạo cho giọng nói. - Tổng hợp lời nói: Truyền
voice_idở bất kỳ nơi nào chấp nhận tên giọng nói trong các yêu cầu tổng hợp của bạn.
Tổng hợp lời nói bằng giọng nói do bạn thiết kế
Sau khi bạn tạo một giọng nói, hãy truyền id (voice_...) của giọng nói đó đến Interactions API để tạo lời nói:
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"}
]
}
}'
Quản lý giọng nói
Bạn có thể liệt kê, lọc, kiểm tra và xoá giọng nói đã lưu trữ bất cứ lúc nào bằng Voices API (xem phần Thư viện giọng nói mở rộng và tính năng lọc để biết tất cả các tham số lọc).
- Giới hạn bộ nhớ và TTL: Giọng nói có trạng thái (
store=True, được chia sẻ giữa giọng nói được nhắc và giọng nói được sao chép) có giới hạn là 200 giọng nói cho mỗi dự án và TTL (thời gian tồn tại) là 1 năm. sample_audiophạm vi cung cấp:voices.create()(CreateVoice) vàvoices.get()(GetVoice) điền sẵnsample_audio(mime_type: "audio/wav",datađược mã hoá bằng base64) cho các giọng nói"prompted". Để giữ cho danh sách gọn nhẹ,voices.list()(ListVoices) sẽ bỏ quasample_audio(vàsample_audiosẽ không được đặt cho giọng nói"replicated"và"prebuilt").
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"
Các phương pháp hay nhất để viết câu lệnh cho thiết kế giọng nói
- Đưa các đặc điểm giọng nói cố định vào phần Thiết kế giọng nói, không phải
style: Xác định các đặc điểm không thay đổi (chẳng hạn như độ tuổi, giới tính, âm sắc, chất giọng và giọng vùng miền) khi tạo giọng nói trongvoices.create. - Dành
speech_metadata.stylecho cảm xúc theo tình huống: Sau khi tạo giọng nói tuỳ chỉnh, hãy dùng các câu lệnh ngắnstyle(ví dụ:"whispered urgently"hoặc"cheerful and energetic") để hướng dẫn diễn xuất từng bước mà không làm thay đổi danh tính cốt lõi của người nói. - Nêu cụ thể và ngắn gọn: Nội dung mô tả rõ ràng từ 1 đến 2 câu (chẳng hạn như "Một người dẫn chương trình thể thao sắc sảo, tràn đầy năng lượng ở độ tuổi 30, có giọng nói hơi pha tạp của vùng Trung Tây") sẽ tạo ra kết quả rõ ràng và nhất quán hơn so với các đoạn văn mâu thuẫn hoặc quá dài.
Bước tiếp theo
- Tìm hiểu cách nhân bản giọng nói của một người nói hiện có trong tính năng Nhân bản giọng nói.
- Khám phá kiểu theo lượt, thẻ nội tuyến và đoạn hội thoại nhiều người nói trong Hướng dẫn về tính năng chuyển văn bản sang lời nói.