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.
Follow the SDK installation guide to install and configure your API key.
Synthesize two text segments with the
Pass
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.
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 | string | cosyvoice-v3-plus | Model ID. Set the model for this family explicitly. |
Voice | string | longanyang | Voice ID matched to the selected model. See 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 | Target language for synthesis. 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.Only cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2 support this option. |
AigcPropagator | string? | null | AIGC content propagator identifier. Sent only when EnableAigcTag is true; supported only by cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2. |
AigcPropagateId | string? | null | Identifier of the AIGC content distribution event. Sent only when EnableAigcTag is true; supported only by cosyvoice-v3-flash, cosyvoice-v3-plus, and cosyvoice-v2. |
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. |