Skip to main content

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
  • CosyVoice
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.ap-southeast-1.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
Modelstringqwen-audio-3.1-tts-flashModel ID. Set the model for this family explicitly.
Voicestringlonganfengyue_v3.1Voice ID matched to the selected model. Qwen-Audio-TTS: Supported voices; CosyVoice: Supported voices.
FormatstringpcmAudio format.
SampleRateint22050Audio sample rate in Hz.
Volumeint?nullVolume, from 0 to 100.
Ratedouble?nullSpeech rate multiplier.
Pitchdouble?nullPitch multiplier.
BitRateint?nullOutput bitrate in kbps (6 to 510). Effective only when Format is opus or mp3.
Seedint?nullRandom seed.
EnableSsmlbool?nullEnable 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.
WordTimestampEnabledbool?nullReturn word-level timestamps.
LanguageHintsIList<string>?nullFor 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.
Instructionstring?nullVoice instruction.
EnableAigcTagbool?nullEmbed AIGC metadata in wav, mp3, or opus audio.
Among CosyVoice models, only cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2 support this option.
AigcPropagatorstring?nullAIGC content propagator identifier.
Sent only when EnableAigcTag is true; in the CosyVoice series, supported only by cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2.
AigcPropagateIdstring?nullIdentifier of the AIGC content distribution event.
Sent only when EnableAigcTag is true. Among CosyVoice models, only cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2 support this option.
HotFixJsonstring?nullJSON object with pronunciation and replace rules. cosyvoice-v2 does not support this parameter.
ExtraParametersJsonstring?nullPass a JSON object as a string. Its fields are merged into the protocol parameters object.
EnableMarkdownFilterbool?nullFilter Markdown markers in input. cosyvoice-v2 does not support this parameter.