Qwen-Audio-TTS / CosyVoice C# SDK overview
Use the C# SDK to synthesize speech with Qwen-Audio-TTS / CosyVoice and receive audio chunks.
Quick start
Follow SDK overview to install the SDK and configure your API key. Replace WorkspaceId in the example endpoint with your workspace ID from the same region.
Qwen-Audio-TTS
Synthesize two text segments with the longanfengyue_v3.1 voice and write the audio chunks to output.wav in order. Play the file when the program finishes.
using DashScope.Speech;
await using var client = new CosyVoiceSpeechSynthesizer(new CosyVoiceSynthesizerOptions
{
Model = "qwen-audio-3.1-tts-flash",
Voice = "longanfengyue_v3.1",
Url = "wss://WorkspaceId.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference",
Format = "wav",
SampleRate = 16000,
});
using var output = File.Create("output.wav");
await foreach (var chunk in client.StreamAudioAsync(["Good afternoon. ", "Welcome to our meeting."]))
{
await output.WriteAsync(chunk);
}
CosyVoice
Synthesize two text segments with the longanhuan_v3 voice and write the audio chunks to output.wav in order. Play the file when the program finishes.
using DashScope.Speech;
await using var client = new CosyVoiceSpeechSynthesizer(new CosyVoiceSynthesizerOptions
{
Model = "cosyvoice-v3-flash",
Voice = "longanhuan_v3",
Url = "wss://WorkspaceId.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference",
Format = "wav",
SampleRate = 16000,
});
using var output = File.Create("output.wav");
await foreach (var chunk in client.StreamAudioAsync(["Good afternoon. ", "Welcome to our meeting."]))
{
await output.WriteAsync(chunk);
}
Call workflow
- Create a
CosyVoiceSpeechSynthesizerinstance with a voice and output format matching the model, and subscribe to events such asAudioReceivedandSentenceProgressas needed. - When using
SendTextAsync, callStartAsyncto connect and sendrun-task. - Call
SendTextAsyncto send text segments; if you already have all segments, you can callStreamAudioAsyncorSynthesizeAsyncdirectly without callingStartAsyncfirst. - Receive audio chunks in
AudioReceivedand sentence progress or timestamps inSentenceProgress(or read chunks in order fromStreamAudioAsync). - When using
SendTextAsync, callStopAsyncafter sending all text and wait fortask-finished(SynthesisCompleted). - To interrupt the current synthesis task while keeping the connection open, call
CancelSynthesisKeepConnectionAsync; callDisposeAsyncwhen done.
Request parameters
Pass CosyVoiceSynthesizerOptions to the CosyVoiceSpeechSynthesizer constructor. When EnableSsml, WordTimestampEnabled, EnableAigcTag, and EnableMarkdownFilter are null, the SDK still sends them as false.
| Parameter | Type | Default | Description |
|---|---|---|---|
|
|
| Model ID. Set the model for this family explicitly. |
|
|
| Voice ID matched to the selected model. Qwen-Audio-TTS: Supported voices; CosyVoice: Supported voices. |
|
|
| Audio format. |
|
|
| Audio sample rate in Hz. |
|
|
| Volume, from 0 to 100. |
|
|
| Speech rate multiplier. |
|
|
| Pitch multiplier. |
|
|
| Output bitrate in kbps ( |
|
|
| Random seed. |
|
|
| Enable SSML parsing for complete SSML input. Send the complete SSML text in a single |
|
|
| Return word-level timestamps. |
|
|
| For multilingual or dialect voices on |
|
|
| Voice instruction. |
|
|
| Embed AIGC metadata in NoteAmong CosyVoice models, only |
|
|
| AIGC content propagator identifier. NoteSent only when |
|
|
| Identifier of the AIGC content distribution event. NoteSent only when |
|
|
| JSON object with |
|
|
| Pass a JSON object as a string. Its fields are merged into the protocol |
|
|
| Filter Markdown markers in input. |