语音合成API
接口描述
语音合成接口。
请求说明
URL
https://openapi.sys303.com/api/cognitive/tts/stream
参数
Header参数
| 参数 | 值 |
|---|---|
| Content-Type | application/json |
| X-Secret | 点击获取 |
Body参数
| 参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
|---|---|---|---|---|
| lang | 是 | int | [0, 1, 4, 5] | 0:藏文-卫藏,1:中文,4:藏文-康巴,5:藏文-安多 |
| vcn | 否 | string | ["xiaoxiao","yunjian"] | 【当lang为1时 可选,可选"xiaoxiao"和"yunjian";lang为其余值,传入空。】 |
| inputText | 是 | string | 待语音合成文字【unicode 长度3000字以内】 |
请求样例
- bash
- python
- C#
- Java
curl https://openapi.sys303.com/api/cognitive/tts/stream \
--request POST \
--header 'Accept: */*' \
--header 'Content-Type: application/json' \
--header 'X-Secret: {X-Secret}' \
--data '{
"lang": 0,
"inputText": "语音合成文字",
"vcn": ""
}' \
--output out.pcm
import requests
url = "https://openapi.sys303.com/api/cognitive/tts/stream"
headers = {
"Accept": "*/*",
"Content-Type": "application/json",
"X-Secret": "{your X-Secret}"
}
data = {
"lang": 0,
"inputText": "语音合成文字",
"vcn": ""
}
resp = requests.post(url, headers=headers, json=data, stream=True)
if resp.status_code == 200:
with open("out.pcm", "wb") as f:
for chunk in resp.iter_content(chunk_size=4096):
if chunk:
f.write(chunk)
print("保存成功 out.pcm")
else:
print("请求失败")
print(resp.text)
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.IO;
class Program
{
static async Task Main()
{
var url = "https://openapi.sys303.com/api/cognitive/tts/stream";
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "*/*");
client.DefaultRequestHeaders.Add("X-Secret", "{your X-Secret}");
var json = @"{
""lang"": 0,
""inputText"": ""语音合成文字"",
""vcn"": """"
}";
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
var bytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("out.pcm", bytes);
Console.WriteLine("保存成功 out.pcm");
}
else
{
var err = await response.Content.ReadAsStringAsync();
Console.WriteLine("请求失败: " + err);
}
}
}
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
public class TtsDemo {
public static void main(String[] args) throws Exception {
String json = """
{
"lang": 0,
"inputText": "语音合成文字",
"vcn": ""
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://openapi.sys303.com/api/cognitive/tts/stream"))
.header("Accept", "*/*")
.header("Content-Type", "application/json")
.header("X-Secret", "{your X-Secret}")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<Path> response = client.send(
request,
HttpResponse.BodyHandlers.ofFile(Path.of("out.pcm"))
);
System.out.println("HTTP状态码: " + response.statusCode());
System.out.println("已保存 out.pcm");
}
}
返回说明
正常请求
返回PCM文件流。16000 码率;单声道。
可以使用
ffplay -f s16le -ar 16000 out.pcm
进行播放验证。
异常请求参数说明
| 字段 | 是否必选 | 类型 | 状态 | 说明 |
|---|---|---|---|---|
| traceId | 是 | String | - | 唯一的traceId,用于问题定位 |
| msgId | 是 | String | - | 业务状态码 |
| msg | 是 | String | 错误描述 | |
| data | 是 | String | - | 空值 |
返回样例
失败
认证失败:
{
"traceId": "0HNLN3J9DCJSE:00000001",
"msgId": "4011001",
"msg": "AuthFailed",
"data": null
}
传入参数错误:
{
"traceId": "0HNL06O44AU14:00000001",
"msgId": "500",
"msg": "Specified argument was out of the range of valid values. (Parameter 'lang')",
"data": null
}
次数不足:
{
"traceId": "0HNL06O44AU1F:00000001",
"msgId": "2001001",
"msg": "InsufficientInventory",
"data": null
}
字符长度超长:
{
"traceId": "0HNL06O44AUVB:00000001",
"msgId": "4004002",
"msg": "InputTooLong",
"data": null
}