合成語音
小提示
有關更多詳細信息,請參閱 文本和圖像 選項卡!
語音合成,或稱 文字轉語音,是語音轉文字的反向。 它涉及將文字提交給模型,模型會傳回語音化文字的音訊串流。
支援文字轉語音操作的模型包括:
- GPT-4O-TTS
- GPT-4O-MINI-TTS
備註
模型可用性會依區域而有所不同。 請參考 Microsoft Foundry 文件中的 模型區域可用性 表。
使用文字轉語音模型
類似語音轉文字模型,你可以使用 OpenAI SDK 中的 AzureOpenAI 用戶端連接 Microsoft Foundry 資源的端點,並將文字上傳到文字轉語音模型進行語音合成。
from openai import AzureOpenAI
from pathlib import Path
# Create an AzureOpenAI client
client = AzureOpenAI(
azure_endpoint=YOUR_FOUNDRY_ENDPOINT,
api_key=YOUR_FOUNDRY_KEY,
api_version="2025-03-01-preview"
)
# Path for audio output file
speech_file_path = Path("output_speech.wav")
# Generate speech and save to file
with client.audio.speech.with_streaming_response.create(
model=YOUR_MODEL_DEPLOYMENT,
voice="alloy",
input="This speech was AI-generated!",
instructions="Speak in an upbeat, excited tone.",
) as response:
response.stream_to_file(speech_file_path)
print(f"Speech generated and saved to {speech_file_path}")