Qwen-Audio-TTS / CosyVoice C# SDK overview

Updated at:

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

  1. Create a CosyVoiceSpeechSynthesizer instance with a voice and output format matching the model, and subscribe to events such as AudioReceived and SentenceProgress as needed.
  2. When using SendTextAsync, call StartAsync to connect and send run-task.
  3. Call SendTextAsync to send text segments; if you already have all segments, you can call StreamAudioAsync or SynthesizeAsync directly without calling StartAsync first.
  4. Receive audio chunks in AudioReceived and sentence progress or timestamps in SentenceProgress (or read chunks in order from StreamAudioAsync).
  5. When using SendTextAsync, call StopAsync after sending all text and wait for task-finished (SynthesisCompleted).
  6. To interrupt the current synthesis task while keeping the connection open, call CancelSynthesisKeepConnectionAsync; call DisposeAsync when done.

Request parameters

Pass CosyVoiceSynthesizerOptions to the CosyVoiceSpeechSynthesizer constructor. When EnableSsml, WordTimestampEnabled, EnableAigcTag, and EnableMarkdownFilter are null, the SDK still sends them as false.

ParameterTypeDefaultDescription

Model

string

qwen-audio-3.1-tts-flash

Model ID. Set the model for this family explicitly.

Voice

string

longanfengyue_v3.1

Voice ID matched to the selected model. Qwen-Audio-TTS: Supported voices; CosyVoice: Supported voices.

Format

string

pcm

Audio format.

SampleRate

int

22050

Audio sample rate in Hz.

Volume

int?

null

Volume, from 0 to 100.

Rate

double?

null

Speech rate multiplier.

Pitch

double?

null

Pitch multiplier.

BitRate

int?

null

Output bitrate in kbps (6 to 510). Effective only when Format is opus or mp3.

Seed

int?

null

Random seed.

EnableSsml

bool?

null

Enable SSML parsing for complete SSML input. Send the complete SSML text in a single continue-task. With SynthesizeAsync, pass only one text segment. Audio can still be received as a stream.

WordTimestampEnabled

bool?

null

Return word-level timestamps.

LanguageHints

IList<string>?

null

For multilingual or dialect voices on qwen-audio-3.1-tts-flash, see Qwen-Audio-TTS. For CosyVoice, pass one target language; only the first value is used. See CosyVoice · Client events.

Instruction

string?

null

Voice instruction.

EnableAigcTag

bool?

null

Embed AIGC metadata in wav, mp3, or opus audio.

NoteAmong CosyVoice models, only cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2 support this option.

AigcPropagator

string?

null

AIGC content propagator identifier.

NoteSent only when EnableAigcTag is true; in the CosyVoice series, supported only by cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2.

AigcPropagateId

string?

null

Identifier of the AIGC content distribution event.

NoteSent only when EnableAigcTag is true. Among CosyVoice models, only cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2 support this option.

HotFixJson

string?

null

JSON object with pronunciation and replace rules. cosyvoice-v2 does not support this parameter.

ExtraParametersJson

string?

null

Pass a JSON object as a string. Its fields are merged into the protocol parameters object.

EnableMarkdownFilter

bool?

null

Filter Markdown markers in input. cosyvoice-v2 does not support this parameter.