Repository navigation
Add an opt-in C ABI for in-process embedding - #530
Conversation
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
|
@christopherthompson81 Thanks! No blockers found. I will merge it after the new release. |
|
Would you be willing to take the C# bindings, or do you want those to be external? |
|
@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. |
|
@christopherthompson81 Merged 🎉 Thanks! |
.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]>
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_APIdefaults to OFF; with it off,configure produces no new targets, no tests, and no changed flags.
include/audiocpp.hsrc/capi/audiocpp.cppsrc/capi/audiocpp.map/.symbolstests/capi/docs/c_api.mdDesign
A translation layer over
ModelRegistry→ILoadedVoiceModel→IVoiceTaskSession,the same surfaces
audiocpp_cliuses. It depends only on framework headers, never onapp/, so the CLI and the C API stay independent.Three properties of the runtime keep it thin. Options are already
unordered_map<string, string>inModelLoadRequest,SessionOptionsandTaskRequest, so the ABI carries no per-family knowledge and adding a model familynever changes the header.
TaskRequest→TaskResultis already generic, so thereare two run paths to wrap. And
ModelInspection::cliis already self-describing, soit 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
TaskResultcan gain fields without breaking the ABI; returned strings are neverNULL. 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,ModelLoadRequestandCliOptionInfocarry, so a calleris 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_vendoris now compiled withCJSON_HIDE_SYMBOLS. cJSON annotates its public functions with__declspec(dllexport)by default, and sincedllexportis additive, any DLLabsorbing 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
Every platform produces the same results for the same inputs.
audiocpp_c_api_path— the ABI contract, offline and streaming, against theSilero VAD model and speech clip already in the repo. No download, no network.
audiocpp_c_api_model— five families across five task types, covering everyresult 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 throughaudiocpp_cli, compared. TheC 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, onall 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 throughP/Invoke at runtime.
Notes for review
CMAKE_POSITION_INDEPENDENT_CODEis set insideif (AUDIOCPP_BUILD_C_API)only —BUILD_SHARED_LIBSis forced OFF atCMakeLists.txt:297, so the objects areotherwise non-PIC. Enabling the option forces a full ggml rebuild.
enable_testing()is called in the C API block. The existing call is further downand gated behind
ENGINE_BUILD_*, so anadd_test()here would silently neverregister.
app/cli/args.cpp, to keep the C APIindependent of
app/. Glad to lift it into the framework instead.omp_set_num_threads(), unlike the CLI — that isprocess-global state a library shouldn't touch. Threads come from
audiocpp_backend_config.🤖 Generated with Claude Code
https://claude.ai/code/session_01EkxqpYvUbjCpRDnFiNiVfx