同声传译BetaReal-time Interpretation Beta

通过 WebSocket 连接实现实时音频流翻译,毫秒级低延迟,适用于在线会议、直播同传等场景。Real-time audio streaming translation via WebSocket with millisecond-level latency. Ideal for online meetings and live interpretation.

⚠️ Beta 功能Beta Feature 同声传译目前处于 Beta 阶段,接口可能发生变化。 Real-time interpretation is currently in Beta. The API may change.

连接端点

Endpoint

WSSwss://api.itranslator.cc/v1/realtime/translate

查询参数:Query Parameters: ?token={access_token}&source_lang={lang}&target_lang={lang}

认证

Authentication

通过查询参数传递 Bearer Token。Pass Bearer Token as a query parameter.

WebSocket 协议

WebSocket Protocol

连接参数Connection Parameters

参数Parameter类型Type必填Required说明Description
tokenstringrequiredAccess TokenAccess Token
source_langstringrequired源语言代码Source language code
target_langstringrequired目标语言代码Target language code
sample_rateintegeroptional采样率(Hz),默认 16000Sample rate (Hz), default 16000

消息格式Message Format

客户端 → 服务端Client → Server发送二进制音频数据(PCM 16-bit,小端)Send binary audio data (PCM 16-bit, little-endian)

服务端 → 客户端Server → Client返回 JSON 消息Returns JSON messages

{
  "type": "translation",
  "is_final": false,
  "source_text": "我们今天要讨论的是...",
  "translated_text": "What we're going to discuss today is...",
  "timestamp": 12.45
}

{
  "type": "translation",
  "is_final": true,
  "source_text": "我们今天要讨论的是人工智能在医疗领域的应用。",
  "translated_text": "What we're going to discuss today is the application of AI in the medical field.",
  "timestamp": 15.82
}

请求示例

Request Examples

const ws = new WebSocket(
  `wss://api.itranslator.cc/v1/realtime/translate?token=${token}&source_lang=zh&target_lang=en`
);

ws.onopen = () => {
  // Start sending audio chunks
  navigator.mediaDevices.getUserMedia({ audio: true })
    .then(stream => {
      const ctx = new AudioContext({ sampleRate: 16000 });
      const source = ctx.createMediaStreamSource(stream);
      const processor = ctx.createScriptProcessor(4096, 1, 1);
      processor.onaudioprocess = e => {
        ws.send(e.inputBuffer.getChannelData(0).buffer);
      };
      source.connect(processor);
      processor.connect(ctx.destination);
    });
};

ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  if (msg.is_final) {
    console.log(`Final: ${msg.translated_text}`);
  } else {
    console.log(`Partial: ${msg.translated_text}`);
  }
};

限制说明

Limitations

项目Item限制Limit
单连接最长时长Max connection duration60 分钟minutes
音频采样率Sample rate8000 ~ 48000 Hz
并发连接数Concurrent connections企业版 50,基础版 5Enterprise 50, Basic 5
平均延迟Average latency< 500ms

错误码

Error Codes

HTTP Code错误码Error Code说明Description
2000成功Success
4001001参数错误,请检查必填参数和参数格式Invalid parameter; check required fields and format
4001002不支持的语言代码Unsupported language code
4012001认证失败,Token 无效或已过期Authentication failed; invalid or expired token
4032003无权限访问该资源Access denied; insufficient permissions
4133001请求文本超出长度限制(最大 50,000 字符)Text exceeds maximum length (50,000 chars)
4294001请求频率超限,请稍后重试Rate limit exceeded; please retry later
5005001服务器内部错误,请重试或联系技术支持Internal server error; retry or contact support
使用说明
  • 所有 API 请求均使用 HTTPS,建议开启 HTTP Keep-Alive 以提高性能。
  • All API requests use HTTPS; enable HTTP Keep-Alive for better performance.
  • 请勿在客户端代码中暴露 Access Token,建议通过后端代理调用。
  • Do not expose your Access Token in client-side code; use a backend proxy.
  • 推荐设置合理的超时时间(30 秒),并实现指数退避重试策略。
  • Set a reasonable timeout (30s) and implement exponential backoff for retries.