Skip to content

Add live audio transcription streaming support to Foundry Local C# SDK#485

Open
rui-ren wants to merge 39 commits intomainfrom
ruiren/audio-streaming-support-sdk
Open

Add live audio transcription streaming support to Foundry Local C# SDK#485
rui-ren wants to merge 39 commits intomainfrom
ruiren/audio-streaming-support-sdk

Conversation

@rui-ren
Copy link
Copy Markdown

@rui-ren rui-ren commented Mar 5, 2026

Here's the cleaned version:


Description:

Adds real-time audio streaming support to the Foundry Local C# SDK, enabling live microphone-to-text transcription via ONNX Runtime GenAI's StreamingProcessor API (Nemotron ASR).

The existing OpenAIAudioClient only supports file-based transcription. This PR introduces LiveAudioTranscriptionSession that accepts continuous PCM audio chunks (e.g., from a microphone) and returns partial/final transcription results as an async stream.

What's included

New files

  • src/OpenAI/LiveAudioTranscriptionClient.cs — Streaming session with StartAsync(), AppendAsync(), GetTranscriptionStream(), StopAsync()
  • src/OpenAI/LiveAudioTranscriptionTypes.csLiveAudioTranscriptionResponse (extends AudioCreateTranscriptionResponse) and CoreErrorResponse types
  • test/FoundryLocal.Tests/LiveAudioTranscriptionTests.cs — Unit tests for deserialization, settings, state guards

Modified files

  • src/OpenAI/AudioClient.cs — Added CreateLiveTranscriptionSession() factory method
  • src/Detail/ICoreInterop.cs — Added StreamingRequestBuffer struct, StartAudioStream, PushAudioData, StopAudioStream interface methods
  • src/Detail/CoreInterop.cs — Routes audio commands through existing execute_command / execute_command_with_binary native entry points
  • src/Detail/JsonSerializationContext.cs — Registered LiveAudioTranscriptionResponse for AOT compatibility
  • README.md — Added live audio transcription documentation

API surface

var audioClient = await model.GetAudioClientAsync();
var session = audioClient.CreateLiveTranscriptionSession();

session.Settings.SampleRate = 16000;
session.Settings.Channels = 1;
session.Settings.Language = "en";

await session.StartAsync();

// Push audio from microphone callback
await session.AppendAsync(pcmBytes);

// Read results as async stream
await foreach (var result in session.GetTranscriptionStream())
{
    Console.Write(result.Text);
}

await session.StopAsync();

Design highlights

  • Output type alignmentLiveAudioTranscriptionResponse extends AudioCreateTranscriptionResponse for consistent output format with file-based transcription
  • Internal push queue — Bounded Channel<T> serializes audio pushes from any thread (safe for mic callbacks) with backpressure
  • Fail-fast on errors — Push loop terminates immediately on any native error (no retry logic)
  • Settings freeze — Audio format settings are snapshot-copied at StartAsync() and immutable during the session
  • Cancellation-safe stopStopAsync always calls native stop even if cancelled, preventing native session leaks
  • Dedicated session CTS — Push loop uses its own CancellationTokenSource, decoupled from the caller's token
  • Routes through existing exportsStartAudioStream and StopAudioStream route through execute_command; PushAudioData routes through execute_command_with_binary — no new native entry points required

Core integration (neutron-server)

The Core side (AudioStreamingSession.cs) uses StreamingProcessor + Generator + Tokenizer + TokenizerStream from onnxruntime-genai to perform real-time RNNT decoding. The native commands (audio_stream_start/push/stop) are handled as cases in NativeInterop.ExecuteCommandManaged / ExecuteCommandWithBinaryManaged.

Verified working

  • ✅ SDK build succeeds (0 errors, 0 warnings)
  • ✅ Unit tests for JSON deserialization, type inheritance, settings, state guards
  • ✅ GenAI StreamingProcessor pipeline verified with WAV file (correct transcript)
  • ✅ Core TranscribeChunk byte[] PCM path matches reference float[] path exactly
  • ✅ Full E2E simulation: SDK Channel + JSON serialization + session management
  • ✅ Live microphone test: real-time transcription through SDK → Core → GenAI

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
foundry-local Ready Ready Preview, Comment Mar 30, 2026 9:08pm

Request Review

ruiren_microsoft added 2 commits March 10, 2026 18:09
@rui-ren rui-ren changed the title Add real-time audio streaming support (Microphone ASR) - c# Add live audio transcription streaming support to Foundry Local C# SDK Mar 13, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new C# SDK API for live/streaming audio transcription sessions (push PCM chunks, receive incremental/final text results) and includes a Windows microphone demo sample.

Changes:

  • Introduces LiveAudioTranscriptionSession + result/error types for streaming ASR over Core interop.
  • Extends Core interop to support audio stream start/push/stop (including binary payload routing).
  • Adds a samples/cs/LiveAudioTranscription demo project and updates the audio client factory API.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
