Skip to content

[Proposal] A stable C ABI (audiocpp.h + optional shared library) for in-process embedding #525

Closed

Description

@christopherthompson81

Summary

audio.cpp currently exposes three surfaces: audiocpp_cli, audiocpp_server, and the WebUI. All three are
process-level. There is no way to embed the engine in-process in a non-C++ application, because
include/engine/** is C++ headers in engine::runtime (templates, std::optional, std::filesystem,
virtual interfaces, exceptions), and CMakeLists.txt:297 force-sets BUILD_SHARED_LIBS OFF outside the
CPU-all-variants path.

I'd like to propose adding a thin, additive C ABI over the existing runtime interfaces, plus an
off-by-default CMake option that builds it as a shared library. This is the "app-facing workflows" item in
CONTRIBUTING.md, approached from the embedding side rather than the serving side.

The runtime is already shaped almost perfectly for a C facade, which is what makes me think it's worth
proposing rather than just forking:

  1. Options are already std::unordered_map<std::string, std::string> — in ModelLoadRequest::options,
    SessionOptions::options, and TaskRequest::options. The C layer needs a string→string bag and nothing
    else. It carries zero per-family knowledge, so adding a model family never touches the ABI.

  2. The task surface is already generic. TaskRequest → TaskResult covers audio in/out, text,
    speech_segments, speaker_turns, word_timestamps, and artifacts for every family. There are
    exactly two run interfaces to wrap: IOfflineVoiceTaskSession::run and the
    IStreamingVoiceTaskSession chunk/finalize pair.

  3. It's already self-describing. ModelInspection::cli carries request_options, session_options,
    and load_options as CliOptionInfo (name, value name, description, required, default, min, max). If
    the C API exposes that, a binding can enumerate a model's options at runtime and generate typed
    wrappers from it — instead of hardcoding per-family knowledge that rots.

  4. Streaming is already pull-based. next_stream_event() avoids callbacks across the FFI boundary,
    which is the part that's genuinely painful from GC'd runtimes.

So the shim is a facade over ModelRegistry → ILoadedVoiceModel → IVoiceTaskSession, not a new
abstraction. On the order of 50 functions, one translation unit, no changes to src/framework or
src/models.

Sketch

/* audiocpp.h — C99, no exceptions cross this boundary. */

#define AUDIOCPP_ABI_VERSION_MAJOR 0
uint32_t audiocpp_abi_version(void);

typedef struct audiocpp_registry audiocpp_registry;
typedef struct audiocpp_model    audiocpp_model;
typedef struct audiocpp_session  audiocpp_session;
typedef struct audiocpp_options  audiocpp_options;
typedef struct audiocpp_request  audiocpp_request;
typedef struct audiocpp_result   audiocpp_result;

typedef enum {
    AUDIOCPP_OK = 0,
    AUDIOCPP_ERR_INVALID_ARGUMENT,
    AUDIOCPP_ERR_UNSUPPORTED_FAMILY,
    AUDIOCPP_ERR_LOAD_FAILED,
    AUDIOCPP_ERR_RUNTIME,
    AUDIOCPP_ERR_OUT_OF_MEMORY
} audiocpp_status;

/* Thread-local detail for the last failing call on this thread. */
const char * audiocpp_last_error(void);

/* Options: mirrors the existing string->string maps 1:1. */
audiocpp_options * audiocpp_options_create(void);
void               audiocpp_options_set(audiocpp_options *, const char * key, const char * value);
void               audiocpp_options_free(audiocpp_options *);

/* Load. family_hint may be NULL to let the registry sniff the path. */
audiocpp_status audiocpp_model_load(audiocpp_registry *, const char * model_path,
                                    const char * family_hint, const audiocpp_options *,
                                    audiocpp_model ** out);

/* Introspection, straight off ModelInspection::cli — this is what lets a
   binding generate typed wrappers instead of hardcoding families. */
size_t          audiocpp_model_request_option_count(const audiocpp_model *);
audiocpp_status audiocpp_model_request_option(const audiocpp_model *, size_t index,
                                              const char ** name, const char ** description,
                                              const char ** default_value, int * required);

