异步语音合成
平台提供异步长文本语音合成 API,适用于长文本的音频合成任务。
- 支持系统音色、复刻音色自主选择
- 支持语调、语速、音量、输出格式调整
- 支持直接传入字符串与上传文本文件两种方式进行待合成文本的输入
支持音色
表格中为云知声 Token Hub大模型服务平台系统音色,为您提供语音合成选择。
| 序号 | 语言 | 音色 ID (Voice ID) | 音色名称 (Voice Name) |
|---|---|---|---|
| 1 | 中文 | chenyu | 晨宇 |
| 2 | 中文 | shasha | 莎莎 |
| 3 | 中文 | ruolin | 若琳 |
| 4 | 中文 | jiajia | 佳佳 |
使用流程
- 若使用文件输入,需先调用文件上传 API 上传文本并获取
file_id。若使用文本作为输入,则跳过此步骤 - 调用 创建异步语音合成任务,获取
task_id - 调用 查询语音生成任务状态,基于
task_id获取语音合成任务进度 - 当任务完成时,上述调用查询语音生成任务状态 API 返回的
file_id可用于调用文件下载 API 下载音频结果
过程示例
1. 获取 file_id
Python
"""
本示例用于待合成文本的 file_id。注意:需要先将密钥信息写入环境变量 `API_KEY`。
"""
import requests
import os
api_key = os.environ.get("API_KEY")
url = "https://maas-api.hivoice.cn/v1/files/upload"
payload = {'purpose': 't2a_async_input'}
files=[
('file',('input_files.txt',open('path/to/input_files.txt','rb'),'text/plain'))
]
headers = {
'Authorization': f'Bearer {api_key}'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
2. 创建语音合成任务
Python
"""
本示例用于创建语音合成任务,若使用文件作为输入,则需要将<text_file_id>替换为文本文件的file_id,若使用文本作为输入,则设置"text"字段。注意:需要先将密钥信息写入环境变量 `API_KEY`。
"""
import requests
import json
import os
api_key = os.environ.get("API_KEY")
url = "https://maas-api.hivoice.cn/v1/audio/speech/tasks"
payload = json.dumps({
"model": "u2-tts",
"text": "水泊梁山的故事家喻户晓",
#"text_file_id": <file_id>,
"voice_setting": {
"voice_id": "chenyu",
"speed": 50,
"vol": 50,
"pitch": 50
},
"pronunciation_dict": {
"tone": ["水泊梁山/水泊<py>po1</py>梁山"]
},
"audio_setting": {
"audio_sample_rate": 16000,
"format": "mp3"
}
})
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
3. 查询语音合成进度
Python
"""
本示例用于查询语音合成进度。注意:需要先将密钥信息写入环境变量 `API_KEY`,并将需要查询任务的 id 写入环境变量 `TASK_ID`。
"""
import requests
import os
task_id = os.environ.get("TASK_ID")
api_key = os.environ.get("API_KEY")
url = f"https://maas-api.hivoice.cn/v1/audio/speech/tasks?task_id={task_id}"
payload = {}
headers = {
'Authorization': f'Bearer {api_key}',
'content-type': 'application/json',
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
4. 下载语音合成文件
Python
"""
本示例用于下载语音合成文件。注意:需要先将密钥信息写入环境变量 `API_KEY`,并将待下载文件的 id 写入环境变量 `FILE_ID`。
"""
import requests
import os
api_key = os.environ.get("API_KEY")
file_id = os.environ.get("FILE_ID")
url = f"https://maas-api.hivoice.cn/v1/files/retrieve_content?file_id={file_id}"
payload = {}
headers = {
'content-type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
response = requests.request("GET", url, headers=headers, data=payload)
with open('output_audio.mp3', 'wb') as f:
f.write(response.content)