生成纯音乐
通过提示词 prompt描述纯音乐的风格与主题,调用 POST /api/v1/song/instrumental 提交生成纯音乐任务。该接口为异步模式:提交后返回 task_id,需轮询 GET /api/v1/task/query 获取生成进度与结果。
请求参数说明
| 参数 | 必填 | 说明 |
|---|
| model | 是 | 模型版本,当前支持 v4.0 |
| prompt | 是 | 提示词 prompt:纯音乐的风格、情绪、主题等文字描述,最长 3000 字符 |
| n | 否 | 生成纯音乐的数量,默认 2,最大 2 |
1. 提交纯音乐任务
curl -X POST "https://open.yinchaoyongxian.com/api/v1/song/instrumental" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "v4.0",
"prompt": "轻快的原声吉他 BGM,适合咖啡馆氛围",
"n": 2
}'
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"task_type": "instrumental",
"choices": [],
"create_at": 1710000000
}
2. 轮询任务状态
歌曲生成通常需要数十秒。建议每隔 2~5 秒调用一次 query 接口,直到 choices 中歌曲的 status 为 done 或 fail。
| status | 含义 |
|---|
| pending | 排队中 |
| running | 生成中 |
| stream | 可流式播放(pipe_url 可用) |
| done | 生成完成(audio_url 可用) |
| fail | 生成失败,查看 error 字段 |
import time
import requests
API_KEY = "YOUR_API_KEY"
BASE = "https://open.yinchaoyongxian.com"
headers = {"Authorization": f"Bearer {API_KEY}"}
task_id = "3fa85f64-5717-4562-b3fc-2c963f66afa6"
while True:
resp = requests.get(
f"{BASE}/api/v1/task/query",
params={"task_id": task_id},
headers=headers,
)
task = resp.json()
for song in task.get("choices", []):
print(song["id"], song["status"])
if song["status"] == "done":
print("audio_url:", song["audio_url"])
elif song["status"] == "fail":
print("error:", song["error"])
if all(s["status"] in ("done", "fail") for s in task.get("choices", [])):
break
time.sleep(3)
相关接口