语音合成教程
FoxAPI 支持 CosyVoice 系列语音合成模型,将文本转换为自然流畅的语音。
接口
POST https://foxapi.chat/v1/audio/tts
可用模型
| 模型 | 价格 | 说明 |
|---|---|---|
| cosyvoice-v3-flash | ¥0.10/千字符 | 支持声音克隆,5-20秒参考音频即可生成高度相似声音 |
| cosyvoice-v3.5-flash | ¥0.08/千字符 | 高性能合成,大幅减少首包延迟 |
基本用法
curl 示例
bash
curl -X POST https://foxapi.chat/v1/audio/tts \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的Key" \
-d '{
"model": "cosyvoice-v3-flash",
"input": {
"text": "大地母亲在忽悠着你!",
"voice": "longanyang",
"format": "mp3"
}
}'Python 示例
python
import requests
import urllib.request
response = requests.post(
"https://foxapi.chat/v1/audio/tts",
headers={
"Authorization": "Bearer sk-你的Key",
"Content-Type": "application/json"
},
json={
"model": "cosyvoice-v3-flash",
"input": {
"text": "大地母亲在忽悠着你!",
"voice": "longanyang",
"format": "mp3"
}
}
)
result = response.json()
audio_url = result["output"]["audio"]["url"]
# 下载音频文件
urllib.request.urlretrieve(audio_url, "output.mp3")
print("音频已下载为 output.mp3")Node.js 示例
typescript
const response = await fetch("https://foxapi.chat/v1/audio/tts", {
method: "POST",
headers: {
"Authorization": "Bearer sk-你的Key",
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "cosyvoice-v3-flash",
input: {
text: "大地母亲在忽悠着你!",
voice: "longanyang",
format: "mp3"
}
})
});
const result = await response.json();
console.log("音频URL:", result.output.audio.url);参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | cosyvoice-v3-flash 或 cosyvoice-v3.5-flash |
| input.text | string | 是 | 要合成的文本内容 |
| input.voice | string | 是 | 音色名称 |
| input.format | string | 是 | 音频格式:mp3/wav/pcm |
音色列表
| 音色名称 | 说明 |
|---|---|
| longanyang | 通用男声 |
| longanhuan | 通用女声 |
更多音色请联系客服获取
响应示例
json
{
"output": {
"audio": {
"url": "https://...mp3?Expires=...",
"id": "audio_xxx",
"expires_at": 1783246243
},
"finish_reason": "stop"
},
"usage": {"characters": 20},
"request_id": "ad258b65-xxx"
}应用场景
- 有声读物:将电子书、文章批量转为语音
- 智能客服:为聊天机器人添加语音回复能力
- 导航播报:将导航指令转为语音播报
- 内容创作:为视频、播客生成配音
- 无障碍辅助:为视障用户朗读屏幕内容
常见问题
Q: 音频URL有有效期吗?
有。返回的 expires_at 是过期时间戳,请及时下载保存。
Q: 单次请求文本长度有限制吗?
建议不超过 5000 字符。
Q: 支持声音克隆吗?
支持 cosyvoice-v3-flash。只需提供 5-20 秒的参考音频,即可生成高度相似的声音。