audiocpp_status audiocpp_session_create(const audiocpp_model *, const char * task /* "tts" */,
                                        const char * mode /* "offline" */,
                                        const audiocpp_options *, audiocpp_session ** out);

audiocpp_status audiocpp_request_set_audio(audiocpp_request *, const float * samples,
                                           size_t frames, int sample_rate, int channels);
audiocpp_status audiocpp_session_run(audiocpp_session *, const audiocpp_request *,
                                     audiocpp_result ** out);

/* Accessors rather than struct copies, so TaskResult can gain fields
   without breaking the ABI. */
audiocpp_status audiocpp_result_audio(const audiocpp_result *, const float ** samples,
                                      size_t * frames, int * sample_rate, int * channels);
size_t          audiocpp_result_word_count(const audiocpp_result *);
audiocpp_status audiocpp_result_word(const audiocpp_result *, size_t index, const char ** word,
                                     int64_t * start_sample, int64_t * end_sample, float * confidence);
void            audiocpp_result_free(audiocpp_result *);

/* Streaming mirrors process_audio_chunk / next_stream_event / finalize. */
audiocpp_status audiocpp_stream_push(audiocpp_session *, const float * samples, size_t frames,
                                     int64_t start_sample);

Design rules I'd hold to:

  • Opaque handles only. No C++ type ever crosses the boundary.
  • No exceptions escape. Every entry point is try/catch(...) → status code, detail in
    audiocpp_last_error().
  • Accessors, not structs, for anything derived from a framework type that may grow fields.
  • Borrowed pointers are valid until the owning handle is freed, documented per function.
  • Semver on the ABI, SOVERSION on the shared library.

Issue #34 and CONTRIBUTING both say plainly that the internal framework APIs are still evolving fast, and
that changing shared code is high risk. I read that as an argument for this layer rather than against it,
but only if it's built the right way round:

  • The facade pins nothing internally. It depends on ModelRegistry, ILoadedVoiceModel,
    IVoiceTaskSession, TaskRequest, TaskResult — the same surfaces audiocpp_cli already depends on.
    If those move, the CLI moves too, and the shim moves with it in one file.
  • It absorbs churn instead of transmitting it. Right now, anyone embedding audio.cpp is coupled to your
    internal C++ signatures. A C facade is the thing that lets you keep changing them freely.
  • It is purely additive — no lines changed in src/framework or src/models, per the CONTRIBUTING
    guidance to prefer additive work.
  • Off by default: AUDIOCPP_BUILD_C_API=OFF. Nothing changes for existing users or CI unless enabled.

The PR I'd propose is:

  1. include/audiocpp.h — the C header.
  2. src/capi/audiocpp.cpp — the facade.
  3. AUDIOCPP_BUILD_C_API option producing libaudiocpp.{so,dylib,dll} with SOVERSION, off by default.
  4. A path test that loads a model, runs one offline request, and checks the result — through the C API only.
  5. docs/c_api.md with the ownership and lifetime rules.

Binding can follow once the offline shape is agreed, or any install/export target rework.

Activity

  1. langfod commented on Sep 12, 2026

    @langfod

    this would be easier if the vendor library files were done as vcpkg/diff packages instead of loose files with random changes.
    (some of the changes have been superseded upstream anyway)
    then rolling the great audio.cpp work into a project is much easier. right now that is the most difficult part of using audio.cpp inside another project.

  2. 0xShug0 commented on Sep 12, 2026

    @0xShug0
    Owner

    @christopherthompson81 Thank you for the proposal and it looks great! I’m eager to see the PR.

    @langfod I originally maintained a series of patches and tried to bump the ggml version while reapplying them: #91 (comment)

    But as the number of models has grown, we’ve also accumulated more ggml optimizations from the community that are specific to audio workloads. At this point, I’m starting to think of audio.cpp’s vendored ggml more as a fork customized for audio models rather than just upstream ggml with a few patches. I’d rather cherry-pick only the relevant optimizations from upstream. In fact, there’s going to be another set of changes that combines newer upstream work with optimized Conv3D support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions