@gjsify/sherpa-native 0.0.0-stage → 0.64.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +330 -2
- package/lib/esm/_virtual/_rolldown/runtime.js +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/types/index.d.ts +47 -0
- package/meson.build +563 -0
- package/package.json +93 -5
- package/src/c/gjsify-sherpa-shim.c +575 -0
- package/src/c/gjsify-sherpa-shim.h +259 -0
- package/src/c/gjsify-sherpa.map +27 -0
- package/src/ts/gjsifysherpa-1.0.d.ts +1159 -0
- package/src/ts/index.ts +95 -0
- package/src/vala/offline-recognizer.vala +238 -0
- package/src/vala/online-recognizer.vala +303 -0
- package/src/vala/sherpa-shim.vapi +178 -0
- package/src/vala/sherpa.vala +76 -0
- package/src/vala/vad.vala +207 -0
- package/subprojects/packagefiles/sherpa-onnx-shared-onnxruntime/cmake/onnxruntime-linux-x86_64-static.cmake +112 -0
- package/subprojects/sherpa-onnx.wrap +56 -0
- package/test/api.gjs.js +155 -0
- package/test/check-elf-deps.py +213 -0
- package/test/shim-smoke.c +146 -0
- package/test/vad-model.gjs.js +112 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* gjsify-sherpa-shim.h — a flat C façade over sherpa-onnx's C API, for Vala.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS FILE EXISTS
|
|
5
|
+
* ────────────────────
|
|
6
|
+
* Vala reaches a C library through a VAPI, and a VAPI that declares a struct
|
|
7
|
+
* RESTATES its memory layout: field order, field types, and the layout of every
|
|
8
|
+
* nested struct. sherpa-onnx's configuration structs are not candidates for
|
|
9
|
+
* that. `SherpaOnnxOfflineModelConfig` alone nests TWENTY-TWO sub-structs
|
|
10
|
+
* (transducer, paraformer, nemo_ctc, whisper, tdnn, sense_voice, moonshine,
|
|
11
|
+
* fire_red_asr, dolphin, zipformer_ctc, canary, wenet_ctc, omnilingual, medasr,
|
|
12
|
+
* funasr_nano, fire_red_asr_ctc, qwen3_asr, cohere_transcribe, …) and upstream
|
|
13
|
+
* appends to them every release — v2.0.1 added two. A VAPI copy of that is a
|
|
14
|
+
* second truth that drifts, and the way it fails is silent: a struct whose
|
|
15
|
+
* layout is one field stale is not a compile error, it is a model path read out
|
|
16
|
+
* of the middle of another field.
|
|
17
|
+
*
|
|
18
|
+
* So the layout stays in C, where the real header supplies it, and this file
|
|
19
|
+
* exposes only scalars and strings — which a VAPI can state without knowing any
|
|
20
|
+
* layout at all. The functions here `memset` each config to zero and set the
|
|
21
|
+
* fields by name, exactly as every upstream example does.
|
|
22
|
+
*
|
|
23
|
+
* WHAT IS AND IS NOT HERE
|
|
24
|
+
* ───────────────────────
|
|
25
|
+
* Both recognizers are TRANSDUCER-ONLY: `encoder`/`decoder`/`joiner` + `tokens`.
|
|
26
|
+
* That is what the two engines ADR 0127 measured need — the streaming
|
|
27
|
+
* `zipformer-de-kroko` (online) and `parakeet-tdt-0.6b-v3` (offline) — and a
|
|
28
|
+
* model family nobody has run is a claim, not a binding. Adding one is a
|
|
29
|
+
* `..._open` parameter and a field assignment in the .c file, with no VAPI
|
|
30
|
+
* change at all; the whisper family in particular belongs in
|
|
31
|
+
* `@gjsify/whisper-native` (ADR 0127 decision 7), not here.
|
|
32
|
+
*
|
|
33
|
+
* Every string this returns is `g_strdup`ed and every array `g_malloc`ed, so
|
|
34
|
+
* Vala's ordinary ownership transfer (which frees with `g_free`) is correct.
|
|
35
|
+
* The `gjsify_sherpa_shim_` prefix keeps these out of the way of the
|
|
36
|
+
* `gjsify_sherpa_*` symbols valac generates for the GjsifySherpa namespace.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
#ifndef GJSIFY_SHERPA_SHIM_H
|
|
40
|
+
#define GJSIFY_SHERPA_SHIM_H
|
|
41
|
+
|
|
42
|
+
#include <stdint.h>
|
|
43
|
+
|
|
44
|
+
#ifdef __cplusplus
|
|
45
|
+
extern "C" {
|
|
46
|
+
#endif
|
|
47
|
+
|
|
48
|
+
/* ------------------------------------------------------------------------- */
|
|
49
|
+
/* Build information — what this prebuild actually contains. */
|
|
50
|
+
/* ------------------------------------------------------------------------- */
|
|
51
|
+
|
|
52
|
+
/** sherpa-onnx's own version string, e.g. `2.0.1`. Statically linked in. */
|
|
53
|
+
const char *gjsify_sherpa_shim_version (void);
|
|
54
|
+
|
|
55
|
+
/** The onnxruntime version sherpa-onnx was built against, e.g. `1.28.2`. */
|
|
56
|
+
const char *gjsify_sherpa_shim_onnxruntime_version (void);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Non-zero when @filename is readable.
|
|
60
|
+
*
|
|
61
|
+
* sherpa-onnx's `..._open` functions below answer only NULL on failure, so the
|
|
62
|
+
* model paths are checked here first and the missing one is named. Uses
|
|
63
|
+
* sherpa-onnx's own `SherpaOnnxFileExists` so the answer is the one the engine
|
|
64
|
+
* would get, not a second opinion from GLib.
|
|
65
|
+
*/
|
|
66
|
+
int32_t gjsify_sherpa_shim_file_exists (const char *filename);
|
|
67
|
+
|
|
68
|
+
/* ------------------------------------------------------------------------- */
|
|
69
|
+
/* Voice activity detection — Silero. */
|
|
70
|
+
/* ------------------------------------------------------------------------- */
|
|
71
|
+
|
|
72
|
+
/** An open Silero VAD, plus the one speech segment `..._peek` last fetched. */
|
|
73
|
+
typedef struct _GjsifySherpaShimVad GjsifySherpaShimVad;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Open a Silero VAD over @model (`silero_vad.onnx`).
|
|
77
|
+
*
|
|
78
|
+
* Returns NULL when @model is unreadable or sherpa-onnx refuses the
|
|
79
|
+
* configuration; sherpa-onnx writes its own reason to stderr.
|
|
80
|
+
*/
|
|
81
|
+
GjsifySherpaShimVad *gjsify_sherpa_shim_vad_open (const char *model,
|
|
82
|
+
float threshold,
|
|
83
|
+
float min_silence_duration,
|
|
84
|
+
float min_speech_duration,
|
|
85
|
+
float max_speech_duration,
|
|
86
|
+
int32_t window_size,
|
|
87
|
+
int32_t sample_rate,
|
|
88
|
+
int32_t num_threads,
|
|
89
|
+
float buffer_size_in_seconds,
|
|
90
|
+
int32_t debug);
|
|
91
|
+
|
|
92
|
+
void gjsify_sherpa_shim_vad_close (GjsifySherpaShimVad *vad);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Feed @n mono float samples in [-1, 1].
|
|
96
|
+
*
|
|
97
|
+
* @n is free: the detector buffers what does not fill a window
|
|
98
|
+
* (`voice-activity-detector.cc`, `last_`), so a caller may hand it whatever a
|
|
99
|
+
* capture source produced.
|
|
100
|
+
*/
|
|
101
|
+
void gjsify_sherpa_shim_vad_accept (GjsifySherpaShimVad *vad,
|
|
102
|
+
const float *samples,
|
|
103
|
+
int32_t n);
|
|
104
|
+
|
|
105
|
+
/** Non-zero while the detector is INSIDE speech — the speech-start edge. */
|
|
106
|
+
int32_t gjsify_sherpa_shim_vad_is_speech (GjsifySherpaShimVad *vad);
|
|
107
|
+
|
|
108
|
+
/** Non-zero when no COMPLETED segment is queued. */
|
|
109
|
+
int32_t gjsify_sherpa_shim_vad_is_empty (GjsifySherpaShimVad *vad);
|
|
110
|
+
|
|
111
|
+
/** Close an in-progress segment and queue it — end of input. */
|
|
112
|
+
void gjsify_sherpa_shim_vad_flush (GjsifySherpaShimVad *vad);
|
|
113
|
+
|
|
114
|
+
/** Drop every queued segment, keeping the model state. */
|
|
115
|
+
void gjsify_sherpa_shim_vad_clear (GjsifySherpaShimVad *vad);
|
|
116
|
+
|
|
117
|
+
/** Drop every queued segment AND the model state. */
|
|
118
|
+
void gjsify_sherpa_shim_vad_reset (GjsifySherpaShimVad *vad);
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Fetch the front segment into @vad and report its length, or -1 if none.
|
|
122
|
+
*
|
|
123
|
+
* Two calls rather than one returning an array: the length is not known before
|
|
124
|
+
* the fetch, and a Vala out-array with a separate out-length is the one binding
|
|
125
|
+
* form whose parameter ordering the VAPI would have to get right by hand. The
|
|
126
|
+
* segment is held by @vad until `..._pop`, so `..._read` needs no second fetch.
|
|
127
|
+
*
|
|
128
|
+
* @start receives the segment's absolute offset in input samples.
|
|
129
|
+
*/
|
|
130
|
+
int32_t gjsify_sherpa_shim_vad_peek (GjsifySherpaShimVad *vad, int64_t *start);
|
|
131
|
+
|
|
132
|
+
/** Copy @n samples of the segment `..._peek` fetched into @dest. */
|
|
133
|
+
void gjsify_sherpa_shim_vad_read (GjsifySherpaShimVad *vad, float *dest, int32_t n);
|
|
134
|
+
|
|
135
|
+
/** Release the fetched segment and remove it from the queue. */
|
|
136
|
+
void gjsify_sherpa_shim_vad_pop (GjsifySherpaShimVad *vad);
|
|
137
|
+
|
|
138
|
+
/* ------------------------------------------------------------------------- */
|
|
139
|
+
/* Online (streaming) transducer recognizer. */
|
|
140
|
+
/* ------------------------------------------------------------------------- */
|
|
141
|
+
|
|
142
|
+
typedef struct _GjsifySherpaShimOnline GjsifySherpaShimOnline;
|
|
143
|
+
|
|
144
|
+
/** A stream, carrying the recognizer every per-stream call needs anyway. */
|
|
145
|
+
typedef struct _GjsifySherpaShimOnlineStream GjsifySherpaShimOnlineStream;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Open a streaming transducer recognizer.
|
|
149
|
+
*
|
|
150
|
+
* @provider, @model_type and @decoding_method may be NULL, in which case
|
|
151
|
+
* sherpa-onnx's own defaults (`cpu`, autodetect, `greedy_search`) apply.
|
|
152
|
+
* @hotwords is the contextual-biasing list as a newline-separated string, the
|
|
153
|
+
* form `SherpaOnnxCreateOnlineStreamWithHotwords` takes per stream; it only
|
|
154
|
+
* applies with `decoding_method = "modified_beam_search"`.
|
|
155
|
+
*/
|
|
156
|
+
GjsifySherpaShimOnline *gjsify_sherpa_shim_online_open (const char *encoder,
|
|
157
|
+
const char *decoder,
|
|
158
|
+
const char *joiner,
|
|
159
|
+
const char *tokens,
|
|
160
|
+
const char *model_type,
|
|
161
|
+
const char *provider,
|
|
162
|
+
int32_t num_threads,
|
|
163
|
+
const char *decoding_method,
|
|
164
|
+
int32_t max_active_paths,
|
|
165
|
+
float hotwords_score,
|
|
166
|
+
float blank_penalty,
|
|
167
|
+
int32_t enable_endpoint,
|
|
168
|
+
float rule1_min_trailing_silence,
|
|
169
|
+
float rule2_min_trailing_silence,
|
|
170
|
+
float rule3_min_utterance_length,
|
|
171
|
+
int32_t sample_rate,
|
|
172
|
+
int32_t feature_dim,
|
|
173
|
+
int32_t debug);
|
|
174
|
+
|
|
175
|
+
void gjsify_sherpa_shim_online_close (GjsifySherpaShimOnline *recognizer);
|
|
176
|
+
|
|
177
|
+
/** A new stream; @hotwords may be NULL for the recognizer's own list. */
|
|
178
|
+
GjsifySherpaShimOnlineStream *
|
|
179
|
+
gjsify_sherpa_shim_online_stream_open (GjsifySherpaShimOnline *recognizer,
|
|
180
|
+
const char *hotwords);
|
|
181
|
+
|
|
182
|
+
void gjsify_sherpa_shim_online_stream_close (GjsifySherpaShimOnlineStream *stream);
|
|
183
|
+
|
|
184
|
+
/** Feed @n mono float samples recorded at @sample_rate. */
|
|
185
|
+
void gjsify_sherpa_shim_online_stream_accept (GjsifySherpaShimOnlineStream *stream,
|
|
186
|
+
int32_t sample_rate,
|
|
187
|
+
const float *samples,
|
|
188
|
+
int32_t n);
|
|
189
|
+
|
|
190
|
+
/** Tell the stream no more samples follow, so the tail is padded and decoded. */
|
|
191
|
+
void gjsify_sherpa_shim_online_stream_input_finished (GjsifySherpaShimOnlineStream *stream);
|
|
192
|
+
|
|
193
|
+
/** Non-zero while enough frames are buffered for another `..._decode`. */
|
|
194
|
+
int32_t gjsify_sherpa_shim_online_stream_is_ready (GjsifySherpaShimOnlineStream *stream);
|
|
195
|
+
|
|
196
|
+
/** Non-zero when the endpointing rules say this utterance has ended. */
|
|
197
|
+
int32_t gjsify_sherpa_shim_online_stream_is_endpoint (GjsifySherpaShimOnlineStream *stream);
|
|
198
|
+
|
|
199
|
+
void gjsify_sherpa_shim_online_stream_decode (GjsifySherpaShimOnlineStream *stream);
|
|
200
|
+
|
|
201
|
+
/** Clear the decoder state after an endpoint, keeping the stream. */
|
|
202
|
+
void gjsify_sherpa_shim_online_stream_reset (GjsifySherpaShimOnlineStream *stream);
|
|
203
|
+
|
|
204
|
+
/** The text decoded so far — the interim result. Caller frees. */
|
|
205
|
+
char *gjsify_sherpa_shim_online_stream_text (GjsifySherpaShimOnlineStream *stream);
|
|
206
|
+
|
|
207
|
+
/** The full result as JSON (tokens, timestamps, …). Caller frees. */
|
|
208
|
+
char *gjsify_sherpa_shim_online_stream_json (GjsifySherpaShimOnlineStream *stream);
|
|
209
|
+
|
|
210
|
+
/* ------------------------------------------------------------------------- */
|
|
211
|
+
/* Offline (whole-utterance) transducer recognizer. */
|
|
212
|
+
/* ------------------------------------------------------------------------- */
|
|
213
|
+
|
|
214
|
+
typedef struct _GjsifySherpaShimOffline GjsifySherpaShimOffline;
|
|
215
|
+
typedef struct _GjsifySherpaShimOfflineStream GjsifySherpaShimOfflineStream;
|
|
216
|
+
|
|
217
|
+
/** Open a non-streaming transducer recognizer. NULLs as in `..._online_open`. */
|
|
218
|
+
GjsifySherpaShimOffline *gjsify_sherpa_shim_offline_open (const char *encoder,
|
|
219
|
+
const char *decoder,
|
|
220
|
+
const char *joiner,
|
|
221
|
+
const char *tokens,
|
|
222
|
+
const char *model_type,
|
|
223
|
+
const char *provider,
|
|
224
|
+
int32_t num_threads,
|
|
225
|
+
const char *decoding_method,
|
|
226
|
+
int32_t max_active_paths,
|
|
227
|
+
float hotwords_score,
|
|
228
|
+
float blank_penalty,
|
|
229
|
+
int32_t sample_rate,
|
|
230
|
+
int32_t feature_dim,
|
|
231
|
+
int32_t debug);
|
|
232
|
+
|
|
233
|
+
void gjsify_sherpa_shim_offline_close (GjsifySherpaShimOffline *recognizer);
|
|
234
|
+
|
|
235
|
+
GjsifySherpaShimOfflineStream *
|
|
236
|
+
gjsify_sherpa_shim_offline_stream_open (GjsifySherpaShimOffline *recognizer,
|
|
237
|
+
const char *hotwords);
|
|
238
|
+
|
|
239
|
+
void gjsify_sherpa_shim_offline_stream_close (GjsifySherpaShimOfflineStream *stream);
|
|
240
|
+
|
|
241
|
+
void gjsify_sherpa_shim_offline_stream_accept (GjsifySherpaShimOfflineStream *stream,
|
|
242
|
+
int32_t sample_rate,
|
|
243
|
+
const float *samples,
|
|
244
|
+
int32_t n);
|
|
245
|
+
|
|
246
|
+
/** Decode everything accepted so far. One stream decodes once. */
|
|
247
|
+
void gjsify_sherpa_shim_offline_stream_decode (GjsifySherpaShimOfflineStream *stream);
|
|
248
|
+
|
|
249
|
+
/** The decoded text. Caller frees. */
|
|
250
|
+
char *gjsify_sherpa_shim_offline_stream_text (GjsifySherpaShimOfflineStream *stream);
|
|
251
|
+
|
|
252
|
+
/** The full result as JSON. Caller frees. */
|
|
253
|
+
char *gjsify_sherpa_shim_offline_stream_json (GjsifySherpaShimOfflineStream *stream);
|
|
254
|
+
|
|
255
|
+
#ifdef __cplusplus
|
|
256
|
+
}
|
|
257
|
+
#endif
|
|
258
|
+
|
|
259
|
+
#endif /* GJSIFY_SHERPA_SHIM_H */
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* The ELF export list of libgjsifysherpa.so.
|
|
2
|
+
*
|
|
3
|
+
* sherpa-onnx AND onnxruntime are linked in statically, and ELF has ONE flat
|
|
4
|
+
* symbol namespace: an exported `OrtGetApiBase` here would interpose on (or be
|
|
5
|
+
* interposed by) any other onnxruntime in the process. That is not theoretical
|
|
6
|
+
* on the development host — Fedora 44 ships `libonnxruntime.so.1` (1.22.2),
|
|
7
|
+
* while this library carries 1.28.2 — and the two ABIs are not interchangeable.
|
|
8
|
+
* `local: *` plus `-Wl,--exclude-libs,ALL` (meson.build) is what keeps both
|
|
9
|
+
* engines private; test/check-elf-deps.py reads the result back from the built
|
|
10
|
+
* file so a regression here fails the build rather than a user's process.
|
|
11
|
+
*
|
|
12
|
+
* The one glob covers BOTH halves of this library's own API: the
|
|
13
|
+
* `gjsify_sherpa_*` symbols valac generates for the GjsifySherpa namespace, and
|
|
14
|
+
* the `gjsify_sherpa_shim_*` ones from src/c/gjsify-sherpa-shim.c, whose prefix
|
|
15
|
+
* is a subset. Keeping the shim exported too is deliberate rather than
|
|
16
|
+
* overlooked: an ld version script's behaviour when two WILDCARDS match the same
|
|
17
|
+
* symbol is unspecified, so excluding the narrower prefix would be a trick that
|
|
18
|
+
* works until a linker disagrees — and the shim's prefix belongs to nobody else,
|
|
19
|
+
* so exporting it costs no collision risk. The hazard this file exists for is
|
|
20
|
+
* the vendored engines' symbols, and those match neither pattern.
|
|
21
|
+
*/
|
|
22
|
+
{
|
|
23
|
+
global:
|
|
24
|
+
gjsify_sherpa_*;
|
|
25
|
+
local:
|
|
26
|
+
*;
|
|
27
|
+
};
|