sdk_v2/cs/test/FoundryLocal.Tests/Utils.cs Replaced prior test utilities with ad-hoc top-level streaming harness code (currently breaks test build).
sdk_v2/cs/test/FoundryLocal.Tests/ModelTests.cs Adds trailing blank lines (formatting noise).
sdk_v2/cs/src/OpenAI/LiveAudioTranscriptionTypes.cs Adds LiveAudioTranscriptionResult and a structured Core error type.
sdk_v2/cs/src/OpenAI/LiveAudioTranscriptionClient.cs Adds LiveAudioTranscriptionSession implementation (channels, retry, stop semantics).
sdk_v2/cs/src/OpenAI/AudioClient.cs Adds CreateLiveTranscriptionSession() and removes the public file streaming transcription API.
sdk_v2/cs/src/Detail/JsonSerializationContext.cs Registers new audio streaming types for source-gen JSON.
sdk_v2/cs/src/Detail/ICoreInterop.cs Adds interop structs + methods for audio stream start/push/stop.
sdk_v2/cs/src/Detail/CoreInterop.cs Implements binary command routing via execute_command_with_binary and start/stop routing via execute_command.
sdk_v2/cs/src/AssemblyInfo.cs Adds InternalsVisibleTo("AudioStreamTest").
samples/cs/LiveAudioTranscription/README.md Documentation for the live transcription demo sample.
samples/cs/LiveAudioTranscription/Program.cs Windows microphone demo using NAudio + new session API.
samples/cs/LiveAudioTranscription/LiveAudioTranscription.csproj Adds sample project dependencies and references the SDK project (path currently incorrect).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

…g-support-sdk

# Conflicts:
#	sdk/js/test/openai/chatClient.test.ts
ruiren_microsoft and others added 2 commits March 27, 2026 14:24
…ionItem pattern (#561)

### Description

Redesigns `LiveAudioTranscriptionResponse` to follow the OpenAI Realtime
API's `ConversationItem` shape, enabling forward compatibility with a
future WebSocket-based architecture.

**Motivation:**
- Customers using OpenAI's Realtime API access transcription via
`result.content[0].transcript`
- By adopting this pattern now, customers who write
`result.Content[0].Text` won't need to change their code when we migrate
to WebSocket transport
- Aligns with the team's plan to move toward OpenAI Realtime API
compatibility

**Before:**
```csharp
// Extended AudioCreateTranscriptionResponse from Betalgo
await foreach (var result in session.GetTranscriptionStream())
{
    Console.Write(result.Text);           // inherited from base
    bool final = result.IsFinal;          // custom field
    var segments = result.Segments;       // inherited from base
}
```

**After:**
```csharp
// Own type shaped like OpenAI Realtime ConversationItem
await foreach (var result in session.GetTranscriptionStream())
{
    Console.Write(result.Content[0].Text);       // ConversationItem pattern
    Console.Write(result.Content[0].Transcript); // alias for Text (Realtime compat)
    bool final = result.IsFinal;
    double? start = result.StartTime;
}
```

**Changes:**

| File | Change |
|------|--------|
| LiveAudioTranscriptionTypes.cs | Removed
`AudioCreateTranscriptionResponse` inheritance. New standalone
`LiveAudioTranscriptionResponse` with `Content` list + new
`TranscriptionContentPart` type |
| LiveAudioTranscriptionClient.cs | Updated text checks: `.Text` →
`.Content?[0]?.Text` |
| JsonSerializationContext.cs | Registered `TranscriptionContentPart`,
removed `AudioCreateTranscriptionResponse.Segment` |
| LiveAudioTranscriptionTests.cs | Updated assertions to match new type
shape |
| Program.cs (sample) | Updated result reading to
`result.Content?[0]?.Text` |
| README.md | Updated docs and output type table |

**Key design decisions:**
- `TranscriptionContentPart` has both `Text` and `Transcript` (set to
the same value) for maximum compatibility with both Whisper and Realtime
API patterns
- `StartTime`/`EndTime` are top-level on the response (not nested in
Segments) — simpler access, maps to Realtime's
`audio_start_ms`/`audio_end_ms`
- No dependency on Betalgo's `ConversationItem` — we own the type to
avoid carrying unused chat/tool-calling fields
- `LiveAudioTranscriptionRaw` (Core JSON deserialization) is unchanged —
this is purely an SDK presentation change, no Core/neutron-server impact

**No breaking changes to:** Core API, native interop, audio pipeline,
session lifecycle

---------

Co-authored-by: ruiren_microsoft <ruiren@microsoft.com>
…g-support-sdk

# Conflicts:
#	.github/workflows/build-js-steps.yml
#	sdk/js/script/install.cjs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants