@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,155 @@
1
+ #!/usr/bin/env -S gjs -m
2
+ /*
3
+ * api.gjs.js — the GObject API through the typelib, under gjs.
4
+ *
5
+ * shim-smoke.c measures the C underneath; this measures the layer that
6
+ * @gjsify/vad actually imports, which is a different claim. Three things only
7
+ * the typelib can get wrong, and all three have bitten a bridge in this repo
8
+ * before:
9
+ *
10
+ * • the typelib names a shared library that does not load (docs/prebuilds.md
11
+ * calls this the silent one: GI finds the typelib, the loader refuses the
12
+ * library, and the error names neither);
13
+ * • valac's generated GIR does not carry the construct properties, so
14
+ * `new Vad({ … })` silently ignores them;
15
+ * • the errordomain does not survive into GI, so a missing model arrives in
16
+ * JavaScript as an opaque failure instead of something a caller can branch
17
+ * on — which is the whole reason @gjsify/vad can tell "no model yet, go
18
+ * download one" from "this model is broken".
19
+ *
20
+ * NO MODEL AND NO AUDIO DEVICE: everything here runs on a bare runner.
21
+ */
22
+
23
+ import GjsifySherpa from 'gi://GjsifySherpa?version=1.0';
24
+ import System from 'system';
25
+
26
+ let failures = 0;
27
+
28
+ const check = (what, fn) => {
29
+ try {
30
+ fn();
31
+ console.log(`ok ${what}`);
32
+ } catch (error) {
33
+ failures += 1;
34
+ console.log(`FAIL ${what}: ${error.message}`);
35
+ }
36
+ };
37
+
38
+ const assert = (condition, message) => {
39
+ if (!condition) throw new Error(message);
40
+ };
41
+
42
+ // The library loaded at all — importing the typelib above only resolved the
43
+ // typelib. The first actual call is what makes the loader open the .so.
44
+ check('version strings', () => {
45
+ const sherpa = GjsifySherpa.version();
46
+ const ort = GjsifySherpa.onnxruntime_version();
47
+ assert(typeof sherpa === 'string' && sherpa.length > 0, `sherpa version was ${sherpa}`);
48
+ assert(typeof ort === 'string' && ort.length > 0, `onnxruntime version was ${ort}`);
49
+ console.log(` sherpa-onnx ${sherpa}, onnxruntime ${ort}`);
50
+ });
51
+
52
+ check('file_exists', () => {
53
+ assert(GjsifySherpa.file_exists('/proc/self/exe') === true, '/proc/self/exe reported missing');
54
+ assert(GjsifySherpa.file_exists('/nonexistent/silero_vad.onnx') === false, 'a nonexistent path reported present');
55
+ });
56
+
57
+ // The construct properties, read back. If valac's GIR had not carried them,
58
+ // these would be undefined rather than the documented defaults — and the one
59
+ // that matters is minSilenceDuration, which is ADR 0127 decision 8's 300 ms.
60
+ check('Vad construct properties', () => {
61
+ const vad = new GjsifySherpa.Vad({ model: '/nonexistent/silero_vad.onnx' });
62
+ assert(vad.model === '/nonexistent/silero_vad.onnx', `model was ${vad.model}`);
63
+ assert(
64
+ Math.abs(vad.min_silence_duration - 0.3) < 1e-6,
65
+ `min-silence-duration defaulted to ${vad.min_silence_duration}, not 0.3`,
66
+ );
67
+ assert(vad.sample_rate === 16000, `sample-rate defaulted to ${vad.sample_rate}`);
68
+ assert(vad.is_open === false, 'a fresh Vad reported itself open');
69
+
70
+ const configured = new GjsifySherpa.Vad({
71
+ model: '/nonexistent/silero_vad.onnx',
72
+ min_silence_duration: 0.8,
73
+ threshold: 0.7,
74
+ });
75
+ assert(
76
+ Math.abs(configured.min_silence_duration - 0.8) < 1e-6,
77
+ 'min-silence-duration was not taken from the constructor',
78
+ );
79
+ assert(Math.abs(configured.threshold - 0.7) < 1e-6, 'threshold was not taken from the constructor');
80
+ });
81
+
82
+ // The errordomain, which is the part @gjsify/vad branches on.
83
+ check('Vad.open names the missing model', () => {
84
+ const vad = new GjsifySherpa.Vad({ model: '/nonexistent/silero_vad.onnx' });
85
+ let thrown = null;
86
+ try {
87
+ vad.open();
88
+ } catch (error) {
89
+ thrown = error;
90
+ }
91
+ assert(thrown !== null, 'opening a nonexistent model did not throw');
92
+ assert(
93
+ thrown.code === GjsifySherpa.SherpaError.MODEL_MISSING,
94
+ `code was ${thrown.code}, not SherpaError.MODEL_MISSING`,
95
+ );
96
+ assert(
97
+ thrown.message.includes('/nonexistent/silero_vad.onnx'),
98
+ `the message does not name the file: ${thrown.message}`,
99
+ );
100
+ });
101
+
102
+ // Using a closed object is NOT_OPEN rather than a crash — the mistake every
103
+ // caller makes once.
104
+ check('Vad methods refuse a closed detector', () => {
105
+ const vad = new GjsifySherpa.Vad({ model: '/nonexistent/silero_vad.onnx' });
106
+ let thrown = null;
107
+ try {
108
+ vad.accept(new Float32Array(512));
109
+ } catch (error) {
110
+ thrown = error;
111
+ }
112
+ assert(thrown !== null, 'accept() on a closed Vad did not throw');
113
+ assert(thrown.code === GjsifySherpa.SherpaError.NOT_OPEN, `code was ${thrown.code}, not SherpaError.NOT_OPEN`);
114
+ });
115
+
116
+ for (const [name, Klass] of [
117
+ ['OnlineRecognizer', GjsifySherpa.OnlineRecognizer],
118
+ ['OfflineRecognizer', GjsifySherpa.OfflineRecognizer],
119
+ ]) {
120
+ check(`${name}.open names the missing model`, () => {
121
+ const recognizer = new Klass({
122
+ encoder: '/nonexistent/encoder.onnx',
123
+ decoder: '/nonexistent/decoder.onnx',
124
+ joiner: '/nonexistent/joiner.onnx',
125
+ tokens: '/nonexistent/tokens.txt',
126
+ });
127
+ assert(recognizer.provider === 'cpu', `provider defaulted to ${recognizer.provider}`);
128
+ assert(
129
+ recognizer.decoding_method === 'greedy_search',
130
+ `decoding-method defaulted to ${recognizer.decoding_method}`,
131
+ );
132
+
133
+ let thrown = null;
134
+ try {
135
+ recognizer.open();
136
+ } catch (error) {
137
+ thrown = error;
138
+ }
139
+ assert(thrown !== null, 'opening nonexistent weights did not throw');
140
+ assert(
141
+ thrown.code === GjsifySherpa.SherpaError.MODEL_MISSING,
142
+ `code was ${thrown.code}, not SherpaError.MODEL_MISSING`,
143
+ );
144
+ assert(thrown.message.includes('encoder'), `the message does not name it: ${thrown.message}`);
145
+ });
146
+ }
147
+
148
+ // Anything that needs real weights is test/vad-model.gjs.js, a separate meson
149
+ // test that reports a true SKIP when no model is cached. It is separate so that
150
+ // this file keeps the promise in its header and runs everywhere.
151
+
152
+ if (failures > 0) {
153
+ console.error(`${failures} check(s) failed`);
154
+ System.exit(1);
155
+ }
@@ -0,0 +1,213 @@
1
+ #!/usr/bin/env python3
2
+ """What libgjsifysherpa.so links and exports, read back from the built file
3
+ rather than from meson.build.
4
+
5
+ 1. DT_NEEDED names only libc, libm, libdl, libpthread, libstdc++, the dynamic
6
+ loader, GLib/GObject — and `libonnxruntime.so`, which is REQUIRED to be
7
+ there and REQUIRED to exist beside the library. sherpa-onnx is linked
8
+ statically but onnxruntime cannot be (meson.build § onnxruntime: the
9
+ prebuilt static archive collides with a current GCC's own vague-linkage
10
+ symbols and corrupts the heap on the first inference session). What makes
11
+ the shared engine safe to ship rather than a dependency on the host is that
12
+ it travels in the same prebuild and is found through RUNPATH `$ORIGIN`;
13
+ this test is what holds that true.
14
+ 2. The dynamic symbol table exports only `gjsify_sherpa_*`. ELF has ONE flat
15
+ symbol namespace, so an exported `OrtGetApiBase` or `SherpaOnnxCreateOfflineRecognizer`
16
+ here would interpose on — or be interposed by — any other onnxruntime in
17
+ the process. That is not hypothetical: Fedora 44 ships libonnxruntime.so.1
18
+ (1.22.2) while this library carries 1.28.2 beside it, and the two ABIs are
19
+ not interchangeable.
20
+ 3. The engine's OWN exports, for the same reason one level down. It is a
21
+ second shared object in the prebuild, so whatever it exports is in that
22
+ flat namespace too — and the premise of linking libstdc++ statically into
23
+ libgjsifysherpa.so is that no C++ symbol crosses either boundary.
24
+ 4. The engine's GLIBCXX floor. It is prebuilt against glibc 2.17 by an old
25
+ GCC, and that is the only reason its own DT_NEEDED on libstdc++.so.6 does
26
+ not cost the prebuild its portability. A wrap bump to an archive built by a
27
+ newer toolchain would raise the floor silently, so the bound is asserted
28
+ rather than assumed.
29
+ 5. With `--require-stripped-engine`: that the staged engine has no `.symtab`.
30
+ Upstream ships the release unstripped — 27.0 MB, of which 6.0 MB is a debug
31
+ symbol table nothing in this prebuild can use — and meson.build strips it
32
+ while staging. The flag is passed only when `strip` was found, so the
33
+ assertion holds exactly where the saving was actually taken.
34
+
35
+ Prints every list so the CI log carries the measurement, and exits 1 on anything
36
+ outside them. Uses `readelf` (binutils) — the file format is the contract, not
37
+ whatever meson was asked to do. Mirrors
38
+ packages/web/gamepad-native/test/check-elf-deps.py, which makes the same promise
39
+ about SDL.
40
+ """
41
+ import os
42
+ import re
43
+ import subprocess
44
+ import sys
45
+
46
+ # `libmvec.so.1` is glibc's vectorised libm, and it is on this list for a
47
+ # measured reason rather than a hopeful one: GCC auto-vectorises the feature
48
+ # maths in kaldi-native-fbank, and from glibc 2.22 onward `-lm` resolves the
49
+ # resulting `_ZGVbN2v_*` calls out of libmvec, which ld then records. It ships
50
+ # as part of glibc itself — the same package as libc.so.6 and libm.so.6 — so a
51
+ # host that can load one can load it. Keeping it out of the allowlist would red
52
+ # this test for a dependency on the C library.
53
+ ALLOWED_NEEDED = re.compile(
54
+ r"^(libc\.so\.6|libm\.so\.6|libmvec\.so\.1|libdl\.so\.2|libpthread\.so\.0"
55
+ r"|ld-linux[-\w.]*\.so\.\d+"
56
+ r"|libstdc\+\+\.so\.6|libgcc_s\.so\.1|libglib-2\.0\.so\.0|libgobject-2\.0\.so\.0"
57
+ r"|libonnxruntime\.so)$"
58
+ )
59
+
60
+ # Unversioned on purpose: that is the SONAME of the archive the wrap pins, and
61
+ # an unversioned name is only resolvable through RUNPATH to the sibling copy —
62
+ # never accidentally against a distribution's `libonnxruntime.so.1`.
63
+ ENGINE = "libonnxruntime.so"
64
+
65
+ # With `--require-static-libstdcxx` these two become failures for
66
+ # libgjsifysherpa.so. They are allowed above because a host without
67
+ # `libstdc++.a` still gets a working build (meson.build § THE LIBSTDC++
68
+ # DECISION) — it is only a SHIPPABLE prebuild that must not have them, since
69
+ # each carries a GLIBCXX_/GCC_ version floor set by the build host, and that
70
+ # floor is what makes a prebuild from a new Fedora refuse to load on an older
71
+ # Debian. Measured on Fedora 44 / GCC 16: a dynamic libstdc++ here demands
72
+ # GLIBCXX_3.4.30, against the ENGINE's 3.4.19 — so linking it dynamically would
73
+ # raise the floor of the pair to this build host's.
74
+ #
75
+ # It is only the GLIBCXX axis that this buys. The GLIBC floor is the build
76
+ # image's either way (Fedora 44 measures sqrtf@GLIBC_2.43 out of the feature
77
+ # maths no matter how libstdc++ is linked), which is why prebuilds.yml pins
78
+ # fedora:43 — see the banner over its `container:`. Not asserted here: it is a
79
+ # property of the image, identical for every prebuild in the tree, and
80
+ # `gjsify.glibcRequires` is where the tree records it.
81
+ #
82
+ # The ENGINE is exempt and is checked by ENGINE_MAX_GLIBCXX below instead: it is
83
+ # a binary release that always links libstdc++ dynamically, and this is not a
84
+ # flag that could change that.
85
+ STATIC_LIBSTDCXX_FORBIDS = re.compile(r"^(libstdc\+\+\.so\.6|libgcc_s\.so\.1)$")
86
+
87
+ # GCC 4.9. The pinned archive measures 3.4.19 (GCC 4.8), so there is one release
88
+ # of slack; anything above this is a newer toolchain than the glibc2_17 releases
89
+ # use, which is the signal worth failing on. GLIBCXX_3.4.20 is in Debian 8 and
90
+ # RHEL 7, so the bound is also roughly where "runs anywhere" stops being true.
91
+ ENGINE_MAX_GLIBCXX = (3, 4, 20)
92
+
93
+ # The two entry points sherpa-onnx calls, plus the version-node name readelf
94
+ # lists alongside them. Nothing else may be visible — above all nothing `std::`.
95
+ ENGINE_ALLOWED_EXPORTS = {
96
+ "OrtGetApiBase",
97
+ "OrtSessionOptionsAppendExecutionProvider_CPU",
98
+ "VERS_1.28.2",
99
+ }
100
+
101
+
102
+ def readelf(*args):
103
+ return subprocess.run(["readelf", "--wide", *args], check=True, capture_output=True, text=True).stdout
104
+
105
+
106
+ def needed_of(path):
107
+ return re.findall(r"\(NEEDED\)\s+Shared library: \[([^\]]+)\]", readelf("-d", path))
108
+
109
+
110
+ def exports_of(path):
111
+ out = []
112
+ for line in readelf("--dyn-syms", path).splitlines():
113
+ cols = line.split()
114
+ # Num: Value Size Type Bind Vis Ndx Name — defined (Ndx != UND), GLOBAL/WEAK.
115
+ if len(cols) >= 8 and cols[4] in ("GLOBAL", "WEAK") and cols[6] != "UND":
116
+ out.append(cols[7].split("@")[0])
117
+ return out
118
+
119
+
120
+ def glibcxx_versions(path):
121
+ found = re.findall(r"GLIBCXX_(\d+(?:\.\d+)*)", readelf("-V", path))
122
+ return sorted({tuple(int(n) for n in v.split(".")) for v in found})
123
+
124
+
125
+ def is_stripped(path):
126
+ # `.symtab` is the debug symbol table `strip --strip-all` removes, and it is
127
+ # exactly what `file` reports as "not stripped". `.dynsym` survives either
128
+ # way — the loader reads it — so its presence says nothing about stripping.
129
+ return not re.search(r"\s\.symtab\s", readelf("-S", path))
130
+
131
+
132
+ def check_engine(path, require_stripped=False):
133
+ """The engine's own surface and portability floor. Returns a problem list."""
134
+ exported = sorted(set(exports_of(path)))
135
+ stray = [s for s in exported if s not in ENGINE_ALLOWED_EXPORTS]
136
+ versions = glibcxx_versions(path)
137
+ floor = versions[-1] if versions else (0, 0, 0)
138
+ stripped = is_stripped(path)
139
+
140
+ print(f" {ENGINE} exported ({len(exported)}): {', '.join(exported) or '(none)'}")
141
+ print(f" {ENGINE} GLIBCXX floor: {'.'.join(str(n) for n in floor)}")
142
+ print(f" {ENGINE} stripped: {'yes' if stripped else 'no'}")
143
+
144
+ problems = [f"{ENGINE} exports {s}, which is not one of the Ort entry points"
145
+ for s in stray[:20]]
146
+ if require_stripped and not stripped:
147
+ problems.append(
148
+ f"{ENGINE} still carries .symtab — the upstream release is shipped "
149
+ "unstripped and meson.build strips it while staging, so this is the "
150
+ "unstripped copy and the prebuild is 6 MB bigger than it has to be"
151
+ )
152
+ if floor > ENGINE_MAX_GLIBCXX:
153
+ problems.append(
154
+ f"{ENGINE} needs GLIBCXX_{'.'.join(str(n) for n in floor)}, above the "
155
+ f"{'.'.join(str(n) for n in ENGINE_MAX_GLIBCXX)} this prebuild promises — "
156
+ "the wrap now points at an archive built by a newer toolchain"
157
+ )
158
+ if not exported:
159
+ problems.append(f"{ENGINE} exports nothing at all — readelf's format moved")
160
+ return problems
161
+
162
+
163
+ def main(path, require_static_libstdcxx=False, require_stripped_engine=False):
164
+ needed = needed_of(path)
165
+ exported = exports_of(path)
166
+ stray = [s for s in exported if not s.startswith("gjsify_sherpa_")]
167
+
168
+ print(f"{path}")
169
+ print(f" NEEDED ({len(needed)}): {', '.join(needed)}")
170
+ print(f" exported ({len(exported)}): "
171
+ + ("all gjsify_sherpa_*" if not stray else ", ".join(stray[:20])))
172
+
173
+ problems = [f"NEEDED {n} is not an OS/GLib library or the engine" for n in needed
174
+ if not ALLOWED_NEEDED.match(n)]
175
+ problems += [f"exports {s}, which is not gjsify_sherpa_*" for s in stray[:20]]
176
+ if require_static_libstdcxx:
177
+ problems += [f"NEEDED {n}, but this build linked libstdc++ statically"
178
+ for n in needed if STATIC_LIBSTDCXX_FORBIDS.match(n)]
179
+ # An empty parse would pass everything above; the library links libc and
180
+ # exports the whole API, so zero of either means the parse stopped reading.
181
+ if not needed or len(exported) < 20:
182
+ problems.append(f"parsed {len(needed)} NEEDED / {len(exported)} exports — readelf's format moved")
183
+
184
+ # The engine, from both sides: recorded as a dependency, and present as a
185
+ # sibling. Either one alone is a prebuild that fails to load.
186
+ engine_path = os.path.join(os.path.dirname(os.path.abspath(path)), ENGINE)
187
+ if ENGINE not in needed:
188
+ problems.append(
189
+ f"no DT_NEEDED on {ENGINE} — sherpa-onnx was linked against a STATIC "
190
+ "onnxruntime, which corrupts the heap on the first inference session "
191
+ "(meson.build § onnxruntime)"
192
+ )
193
+ if not os.path.exists(engine_path):
194
+ problems.append(
195
+ f"{ENGINE} is not beside the library. meson.build copies it into the build "
196
+ "directory so that scripts/stage-prebuild.mjs picks it up; without the "
197
+ "sibling, RUNPATH $ORIGIN has nothing to resolve"
198
+ )
199
+ else:
200
+ problems += check_engine(engine_path, require_stripped_engine)
201
+
202
+ for p in problems:
203
+ print(f" FAIL: {p}")
204
+ return 1 if problems else 0
205
+
206
+
207
+ if __name__ == "__main__":
208
+ args = [a for a in sys.argv[1:] if not a.startswith("--")]
209
+ sys.exit(main(
210
+ args[0],
211
+ "--require-static-libstdcxx" in sys.argv[1:],
212
+ "--require-stripped-engine" in sys.argv[1:],
213
+ ))
@@ -0,0 +1,146 @@
1
+ /*
2
+ * shim-smoke.c — the engines are linked in and they answer.
3
+ *
4
+ * This is deliberately a MODEL-LESS test, because that is the only kind a CI
5
+ * runner can have: the models are hundreds of megabytes under licences this
6
+ * repo may not redistribute (ADR 0127 decision 14). What is still worth
7
+ * asserting without one is most of what goes wrong in a build like this:
8
+ *
9
+ * • the static onnxruntime is actually in the image and initialises — a
10
+ * version string proves the library linked rather than merely compiled;
11
+ * • a missing model is REFUSED BY PATH, not by segfault, which is the failure
12
+ * every user of this package will hit first;
13
+ * • repeating that refusal leaks nothing (valgrind, when it is present);
14
+ * • the VAD lifecycle is reachable, including the empty-queue answers that a
15
+ * caller's loop depends on before any audio arrives.
16
+ *
17
+ * Linked against the shim's OBJECTS rather than the shared library, so the
18
+ * version script (which exists to hide exactly these engines) does not stand
19
+ * between the test and what it measures.
20
+ */
21
+
22
+ #include <glib.h>
23
+ #include <string.h>
24
+
25
+ #include "gjsify-sherpa-shim.h"
26
+
27
+ static void
28
+ test_versions (void)
29
+ {
30
+ const char *sherpa = gjsify_sherpa_shim_version ();
31
+ const char *ort = gjsify_sherpa_shim_onnxruntime_version ();
32
+
33
+ /* Not an equality check against a hardcoded "2.0.1": the wrap file is the
34
+ * statement of which revision this is, and duplicating it here would make
35
+ * every bump touch two places. That it is non-empty is what proves the
36
+ * library linked. */
37
+ g_assert_nonnull (sherpa);
38
+ g_assert_cmpuint (strlen (sherpa), >, 0);
39
+
40
+ g_assert_nonnull (ort);
41
+ g_assert_cmpuint (strlen (ort), >, 0);
42
+
43
+ g_test_message ("sherpa-onnx %s, onnxruntime %s", sherpa, ort);
44
+ }
45
+
46
+ static void
47
+ test_file_exists (void)
48
+ {
49
+ g_assert_cmpint (gjsify_sherpa_shim_file_exists ("/nonexistent/silero_vad.onnx"), ==, 0);
50
+
51
+ /* Something that certainly exists, to show the function is not simply
52
+ * answering 0 to everything — which is how a broken check would look. */
53
+ g_assert_cmpint (gjsify_sherpa_shim_file_exists ("/proc/self/exe"), !=, 0);
54
+ }
55
+
56
+ static void
57
+ test_vad_missing_model (void)
58
+ {
59
+ /* Twenty times, because the interesting part is that the refusal path frees
60
+ * what it allocated: under valgrind (meson.build) a leak per attempt shows
61
+ * up here, and a caller retrying a download is exactly this loop. */
62
+ for (int i = 0; i < 20; i++) {
63
+ GjsifySherpaShimVad *vad = gjsify_sherpa_shim_vad_open (
64
+ "/nonexistent/silero_vad.onnx",
65
+ 0.5f, 0.3f, 0.25f, 20.0f, 512, 16000, 1, 30.0f, 0);
66
+ g_assert_null (vad);
67
+ }
68
+ }
69
+
70
+ static void
71
+ test_recognizer_missing_model (void)
72
+ {
73
+ GjsifySherpaShimOnline *online = gjsify_sherpa_shim_online_open (
74
+ "/nonexistent/encoder.onnx", "/nonexistent/decoder.onnx",
75
+ "/nonexistent/joiner.onnx", "/nonexistent/tokens.txt",
76
+ NULL, "cpu", 1, "greedy_search", 4, 1.5f, 0.0f,
77
+ 1, 2.4f, 1.2f, 20.0f, 16000, 80, 0);
78
+ g_assert_null (online);
79
+
80
+ GjsifySherpaShimOffline *offline = gjsify_sherpa_shim_offline_open (
81
+ "/nonexistent/encoder.onnx", "/nonexistent/decoder.onnx",
82
+ "/nonexistent/joiner.onnx", "/nonexistent/tokens.txt",
83
+ NULL, "cpu", 1, "greedy_search", 4, 1.5f, 0.0f, 16000, 80, 0);
84
+ g_assert_null (offline);
85
+ }
86
+
87
+ /*
88
+ * The real VAD, when a model is available.
89
+ *
90
+ * Skipped rather than omitted: a leg that is simply not registered is a gap
91
+ * nobody can see in a test report. GJSIFY_SHERPA_VAD_MODEL is how a developer
92
+ * with `silero_vad.onnx` on disk — or the scheduled job that fetches one — runs
93
+ * the part that needs weights.
94
+ */
95
+ static void
96
+ test_vad_with_model (void)
97
+ {
98
+ const char *model = g_getenv ("GJSIFY_SHERPA_VAD_MODEL");
99
+
100
+ if (model == NULL || gjsify_sherpa_shim_file_exists (model) == 0) {
101
+ g_test_skip ("set GJSIFY_SHERPA_VAD_MODEL to a silero_vad.onnx to run this");
102
+ return;
103
+ }
104
+
105
+ GjsifySherpaShimVad *vad = gjsify_sherpa_shim_vad_open (
106
+ model, 0.5f, 0.3f, 0.25f, 20.0f, 512, 16000, 1, 30.0f, 0);
107
+ g_assert_nonnull (vad);
108
+
109
+ /* Nothing fed yet: not in speech, and no completed segment queued. Both are
110
+ * what a caller's loop asks before the first buffer arrives. */
111
+ g_assert_cmpint (gjsify_sherpa_shim_vad_is_speech (vad), ==, 0);
112
+ g_assert_cmpint (gjsify_sherpa_shim_vad_is_empty (vad), !=, 0);
113
+
114
+ gint64 start = -1;
115
+ g_assert_cmpint (gjsify_sherpa_shim_vad_peek (vad, &start), ==, -1);
116
+
117
+ /* Two seconds of silence, handed over in a length that is NOT a multiple of
118
+ * the 512-sample window — the detector buffers the remainder, and a capture
119
+ * source's buffers do not line up with it either. */
120
+ float *silence = g_new0 (float, 700);
121
+ for (int i = 0; i < 46; i++) {
122
+ gjsify_sherpa_shim_vad_accept (vad, silence, 700);
123
+ }
124
+ g_free (silence);
125
+
126
+ /* Silence is not speech, and silence produces no segment. */
127
+ g_assert_cmpint (gjsify_sherpa_shim_vad_is_speech (vad), ==, 0);
128
+ g_assert_cmpint (gjsify_sherpa_shim_vad_is_empty (vad), !=, 0);
129
+
130
+ gjsify_sherpa_shim_vad_reset (vad);
131
+ gjsify_sherpa_shim_vad_close (vad);
132
+ }
133
+
134
+ int
135
+ main (int argc, char **argv)
136
+ {
137
+ g_test_init (&argc, &argv, NULL);
138
+
139
+ g_test_add_func ("/sherpa/versions", test_versions);
140
+ g_test_add_func ("/sherpa/file-exists", test_file_exists);
141
+ g_test_add_func ("/sherpa/vad/missing-model", test_vad_missing_model);
142
+ g_test_add_func ("/sherpa/recognizer/missing-model", test_recognizer_missing_model);
143
+ g_test_add_func ("/sherpa/vad/with-model", test_vad_with_model);
144
+
145
+ return g_test_run ();
146
+ }
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env -S gjs -m
2
+ /*
3
+ * vad-model.gjs.js — the one test that LOADS A REAL MODEL, and the regression
4
+ * guard for the worst bug this package has had.
5
+ *
6
+ * Why it is worth a test of its own, with a model, when nothing else here needs
7
+ * one: `Vad.open()` builds an onnxruntime inference session, and that call used
8
+ * to abort with `free(): invalid pointer`. Not on a bad model and not
9
+ * intermittently — every time, with a correct model, on a build that compiled,
10
+ * linked and passed every other test in this directory. The cause was two
11
+ * static archives built by different GCCs defining the same weak
12
+ * `std::__detail::_Scanner<char>` symbols with different layouts
13
+ * (meson.build § onnxruntime). NOTHING SHORT OF OPENING A SESSION SEES IT: the
14
+ * version strings, the error domains, the ELF read-back and the C smoke test
15
+ * were all green throughout.
16
+ *
17
+ * So the guard has to run a session, and a session needs weights this repo may
18
+ * not redistribute (ADR 0127 decision 14). It therefore EXITS 77 — meson's skip
19
+ * code — when no model is cached, which puts a line in the test report. A leg
20
+ * nobody can see in the report is how a gap stays open.
21
+ *
22
+ * Point it at one with GJSIFY_SHERPA_VAD_MODEL, or let it find the model
23
+ * @gjsify/vad downloads into the user cache.
24
+ *
25
+ * NO AUDIO DEVICE, here or anywhere else in this directory: the samples are a
26
+ * Float32Array of zeros.
27
+ */
28
+
29
+ import GLib from 'gi://GLib?version=2.0';
30
+ import GjsifySherpa from 'gi://GjsifySherpa?version=1.0';
31
+ import System from 'system';
32
+
33
+ // The same location @gjsify/vad installs into, so a developer who has run that
34
+ // package's `install()` gets this test for free.
35
+ const cached = GLib.build_filenamev([GLib.get_user_cache_dir(), 'gjsify', 'speech-models', 'silero_vad_v5.onnx']);
36
+ const model = GLib.getenv('GJSIFY_SHERPA_VAD_MODEL') || cached;
37
+
38
+ if (!GjsifySherpa.file_exists(model)) {
39
+ console.log(`no Silero model at ${model}`);
40
+ console.log("set GJSIFY_SHERPA_VAD_MODEL, or run @gjsify/vad's install() to cache one");
41
+ System.exit(77); // meson: skipped
42
+ }
43
+
44
+ let failures = 0;
45
+
46
+ const check = (what, fn) => {
47
+ try {
48
+ fn();
49
+ console.log(`ok ${what}`);
50
+ } catch (error) {
51
+ failures += 1;
52
+ console.log(`FAIL ${what}: ${error.message}`);
53
+ }
54
+ };
55
+
56
+ const assert = (condition, message) => {
57
+ if (!condition) throw new Error(message);
58
+ };
59
+
60
+ console.log(`model ${model}`);
61
+
62
+ // THE REGRESSION GUARD. open() is the call that used to abort the process, so
63
+ // reaching the next line at all is the measurement; the assertions after it are
64
+ // a bonus.
65
+ check('open() builds an inference session', () => {
66
+ const vad = new GjsifySherpa.Vad({ model });
67
+ vad.open();
68
+ assert(vad.is_open === true, 'open() did not report the detector open');
69
+ vad.close();
70
+ assert(vad.is_open === false, 'close() did not report the detector closed');
71
+ });
72
+
73
+ // Reopening, because the crash was in session teardown as much as setup and a
74
+ // single open/close pair can get away with a corrupted heap.
75
+ check('open/close survives three cycles', () => {
76
+ const vad = new GjsifySherpa.Vad({ model });
77
+ for (let i = 0; i < 3; i++) {
78
+ vad.open();
79
+ vad.accept(new Float32Array(512));
80
+ vad.close();
81
+ }
82
+ });
83
+
84
+ check('silence produces no speech and no segment', () => {
85
+ const vad = new GjsifySherpa.Vad({ model });
86
+ vad.open();
87
+ assert(vad.is_speech() === false, 'silence before any audio read as speech');
88
+ assert(vad.has_segment() === false, 'a fresh detector had a segment queued');
89
+
90
+ // A length that is NOT a multiple of the 512-sample window: the detector
91
+ // buffers the remainder, and a capture source's buffers do not line up with
92
+ // it either.
93
+ const chunk = new Float32Array(700);
94
+ for (let i = 0; i < 46; i++) vad.accept(chunk);
95
+
96
+ assert(vad.is_speech() === false, 'two seconds of silence read as speech');
97
+ assert(vad.has_segment() === false, 'silence produced a speech segment');
98
+
99
+ const [got, segment] = vad.next_segment();
100
+ assert(got === false, 'next_segment() claimed a segment for silence');
101
+ assert(segment.length === 0, `next_segment() returned ${segment.length} samples for silence`);
102
+
103
+ vad.flush();
104
+ assert(vad.has_segment() === false, 'flushing silence produced a segment');
105
+
106
+ vad.close();
107
+ });
108
+
109
+ if (failures > 0) {
110
+ console.error(`${failures} check(s) failed`);
111
+ System.exit(1);
112
+ }