#pragma once /* * nxstream — scorpiox-next-whisper-stream public C API * * Live streaming ASR on top of the same statically-embedded whisper.cpp (ggml * CPU-only) used by scorpiox-next-whisper-transcription. * * No SDL2, no microphone, no platform audio code: the caller (WinUI3 AudioGraph, * CoreAudio, WASAPI, ALSA, or a test harness reading a WAV) pushes float32 16 kHz * mono PCM into nxstream_write() and polls nxstream_read(). Exactly the * `rt_*` shape used by the AI Voice Morph live tab: write on the audio thread, * read on a DispatcherTimer. * * Utterance model (whisper.cpp examples/stream's VAD path, minus SDL2): * * - PCM lands in a ring buffer; a background thread consumes it in `step_ms` * chunks and runs Silero VAD on each one. * - VAD defines an UTTERANCE: speech after silence opens it, silence after speech * closes it. An utterance is always decoded from its own start, so successive * decodes of the same utterance are re-transcriptions of the same audio and can * safely overwrite each other. * - While the utterance is open you get its current best transcript with the SAME * segment id and `"final":false` — a UI replaces that row in place. * - On close (silence, or the utterance cap) one last decode publishes the same id * with `"final":true`. So: ids increase monotonically, each id reaches final at * most once, t0 never goes backwards, and nothing is ever duplicated. * * Threading: one background decode thread per start(). nxstream_write() never blocks * on inference; nxstream_read() never blocks on the decoder. Every function is safe * to call from any thread. * * String returns are UTF-8 JSON (malloc'd; free with nxstream_free_string), except * nxstream_last_error()/nxstream_version() which return static buffers valid until * the next call. */ #ifdef _WIN32 #if defined(NXSTREAM_BUILD) #define SCORPIOX_API __declspec(dllexport) #else #define SCORPIOX_API __declspec(dllimport) #endif #define NXSTREAM_CDECL __cdecl #else #define SCORPIOX_API __attribute__((visibility("default"))) #define NXSTREAM_CDECL #endif #ifdef __cplusplus extern "C" { #endif /* * Start a stream. * * model_path ggml whisper model (ggml-base.bin, ggml-small.bin, ...) * step_ms decode cadence in ms. <= 0 -> 500 (a 500 ms step is the sweet spot: * ~400 ms of CPU per decode on a small model, so it keeps up live). * length_ms longest utterance decoded as one segment, ms. <= 0 -> 5000. * Clamped to [step_ms, 30000]; whisper's encoder is a 30 s window. * * VAD model: $NXSTREAM_VAD_MODEL, else /ggml-silero-v6.2.0.bin. * If that file does not exist the engine falls back to an RMS energy gate and reports * it in stats.gate ("energy" instead of "silero"). $NXSTREAM_VAD=0 disables gating. * * Other environment knobs (all optional): * NXSTREAM_LANGUAGE "auto" (default) or a 2-letter code * NXSTREAM_THREADS whisper threads (default min(cores, 16)) * NXSTREAM_MAX_TOKENS max tokens per decode (default 256) * NXSTREAM_MAX_UTTERANCE_MS utterance cap (default 20000, max 30000) * NXSTREAM_VAD_THRESHOLD silero probability threshold (default 0.5) * NXSTREAM_VAD_PAD_MS audio kept before the detected onset (default 100) * NXSTREAM_ENERGY_THRESHOLD RMS gate threshold (default 0.008) * NXSTREAM_LOG=1 let whisper/ggml log to stderr (default: errors only) * * Returns 0 on success, -1 on failure (see nxstream_last_error). * Calling start() while a stream is running stops it first. */ SCORPIOX_API int NXSTREAM_CDECL nxstream_start(const char *model_path, int step_ms, int length_ms); /* * Append float32 16 kHz mono PCM. n_samples > 0. * Returns n_samples (accepted), or -1 on error (not started, NULL pcm). * Never blocks on inference. The ring holds the utterance cap plus 20 s of slack; * if the caller ever outruns the decoder the oldest audio is dropped and * stats.dropped_samples counts it (callers should watch that number, not poll faster). */ SCORPIOX_API int NXSTREAM_CDECL nxstream_write(const float *pcm, int n_samples); /* * Everything that changed since the previous call, as JSON: * * {"count":2, * "segments":[ * {"id":2,"t0_ms":0,"t1_ms":1180,"text":"...","latency_ms":610,"final":true}, * {"id":3,"t0_ms":1240,"t1_ms":3120,"text":"...","latency_ms":480,"final":false} * ], * "stats":{ * "steps":24,"decodes":21,"vad_skips":3,"final_segments":2, * "dropped_samples":0,"step_ms":500,"max_utterance_ms":20000, * "audio_ms":24200,"processed_ms":24000,"elapsed_ms":24310, * "next_id":4,"committed_t1_ms":1180,"live":true,"live_id":3,"gate":"silero", * "lat_avg_ms":520,"lat_max_ms":980,"lat_samples":9,"decode_avg_ms":390 * }} * * Timestamps are on the stream timeline (ms since start()). `latency_ms` is * "this text became available N ms after its audio ended" — the number to quote as * streaming latency. Returns malloc'd JSON; the "error" shape if no stream is running. */ SCORPIOX_API const char * NXSTREAM_CDECL nxstream_read(void); /* Free a string returned by this library. Safe with NULL. */ SCORPIOX_API void NXSTREAM_CDECL nxstream_free_string(char *ptr); /* * Stop the stream and release the model. * If an utterance is still open it is decoded and published as final first, so the * caller's tail is never lost — read() once more after stop() to collect it. * Safe to call when not running. Blocks until the decode thread has exited. */ SCORPIOX_API void NXSTREAM_CDECL nxstream_stop(void); /* Last error / status message (static buffer, valid until the next call). */ SCORPIOX_API const char * NXSTREAM_CDECL nxstream_last_error(void); /* Library version string. */ SCORPIOX_API const char * NXSTREAM_CDECL nxstream_version(void); #ifdef __cplusplus } #endif