语音合成(同步)

本接口支持基于文本到语音的同步生成,单次可处理最长 500 字符的文本。

  1. 支持系统音色自主选择
  2. 支持语调、语速、音量、输出格式调整
  3. 支持流式输出。

支持音色

表格中为云知声Maas平台系统音色,为您提供语音合成选择。

序号音色名称 (Voice Name)音色 ID (Voice ID)语种支持方言
1晨宇cn_male_chenyu中文、菲律宾语粤语、四川话、默认
2晨宇机器人特效cn_male_chenyu_robot中文-
3晨宇苍老cn_male_chenyu_elderly中文-
4晨宇沉稳cn_male_chenyu_steady中文-
5晨宇流利cn_male_chenyu_fluent中文-
6莎莎cn_female_shasha中文、日语、韩语、泰语、越南语、印尼语、柬埔寨语-
7佳佳cn_female_jiajia中文、印尼语、马来语、缅甸语、老挝语-
8若琳cn_female_ruolin中文-
9晓迪温暖cn_female_xiaodi_warm中文-
10Johnnyen_male_johnny英语-
11Janeen_female_jane英语-

发音情绪

高兴,愤怒,沮丧,低语,大声,中性。目前仅 cn_male_chenyu 发音人,且语种为中文、方言为默认时支持。

使用流程

  1. 在 API Key 管理 获取密钥,并写入环境变量 API_KEY
  2. 建立 WebSocket 连接:wss://maas-api.unisound.com/v1/ws/tts?model=u2-tts(详见 同步语音合成 API)。
  3. 发送文本帧 start,在其中携带待合成全文 text,并配置 voice_setting / audio_setting
  4. 按序接收服务端二进制音频帧(按 audio_setting.format 编码,拼接即为完整音频),并解析文本控制帧
  5. 当控制帧 end=true 时,本轮合成结束

过程示例

1. 语音合成

Python
"""
本示例演示同步语音合成。
注意:需要先将密钥写入环境变量 API_KEY;并安装 websockets:pip install websockets
"""
import asyncio
import json
import os

import websockets

API_KEY = os.environ["API_KEY"]
WS_URL = "wss://maas-api.unisound.com/v1/ws/tts?model=u2-tts"

START_FRAME = {
  "type": "start",
  "text": "水泊梁山的故事家喻户晓",
  "voice_setting": {
    "voice_id": "cn_male_chenyu",
    "volume": 50,
    "pitch": 50,
    "bright": 50,
    "language": "zh"
  },
  "pronunciation_dict": {
    "tone": ["水泊梁山/水泊<py>po1</py>梁山"]
  },
  "audio_setting": {
    "audio_sample_rate": 32000,
    "format": "mp3"
  }
}


async def run(output_path: str = "output_audio.mp3"):
  headers = {"Authorization": f"Bearer {API_KEY}"}
  audio_chunks = []

  async with websockets.connect(WS_URL, additional_headers=headers) as ws:
    await ws.send(json.dumps(START_FRAME))
    while True:
      msg = await ws.recv()
      if isinstance(msg, (bytes, bytearray)):
        audio_chunks.append(bytes(msg))
        continue

      frame = json.loads(msg)
      print(frame)
      status_code = frame.get("base_resp", {}).get("status_code", 0)
      if status_code != 0:
        raise RuntimeError(frame.get("base_resp", {}).get("status_msg", "tts failed"))
      if frame.get("end"):
        break

  with open(output_path, "wb") as f:
    for chunk in audio_chunks:
      f.write(chunk)
  print(f"saved: {output_path}, bytes={sum(len(c) for c in audio_chunks)}")


if __name__ == "__main__":
  asyncio.run(run())

2. 解析识别结果

服务端以二进制音频帧流式返回合成结果,并以文本 JSON 控制帧标识本轮结束。当 end=true 时表示本轮合成结束。

Python
# 典型控制帧(合成结束)
{
  "sid": "与 start 中 sid 一致(若客户端传入)",
  "base_resp": {
    "status_code": 0,
    "status_msg": "success"
  },
  "end": true
}

更多字段说明、音频格式与错误码,请参阅 同步语音合成 API。也可在 模型体验 · 语音合成(同步) 中在线调试。