Skip to content

Add an opt-in C ABI for in-process embedding - #530

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
christopherthompson81:feat/c-abi
Sep 13, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
christopherthompson81:feat/c-abi

Conversation

@christopherthompson81

Copy link
Copy Markdown
Contributor

Implements the C ABI proposed in #525 — an in-process entry point for applications
that embed audio.cpp rather than shelling out to the CLI or running the server.

One commit, 10 files, +3565/-0. AUDIOCPP_BUILD_C_API defaults to OFF; with it off,
configure produces no new targets, no tests, and no changed flags.

include/audiocpp.h the C header — C99, 69 entry points
src/capi/audiocpp.cpp the facade — one translation unit
src/capi/audiocpp.map / .symbols ELF / Mach-O export lists
tests/capi/ four tests, the C ones compiled as C
docs/c_api.md ownership and lifetime rules

Design

A translation layer over ModelRegistry → ILoadedVoiceModel → IVoiceTaskSession,
the same surfaces audiocpp_cli uses. It depends only on framework headers, never on
app/, so the CLI and the C API stay independent.

Three properties of the runtime keep it thin. Options are already
unordered_map<string, string> in ModelLoadRequest, SessionOptions and
TaskRequest, so the ABI carries no per-family knowledge and adding a model family
never changes the header
. TaskRequest → TaskResult is already generic, so there
are two run paths to wrap. And ModelInspection::cli is already self-describing, so
it is exposed as-is: a binding enumerates a family's options at runtime — names,
types, defaults, bounds — instead of hardcoding them.

Opaque handles only; no exception escapes; accessors rather than struct copies so
TaskResult can gain fields without breaking the ABI; returned strings are never
NULL. Handles hold their parents alive, so freeing out of order is safe — callers
from garbage-collected runtimes cannot control finalizer order.

Two notes against the proposal

The surface is 69 functions, not the ~50 sketched in #525, and includes streaming,
artifacts and style conditioning rather than deferring them. It now covers everything
TaskRequest, TaskResult, ModelLoadRequest and CliOptionInfo carry, so a caller
is never pushed back to the CLI mid-workflow. Happy to split the offline core out if
you would rather review that first.

One line falls outside the new files: cjson_vendor is now compiled with
CJSON_HIDE_SYMBOLS. cJSON annotates its public functions with
__declspec(dllexport) by default, and since dllexport is additive, any DLL
absorbing that static library re-exports all 78 — the first Windows build produced 147
exports against 69 intended. No DLL is built from this tree today, so this was latent;
this is simply the first target to expose it.

Validation

platform
Linux x86_64 CPU and CUDA (RTX 3090, CUDA 12.8); single- and multi-config generators
macOS arm64 CMake 3.31.12 and 4.4.3, Metal on and off, Xcode/clang 21
macOS x86_64 links, exports verified
Windows x64 MSVC 14.44.35207, SDK 10.0.26100, VS 2022 generator

Every platform produces the same results for the same inputs.

  • audiocpp_c_api_path — the ABI contract, offline and streaming, against the
    Silero VAD model and speech clip already in the repo. No download, no network.
  • audiocpp_c_api_model — five families across five task types, covering every
    result accessor: Kokoro (audio, 12 declared options), Citrinet and Parakeet
    (transcript, 28 word timestamps), Sortformer (5 speaker turns), BS-RoFormer (2 named
    streams).
  • audiocpp_c_api_parity — the same inputs through audiocpp_cli, compared. The
    C API drives the same runtime, so it must produce not merely plausible output but
    the same output. Passes across all five families. Kokoro draws noise per run, so
    both sides are seeded.
  • audiocpp_c_api_exports — asserts the library exports exactly this header, on
    all three platforms. 69 declared, 69 exported, nothing else.

The last three report SKIP (exit 77) when models or tooling are absent, so a checkout
with no downloads stays green.

Session reuse is the reason to embed rather than shell out, so: one session, repeated
requests, RSS sampled after warm-up — 1072 kB delta at 200, 500 and 1000 iterations.
Flat, not linear. 40.5 ms per request on CPU (347× realtime, 14 s of audio per
request).

