您可以使用語音設計功能,根據自然語言描述建立自訂語音。您不必選擇預先建立的語音或錄製參考音訊,只要描述角色的年齡、音色、口音和基本語調,就能建立語音。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欄位則會指定編碼名稱。根據預設,樣本是完整的 WAV 檔案 (audio/wav),具有 RIFF 標頭:16 位元 PCM、24 kHz、單聲道。部分語音可能會改為傳回無標頭的 16 位元 PCM (audio/l16; rate=24000; channels=1),因此請先檢查mime_type,再儲存音訊。usage:要求使用的權杖數量。詳情請參閱「定價」。
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 費率,以文字輸入詞元和音訊輸出詞元計費。音訊輸出詞元涵蓋 Voices API 生成的語音樣本。create 回應中的 usage 欄位會回報這兩項計數。舉例來說,建立範例中的單句說明會使用約 220 個輸入詞元和約 1,100 個輸出詞元。
get、list 和 delete 方法不會產生費用。使用設計的語音生成語音時,會依您呼叫的模型費率計費。
如要瞭解權杖費率,請參閱「Gemini Enterprise 定價」。
提示最佳做法
- 在語音說明中加入永久特徵:建立語音時,請定義年齡、性別、音色、音質和地區口音,而非在
speech_metadata.style中定義。 - 使用
speech_metadata.style呈現情境情緒:建立語音後,請使用簡短的style值 (例如"whispered urgently"或"cheerful and energetic") 指導每輪對話,而不變更說話者的身分。 - 具體簡潔:一兩句清楚的描述,例如「30 多歲、口齒清晰、活力充沛的體育播報員,帶有輕微的美國中西部口音」,比冗長或矛盾的段落更能產生一致的結果。
後續步驟
- 使用語音混音功能編輯設計或預建的語音。
- 使用語音複製功能複製現有說話者的聲音。
- 如要瞭解對話層級樣式、內嵌標記和多人對話,請參閱「使用 Gemini TTS 生成語音」。