Skip to main content
SDK access

CosyVoice C# SDK

Use the C# SDK to synthesize speech with CosyVoice and receive audio chunks.

Supported regions: Singapore and China North 2 (Beijing). Use an API key and endpoint from the region you call; see Regions and endpoints.

Quick start

Follow the SDK installation guide to install and configure your API key. Synthesize two text segments with the longanyang 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-plus",
    Voice = "longanyang",
    // Singapore (ap-southeast-1). For another region, replace ap-southeast-1 in the URL.
    // API keys are region-specific. See https://docs.qwencloud.com/developer-guides/getting-started/regions
    Url = "wss://ap-southeast-1.maas.qwencloudapi.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
Modelstringcosyvoice-v3-plusModel ID. Set the model for this family explicitly.
VoicestringlonganyangVoice ID matched to the selected model. See 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>?nullTarget language for synthesis. Only the first value is used. See CosyVoice · Client events.
Instructionstring?nullVoice instruction.
EnableAigcTagbool?nullEmbed AIGC metadata in wav, mp3, or opus audio.
Only cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2 support this option.
AigcPropagatorstring?nullAIGC content propagator identifier.
Sent only when EnableAigcTag is true; 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; supported only by cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2.
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.
CosyVoice C# SDK - QwenCloud