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]. |
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 | Specifies the target language for speech synthesis to improve output quality.Use this parameter when numbers, abbreviations, or symbols are not pronounced as expected, or when synthesis quality for secondary languages is poor. Examples:
Valid values
|
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: Alibaba Cloud UID. |
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).
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.
- Non-streaming call
- Streaming call
A non-streaming call sets
stream=False, waits for synthesis to complete, and returns an audio URL. Download the audio file from this URL.