A C# binding over this header exists in my fork
(christopherthompson81#2) and is not proposed here — #525 scopes
language bindings out. It is worth mentioning only as validation: an independent
consumer, in another language, exercising the out-of-order-free guarantee that
motivated the design, since .NET finalizer order is not deterministic. Its tests and
the C tests report identical values across all five families — 11 lines,
byte-identical — on Linux, macOS and Windows. It also confirms the cJSON change
removed nothing a real consumer needs: every audiocpp_* symbol resolves through
P/Invoke at runtime.

Notes for review

  • CMAKE_POSITION_INDEPENDENT_CODE is set inside if (AUDIOCPP_BUILD_C_API) only —
    BUILD_SHARED_LIBS is forced OFF at CMakeLists.txt:297, so the objects are
    otherwise non-PIC. Enabling the option forces a full ggml rebuild.
  • enable_testing() is called in the C API block. The existing call is further down
    and gated behind ENGINE_BUILD_*, so an add_test() here would silently never
    register.
  • The backend-name mapping is duplicated from app/cli/args.cpp, to keep the C API
    independent of app/. Glad to lift it into the framework instead.
  • The library does not call omp_set_num_threads(), unlike the CLI — that is
    process-global state a library shouldn't touch. Threads come from
    audiocpp_backend_config.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EkxqpYvUbjCpRDnFiNiVfx

audio.cpp's three existing surfaces -- the CLI, the server and the WebUI --
are all process-level, so an application that wants the engine in its own
process has no entry point: include/engine/** is C++ in engine::runtime,
and BUILD_SHARED_LIBS is forced OFF.

This adds a thin facade over the existing runtime interfaces. It has no
behaviour of its own -- every call maps onto ModelRegistry ->
ILoadedVoiceModel -> IVoiceTaskSession, the same surfaces audiocpp_cli
uses -- and it depends only on the framework, never on app/, so the CLI
and the C API stay independent of each other.

The framework already does the hard part. Options are
unordered_map<string, string> in ModelLoadRequest, SessionOptions and
TaskRequest, and TaskRequest -> TaskResult is generic across families, so
the ABI carries zero per-family knowledge and adding a model never
changes this header. ModelInspection::cli is exposed too, so a binding
can enumerate what a family accepts at runtime -- names, types, defaults
and bounds -- instead of hardcoding it.

Handles hold their parents alive (session -> model -> registry), so
freeing out of order is safe. That is deliberate: callers from
garbage-collected runtimes cannot control finalizer order, and an ABI
that required it would be a trap.

libaudiocpp exports exactly the header's 69 symbols on all three
platforms, which takes a different mechanism on each. The visibility
presets only reach this target's own sources, so the static libraries
linked in need an explicit allowlist: a version script on ELF, an
exported-symbols list on Mach-O. Windows has no equivalent, because
__declspec(dllexport) is additive -- it says "also export this", never
"export only this" -- so a DLL's surface is its own annotations plus its
dependencies'. Vendored cJSON annotates all 78 of its public functions by
default, which put the first Windows build at 147 exports; cjson_vendor
is now compiled with CJSON_HIDE_SYMBOLS, cJSON's own mechanism for this.
That is the one line outside the new files, and it is a latent defect in
the vendored integration rather than new behaviour -- no DLL is built
from this tree today, so nothing was exposed to it before.

Purely additive otherwise. AUDIOCPP_BUILD_C_API defaults to OFF and
nothing changes in a build that leaves it off. When on, it also sets
POSITION_INDEPENDENT_CODE, which the static and object libraries need
before they can link into a shared library.

Tests, all compiled as C so the header is proven consumable without a C++
runtime on the caller's side:

- audiocpp_c_api_path covers the ABI contract, offline and streaming,
  against the Silero VAD model and speech clip that ship in-repo, so it
  needs no download or network.
- audiocpp_c_api_model covers real families across five task types and
  every result accessor.
- audiocpp_c_api_parity runs the same inputs through audiocpp_cli and
  compares, because the C API drives the same runtime and so must not
  merely produce plausible output but the same output.

The last two report SKIP (exit 77) when models are absent, so a checkout
with no downloads still goes green.

Verified on Linux x86_64 (CPU and CUDA), macOS arm64 and x86_64 (CMake
3.31 and 4.4.3, Metal on and off), and Windows x64 (MSVC 14.44).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01EkxqpYvUbjCpRDnFiNiVfx
@0xShug0

0xShug0 commented Sep 12, 2026

Copy link
Copy Markdown
Owner

@christopherthompson81 Thanks! No blockers found. I will merge it after the new release.

@christopherthompson81

Copy link
Copy Markdown
Contributor Author

Would you be willing to take the C# bindings, or do you want those to be external?

@0xShug0

0xShug0 commented Sep 12, 2026

Copy link
Copy Markdown
Owner

@christopherthompson81 I’d prefer to keep language bindings as a separate repo/package for now rather than inside the main audio.cpp repo. The main repo should stay focused on the C++ runtime. Also, I don’t think I currently have the bandwidth or expertise to maintain that properly within the core repo. The language bindings could follow a structure similar to https://github.com/0xShug0/audio.cpp-server-frontends, staying well decoupled from the main repo. If you create a separate repo for the bindings, I’m happy to link to it from audio.cpp docs, and we can consider adding it as a submodule if tighter integration becomes useful.

@0xShug0
0xShug0 merged commit 03f5f8f into 0xShug0:main Sep 13, 2026
6 checks passed
@0xShug0

0xShug0 commented Sep 13, 2026

Copy link
Copy Markdown
Owner

@christopherthompson81 Merged 🎉 Thanks!

@christopherthompson81
christopherthompson81 deleted the feat/c-abi branch September 13, 2026 11:48
0xShug0 pushed a commit that referenced this pull request Sep 23, 2026
.NET bindings over the C ABI merged in #530, an Avalonia desktop app that
mirrors the web UI, and a C# reimplementation of the HTTP API.

The entry names all three parts because the list already carries
AudioCpp.NET, which is also .NET bindings; what distinguishes this one is
the desktop app and the reimplemented server, not the binding layer.

Also bumps the section's "Last update" line, which is what signals the
list changed.


Claude-Session: https://claude.ai/code/session_01TMmMgd5xNGnjsQgybuUiK3

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

2 participants