Custom
通过实现 TRTC 自定义流式协议,接入您自己的 TTS 引擎。此选项让您完全掌控语音合成管线——可使用自研模型、私有化部署或任何未原生支持的第三方服务。适用于有特殊音色需求或需要集成自研 TTS 解决方案的场景。
使用方式
// json — TTSConfig{"TTSType": "custom","APIKey": "<your_api_key>","APIUrl": "http://0.0.0.0:8080/stream-audio","AudioFormat": "wav","SampleRate": 16000,"AudioChannel": 1}
参数说明
字段 | 类型 | 必填 | 描述 |
TTSType | String | 是 | 固定值: "custom"。 |
APIKey | String | 是 | 您的 TTS 服务的 API 鉴权密钥。 |
APIUrl | String | 是 | 您的 TTS 服务端点 URL。 |
AudioFormat | String | 否 | 输出音频格式。当前支持: pcm、wav。默认值:wav。 |
SampleRate | Integer | 否 | 音频采样率。默认值:16000(16 kHz)。推荐:16000。 |
AudioChannel | Integer | 否 | 音频声道数。 1(单声道)或 2(立体声)。默认值:1。 |
自定义 TTS 流式协议
您的 TTS 服务必须实现以下 HTTP 流式协议。
调用方式
POST: http://xxxxxxxxxxxx/api/v1/tts/stream
示例
HTTP 请求头
Content-Type: application/json;charset=UTF-8X-Task-Id: task_id_valueX-Rquest-Id: request_idX-Sdk-App-Id: SdkAppIdX-User-Id: UserIdX-Room-Id: RoomIdX-Room-Id-Type: "0"Authorization: Bearer "API-KEY"
请求体
{"Text": "Hello, world! This is a test for the streaming TTS API.","Format": "wav","SampleRate": 16000,"Channel": 1}
HTTP 请求头
字段 | 描述 |
Content-Type | application/json |
charset | UTF-8 |
X-Task-Id | 会话任务 ID。 |
X-Rquest-Id | 请求 ID。重试时携带相同的 RequestID。 |
X-Sdk-App-Id | SDK 的 AppId。 |
X-User-Id | 用户 ID。 |
X-Room-Id | 房间 ID。 |
X-Room-Id-Type | 房间 ID 类型。0:数字房间号,1:字符串房间号。 |
Authorization | 鉴权信息,格式为 Bearer "API-KEY"。 |
请求参数
参数 | 必填 | 类型 | 描述 |
Text | 是 | String | 要合成的语音文本。 |
Format | 否 | String | 输出音频格式,如 mp3、ogg_opus、pcm、wav。默认值为 wav。当前仅支持 pcm 和 wav。 |
SampleRate | 否 | Integer | 音频采样率。默认值为 16000(16k),推荐值为 16000。 |
Channel | 否 | Integer | 音频声道数。有效值:1、2。默认值为 1。 |
响应
通过 Content-Type 的值判断 TTS 是否成功。
成功时,返回二进制语音数据。不同音频格式对应的 Content-Type 如下,HTTP 响应头中需设置 Transfer-Encoding: chunked。
音频格式 | Content-Type |
mp3 | audio/mpeg |
ogg_opus | audio/ogg |
pcm | audio/L16 |
wav | audio/wav |
失败时,返回 JSON 结果,响应头为 Content-type: application/json。响应格式:
{"error": {"code": "ERROR_CODE","message": "A description of the error"}}