Skip to content

C# bindings for the C ABI - #2

Closed
christopherthompson81 wants to merge 1 commit into
feat/c-abifrom
feat/csharp-bindings
Closed

christopherthompson81 wants to merge 1 commit into
feat/c-abifrom
feat/csharp-bindings

Conversation

@christopherthompson81

Copy link
Copy Markdown
Owner

C# bindings over the C ABI from #1, in a branch off it.

Fork-only by design: 0xShug0#525 explicitly keeps language bindings out of scope for
the upstream proposal.

What it adds

project
AudioCpp the binding — net10.0, AOT-compatible, no package references
AudioCpp.PathTest C# counterpart of tests/capi/path_test.c
AudioCpp.ModelTest C# counterpart of tests/capi/model_test.c
run-tests.sh runs both, then checks C# and C agree

All 69 ABI entry points are bound, verified by count against the header rather
than by eye.

This is the test the ABI was designed for

The out-of-order-free guarantee in the C ABI exists specifically because .NET
finalizer order is not deterministic — an API that required a disposal order
would be unusable from here no matter how carefully a caller wrote their using
statements. AudioCpp.PathTest exercises exactly that: it disposes the model and
the registry, then keeps using the session, and forces a collection with live
handles in between. Both give identical results.

Design

  • SafeHandle for every handle type, so disposal is thread-safe and
    finalization automatic.
  • Accessors copy on the way out, because everything the ABI returns is
    borrowed from the handle that produced it. Returned strings are IntPtr in the
    interop layer rather than string, so the marshaller is never in a position to
    free a pointer it does not own.
  • Non-OK statuses become AudioCppException carrying Status and the native
    Detail, read synchronously on the calling thread since the native detail is
    thread-local and a later call may clear it.
  • Model families are discovered through the introspection surface rather than
    hardcoded — GetOptions returns names, types, defaults and bounds.
  • The native library is found via AUDIOCPP_NATIVE_DIR, installed from a static
    constructor on the interop class rather than a module initializer (CA2255).

Verification

The bindings and the C tests report 11 parity lines, byte-identical, across
all five families:

$ diff <(audiocpp_c_api_model_test ...) <(dotnet run --project AudioCpp.ModelTest ...)
C and C# agree on 11 reported values

That closes the loop: C API ↔ C tests, C API ↔ C# bindings, and both ↔
audiocpp_cli.

family task exercises
silero_vad vad segments, offline + streaming
kokoro_tts tts audio out, 12 declared options
citrinet_asr / parakeet_tdt asr transcript, 28 word timestamps
sortformer_diar diar 5 speaker turns
bs_roformer sep 2 named streams

Library builds clean with TreatWarningsAsErrors, the trim analyzer and
IsAotCompatible.

Also in this branch

Two fixes to the C side, folded in from feat/c-abi:

  • The model test pinned 4 threads on a 16-core machine. Separation dominates its
    wall time and scales (191s → 113s), and stems are bit-identical at both thread
    counts, so CMake now passes the host's cores. Suite wall time 222s → 142s.
  • The parity check was comparing four families instead of five and still
    reporting PARITY OK.
    It invoked the model test without the alternate
    44.1 kHz clip BS-RoFormer needs, and under CTest the working directory is the
    build tree, so the repo-relative default did not resolve. Separation produced
    nothing to compare and parity blamed "family not linked into this build" —
    which was false. Fixture paths are now resolved up front, a missing one skips
    loudly, and the skip message distinguishes "not linked" from "ran and produced
    nothing".

🤖 Generated with Claude Code

https://claude.ai/code/session_01EkxqpYvUbjCpRDnFiNiVfx

A net10.0 binding over include/audiocpp.h, plus C# counterparts of the two
C tests. All 69 ABI entry points are bound, checked by count against the
header rather than by eye.

This is also the test the C ABI was designed for. The out-of-order-free
guarantee exists because .NET finalizer order is not deterministic, and
AudioCpp.PathTest exercises exactly that: it disposes the model and
registry, then keeps using the session, and forces a collection with live
handles in between.

Shape:

- SafeHandle for every handle type, so disposal is thread-safe and
  finalization automatic.
- Accessors copy on the way out, because everything the ABI returns is
  borrowed. Returned strings are IntPtr in the interop layer rather than
  string, so the marshaller is never in a position to free a pointer it
  does not own.
- Non-OK statuses become AudioCppException carrying the native detail,
  read synchronously on the calling thread since it is thread-local.
- Model families are discovered through the introspection surface rather
  than hardcoded.
- The native library is located via AUDIOCPP_NATIVE_DIR from a static
  constructor on the interop class, not a module initializer (CA2255).

run-tests.sh resolves the native directory by probing rather than assuming
bin/. Multi-config generators -- Visual Studio, Xcode, Ninja Multi-Config
-- put outputs in a per-configuration subdirectory, so the original
assumption skipped the whole suite on Windows while the library sat right
there, and reported exit 77. It also looks for the C test binary with and
without .exe, and prints the directory it resolved.

Verified on Linux x86_64 (single- and multi-config), macOS arm64 and
Windows x64. The bindings and the C tests report identical values on every
platform they have both run on.

Kept in the fork: 0xShug0#525 deliberately scopes language bindings out of the
upstream proposal.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01EkxqpYvUbjCpRDnFiNiVfx
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.

1 participant