Speech Synthesis (Sync)
This API supports synchronous text-to-speech generation, processing up to 500 characters of text per request.
- Supports independent selection of system voices.
- Supports adjustment of intonation, speech rate, volume, and output format.
- 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 Name | Voice ID | Language | Supported Dialects |
|---|---|---|---|---|
| 1 | Chenyu | cn_male_chenyu | Chinese, Filipino | Cantonese, Sichuanese, Default |
| 2 | Chenyu Robot Effect | cn_male_chenyu_robot | Chinese | - |
| 3 | Chenyu Elderly | cn_male_chenyu_elderly | Chinese | - |
| 4 | Chenyu Steady | cn_male_chenyu_steady | Chinese | - |
| 5 | Chenyu Fluent | cn_male_chenyu_fluent | Chinese | - |
| 6 | Shasha | cn_female_shasha | Chinese, Japanese, Korean, Thai, Vietnamese, Indonesian, Khmer | - |
| 7 | Jiajia | cn_female_jiajia | Chinese, Indonesian, Malay, Burmese, Lao | - |
| 8 | Ruolin | cn_female_ruolin | Chinese | - |
| 9 | Xiaodi Warm | cn_female_xiaodi_warm | Chinese | - |
| 10 | Johnny | en_male_johnny | English | - |
| 11 | Jane | en_female_jane | English | - |
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
- Obtain a key from API Key Management and set the
API_KEYenvironment variable. - Open a WebSocket connection:
wss://maas-api.unisound.com/v1/ws/tts?model=u2-tts(see Sync Speech Synthesis API). - Send a text frame
startwith the fulltextto synthesize, plusvoice_setting/audio_setting. - Receive binary audio frames in order (encoded as
audio_setting.format; concatenate for the full audio) and parse text control frames. - 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).