Appearance
TTS 流式生成文档
请求地址以及请求头
streaming-api.dubbingx.com/ws
必要参数 | 值 |
---|---|
api_key | 请求的api_key |
api_secret | 请求的api_secrect |
API Key以及secret在客户端中生成即可
1.连接ws服务器
鉴权
ws://streaming-api.dubbingx.com/ws?date={date}&authorization={authorization}&api_key={api_key}
or wss
wss://streaming-api.dubbingx.com/ws?date={date}&authorization={authorization}&api_key={api_key}
必要参数 | 值 |
---|---|
date | Thu, 26 Sep 2024 06:43:00 UTC |
api_key | api_key |
authorization | 见下方法 |
JS示例代码
javascript
let date = new Date().toUTCString();
getAuthStr(date) {
// SHA256
let signatureSha = CryptoJS.HmacSHA256(date, this.config.apiSecret)
let signature = CryptoJS.enc.Base64.stringify(signatureSha)
let authorizationOrigin = 'api_key='+this.config.apiKey+',date='+date+',signature='+signature;
let authStr = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(authorizationOrigin))
return authStr
}
//ws?date={date}&authorization={authorization}&api_key={api_key}
JAVA示例代码
java
// 1. 格式化日期
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = format.format(new Date());
// 2. SHA256 HMAC
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
mac.init(spec);
byte[] hexDigits = mac.doFinal(date.getBytes(StandardCharsets.UTF_8));
String sha = Base64.getEncoder().encodeToString(hexDigits);
// 3. 组装 authorizationOrigin
String authorizationOrigin = "api_key=" + apiKey + ",date=" + date + ",signature=" + sha;
String authStr = Base64.getEncoder().encodeToString(authorizationOrigin.getBytes(StandardCharsets.UTF_8));
// 4. URL编码
String encodedDate = URLEncoder.encode(date, StandardCharsets.UTF_8.name());
String encodedAuthStr = URLEncoder.encode(authStr, StandardCharsets.UTF_8.name());
String encodedApiKey = URLEncoder.encode(apiKey, StandardCharsets.UTF_8.name());
// 5. 拼接 ws url
String wsUrl = String.format("wss://host/ws?date=%s&authorization=%s&api_key=%s",
encodedDate, encodedAuthStr, encodedApiKey);
Python示例代码
python
import base64
import hmac
import hashlib
from datetime import datetime, timezone
import pytz
# 输入参数
api_key = "your_api_key"
api_secret = "your_api_secret"
# 1. 格式化 GMT 时间字符串
date = datetime.now(timezone.utc).strftime('%a, %d %b %Y %H:%M:%S GMT')
# 2. 生成 HMAC SHA256 签名
mac = hmac.new(api_secret.encode('utf-8'), date.encode('utf-8'), digestmod=hashlib.sha256)
signature = base64.b64encode(mac.digest()).decode('utf-8')
# 3. 拼接 Authorization 原始字符串
authorization_origin = f"api_key={api_key},date={date},signature={signature}"
# 4. Base64 编码 Authorization 字符串
auth_str = base64.b64encode(authorization_origin.encode('utf-8')).decode('utf-8')
# 5. 拼接 ws 地址参数
ws_url = f"ws?date={date}&authorization={auth_str}&api_key={api_key}"
步骤解析:
1.使用api_secrect对date进行SHA加密
2.对步骤一的结果进行base64
3.进行字符串拼接api_key={api_key},date={date},signature={signature}
顺序必须保持一致
4.对3结果进行base64
2.获取音色列表
https://tts-api.dubbingx.com/v1/getTTSTimbreList
请求参数
json
{"pageIndex":1,"pageSize":100}
请求方法 POST 返回值
字段 | 备注 |
---|---|
id | 音色ID |
grade | premium 多语态;ordinary 单语态 |
gender | 0女;1男; |
voiceUrl | 试听音频 |
json
{
"code": 200,
"success": true,
"msg": "操作成功",
"data": {
"total": 1,
"list": [
{
"id": "30002",
"grade": "premium",
"isOfficial": true,
"name": "智吾褚",
"description": "中青年 稳重 温暖",
"gender": 1,
"avatar": "https://public.dubbingx.com/avatar/10092/20240329-143217.png",
"voiceUrl": "https://public.dubbingx.com/audition/10092/mujin.wav",
"status": true,
"createTime": "2023-11-03 18:24:12"
}
]
},
"time": "2024-04-10 16:24:55",
"traceId": "1777976038257512450"
}
3.发送TTS合成指令(ws)
使用SSML格式传递
xml
<speak voiceId="30065" emotion="常规-日常说话-1" language="zh" audioPitch="1" audioSpeed="1" messageId="1234567890">这是一<phoneme ph="duan2">段</phoneme>测试音频</speak>
字段 | 备注 |
---|---|
voiceId | getTTSTimbreList获取的音色ID |
emotion | 如果不传递将根据文本自动判断情绪,但会增加生成时间,也可以手动指定,https://public.dubbingx.com/emotion/emotion_language.json 下的各个情绪,每个情绪有5档,需手动拼接, |
messageId | 必传,消息ID,为Int类型,流式返回时会原样返回 |
language | zh;jp;en;yue;分别表示中,日,英 粤 |
audioPitch | 语调,1.0为原音高,0.7到1.3,可不传 |
audioSpeed | 语速,1.0为原语速,0.7到1.3,可不传 |
phoneme | 音素标注,只支持中文,包裹内只支持一个文字,多个文字请分多次包裹,格式为<phoneme ph="duan2">段</phoneme> |
返回值 数据将会以json格式进行返回
字段 | 备注 |
---|---|
id | 如果开始进入合成队列,会返回任务ID |
audioBase64 | 音频的base64格式化数据,当前格式为mp3格式,后续提供ogg等格式 |
status | 0表示待开始,-1表示失败,1表示进行中,2表示结束 |
text | 当前音频的文字 |
json
{
"id": 1804052251079184400,
"audioBase64":"",
"messageId": 1234567890,
"msg": "操作成功",
"status": "0",
"text": ""
}