TTS 文本转语音
NUWA 提供统一的文本转语音接口,聚合多家主流模型,保持与 OpenAI 风格一致的调用方式,适用于语音播报、AI 配音、数字人、智能客服、有声内容等场景。
支持的模型
gpt-4o-audio-preview:对话式音频生成,支持多模态输出(文本 + 音频)gpt-4o-mini-tts:实时语音合成,支持通过提示词或instructions控制语音风格tts-1-hd:高清音质的上一代 TTS 模型tts-1:标准 TTS,兼顾质量与速度
性能建议:追求速度时使用
wav或pcm;高质量音频可选tts-1-hd,实时应用推荐gpt-4o-mini-tts。
接口规格
- Python
- JavaScript
- TypeScript
- Curl
from openai import OpenAI
client = OpenAI(
api_key="sk-***", # 在 NUWA 控制台生成的 Key
base_url="https://api.nuwaapi.com/v1"
)
with client.audio.speech.with_streaming_response.create(
model="<model_id>", # tts-1 / tts-1-hd / gpt-4o-mini-tts
input="your text",
voice="alloy",
response_format="mp3",
) as response:
response.stream_to_file("speech.mp3")
print("已保存到 speech.mp3")
const fs = require("fs");
async function main() {
const response = await fetch("https://api.nuwaapi.com/v1/audio/speech", {
method: "POST",
headers: {
"Authorization": "sk-***",
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "tts-1", // tts-1 / tts-1-hd / gpt-4o-mini-tts
input: "your text",
voice: "alloy",
response_format: "mp3"
})
});
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("speech.mp3", buffer);
console.log("✅ 已保存到 speech.mp3");
}
main().catch(e => console.error("❌ 出错:", e.message));
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({
apiKey: "sk-***",
baseURL: "https://api.nuwaapi.com/v1"
});
async function main(): Promise<void> {
const response = await client.audio.speech.create({
model: "tts-1", // tts-1 / tts-1-hd / gpt-4o-mini-tts
input: "your text",
voice: "alloy",
response_format: "mp3"
});
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("speech.mp3", buffer);
console.log("✅ 已保存到 speech.mp3");
}
main().catch(e => console.error("❌ 出错:", e.message));
curl https://api.nuwaapi.com/v1/audio/speech \
-H "Authorization: sk-***" \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1",
"input": "your text",
"voice": "alloy",
"response_format": "mp3"
}' \
--output speech.mp3
echo "✅ 已保存到 speech.mp3"
model为具体语音模型 ID- 返回内容为音频二进制流
标准 TTS 参数(tts-1 / tts-1-hd / gpt-4o-mini-tts)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 选择 tts-1、tts-1-hd 或 gpt-4o-mini-tts |
input | string | 是 | 待合成文本,最长约 4096 字符 |
voice | string | 否 | 发音人:alloy、echo、fable、onyx、nova、shimmer |
response_format | string | 否 | 输出格式:mp3(默认)、opus、aac、flac、wav、pcm |
speed | number | 否 | 语速 0.25–4.0,默认 1.0;gpt-4o-mini-tts 不支持,可用自然语言控制 |
instructions | string | 否 | 仅 gpt-4o-mini-tts 支持,用于指定口音、情绪、语调、语速等 |
gpt-4o-audio-preview 参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 设置为 gpt-4o-audio-preview |
modalities | array | 是 | 必须包含 "text" 和 "audio" |
audio | object | 是 | 音频配置,如 { "voice": "alloy", "format": "mp3" } |
messages | array | 是 | 聊天消息数组,格式同标准 Chat Completions |
调用示例
标准 TTS(tts-1)
- Python
- JavaScript
- TypeScript
- Curl
from openai import OpenAI
client = OpenAI(api_key="sk-***", base_url="https://api.nuwaapi.com/v1")
with client.audio.speech.with_streaming_response.create(
model="tts-1",
input="欢迎使用 NUWA 文本转语音接口。",
voice="alloy",
response_format="mp3",
) as response:
response.stream_to_file("speech.mp3")
print("已保存到 speech.mp3")
const fs = require("fs");
async function main() {
const response = await fetch("https://api.nuwaapi.com/v1/audio/speech", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer sk-***"
},
body: JSON.stringify({
model: "tts-1",
input: "欢迎使用 NUWA 文本转语音接口。",
voice: "alloy",
response_format: "mp3"
})
});
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("speech.mp3", buffer);
console.log("✅ 已保存到 speech.mp3");
}
main().catch(e => console.error("❌ 出错:", e.message));
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({
apiKey: "sk-***",
baseURL: "https://api.nuwaapi.com/v1"
});
async function main(): Promise<void> {
const response = await client.audio.speech.create({
model: "tts-1",
input: "欢迎使用 NUWA 文本转语音接口。",
voice: "alloy",
response_format: "mp3"
});
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("speech.mp3", buffer);
console.log("✅ 已保存到 speech.mp3");
}
main().catch(e => console.error("❌ 出错:", e.message));
curl https://api.nuwaapi.com/v1/audio/speech \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-***" \
-d '{
"model": "tts-1",
"input": "欢迎使用 NUWA 文本转语音接口。",
"voice": "alloy",
"response_format": "mp3"
}' \
--output speech.mp3
echo "✅ 已保存到 speech.mp3"
gpt-4o-mini-tts(指定语音风格)
- Python
- JavaScript
- TypeScript
- Curl
from openai import OpenAI
client = OpenAI(api_key="sk-***", base_url="https://api.nuwaapi.com/v1")
with client.audio.speech.with_streaming_response.create(
model="gpt-4o-mini-tts",
input="请用温暖而清晰的声音播放这一段文字。",
voice="nova",
instructions="温柔、放松的语气,语速稍慢",
response_format="mp3",
) as response:
response.stream_to_file("soft.mp3")
print("已保存到 soft.mp3")
const fs = require("fs");
async function main() {
const response = await fetch("https://api.nuwaapi.com/v1/audio/speech", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer sk-***"
},
body: JSON.stringify({
model: "gpt-4o-mini-tts",
input: "请用温暖而清晰的声音播放这一段文字。",
voice: "nova",
instructions: "温柔、放松的语气,语速稍慢",
response_format: "mp3"
})
});
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("soft.mp3", buffer);
console.log("✅ 已保存到 soft.mp3");
}
main().catch(e => console.error("❌ 出错:", e.message));
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({
apiKey: "sk-***",
baseURL: "https://api.nuwaapi.com/v1"
});
async function main(): Promise<void> {
const response = await client.audio.speech.create({
model: "gpt-4o-mini-tts",
input: "请用温暖而清晰的声音播放这一段文字。",
voice: "nova",
// @ts-ignore
instructions: "温柔、放松的语气,语速稍慢",
response_format: "mp3"
});
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("soft.mp3", buffer);
console.log("✅ 已保存到 soft.mp3");
}
main().catch(e => console.error("❌ 出错:", e.message));
curl https://api.nuwaapi.com/v1/audio/speech \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-***" \
-d '{
"model": "gpt-4o-mini-tts",
"input": "请用温暖而清晰的声音播放这一段文字。",
"voice": "nova",
"instructions": "温柔、放松的语气,语速稍慢",
"response_format": "mp3"
}' \
--output soft.mp3
echo "✅ 已保存到 soft.mp3"
gpt-4o-audio-preview(文本 + 音频)
- Python
- JavaScript
- TypeScript
- Curl
import base64
from pathlib import Path
from openai import OpenAI
client = OpenAI(api_key="sk-***", base_url="https://api.nuwaapi.com/v1")
completion = client.chat.completions.create(
model="gpt-4o-audio-preview",
modalities=["text", "audio"],
audio={"voice": "alloy", "format": "mp3"},
messages=[
{"role": "user", "content": "用亲切的语气介绍一下 NUWA TTS 接口的用法。"}
],
)
# 文本结果(兼容 content 为空的情况)
msg = completion.choices[0].message
text_part = None
if msg.content and len(msg.content) > 0:
first = msg.content[0]
text_part = getattr(first, "text", None) if first is not None else None
if text_part:
print(text_part)
else:
print("未返回文本内容,完整 message 对象:", msg)
# 音频结果(Base64)写入 mp3
audio_b64 = completion.choices[0].message.audio.data
audio_bytes = base64.b64decode(audio_b64)
out_path = Path("preview.mp3")
out_path.write_bytes(audio_bytes)
print(f"音频已保存到 {out_path}")
const fs = require("fs");
async function main() {
const response = await fetch("https://api.nuwaapi.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer sk-***"
},
body: JSON.stringify({
model: "gpt-4o-audio-preview",
modalities: ["text", "audio"],
audio: { voice: "alloy", format: "mp3" },
messages: [
{ role: "user", content: "用亲切的语气介绍一下 NUWA TTS 接口的用法。" }
]
})
});
const data = await response.json();
const msg = data.choices?.[0]?.message ?? {};
const textPart = msg.content?.[0]?.text;
if (textPart) {
console.log(textPart);
}
const audioB64 = msg.audio?.data;
if (audioB64) {
fs.writeFileSync("preview.mp3", Buffer.from(audioB64, "base64"));
console.log("音频已保存到 preview.mp3");
} else {
console.log("未返回音频数据:", JSON.stringify(data, null, 2));
}
}
main().catch(e => console.error("❌ 出错:", e.message));
import OpenAI from "openai";
import fs from "fs";
const client = new OpenAI({
apiKey: "sk-***",
baseURL: "https://api.nuwaapi.com/v1"
});
async function main(): Promise<void> {
const completion = await client.chat.completions.create({
model: "gpt-4o-audio-preview",
modalities: ["text", "audio"] as any,
audio: { voice: "alloy", format: "mp3" } as any,
messages: [
{ role: "user", content: "用亲切的语气介绍一下 NUWA TTS 接口的用法。" }
]
} as any);
const msg: any = completion.choices?.[0]?.message ?? {};
const textPart = msg.content?.[0]?.text;
if (textPart) {
console.log(textPart);
}
const audioB64 = msg.audio?.data;
if (audioB64) {
fs.writeFileSync("preview.mp3", Buffer.from(audioB64, "base64"));
console.log("音频已保存到 preview.mp3");
} else {
console.log("未返回音频数据:", JSON.stringify(completion, null, 2));
}
}
main().catch(e => console.error("❌ 出错:", e.message));
curl https://api.nuwaapi.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-***" \
-d '{
"model": "gpt-4o-audio-preview",
"modalities": ["text", "audio"],
"audio": {"voice": "alloy", "format": "mp3"},
"messages": [
{"role": "user", "content": "用亲切的语气介绍一下 NUWA TTS 接口的用法。"}
]
}' -o response.json
python -c "
import json, base64
with open('response.json') as f:
data = json.load(f)
msg = data.get('choices', [{}])[0].get('message', {})
audio = msg.get('audio', {}).get('data')
if audio:
with open('preview.mp3', 'wb') as f:
f.write(base64.b64decode(audio))
print('音频已保存到 preview.mp3')
else:
print('未返回音频数据:', json.dumps(data, ensure_ascii=False, indent=2))
"
返回说明
- 接口直接返回音频二进制流,建议使用
--output或程序方式保存文件 - 若发生错误,将返回 JSON 结构的错误信息,包含状态码与原因