Use the Qwen-Audio-TTS non-real-time speech synthesis Python SDK to generate audio from text in non-streaming or streaming mode.
Prerequisites
- Obtain an API key completed, and API key configured as an environment variable.
- DashScope Python SDK 1.25.17 or later installed. Install the latest version.
HttpSpeechSynthesizer class
Package path: dashscope.audio.http_tts.http_speech_synthesizer.HttpSpeechSynthesizer
Description: HTTP-based speech synthesis. Use the stream parameter to switch between non-streaming and streaming modes.
call() — speech synthesis
Method signature:
| Parameter | Type | Required | Description |
|---|---|---|---|
model | str | Yes | The speech synthesis model. |
text | str | Yes | The text to synthesize. Supports SSML and LaTeX input. Replace the text content with the corresponding format.
|
voice | str | Yes | The voice for synthesis. Valid values:
|
format | str | No | The audio format. Default value: mp3. Valid values:
|
sample_rate | int | No | The audio sample rate in Hz. Valid values: 8000, 16000, 22050 (default), 24000, 44100, 48000. |
volume | int | No | The volume level. Default value: 50. Valid values: [0, 100]. |
rate | float | No | The speech rate. Default value: 1.0. Valid values: [0.5, 2.0]. |
pitch | float | No | The pitch. Default value: 1.0. Valid values: [0.5, 2.0]. |
bit_rate | int | No | The audio bitrate in kbps. Default value: 32. Valid values: [6, 510]. Tip: This parameter is supported only when format is set to opus. |
enable_ssml | bool | No | Specifies whether to enable SSML. Set this to True when text uses SSML format. Default: False. For supported SSML tags and usage, see SSML. For the SSML usage restrictions (supported models, voices, and APIs), see Limitations. |
word_timestamp_enabled | bool | No | Specifies whether to enable word-level timestamps. Default: False.
|
seed | int | No | The random seed for synthesis. Different seeds produce different outputs. With identical model version, text, voice, and other parameters, the same seed reproduces the same output. Default value: 0. Valid values: [0, 65535]. |
language_hints | list | No | Tip: - This parameter is an array, but the current version processes only the first element. Pass a single value.
|
instruction | str | No | An instruction that controls the synthesis behavior, such as dialect, emotion, or role. For usage details, see Non-real-time speech synthesis. |
enable_aigc_tag | bool | No | Specifies whether to embed an AIGC watermark in the generated audio. When set to True, a watermark is embedded in audio files of supported formats (wav/mp3/opus). Default: False. |
aigc_propagator | str | No | Sets the ContentPropagator field in the AIGC watermark to identify the content distributor. Takes effect only when enable_aigc_tag is True. Default: QwenCloud account ID. |
aigc_propagate_id | str | No | Sets the PropagateID field in the AIGC watermark to uniquely identify a specific distribution action. Takes effect only when enable_aigc_tag is True. Default: The request ID of the current speech synthesis request. |
hot_fix | dict | No | Text hot-fix configuration. Customizes the pronunciation of specified words or substitutes text before speech synthesis. Parameters:
|
stream | bool | No | Specifies whether to enable streaming mode. When set to False (non-streaming), the method returns a result object containing the audio URL. When set to True (streaming), the method returns an iterator that yields audio data chunks. Default: False. |
api_key | str | No | The API key. If not specified, the SDK automatically reads from the DASHSCOPE_API_KEY environment variable. |
-
Non-streaming mode (
stream=False): Returns a result object with the following properties:audio_url: The audio download URL (expires after a limited time).audio_id: The audio ID.expires_at: The URL expiration time.
-
Streaming mode (
stream=True): Returns an iterator. Each element contains the following properties:audio_data: The audio binary data (bytes) for the current chunk.sentences: Sentence-level synthesis information (if available).
In streaming mode, the last element of the iterator contains both an audio data chunk and an
audio_url field (the download URL for the complete audio). To avoid duplicating audio content when concatenating chunks, filter out this element by checking the not chunk.audio_url condition during iteration.Sample code
The following examples show non-streaming and streaming calls to Qwen-Audio-TTS speech synthesis. Set the DASHSCOPE_API_KEY environment variable before running the code.
Use a voice that matches the model and supports the target language. When switching models, update the voice accordingly. See Qwen-Audio-TTS voice list.