@gjsify/sherpa-native 0.0.0-stage → 0.64.15

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.
@@ -0,0 +1,178 @@
1
+ /*
2
+ * sherpa-shim.vapi — Vala's view of src/c/gjsify-sherpa-shim.h.
3
+ *
4
+ * Every declaration here is a scalar, a string, or an OPAQUE pointer. That is
5
+ * the whole point of the shim: sherpa-onnx's own configuration structs nest
6
+ * twenty-two sub-structs that upstream appends to every release, and a VAPI
7
+ * restating such a layout is a second truth that drifts — silently, since a
8
+ * stale struct is not a compile error but a model path read out of the middle
9
+ * of another field. The shim header explains this at length; this file is only
10
+ * allowed to exist because there is no layout left in it to get wrong.
11
+ *
12
+ * The three handle types are [Compact] classes, so Vala frees them on scope
13
+ * exit and on field assignment through their `free_function`. None of them
14
+ * appears in this library's public API — the GjsifySherpa classes hold them in
15
+ * PRIVATE fields — which is what keeps them out of the generated GIR, where a
16
+ * type with no GType has nothing to say.
17
+ *
18
+ * Not a GObject namespace and not installed: this VAPI describes code that is
19
+ * compiled INTO libgjsifysherpa, reached through `--vapidir` (meson.build).
20
+ */
21
+
22
+ [CCode (cheader_filename = "gjsify-sherpa-shim.h")]
23
+ namespace Shim {
24
+
25
+ /** sherpa-onnx's own version string, owned by the library. */
26
+ [CCode (cname = "gjsify_sherpa_shim_version")]
27
+ public unowned string version ();
28
+
29
+ /** The onnxruntime version sherpa-onnx was built against. */
30
+ [CCode (cname = "gjsify_sherpa_shim_onnxruntime_version")]
31
+ public unowned string onnxruntime_version ();
32
+
33
+ /**
34
+ * Whether @filename is readable, through sherpa-onnx's own check.
35
+ *
36
+ * `int32`, not `bool`: the shim is plain C and answers 0/non-zero. The
37
+ * GjsifySherpa classes convert at their boundary.
38
+ */
39
+ [CCode (cname = "gjsify_sherpa_shim_file_exists")]
40
+ public int32 file_exists (string filename);
41
+
42
+ /**
43
+ * An open Silero VAD.
44
+ *
45
+ * `open` is nullable and the handle carries no error detail: sherpa-onnx
46
+ * writes its own reason to stderr and returns NULL, so the callers in
47
+ * vad.vala check the model paths with {@link file_exists} first in order to
48
+ * have something to name.
49
+ */
50
+ [Compact]
51
+ [CCode (cname = "GjsifySherpaShimVad", has_type_id = false,
52
+ free_function = "gjsify_sherpa_shim_vad_close",
53
+ lower_case_cprefix = "gjsify_sherpa_shim_vad_")]
54
+ public class Vad {
55
+ public static Vad? open (string model,
56
+ float threshold,
57
+ float min_silence_duration,
58
+ float min_speech_duration,
59
+ float max_speech_duration,
60
+ int32 window_size,
61
+ int32 sample_rate,
62
+ int32 num_threads,
63
+ float buffer_size_in_seconds,
64
+ int32 debug);
65
+
66
+ /** Any @samples length is legal — the detector buffers the remainder. */
67
+ public void accept ([CCode (array_length_type = "int32_t")] float[] samples);
68
+
69
+ public int32 is_speech ();
70
+ public int32 is_empty ();
71
+ public void flush ();
72
+ public void clear ();
73
+ public void reset ();
74
+
75
+ /** Length of the fetched front segment, or -1 when none is queued. */
76
+ public int32 peek (out int64 start);
77
+
78
+ /** Copies the fetched segment into @dest, which must be `peek` long. */
79
+ public void read ([CCode (array_length_type = "int32_t")] float[] dest);
80
+
81
+ public void pop ();
82
+ }
83
+
84
+ [Compact]
85
+ [CCode (cname = "GjsifySherpaShimOnline", has_type_id = false,
86
+ free_function = "gjsify_sherpa_shim_online_close",
87
+ lower_case_cprefix = "gjsify_sherpa_shim_online_")]
88
+ public class Online {
89
+ public static Online? open (string encoder,
90
+ string decoder,
91
+ string joiner,
92
+ string tokens,
93
+ string? model_type,
94
+ string? provider,
95
+ int32 num_threads,
96
+ string? decoding_method,
97
+ int32 max_active_paths,
98
+ float hotwords_score,
99
+ float blank_penalty,
100
+ int32 enable_endpoint,
101
+ float rule1_min_trailing_silence,
102
+ float rule2_min_trailing_silence,
103
+ float rule3_min_utterance_length,
104
+ int32 sample_rate,
105
+ int32 feature_dim,
106
+ int32 debug);
107
+ }
108
+
109
+ [Compact]
110
+ [CCode (cname = "GjsifySherpaShimOnlineStream", has_type_id = false,
111
+ free_function = "gjsify_sherpa_shim_online_stream_close",
112
+ lower_case_cprefix = "gjsify_sherpa_shim_online_stream_")]
113
+ public class OnlineStream {
114
+ /**
115
+ * A stream of @recognizer. The handle keeps the recognizer POINTER, so
116
+ * the C recognizer has to outlive it; GjsifySherpa.OnlineStream holds a
117
+ * strong reference on its recognizer object for exactly that reason.
118
+ */
119
+ [CCode (cname = "gjsify_sherpa_shim_online_stream_open")]
120
+ public static OnlineStream? open (Online recognizer, string? hotwords);
121
+
122
+ public void accept (int32 sample_rate,
123
+ [CCode (array_length_type = "int32_t")] float[] samples);
124
+
125
+ public void input_finished ();
126
+ public int32 is_ready ();
127
+ public int32 is_endpoint ();
128
+ public void decode ();
129
+ public void reset ();
130
+
131
+ /** The interim text. Owned — the shim `g_strdup`s it. */
132
+ public string text ();
133
+
134
+ /** The full result as JSON. Owned. */
135
+ public string json ();
136
+ }
137
+
138
+ [Compact]
139
+ [CCode (cname = "GjsifySherpaShimOffline", has_type_id = false,
140
+ free_function = "gjsify_sherpa_shim_offline_close",
141
+ lower_case_cprefix = "gjsify_sherpa_shim_offline_")]
142
+ public class Offline {
143
+ public static Offline? open (string encoder,
144
+ string decoder,
145
+ string joiner,
146
+ string tokens,
147
+ string? model_type,
148
+ string? provider,
149
+ int32 num_threads,
150
+ string? decoding_method,
151
+ int32 max_active_paths,
152
+ float hotwords_score,
153
+ float blank_penalty,
154
+ int32 sample_rate,
155
+ int32 feature_dim,
156
+ int32 debug);
157
+ }
158
+
159
+ [Compact]
160
+ [CCode (cname = "GjsifySherpaShimOfflineStream", has_type_id = false,
161
+ free_function = "gjsify_sherpa_shim_offline_stream_close",
162
+ lower_case_cprefix = "gjsify_sherpa_shim_offline_stream_")]
163
+ public class OfflineStream {
164
+ [CCode (cname = "gjsify_sherpa_shim_offline_stream_open")]
165
+ public static OfflineStream? open (Offline recognizer, string? hotwords);
166
+
167
+ public void accept (int32 sample_rate,
168
+ [CCode (array_length_type = "int32_t")] float[] samples);
169
+
170
+ public void decode ();
171
+
172
+ /** The decoded text. Owned. */
173
+ public string text ();
174
+
175
+ /** The full result as JSON. Owned. */
176
+ public string json ();
177
+ }
178
+ }
@@ -0,0 +1,76 @@
1
+ /*
2
+ * GjsifySherpa — what this bridge is, and what it deliberately is not.
3
+ *
4
+ * sherpa-onnx is a C++ speech toolkit whose C API hands out opaque handles and
5
+ * takes configuration in nested structs. Neither travels through JavaScript, so
6
+ * this library is the thin layer that holds the handles: a Silero voice activity
7
+ * detector, a streaming (online) transducer recognizer, and a whole-utterance
8
+ * (offline) one. Everything a caller can express in TypeScript — model
9
+ * discovery and download, PCM plumbing, event shapes, the turn-taking policy —
10
+ * belongs ABOVE this line, in `@gjsify/vad` and `@gjsify/speech-recognition`
11
+ * (ADR 0127 decisions 5 and 8).
12
+ *
13
+ * MODELS ARE NEVER PART OF A PREBUILD. Every path here is supplied by the
14
+ * caller, and this package ships none: the models are hundreds of megabytes and
15
+ * their licences differ from sherpa-onnx's own Apache-2.0 (ADR 0127 decision
16
+ * 14). `@gjsify/vad` fetches Silero on first use.
17
+ *
18
+ * The recognizers are TRANSDUCER-ONLY — encoder/decoder/joiner plus a tokens
19
+ * file. That is the family both engines ADR 0127 measured belong to, and a
20
+ * model family nobody has run is a claim rather than a binding. Adding one is a
21
+ * parameter in src/c/gjsify-sherpa-shim.c; whisper belongs in
22
+ * `@gjsify/whisper-native` (decision 7), not here.
23
+ */
24
+
25
+ namespace GjsifySherpa {
26
+
27
+ /**
28
+ * Every failure in this library.
29
+ *
30
+ * sherpa-onnx's own C API answers NULL and writes its reason to stderr, so
31
+ * the detail in these messages is what this library could establish BEFORE
32
+ * handing a configuration over — above all which model file is missing,
33
+ * which is the overwhelmingly common failure and the one a NULL cannot name.
34
+ */
35
+ public errordomain SherpaError {
36
+ /** A model file does not exist or cannot be read. The message names it. */
37
+ MODEL_MISSING,
38
+ /** sherpa-onnx refused the configuration; its reason went to stderr. */
39
+ OPEN_FAILED,
40
+ /** The object was used before `open()` or after `close()`. */
41
+ NOT_OPEN,
42
+ }
43
+
44
+ /** The statically linked sherpa-onnx version, e.g. `2.0.1`. */
45
+ public string version () {
46
+ return Shim.version ();
47
+ }
48
+
49
+ /** The onnxruntime version sherpa-onnx was built against, e.g. `1.28.2`. */
50
+ public string onnxruntime_version () {
51
+ return Shim.onnxruntime_version ();
52
+ }
53
+
54
+ /**
55
+ * Whether @path is readable, as sherpa-onnx itself would judge it.
56
+ *
57
+ * Exposed because the TypeScript layer's model-cache check wants the
58
+ * engine's own answer rather than a second opinion from Gio.
59
+ */
60
+ public bool file_exists (string path) {
61
+ return Shim.file_exists (path) != 0;
62
+ }
63
+
64
+ /**
65
+ * Throw {@link SherpaError.MODEL_MISSING} naming @path unless it is readable.
66
+ *
67
+ * Called by every `open()` below, for the reason in the domain's own doc:
68
+ * it is the one diagnosis available before sherpa-onnx gets the chance to
69
+ * fail silently.
70
+ */
71
+ internal void require_file (string label, string path) throws SherpaError {
72
+ if (Shim.file_exists (path) == 0) {
73
+ throw new SherpaError.MODEL_MISSING ("%s not found: %s", label, path);
74
+ }
75
+ }
76
+ }
@@ -0,0 +1,207 @@
1
+ /*
2
+ * GjsifySherpa.Vad — Silero voice activity detection over sherpa-onnx.
3
+ *
4
+ * The knobs are CONSTRUCT PROPERTIES rather than ten positional arguments: the
5
+ * detector has nine of them, every one has a sensible default, and
6
+ * `new Vad({ model: …, minSilenceDuration: 0.3 })` is what that looks like from
7
+ * GJS. Construction cannot fail, so opening is its own step — a constructor
8
+ * that throws is not something GObject can express.
9
+ *
10
+ * WHAT THIS REPORTS, AND WHAT IT REFUSES TO
11
+ * ─────────────────────────────────────────
12
+ * Two different things, and conflating them is the mistake ADR 0127 decision 8
13
+ * exists to prevent:
14
+ *
15
+ * • {@link is_speech} is the LIVE edge — true while the detector is inside
16
+ * speech. That is what drives a "listening" indicator.
17
+ * • {@link next_segment} yields a COMPLETED segment, once
18
+ * {@link min_silence_duration} of silence has closed it.
19
+ *
20
+ * Neither is a turn. Whether a pause means the speaker finished is a policy
21
+ * question about a conversation, and it is decided above this layer; a VAD
22
+ * measures acoustics and nothing else.
23
+ */
24
+
25
+ namespace GjsifySherpa {
26
+
27
+ public class Vad : GLib.Object {
28
+
29
+ private Shim.Vad? handle = null;
30
+
31
+ /** Path to `silero_vad.onnx`. Supplied by the caller; never shipped. */
32
+ public string model { get; construct; }
33
+
34
+ /** Speech probability above which a window counts as speech. */
35
+ public float threshold { get; construct set; default = 0.5f; }
36
+
37
+ /**
38
+ * Silence that closes a segment, in seconds.
39
+ *
40
+ * 0.3 is this library's default because it is ADR 0127 decision 8's:
41
+ * 300 ms is short enough that a reply does not feel late and long
42
+ * enough to sit out the pause inside a sentence. sherpa-onnx's own
43
+ * default is 0.5.
44
+ */
45
+ public float min_silence_duration { get; construct set; default = 0.3f; }
46
+
47
+ /** Speech shorter than this is discarded as a click or a cough. */
48
+ public float min_speech_duration { get; construct set; default = 0.25f; }
49
+
50
+ /** A segment is closed at this length even without a pause. */
51
+ public float max_speech_duration { get; construct set; default = 20.0f; }
52
+
53
+ /** Samples per inference window. 512 is Silero v4/v5 at 16 kHz. */
54
+ public int window_size { get; construct set; default = 512; }
55
+
56
+ /** Sample rate of the audio fed to {@link accept}. Silero wants 16 kHz. */
57
+ public int sample_rate { get; construct set; default = 16000; }
58
+
59
+ /** onnxruntime threads. One is enough for Silero and the cheapest. */
60
+ public int num_threads { get; construct set; default = 1; }
61
+
62
+ /**
63
+ * The detector's internal ring buffer, in seconds.
64
+ *
65
+ * It has to exceed {@link max_speech_duration}, or a long segment is
66
+ * truncated at the buffer rather than at the limit.
67
+ */
68
+ public float buffer_size_in_seconds { get; construct set; default = 30.0f; }
69
+
70
+ /** Make sherpa-onnx print its configuration and decisions to stderr. */
71
+ public bool enable_debug { get; construct set; default = false; }
72
+
73
+ /** Whether {@link open} has run and {@link close} has not. */
74
+ public bool is_open {
75
+ get { return this.handle != null; }
76
+ }
77
+
78
+ public Vad (string model) {
79
+ Object (model: model);
80
+ }
81
+
82
+ /**
83
+ * Load the model and start detecting.
84
+ *
85
+ * Calling this twice is a no-op rather than a leak — the second call
86
+ * returns with the first handle in place.
87
+ */
88
+ public void open () throws SherpaError {
89
+ if (this.handle != null) {
90
+ return;
91
+ }
92
+
93
+ require_file ("Silero VAD model", this.model);
94
+
95
+ if (this.buffer_size_in_seconds <= this.max_speech_duration) {
96
+ // Not fatal to sherpa-onnx, but it silently truncates segments
97
+ // at the buffer instead of at max_speech_duration, which reads
98
+ // as a VAD that cuts people off for no reason.
99
+ warning ("buffer-size-in-seconds (%g) should exceed max-speech-duration (%g)",
100
+ this.buffer_size_in_seconds, this.max_speech_duration);
101
+ }
102
+
103
+ this.handle = Shim.Vad.open (this.model,
104
+ this.threshold,
105
+ this.min_silence_duration,
106
+ this.min_speech_duration,
107
+ this.max_speech_duration,
108
+ this.window_size,
109
+ this.sample_rate,
110
+ this.num_threads,
111
+ this.buffer_size_in_seconds,
112
+ this.enable_debug ? 1 : 0);
113
+
114
+ if (this.handle == null) {
115
+ throw new SherpaError.OPEN_FAILED (
116
+ "sherpa-onnx refused the VAD configuration for %s (reason on stderr)",
117
+ this.model);
118
+ }
119
+ }
120
+
121
+ /** Release the model. Idempotent; the object stays reusable via `open`. */
122
+ public void close () {
123
+ this.handle = null;
124
+ }
125
+
126
+ private unowned Shim.Vad require_handle () throws SherpaError {
127
+ if (this.handle == null) {
128
+ throw new SherpaError.NOT_OPEN ("the VAD is not open");
129
+ }
130
+ return this.handle;
131
+ }
132
+
133
+ /**
134
+ * Feed mono float samples in [-1, 1], recorded at {@link sample_rate}.
135
+ *
136
+ * ANY length is fine. The detector keeps what does not fill a window
137
+ * and prepends it to the next call, so a caller may hand over whatever
138
+ * its capture source produced — `@gjsify/media-capture`'s PCM source
139
+ * emits whatever GStreamer buffered, and it does not have to be
140
+ * re-chunked for this.
141
+ */
142
+ public void accept (float[] samples) throws SherpaError {
143
+ this.require_handle ().accept (samples);
144
+ }
145
+
146
+ /** True while the detector is INSIDE speech right now. */
147
+ public bool is_speech () throws SherpaError {
148
+ return this.require_handle ().is_speech () != 0;
149
+ }
150
+
151
+ /** Whether a completed segment is waiting for {@link next_segment}. */
152
+ public bool has_segment () throws SherpaError {
153
+ return this.require_handle ().is_empty () == 0;
154
+ }
155
+
156
+ /**
157
+ * Close an in-progress segment and queue it — end of input.
158
+ *
159
+ * Without this the final utterance of a file or a stopped recording is
160
+ * never reported: nothing followed it that could be silence.
161
+ */
162
+ public void flush () throws SherpaError {
163
+ this.require_handle ().flush ();
164
+ }
165
+
166
+ /** Drop every queued segment, keeping the model state. */
167
+ public void clear () throws SherpaError {
168
+ this.require_handle ().clear ();
169
+ }
170
+
171
+ /** Drop every queued segment AND the model state. */
172
+ public void reset () throws SherpaError {
173
+ this.require_handle ().reset ();
174
+ }
175
+
176
+ /**
177
+ * Take the oldest completed segment. %false when none is queued.
178
+ *
179
+ * @samples is the segment's audio and @start its offset from the first
180
+ * sample ever accepted, in samples — which is what lets a caller place
181
+ * it on a timeline without counting along.
182
+ *
183
+ * A BOOLEAN RETURN rather than the nullable `float[]?` this used to be,
184
+ * because a nullable array does not survive GObject-Introspection:
185
+ * measured under gjs, a NULL C array with length 0 arrives in
186
+ * JavaScript as an EMPTY ARRAY, so `=== null` is false and the two
187
+ * cases that have to be told apart — "nothing queued" and "a segment"
188
+ * — collapse into each other. The predicate is the one part of this
189
+ * that cannot be mis-marshalled, so it is what the API returns.
190
+ */
191
+ public bool next_segment (out float[] samples, out int64 start) throws SherpaError {
192
+ unowned var vad = this.require_handle ();
193
+
194
+ var n = vad.peek (out start);
195
+ if (n < 0) {
196
+ samples = new float[0];
197
+ start = 0;
198
+ return false;
199
+ }
200
+
201
+ samples = new float[n];
202
+ vad.read (samples);
203
+ vad.pop ();
204
+ return true;
205
+ }
206
+ }
207
+ }
@@ -0,0 +1,112 @@
1
+ # OVERLAY — NOT upstream's file. Applied to the sherpa-onnx wrap checkout by
2
+ # `patch_directory = sherpa-onnx-shared-onnxruntime` in ../../sherpa-onnx.wrap,
3
+ # which is where the measurement behind it is written down.
4
+ #
5
+ # In one line: sherpa-onnx is built STATIC and this file makes the one library it
6
+ # must not link statically — onnxruntime — a SHARED imported target instead.
7
+ #
8
+ # Upstream's cmake/onnxruntime.cmake picks the file to include by
9
+ # `BUILD_SHARED_LIBS` alone, so a static sherpa-onnx always lands on
10
+ # `onnxruntime-linux-x86_64-static.cmake` and gets `libonnxruntime.a`. That
11
+ # archive is built by an OLD GCC and defines 54 weak vague-linkage symbols that
12
+ # the host GCC's own `libsherpa_onnx_core.a` also defines, under identical
13
+ # mangled names: `std::__detail::_Scanner<char>`'s nine members,
14
+ # `_State<char>::~_State` and `std::__throw_regex_error` among them. libstdc++
15
+ # puts `regex_traits` in the inline namespace `std::__cxx11` but `_Scanner` in
16
+ # plain `std::__detail` — so for those the NAME is stable across GCC versions
17
+ # while the LAYOUT is not. The static linker dedupes them, onnxruntime's
18
+ # `DeviceDiscovery::GetPciBusId` then builds an old-layout `_Scanner` through a
19
+ # new-layout constructor, and the result is not a link error but
20
+ # `free(): invalid pointer` on the first inference session — i.e. inside
21
+ # `Vad.open()`, with a correct model, every time.
22
+ #
23
+ # Hence: the SHARED engine, whose own `.so` exports exactly two dynamic symbols
24
+ # (`OrtGetApiBase` and one more) and no `std::` anything, so there is nothing
25
+ # left to dedupe. The contents below are upstream's own
26
+ # `cmake/onnxruntime-linux-x86_64.cmake` — the shared x86_64 file — under the
27
+ # static file's name, with its `if(NOT BUILD_SHARED_LIBS) FATAL_ERROR` guard
28
+ # dropped (that guard is the whole reason this cannot be done by options) and
29
+ # the directory-scoped `include_directories()` of the static file kept. Both
30
+ # include paths are needed: the imported target's
31
+ # INTERFACE_INCLUDE_DIRECTORIES is what meson's CMake translator carries into
32
+ # the targets it regenerates, and the directory-scoped one is what sherpa-onnx's
33
+ # own sources compile against.
34
+ #
35
+ # BUMPING sherpa-onnx: diff upstream's two x86_64 files against this one. Only
36
+ # the URL and the hash should differ from the shared file; if the shared file has
37
+ # grown anything else, copy that too.
38
+ # ---------------------------------------------------------------------------
39
+ # Copyright (c) 2022-2023 Xiaomi Corporation
40
+ message(STATUS "CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
41
+ message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
42
+ message(STATUS "gjsify: SHARED onnxruntime overlay in use (static libonnxruntime.a is unusable here)")
43
+
44
+ if(NOT CMAKE_SYSTEM_NAME STREQUAL Linux)
45
+ message(FATAL_ERROR "This file is for Linux only. Given: ${CMAKE_SYSTEM_NAME}")
46
+ endif()
47
+
48
+ if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64)
49
+ message(FATAL_ERROR "This file is for x86_64 only. Given: ${CMAKE_SYSTEM_PROCESSOR}")
50
+ endif()
51
+
52
+ set(onnxruntime_URL "https://github.com/csukuangfj/onnxruntime-libs/releases/download/v1.28.2/onnxruntime-linux-x64-glibc2_17-Release-1.28.2.zip")
53
+ set(onnxruntime_HASH "SHA256=c4f8994d56191d9d2c92a961b39fe790459f2c5d155f912b239506ea31359534")
54
+
55
+ # If you don't have access to the Internet,
56
+ # please download onnxruntime to one of the following locations.
57
+ # You can add more if you want.
58
+ set(possible_file_locations
59
+ $ENV{HOME}/Downloads/onnxruntime-linux-x64-glibc2_17-Release-1.28.2.zip
60
+ ${CMAKE_SOURCE_DIR}/onnxruntime-linux-x64-glibc2_17-Release-1.28.2.zip
61
+ ${CMAKE_BINARY_DIR}/onnxruntime-linux-x64-glibc2_17-Release-1.28.2.zip
62
+ /tmp/onnxruntime-linux-x64-glibc2_17-Release-1.28.2.zip
63
+ /star-fj/fangjun/download/github/onnxruntime-linux-x64-glibc2_17-Release-1.28.2.zip
64
+ )
65
+
66
+ foreach(f IN LISTS possible_file_locations)
67
+ if(EXISTS ${f})
68
+ set(onnxruntime_URL "${f}")
69
+ file(TO_CMAKE_PATH "${onnxruntime_URL}" onnxruntime_URL)
70
+ message(STATUS "Found local downloaded onnxruntime: ${onnxruntime_URL}")
71
+ break()
72
+ endif()
73
+ endforeach()
74
+
75
+ FetchContent_Declare(onnxruntime
76
+ URL
77
+ ${onnxruntime_URL}
78
+ URL_HASH ${onnxruntime_HASH}
79
+ )
80
+
81
+ FetchContent_GetProperties(onnxruntime)
82
+ if(NOT onnxruntime_POPULATED)
83
+ message(STATUS "Downloading onnxruntime from ${onnxruntime_URL}")
84
+ FetchContent_Populate(onnxruntime)
85
+ endif()
86
+ message(STATUS "onnxruntime is downloaded to ${onnxruntime_SOURCE_DIR}")
87
+
88
+ find_library(location_onnxruntime onnxruntime
89
+ PATHS
90
+ "${onnxruntime_SOURCE_DIR}/lib"
91
+ NO_CMAKE_SYSTEM_PATH
92
+ )
93
+
94
+ message(STATUS "location_onnxruntime: ${location_onnxruntime}")
95
+
96
+ # Directory-scoped, as in the static file this replaces: sherpa-onnx's own
97
+ # translation units include <onnxruntime_cxx_api.h> and do not all go through a
98
+ # target that links the imported one below.
99
+ include_directories(${onnxruntime_SOURCE_DIR}/include)
100
+
101
+ add_library(onnxruntime SHARED IMPORTED)
102
+
103
+ set_target_properties(onnxruntime PROPERTIES
104
+ IMPORTED_LOCATION ${location_onnxruntime}
105
+ INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_SOURCE_DIR}/include"
106
+ )
107
+
108
+ # Deliberately NOT `PARENT_SCOPE`: sherpa-onnx/csrc/CMakeLists.txt prefers the
109
+ # `onnxruntime` TARGET when it exists and only falls back to a file list.
110
+ file(GLOB onnxruntime_lib_files "${onnxruntime_SOURCE_DIR}/lib/libonnxruntime*")
111
+ message(STATUS "onnxruntime lib files: ${onnxruntime_lib_files}")
112
+ install(FILES ${onnxruntime_lib_files} DESTINATION lib)
@@ -0,0 +1,56 @@
1
+ # sherpa-onnx (Apache-2.0, docs/attribution.md) — the speech engine this bridge
2
+ # exposes: the Silero VAD @gjsify/vad runs on, and the online/offline transducer
3
+ # recognizers @gjsify/speech-recognition will run on (ADR 0127 decision 5).
4
+ #
5
+ # Consumed as a CMAKE SUBPROJECT, not a meson one — sherpa-onnx has no meson
6
+ # build — exactly as `sdl3.wrap` is in packages/web/gamepad-native. meson's cmake
7
+ # module reads the target graph, so what this build links is `sherpa-onnx-c-api`
8
+ # and whatever CMake says that needs; see meson.build § sherpa-onnx.
9
+ #
10
+ # PINNED TO THE v2.0.1 TAG, which is commit
11
+ # af2d8d81dbfa61c4cea4c568da78f3982edca005 — verify with
12
+ # `git -C subprojects/sherpa-onnx rev-parse HEAD`. A tag rather than that raw sha
13
+ # because `depth = 1` is only possible for a ref the server offers, and the full
14
+ # history is several hundred MB against a 53 MB shallow checkout.
15
+ #
16
+ # sherpa-onnx's own CMake then DOWNLOADS more: onnxruntime 1.28.2, eigen,
17
+ # kaldi-native-fbank, kaldi-decoder, kissfft, kaldifst, openfst, utf8proc,
18
+ # simple-sentencepiece and nlohmann/json, each pinned and checksummed upstream.
19
+ # That is why a first build needs the network even though every revision here is
20
+ # fixed; README.md § Building records how to pre-place the onnxruntime zip for an
21
+ # offline build.
22
+ #
23
+ # ONE FILE OF THE CHECKOUT IS REPLACED, by `patch_directory` below:
24
+ # `cmake/onnxruntime-linux-x86_64-static.cmake`. sherpa-onnx is built static, and
25
+ # upstream's static branch links the prebuilt `libonnxruntime.a` — which was
26
+ # compiled by an old GCC and collides with the HOST GCC's own vague-linkage
27
+ # instantiations: 54 weak symbols defined under identical mangled names by both
28
+ # archives, of which `std::__detail::_Scanner<char>`'s members are the fatal
29
+ # ones (libstdc++ keeps `regex_traits` in the inline namespace `std::__cxx11` but
30
+ # `_Scanner` in plain `std::__detail`, so the name is stable across GCC versions
31
+ # while the layout is not). The linker dedupes them silently and the first
32
+ # inference session dies in `free(): invalid pointer` — reproduced from gjs, from
33
+ # a plain C caller, with the v4 and the v5 Silero model, on a clean build dir.
34
+ # Relinking the identical archives against the SHARED libonnxruntime.so is what
35
+ # fixed it, and that `.so` exports two dynamic symbols and no `std::` anything,
36
+ # so nothing is left to dedupe. The overlay therefore puts upstream's own SHARED
37
+ # x86_64 file under the static file's name.
38
+ #
39
+ # Why an overlay rather than options: see meson.build § onnxruntime for the two
40
+ # arrangements that were tried first and what each one did.
41
+ #
42
+ # Per-ARCHITECTURE: the overlay is one named file, so a new linux target needs
43
+ # its own overlay file beside it and meson.build errors out rather than quietly
44
+ # falling back to upstream's static one.
45
+ #
46
+ # BUMPING sherpa-onnx: change `revision`, update the sha in this comment, diff
47
+ # the two upstream x86_64 onnxruntime files against the overlay (see its header),
48
+ # `rm -rf subprojects/sherpa-onnx` so the patch re-applies, rebuild, re-run
49
+ # `meson test -C build` (the ELF read-back asserts that NOTHING from onnxruntime
50
+ # is exported) and re-measure the prebuild size in README.md § Updating sherpa-onnx.
51
+ [wrap-git]
52
+ directory = sherpa-onnx
53
+ url = https://github.com/k2-fsa/sherpa-onnx.git
54
+ revision = v2.0.1
55
+ depth = 1
56
+ patch_directory = sherpa-onnx-shared-onnxruntime