Skip to main content

Qwen-Audio-Live C# SDK

Open a full-duplex voice session over WebSocket and receive subtitles and audio.

Prerequisites

Install C# SDK 1.2.0 and configure your API key. Replace WorkspaceId with your workspace ID in Singapore. Qwen-Audio-Live requires a workspace-specific domain. Prepare input.pcm as 16 kHz, 16-bit little-endian, mono raw PCM. You can use FFmpeg to convert a recording.
ffmpeg -i input.wav -ar 16000 -ac 1 -f s16le input.pcm

Quick start

Create a .NET console project, add the SDK, and save the code as Program.cs. Run it with dotnet run.
using DashScope.Qwen;

var input = await File.ReadAllBytesAsync("input.pcm");
if (input.Length == 0 || input.Length % 2 != 0)
    throw new ArgumentException("input.pcm must contain 16-bit PCM samples.");

await using var output = File.Create("reply.pcm");
await using var live = new QwenLiveConversation(new QwenLiveConversationOptions
{
    Url = "wss://WorkspaceId.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime",
    Model = "qwen-audio-3.1-live",
    FrontModel = "qwen-audio-4.0-realtime-preview",
    TurnDetectionMode = QwenLiveTurnMode.Duplex,
    Voice = "yunshu",
    Delegation = QwenLiveDelegationMode.Client
});
live.AssistantTextDelta += (_, e) => Console.Write(e.Text);
live.AssistantAudioChunk += (_, e) => output.Write(e.Pcm);
live.ErrorOccurred += (_, e) => Console.Error.WriteLine(e.ServerErrorMessage ?? e.ToString());

var configured = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
live.RawServerEvent += (_, e) =>
{
    if (e.Type == "session.updated")
        configured.TrySetResult();
};
await live.ConnectAsync();
await configured.Task.WaitAsync(TimeSpan.FromSeconds(10));
const int frameSize = 1280;
for (var offset = 0; offset < input.Length; offset += frameSize)
{
    await live.AppendAudioAsync(input.AsMemory(offset, Math.Min(frameSize, input.Length - offset)));
    await Task.Delay(40);
}

var silence = new byte[frameSize];
for (var i = 0; i < 25; i++)
{
    await live.AppendAudioAsync(silence);
    await Task.Delay(40);
}

await Task.Delay(TimeSpan.FromSeconds(30));
await live.FinishAsync();
Console.WriteLine($"\nSession duration: {live.Usage?.Seconds} s");
The terminal prints subtitles. reply.pcm contains 24 kHz, 16-bit little-endian, mono audio. The example allows 30 seconds for the reply. In your app, wait for the full reply before closing the session.

Session options

OptionDescription
ModelModel ID for the connection. Use qwen-audio-3.1-live.
FrontModelModel that talks to the user. The example uses qwen-audio-4.0-realtime-preview.
TurnDetectionModeThe example uses duplex so the model can keep listening while it speaks.
VoiceChoose a voice supported by the frontend model. The example uses yunshu.
DelegationThe example uses Client mode. Set the backend model when you use Responses mode.
See the Qwen-Audio-Live API reference for session configuration.

Methods and events

MethodPurpose
ConnectAsyncOpen the connection, wait for session.created, and send session.update.
AppendAudioAsyncSend an audio frame.
MuteInputAsync / UnmuteInputAsyncMute or unmute input.
SendUserTextAsync / SendItemAsyncAdd text or a custom item to the session.
SendFunctionCallOutputAsyncSubmit the result of a frontend function call.
SendThinkingAsync / SendInstructionAsync / SendCommentaryAsyncSend task progress, instructions, or commentary to the frontend model.
AppendBackendItemAsync / CreateBackendResponseAsyncAppend an item to the backend or trigger a backend response.
SendRawEventAsyncSend a Live client event.
FinishAsyncSend session.close, wait for session.closed, and disconnect.
AbortAsyncDisconnect immediately. Final session usage may still be pending.
Event or callbackDescription
SessionReadyFires when the session ID is available. Wait for session.updated before sending audio.
UserTranscriptDelta / AssistantTextDeltaUser transcript or assistant subtitle deltas.
AssistantAudioChunkA chunk of reply audio.
DelegationCreatedA delegation summary.
BackendEventA backend event in Responses mode.
FunctionCallA frontend function call.
UsageUpdated / ClosedA usage update or session closure. The close event includes final usage.
RawServerEvent / ErrorOccurredOther server events or errors.
  • The delegation summary contains DelegationId, TaskId, Summary, OffsetMs, and Context. Use the delegation ID when sending task progress.
  • Usage exposes the latest usage, including Seconds and the context window usage ratio when available. On normal closure, session.closed reports final usage.
The SDK sends audio with session.input_audio.append. See WebSocket connection and Server events for the protocol and event fields.