@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,95 @@
1
+ // @gjsify/sherpa-native — thin TS wrapper around the GjsifySherpa GIR module.
2
+ //
3
+ // The real implementation lives in src/vala/*.vala and src/c/*.c, compiled to
4
+ // prebuilds/<platform>/libgjsifysherpa.so + GjsifySherpa-1.0.typelib by meson,
5
+ // with sherpa-onnx linked in statically and `libonnxruntime.so` staged beside
6
+ // the library (meson.build § onnxruntime says why that one cannot be static).
7
+ //
8
+ // Loading is intentionally try/catch — the same pattern as
9
+ // @gjsify/sqlite-native — so consuming packages (`@gjsify/vad`) can fall back
10
+ // gracefully when the prebuild is not installed (e.g. an architecture this
11
+ // package has not been built for yet). Callers MUST check `hasNativeSherpa()`
12
+ // before using the module.
13
+ //
14
+ // NO MODEL PATH IS DEFAULTED HERE. Every recognizer and the detector take the
15
+ // files from the caller, because this package ships none (ADR 0127 decision
16
+ // 14); `@gjsify/vad` is the layer that knows where a model cache lives.
17
+
18
+ // Types provided by the auto-generated declaration in src/ts/gjsifysherpa-1.0.d.ts
19
+ import type { GjsifySherpa as GjsifySherpaNS } from 'gi://GjsifySherpa?version=1.0';
20
+ import { loadOptionalNativeModule } from '@gjsify/utils/core';
21
+
22
+ type VadInstance = GjsifySherpaNS.Vad;
23
+ type VadStatic = typeof GjsifySherpaNS.Vad;
24
+ type OnlineRecognizerInstance = GjsifySherpaNS.OnlineRecognizer;
25
+ type OnlineRecognizerStatic = typeof GjsifySherpaNS.OnlineRecognizer;
26
+ type OnlineStreamInstance = GjsifySherpaNS.OnlineStream;
27
+ type OfflineRecognizerInstance = GjsifySherpaNS.OfflineRecognizer;
28
+ type OfflineRecognizerStatic = typeof GjsifySherpaNS.OfflineRecognizer;
29
+ type OfflineStreamInstance = GjsifySherpaNS.OfflineStream;
30
+
31
+ /**
32
+ * The module as `imports.gi.GjsifySherpa` actually presents it.
33
+ *
34
+ * The free functions keep their C names: GJS camel-cases METHODS but not
35
+ * namespace-level functions, so `onnxruntime_version` and `file_exists` are
36
+ * what the typelib hands over — verified against the built library, not
37
+ * assumed.
38
+ */
39
+ export interface NativeSherpaModule {
40
+ Vad: VadStatic;
41
+ OnlineRecognizer: OnlineRecognizerStatic;
42
+ OfflineRecognizer: OfflineRecognizerStatic;
43
+ /** The statically linked sherpa-onnx version, e.g. `2.0.1`. */
44
+ version(): string;
45
+ /** The onnxruntime version sherpa-onnx was built against, e.g. `1.28.2`. */
46
+ onnxruntime_version(): string;
47
+ /** Whether a path is readable, as sherpa-onnx itself would judge it. */
48
+ file_exists(path: string): boolean;
49
+ }
50
+
51
+ export type { VadInstance as Vad };
52
+ export type { OnlineRecognizerInstance as OnlineRecognizer };
53
+ export type { OnlineStreamInstance as OnlineStream };
54
+ export type { OfflineRecognizerInstance as OfflineRecognizer };
55
+ export type { OfflineStreamInstance as OfflineStream };
56
+
57
+ let _native: NativeSherpaModule | null = null;
58
+ let _loaded = false;
59
+ let _loadError: Error | null = null;
60
+
61
+ /**
62
+ * Lazily load the GjsifySherpa typelib. Idempotent — subsequent calls
63
+ * return the cached module (or `null` if the first call failed).
64
+ */
65
+ export function loadNativeSherpa(): NativeSherpaModule | null {
66
+ if (_loaded) return _native;
67
+ _loaded = true;
68
+ // Static `gi://` resolution would happen during ESM linking, before
69
+ // LD_LIBRARY_PATH / GI_TYPELIB_PATH are set; `imports.gi.*` resolves at
70
+ // runtime. A typelib without a loadable library reads as absent rather
71
+ // than failing at first use, with the missing dependency in `getLoadError()`.
72
+ const { module, error } = loadOptionalNativeModule<NativeSherpaModule>('GjsifySherpa', [
73
+ 'Vad',
74
+ 'OnlineRecognizer',
75
+ 'OfflineRecognizer',
76
+ ]);
77
+ _native = module;
78
+ _loadError = error;
79
+ return _native;
80
+ }
81
+
82
+ /**
83
+ * Returns `true` if the GjsifySherpa typelib is loadable (prebuild present).
84
+ */
85
+ export function hasNativeSherpa(): boolean {
86
+ return loadNativeSherpa() !== null;
87
+ }
88
+
89
+ /**
90
+ * Returns the load error from the first `loadNativeSherpa()` call, or `null`
91
+ * if loading succeeded or hasn't been attempted yet.
92
+ */
93
+ export function getLoadError(): Error | null {
94
+ return _loadError;
95
+ }
@@ -0,0 +1,238 @@
1
+ /*
2
+ * GjsifySherpa.OfflineRecognizer — a whole-utterance transducer.
3
+ *
4
+ * "Offline" is sherpa-onnx's word for a model that wants the complete
5
+ * utterance before it decodes; `parakeet-tdt-0.6b-v3`, the multilingual engine
6
+ * ADR 0127 measured, is one. It is the better transcriber and it cannot stream,
7
+ * which is exactly why both kinds are bound here: `@gjsify/vad` cuts the
8
+ * recording into segments, and a segment is precisely what this takes.
9
+ *
10
+ * ONE STREAM DECODES ONCE. There is no reset: the stream holds features, not
11
+ * an incremental decoder state, and a second `decode()` on the same stream
12
+ * decodes the same audio again. A segment gets a fresh stream — they are cheap,
13
+ * since the weights live in the recognizer.
14
+ *
15
+ * var stream = recognizer.create_stream ();
16
+ * stream.accept (16000, segment);
17
+ * stream.decode ();
18
+ * var text = stream.get_text ();
19
+ */
20
+
21
+ namespace GjsifySherpa {
22
+
23
+ public class OfflineRecognizer : GLib.Object {
24
+
25
+ /**
26
+ * The string defaults. valac drops a `default =` on a STRING property —
27
+ * the GParamSpec gets NULL and nothing warns, so every caller coming
28
+ * through GI would read `null`. See
29
+ * {@link OnlineRecognizer.DEFAULT_PROVIDER} for the full note; the
30
+ * `construct` block below is what applies them.
31
+ */
32
+ public const string DEFAULT_PROVIDER = "cpu";
33
+ public const string DEFAULT_DECODING_METHOD = "greedy_search";
34
+
35
+ /** Applies the string defaults, only where the caller supplied nothing. */
36
+ construct {
37
+ if (this.provider == null) {
38
+ this.provider = DEFAULT_PROVIDER;
39
+ }
40
+ if (this.decoding_method == null) {
41
+ this.decoding_method = DEFAULT_DECODING_METHOD;
42
+ }
43
+ }
44
+
45
+ private Shim.Offline? handle = null;
46
+
47
+ /** See {@link OnlineRecognizer.live} — the reference direction is the same. */
48
+ private GLib.List<unowned OfflineStream> live = new GLib.List<unowned OfflineStream> ();
49
+
50
+ /** Path to the transducer encoder. */
51
+ public string encoder { get; construct; }
52
+
53
+ /** Path to the transducer decoder. */
54
+ public string decoder { get; construct; }
55
+
56
+ /** Path to the transducer joiner. */
57
+ public string joiner { get; construct; }
58
+
59
+ /** Path to `tokens.txt`. */
60
+ public string tokens { get; construct; }
61
+
62
+ /**
63
+ * sherpa-onnx's model-type hint; %null lets it autodetect.
64
+ *
65
+ * Worth setting for the NeMo transducers, where it decides which
66
+ * normalisation the features get: `nemo_transducer` for parakeet.
67
+ */
68
+ public string? model_type { get; construct set; default = null; }
69
+
70
+ /** onnxruntime execution provider: `cpu`, `cuda`, `coreml`, … */
71
+ public string provider { get; construct set; }
72
+
73
+ public int num_threads { get; construct set; default = 1; }
74
+
75
+ /** `greedy_search` or `modified_beam_search`; the latter takes hotwords. */
76
+ public string decoding_method { get; construct set; }
77
+
78
+ /** Beam width, for `modified_beam_search` only. */
79
+ public int max_active_paths { get; construct set; default = 4; }
80
+
81
+ /** How strongly a hotword is boosted. Only with `modified_beam_search`. */
82
+ public float hotwords_score { get; construct set; default = 1.5f; }
83
+
84
+ /** Added to the blank logit; above 0 makes the model emit fewer blanks. */
85
+ public float blank_penalty { get; construct set; default = 0.0f; }
86
+
87
+ /** Sample rate the feature extractor expects. */
88
+ public int sample_rate { get; construct set; default = 16000; }
89
+
90
+ /** Filterbank dimension. 80 for both zipformer and NeMo transducers. */
91
+ public int feature_dim { get; construct set; default = 80; }
92
+
93
+ public bool enable_debug { get; construct set; default = false; }
94
+
95
+ public bool is_open {
96
+ get { return this.handle != null; }
97
+ }
98
+
99
+ public OfflineRecognizer (string encoder, string decoder, string joiner, string tokens) {
100
+ Object (encoder: encoder, decoder: decoder, joiner: joiner, tokens: tokens);
101
+ }
102
+
103
+ /** Load the weights. parakeet-tdt-0.6b int8 is ~650 MB on disk. */
104
+ public void open () throws SherpaError {
105
+ if (this.handle != null) {
106
+ return;
107
+ }
108
+
109
+ require_file ("encoder", this.encoder);
110
+ require_file ("decoder", this.decoder);
111
+ require_file ("joiner", this.joiner);
112
+ require_file ("tokens", this.tokens);
113
+
114
+ this.handle = Shim.Offline.open (this.encoder,
115
+ this.decoder,
116
+ this.joiner,
117
+ this.tokens,
118
+ this.model_type,
119
+ this.provider,
120
+ this.num_threads,
121
+ this.decoding_method,
122
+ this.max_active_paths,
123
+ this.hotwords_score,
124
+ this.blank_penalty,
125
+ this.sample_rate,
126
+ this.feature_dim,
127
+ this.enable_debug ? 1 : 0);
128
+
129
+ if (this.handle == null) {
130
+ throw new SherpaError.OPEN_FAILED (
131
+ "sherpa-onnx refused the offline recognizer configuration (reason on stderr)");
132
+ }
133
+ }
134
+
135
+ /** Release the weights, closing every live stream first. */
136
+ public void close () {
137
+ GLib.List<unowned OfflineStream> pending = (owned) this.live;
138
+ this.live = new GLib.List<unowned OfflineStream> ();
139
+
140
+ foreach (unowned OfflineStream stream in pending) {
141
+ stream.close ();
142
+ }
143
+
144
+ this.handle = null;
145
+ }
146
+
147
+ internal unowned Shim.Offline require_handle () throws SherpaError {
148
+ if (this.handle == null) {
149
+ throw new SherpaError.NOT_OPEN ("the offline recognizer is not open");
150
+ }
151
+ return this.handle;
152
+ }
153
+
154
+ internal void forget (OfflineStream stream) {
155
+ this.live.remove (stream);
156
+ }
157
+
158
+ /**
159
+ * A stream for one utterance, to be decoded once.
160
+ *
161
+ * @hotwords is a newline-separated contextual-biasing list, or %null;
162
+ * it only has an effect with `modified_beam_search`.
163
+ */
164
+ public OfflineStream create_stream (string? hotwords = null) throws SherpaError {
165
+ var stream = new OfflineStream (this, hotwords);
166
+ this.live.prepend (stream);
167
+ return stream;
168
+ }
169
+ }
170
+
171
+ /**
172
+ * One utterance's features, decoded once.
173
+ *
174
+ * Obtained from {@link OfflineRecognizer.create_stream}. Holds a strong
175
+ * reference on its recognizer.
176
+ */
177
+ public class OfflineStream : GLib.Object {
178
+
179
+ private Shim.OfflineStream? handle = null;
180
+ private OfflineRecognizer? recognizer = null;
181
+
182
+ public bool is_open {
183
+ get { return this.handle != null; }
184
+ }
185
+
186
+ internal OfflineStream (OfflineRecognizer recognizer, string? hotwords) throws SherpaError {
187
+ this.recognizer = recognizer;
188
+ this.handle = Shim.OfflineStream.open (recognizer.require_handle (), hotwords);
189
+
190
+ if (this.handle == null) {
191
+ this.recognizer = null;
192
+ throw new SherpaError.OPEN_FAILED (
193
+ "sherpa-onnx could not create an offline stream (reason on stderr)");
194
+ }
195
+ }
196
+
197
+ /** Release the features. Idempotent. */
198
+ public void close () {
199
+ this.handle = null;
200
+ if (this.recognizer != null) {
201
+ this.recognizer.forget (this);
202
+ this.recognizer = null;
203
+ }
204
+ }
205
+
206
+ private unowned Shim.OfflineStream require_handle () throws SherpaError {
207
+ if (this.handle == null) {
208
+ throw new SherpaError.NOT_OPEN ("the offline stream is closed");
209
+ }
210
+ return this.handle;
211
+ }
212
+
213
+ /**
214
+ * Add mono float samples in [-1, 1] recorded at @sample_rate.
215
+ *
216
+ * May be called repeatedly before {@link decode} — the features
217
+ * accumulate — but not after it.
218
+ */
219
+ public void accept (int sample_rate, float[] samples) throws SherpaError {
220
+ this.require_handle ().accept (sample_rate, samples);
221
+ }
222
+
223
+ /** Decode everything accepted so far. Once per stream. */
224
+ public void decode () throws SherpaError {
225
+ this.require_handle ().decode ();
226
+ }
227
+
228
+ /** The decoded text. Empty before {@link decode}. */
229
+ public string get_text () throws SherpaError {
230
+ return this.require_handle ().text ();
231
+ }
232
+
233
+ /** The full result as JSON: tokens, timestamps, per-token probabilities. */
234
+ public string get_json () throws SherpaError {
235
+ return this.require_handle ().json ();
236
+ }
237
+ }
238
+ }
@@ -0,0 +1,303 @@
1
+ /*
2
+ * GjsifySherpa.OnlineRecognizer — a streaming transducer, decoded as it arrives.
3
+ *
4
+ * "Online" is sherpa-onnx's word for a model that emits a growing hypothesis
5
+ * from a partial utterance; `zipformer-de-kroko`, the German engine ADR 0127
6
+ * measured, is one. The recognizer holds the weights, a stream holds one
7
+ * utterance's decoder state, and many streams may share one recognizer — which
8
+ * is the entire reason the two are separate objects here rather than one.
9
+ *
10
+ * The decode loop a caller runs is sherpa-onnx's own:
11
+ *
12
+ * stream.accept (16000, samples);
13
+ * while (stream.is_ready ()) stream.decode ();
14
+ * var text = stream.get_text (); // interim, grows
15
+ * if (stream.is_endpoint ()) { … ; stream.reset (); }
16
+ *
17
+ * `is_endpoint` is the model's own trailing-silence rule, which is NOT the same
18
+ * measurement as `@gjsify/vad`'s and is not a turn either (ADR 0127 decision 8).
19
+ */
20
+
21
+ namespace GjsifySherpa {
22
+
23
+ public class OnlineRecognizer : GLib.Object {
24
+
25
+ /**
26
+ * Defaults for the STRING properties below, and why they are constants
27
+ * rather than `default =` on the property.
28
+ *
29
+ * valac honours a property's `default =` only for the scalar types: an
30
+ * `int` property lands in its GParamSpec as `1`, a `float` as `1.5`, a
31
+ * `bool` as TRUE — but a STRING property's default is dropped and the
32
+ * spec gets NULL. Nothing warns. So `default = "cpu"` reads correctly in
33
+ * Vala, is inherited correctly by a Vala subclass, and yet
34
+ * `recognizer.provider` is `null` for every caller coming through GI,
35
+ * which is every caller this package has. The `construct` block below is
36
+ * what actually applies them.
37
+ */
38
+ public const string DEFAULT_PROVIDER = "cpu";
39
+ public const string DEFAULT_DECODING_METHOD = "greedy_search";
40
+
41
+ private Shim.Online? handle = null;
42
+
43
+ /**
44
+ * Streams created from this recognizer and not yet closed.
45
+ *
46
+ * UNOWNED, because the strong reference runs the other way: each
47
+ * {@link OnlineStream} holds a ref on its recognizer, so the weights
48
+ * cannot be finalized while a stream can still reach them. This list
49
+ * exists only so {@link close} can shut the streams down FIRST — the
50
+ * shim's stream handles keep the recognizer pointer, and freeing the
51
+ * recognizer under a live stream is a use-after-free.
52
+ */
53
+ private GLib.List<unowned OnlineStream> live = new GLib.List<unowned OnlineStream> ();
54
+
55
+ /** Path to the transducer encoder, e.g. `encoder-epoch-99-avg-1.onnx`. */
56
+ public string encoder { get; construct; }
57
+
58
+ /** Path to the transducer decoder. */
59
+ public string decoder { get; construct; }
60
+
61
+ /** Path to the transducer joiner. */
62
+ public string joiner { get; construct; }
63
+
64
+ /** Path to `tokens.txt`. */
65
+ public string tokens { get; construct; }
66
+
67
+ /** sherpa-onnx's model-type hint; %null lets it autodetect. */
68
+ public string? model_type { get; construct set; default = null; }
69
+
70
+ /** onnxruntime execution provider: `cpu`, `cuda`, `coreml`, … */
71
+ public string provider { get; construct set; }
72
+
73
+ public int num_threads { get; construct set; default = 1; }
74
+
75
+ /** `greedy_search` or `modified_beam_search`; the latter takes hotwords. */
76
+ public string decoding_method { get; construct set; }
77
+
78
+ /** Beam width, for `modified_beam_search` only. */
79
+ public int max_active_paths { get; construct set; default = 4; }
80
+
81
+ /** How strongly a hotword is boosted. Only with `modified_beam_search`. */
82
+ public float hotwords_score { get; construct set; default = 1.5f; }
83
+
84
+ /** Added to the blank logit; above 0 makes the model emit fewer blanks. */
85
+ public float blank_penalty { get; construct set; default = 0.0f; }
86
+
87
+ /** Whether {@link OnlineStream.is_endpoint} is computed at all. */
88
+ public bool enable_endpoint { get; construct set; default = true; }
89
+
90
+ /** Trailing silence that ends an utterance which decoded something. */
91
+ public float rule1_min_trailing_silence { get; construct set; default = 2.4f; }
92
+
93
+ /** Trailing silence that ends an utterance after the last non-blank. */
94
+ public float rule2_min_trailing_silence { get; construct set; default = 1.2f; }
95
+
96
+ /** Utterance length at which an endpoint is forced, in seconds. */
97
+ public float rule3_min_utterance_length { get; construct set; default = 20.0f; }
98
+
99
+ /** Sample rate the feature extractor expects. */
100
+ public int sample_rate { get; construct set; default = 16000; }
101
+
102
+ /** Filterbank dimension. 80 for every zipformer. */
103
+ public int feature_dim { get; construct set; default = 80; }
104
+
105
+ public bool enable_debug { get; construct set; default = false; }
106
+
107
+ public bool is_open {
108
+ get { return this.handle != null; }
109
+ }
110
+
111
+ /**
112
+ * Applies the string defaults, which the GParamSpec cannot carry — see
113
+ * {@link DEFAULT_PROVIDER}.
114
+ *
115
+ * Only where the caller supplied nothing. A `construct` block runs after
116
+ * GObject has assigned the construct properties, so an explicit
117
+ * `provider: "cuda"` is already in place here and must not be
118
+ * overwritten; `null` is the one value that means "not given", which is
119
+ * also why neither property is nullable in its own right.
120
+ */
121
+ construct {
122
+ if (this.provider == null) {
123
+ this.provider = DEFAULT_PROVIDER;
124
+ }
125
+ if (this.decoding_method == null) {
126
+ this.decoding_method = DEFAULT_DECODING_METHOD;
127
+ }
128
+ }
129
+
130
+ public OnlineRecognizer (string encoder, string decoder, string joiner, string tokens) {
131
+ Object (encoder: encoder, decoder: decoder, joiner: joiner, tokens: tokens);
132
+ }
133
+
134
+ /** Load the weights. Loading a transducer takes seconds and ~500 MB. */
135
+ public void open () throws SherpaError {
136
+ if (this.handle != null) {
137
+ return;
138
+ }
139
+
140
+ require_file ("encoder", this.encoder);
141
+ require_file ("decoder", this.decoder);
142
+ require_file ("joiner", this.joiner);
143
+ require_file ("tokens", this.tokens);
144
+
145
+ this.handle = Shim.Online.open (this.encoder,
146
+ this.decoder,
147
+ this.joiner,
148
+ this.tokens,
149
+ this.model_type,
150
+ this.provider,
151
+ this.num_threads,
152
+ this.decoding_method,
153
+ this.max_active_paths,
154
+ this.hotwords_score,
155
+ this.blank_penalty,
156
+ this.enable_endpoint ? 1 : 0,
157
+ this.rule1_min_trailing_silence,
158
+ this.rule2_min_trailing_silence,
159
+ this.rule3_min_utterance_length,
160
+ this.sample_rate,
161
+ this.feature_dim,
162
+ this.enable_debug ? 1 : 0);
163
+
164
+ if (this.handle == null) {
165
+ throw new SherpaError.OPEN_FAILED (
166
+ "sherpa-onnx refused the online recognizer configuration (reason on stderr)");
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Release the weights, closing every live stream first.
172
+ *
173
+ * The order is the point: a shim stream handle holds the recognizer
174
+ * pointer, so the recognizer may not go first. Same arrangement as
175
+ * GjsifySqlite.Database and its statements.
176
+ */
177
+ public void close () {
178
+ // Emptied before the loop so that each stream's forget() becomes a
179
+ // no-op and cannot mutate a list being walked — the same move
180
+ // GjsifySqlite.Database.close makes, for the same reason.
181
+ GLib.List<unowned OnlineStream> pending = (owned) this.live;
182
+ this.live = new GLib.List<unowned OnlineStream> ();
183
+
184
+ foreach (unowned OnlineStream stream in pending) {
185
+ stream.close ();
186
+ }
187
+
188
+ this.handle = null;
189
+ }
190
+
191
+ internal unowned Shim.Online require_handle () throws SherpaError {
192
+ if (this.handle == null) {
193
+ throw new SherpaError.NOT_OPEN ("the online recognizer is not open");
194
+ }
195
+ return this.handle;
196
+ }
197
+
198
+ internal void forget (OnlineStream stream) {
199
+ this.live.remove (stream);
200
+ }
201
+
202
+ /**
203
+ * A stream for one utterance.
204
+ *
205
+ * @hotwords is a newline-separated contextual-biasing list for THIS
206
+ * stream, or %null for none; it only has an effect with
207
+ * {@link decoding_method} set to `modified_beam_search`.
208
+ */
209
+ public OnlineStream create_stream (string? hotwords = null) throws SherpaError {
210
+ var stream = new OnlineStream (this, hotwords);
211
+ this.live.prepend (stream);
212
+ return stream;
213
+ }
214
+ }
215
+
216
+ /**
217
+ * One utterance's decoder state.
218
+ *
219
+ * Obtained from {@link OnlineRecognizer.create_stream}. Holds a strong
220
+ * reference on its recognizer, so a caller may drop the recognizer and keep
221
+ * decoding.
222
+ */
223
+ public class OnlineStream : GLib.Object {
224
+
225
+ private Shim.OnlineStream? handle = null;
226
+ private OnlineRecognizer? recognizer = null;
227
+
228
+ public bool is_open {
229
+ get { return this.handle != null; }
230
+ }
231
+
232
+ internal OnlineStream (OnlineRecognizer recognizer, string? hotwords) throws SherpaError {
233
+ this.recognizer = recognizer;
234
+ this.handle = Shim.OnlineStream.open (recognizer.require_handle (), hotwords);
235
+
236
+ if (this.handle == null) {
237
+ this.recognizer = null;
238
+ throw new SherpaError.OPEN_FAILED (
239
+ "sherpa-onnx could not create an online stream (reason on stderr)");
240
+ }
241
+ }
242
+
243
+ /** Release the decoder state. Idempotent. */
244
+ public void close () {
245
+ this.handle = null;
246
+ if (this.recognizer != null) {
247
+ this.recognizer.forget (this);
248
+ this.recognizer = null;
249
+ }
250
+ }
251
+
252
+ private unowned Shim.OnlineStream require_handle () throws SherpaError {
253
+ if (this.handle == null) {
254
+ throw new SherpaError.NOT_OPEN ("the online stream is closed");
255
+ }
256
+ return this.handle;
257
+ }
258
+
259
+ /** Feed mono float samples in [-1, 1] recorded at @sample_rate. */
260
+ public void accept (int sample_rate, float[] samples) throws SherpaError {
261
+ this.require_handle ().accept (sample_rate, samples);
262
+ }
263
+
264
+ /** No more samples follow: pad the tail so it can be decoded. */
265
+ public void input_finished () throws SherpaError {
266
+ this.require_handle ().input_finished ();
267
+ }
268
+
269
+ /** Whether enough frames are buffered for another {@link decode}. */
270
+ public bool is_ready () throws SherpaError {
271
+ return this.require_handle ().is_ready () != 0;
272
+ }
273
+
274
+ /**
275
+ * Whether the model's trailing-silence rules say the utterance ended.
276
+ *
277
+ * The model's own measurement, not `@gjsify/vad`'s, and not a turn.
278
+ */
279
+ public bool is_endpoint () throws SherpaError {
280
+ return this.require_handle ().is_endpoint () != 0;
281
+ }
282
+
283
+ /** Decode one chunk. Only legal while {@link is_ready}. */
284
+ public void decode () throws SherpaError {
285
+ this.require_handle ().decode ();
286
+ }
287
+
288
+ /** Clear the decoder state after an endpoint, keeping the stream. */
289
+ public void reset () throws SherpaError {
290
+ this.require_handle ().reset ();
291
+ }
292
+
293
+ /** The text decoded so far — interim, and it grows. */
294
+ public string get_text () throws SherpaError {
295
+ return this.require_handle ().text ();
296
+ }
297
+
298
+ /** The full result as JSON: tokens, timestamps, per-token probabilities. */
299
+ public string get_json () throws SherpaError {
300
+ return this.require_handle ().json ();
301
+ }
302
+ }
303
+ }