Learn about real-time speech synthesis features, streaming calls, and ways to improve synthesis quality.
Features
Real-time speech synthesis accepts text segments and streams audio back, enabling speech output as a large language model generates text.
- Languages and dialects: Select a model and voice for the target language, or use an instruction to specify a dialect. See Dialects.
- Expressive speech: Adjust delivery with natural-language instructions, or insert emotion and nonverbal tags into the text. See Instruction control and Emotion and nonverbal tags.
- Custom voices: Create a voice from an audio sample with voice cloning, or from a text description with voice design.
- Fine-grained control and formulas: Use SSML and LaTeX to control pauses and pronunciation, or convert formulas to speech.
- Audio controls: Adjust speed, pitch, volume, and bitrate, and output common audio formats such as PCM, WAV, MP3, and Opus at sample rates up to 48 kHz. See the corresponding API parameters for supported values and conditions.
Supported features vary by model and voice. Check the scope in the corresponding feature documentation.To submit complete text in one HTTP request, see Non-real-time speech synthesis.
Get started
- Obtain an API key and set the environment variable
DASHSCOPE_API_KEY. Use a key for the region in the example. - For a workspace-specific endpoint, replace
{WorkspaceId}with the workspace ID for that region. - Install the DashScope SDK and choose the Python or Java example.
Mobile applications that require low latency and stable audio transmission on weak networks can use AOQ. For AOQ access, protocol differences, and model support across AOQ, WebRTC, and WebSocket, see Realtime API overview.
- Qwen-Audio-TTS (recommended)
- CosyVoice
This example uses
qwen-audio-3.1-tts-flash and the longanhuan_v3.1 system voice in Beijing. It sends text in segments and saves the streamed audio to output.mp3.Python
Improve synthesis quality
Languages and pronunciation
Choose an approach based on what you need to control:
| Requirement | Approach |
|---|---|
| Set the target language of synthesized speech | Use language_hints to specify the overall synthesis language, guide the pronunciation of numbers, abbreviations, and symbols, and improve synthesis in less widely used languages. |
| Correct specific words or replace text | Use hot_fix to assign pinyin to specific words or replace words before synthesis. This is useful for consistent corrections to names and other local text. |
| Control pauses, number reading, or pronunciation precisely | Mark the relevant text with SSML tags to control pauses, reading rules, and pronunciation. |
| Read mathematical formulas | Use the CosyVoice LaTeX feature to convert supported mathematical formulas into Chinese speech. |
When using SSML, submit the complete SSML text in a single input and receive audio incrementally. Fragmented text input is not supported.
Instruction control
Use instruction to specify the synthesis language and describe the speech rate, emotion, or speaking style in natural language to control the synthesized speech.
To create a new voice with a specific gender, age, or vocal character, or for guidance and examples on writing voice descriptions, see voice design.
- Qwen-Audio-TTS
- CosyVoice
System, cloned, and designed voices for
qwen-audio-3.1-tts-plus, qwen-audio-3.1-tts-flash, qwen-audio-3.0-tts-plus, and qwen-audio-3.0-tts-flash all support natural-language instructions through instruction in synthesis requests.We recommend specifying the synthesis language and desired effect in instruction to improve synthesis quality. The language specified in the instruction must match the language of the text to be synthesized. For example, when synthesizing Dutch text, set:Dialects
Use dialect synthesis to generate speech in Chinese dialects such as Henan or Sichuan dialect. How you select a dialect depends on the model and voice type.
- Qwen-Audio-TTS
- CosyVoice
- System voices: Select a voice from the Qwen-Audio-TTS voice list that supports the dialect. A voice that directly supports the dialect requires no additional setting. For voices that support dialect instructions, use the instruction control parameter
instruction. - Cloned voices: Specify a dialect with the instruction control parameter
instruction.
instruction="请讲上海话。" when initializing SpeechSynthesizer to synthesize Chinese text in Shanghainese.Emotion and nonverbal tags
Applies to
qwen-audio-3.1-tts-plus, qwen-audio-3.1-tts-flash, qwen-audio-3.0-tts-plus, and qwen-audio-3.0-tts-flash.text, without additional parameters.
Usage examples
This text starts with an excited delivery, inserts laughter, and then continues speaking:
Supported tags
- Control tags
- Nonverbal tags
A control tag sets the emotion or style of the following text until the next control tag. When long sentences are automatically segmented, the effect is bounded by the resulting text segment.
| Tag | Description |
|---|---|
[sad] | Sad |
[amazed] | Amazed |
[deep and loud shouting] | Deep, loud shouting |
[trembling] | Trembling |
[angry] | Angry |
[excited] | Excited |
[sarcastic] | Sarcastic |
[curious] | Curious |
[like dracula] | Dracula-like delivery |
[bored] | Bored |
[tired] | Tired |
[scornful] | Scornful |
[shouting] | Shouting |
[asmr] | Soft ASMR delivery |
[panicked] | Panicked |
[mischievously] | Mischievous |
[empathetic] | Empathetic |
[whispers] | Whispering |
[reluctantly] | Reluctant |
[crying] | Crying |
[serious] | Serious |
[very slowly] | Very slow |
[very fast] | Very fast |
Apply in production
Cancel task
With Qwen-Audio-TTS or CosyVoice, send a cancellation instruction to interrupt the current synthesis task. The server ends the task and returns task-finished.
| Method | Version requirement | Usage |
|---|---|---|
| Python SDK | 2.0.1 or later | Call session.cancel() on the session object returned by client.audio.tts.connect. |
| Java SDK | 2.22.26 or later | Call SpeechSynthesizer.streamingCancel(). |
| Raw WebSocket protocol | No SDK required | Send finish-task with directive=cancel in input. |
WebSocket connection reuse
A WebSocket connection can handle successive synthesis rounds. Start the next round as follows for each model series:
| Model series | End-of-round event | Start the next round |
|---|---|---|
| Qwen-Audio-TTS, CosyVoice | task-finished | Send run-task with a new task_id, then send text using the series-specific flow. A new task_id is required for every round, including after cancellation.When calling Qwen-Audio-TTS or CosyVoice with the Python SDK, the session created by client.audio.tts.connect closes the underlying connection when the task completes or is canceled. To reuse a connection, first create a shared WebSocket connection with client.ws_connection() and pass it to client.audio.tts.connect via connection=. You can then run multiple synthesis rounds in sequence over the same connection. Close the connection with WsConnection.close when you are done. |
| Qwen3-TTS | response.done | Continue sending input_text_buffer.append in the same session. Synthesis starts automatically or after input_text_buffer.commit, depending on the session mode. Send session.finish only after all text has been submitted. |
Rate limits
Control request frequency and concurrency according to the model’s rate limits. For limits, common errors, and handling instructions, see Rate limits.
High-concurrency best practices
Use object pools to reuse synthesis objects and connections, and configure concurrency, warm-up, and error handling for your workload. See High-concurrency speech synthesis.
Supported models
- Singapore
- China (Beijing)
API reference
For AOQ access, see Qwen-Audio-TTS AOQ access or CosyVoice AOQ access. For the exact support scope, see Model and protocol support.
For complete raw WebSocket examples, see the following pages. Use the API entry for each series to find Python and Java SDK parameters and methods.
FAQ
How do I troubleshoot silent audio when using a cloned voice?
Check that voice is the ID of a successfully created voice and that its target_model matches the synthesis model. For voice status, source audio requirements, and quality guidance, see Voice cloning.
Why does the actual duration of synthesized audio differ from the duration shown in the WAV file?
For streamed WAV output, the file header may not reflect the final audio length. To obtain accurate duration metadata, set format to pcm, receive the complete audio, and add a WAV header containing the actual length.
Why can't the audio file play?
Troubleshoot based on your scenario:
-
Audio saved as a complete file (such as xx.mp3)
- Audio format consistency: The audio format in the request parameters must match the file extension (for example, if the parameter is wav, the file must be .wav).
- Player compatibility: Confirm that the player supports the audio format and sample rate.
-
Streaming audio playback
- Save the audio stream as a complete file and try playing it with a media player. If the file doesn't play, refer to scenario 1 above.
- If the file plays correctly, the issue is in the streaming playback implementation. Confirm that the player supports streaming playback (such as ffmpeg, pyaudio, AudioFormat, or MediaSource).
Why is audio playback stuttering?
Troubleshoot with the following steps:
- Check the text sending rate: Make sure the sending interval is reasonable to avoid the previous audio segment finishing before the next text arrives.
-
Check callback function performance:
- Confirm that no blocking logic exists in the callback function.
- Callbacks run on the WebSocket thread. Blocking operations affect data reception. Write audio data to a separate buffer and process it in another thread.
- Check network stability: Network fluctuations can cause audio transmission interruptions or delays.