Speech Synthesis (Sync)

This API supports synchronous text-to-speech generation, processing up to 500 characters of text per request.

  1. Supports independent selection of system voices.
  2. Supports adjustment of intonation, speech rate, volume, and output format.
  3. Supports streaming output.

Supported Voices

The table below shows the system voices of the Unisound Maas Platform, providing you with speech synthesis options.

No.Voice NameVoice IDLanguageSupported Dialects
1Chenyucn_male_chenyuChinese, FilipinoCantonese, Sichuanese, Default
2Chenyu Robot Effectcn_male_chenyu_robotChinese-
3Chenyu Elderlycn_male_chenyu_elderlyChinese-
4Chenyu Steadycn_male_chenyu_steadyChinese-
5Chenyu Fluentcn_male_chenyu_fluentChinese-
6Shashacn_female_shashaChinese, Japanese, Korean, Thai, Vietnamese, Indonesian, Khmer-
7Jiajiacn_female_jiajiaChinese, Indonesian, Malay, Burmese, Lao-
8Ruolincn_female_ruolinChinese-
9Xiaodi Warmcn_female_xiaodi_warmChinese-
10Johnnyen_male_johnnyEnglish-
11Janeen_female_janeEnglish-

Voice Emotion

Happy, angry, depressed, whisper, loudly, and neutral. Currently supported only for cn_male_chenyu with Chinese language and the default dialect.

Usage Flow

  1. Obtain a key from API Key Management and set the API_KEY environment variable.
  2. Open a WebSocket connection: wss://maas-api.unisound.com/v1/ws/tts?model=u2-tts (see Sync Speech Synthesis API).
  3. Send a text frame start with the full text to synthesize, plus voice_setting / audio_setting.
  4. Receive binary audio frames in order (encoded as audio_setting.format; concatenate for the full audio) and parse text control frames.
  5. When the control frame has end=true, the round is finished.

Examples

1. Speech synthesis

Python
"""
Sync TTS example.
Set API_KEY first. Install: 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": "The story of Water Margin is widely known",
  "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. Parse results

The server streams synthesis results as binary audio frames and signals completion with a text JSON control frame. When end=true, the round is finished.

Python
# Typical control frame (synthesis finished)
{
  "sid": "same as start.sid if provided by client",
  "base_resp": {
    "status_code": 0,
    "status_msg": "success"
  },
  "end": true
}

For more field descriptions, audio formats, and error codes, see Sync Speech Synthesis API. You can also try it online in Playground · Speech Synthesis (Sync).