@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,575 @@
1
+ /*
2
+ * gjsify-sherpa-shim.c — the only file in this package that knows what
3
+ * sherpa-onnx's structs look like. See gjsify-sherpa-shim.h for why.
4
+ *
5
+ * Every config is `memset` to zero before a field is set, which is what makes
6
+ * the rest of it forward-compatible: a field upstream adds is zero, and zero is
7
+ * the documented "not configured" value for every pointer and every count in
8
+ * these structs. It is also why no field is set from a parameter that is NULL —
9
+ * an empty string and a NULL mean different things to sherpa-onnx in a few
10
+ * places, and zero is the one that reads as absent everywhere.
11
+ */
12
+
13
+ #include "gjsify-sherpa-shim.h"
14
+
15
+ #include <glib.h>
16
+ #include <string.h>
17
+
18
+ #include "sherpa-onnx/c-api/c-api.h"
19
+
20
+ /* ------------------------------------------------------------------------- */
21
+
22
+ const char *
23
+ gjsify_sherpa_shim_version (void)
24
+ {
25
+ return SherpaOnnxGetVersionStr ();
26
+ }
27
+
28
+ const char *
29
+ gjsify_sherpa_shim_onnxruntime_version (void)
30
+ {
31
+ return SherpaOnnxGetOnnxruntimeVersionStr ();
32
+ }
33
+
34
+ int32_t
35
+ gjsify_sherpa_shim_file_exists (const char *filename)
36
+ {
37
+ if (filename == NULL)
38
+ return 0;
39
+ return SherpaOnnxFileExists (filename);
40
+ }
41
+
42
+ /* ------------------------------------------------------------------------- */
43
+ /* VAD */
44
+ /* ------------------------------------------------------------------------- */
45
+
46
+ struct _GjsifySherpaShimVad
47
+ {
48
+ const SherpaOnnxVoiceActivityDetector *vad;
49
+ /* The segment `_peek` fetched and `_read` copies from, owned until `_pop`. */
50
+ const SherpaOnnxSpeechSegment *segment;
51
+ };
52
+
53
+ GjsifySherpaShimVad *
54
+ gjsify_sherpa_shim_vad_open (const char *model,
55
+ float threshold,
56
+ float min_silence_duration,
57
+ float min_speech_duration,
58
+ float max_speech_duration,
59
+ int32_t window_size,
60
+ int32_t sample_rate,
61
+ int32_t num_threads,
62
+ float buffer_size_in_seconds,
63
+ int32_t debug)
64
+ {
65
+ SherpaOnnxVadModelConfig config;
66
+ const SherpaOnnxVoiceActivityDetector *vad;
67
+ GjsifySherpaShimVad *self;
68
+
69
+ if (model == NULL || !SherpaOnnxFileExists (model))
70
+ return NULL;
71
+
72
+ memset (&config, 0, sizeof (config));
73
+ config.silero_vad.model = model;
74
+ config.silero_vad.threshold = threshold;
75
+ config.silero_vad.min_silence_duration = min_silence_duration;
76
+ config.silero_vad.min_speech_duration = min_speech_duration;
77
+ config.silero_vad.max_speech_duration = max_speech_duration;
78
+ config.silero_vad.window_size = window_size;
79
+ config.sample_rate = sample_rate;
80
+ config.num_threads = num_threads;
81
+ config.provider = "cpu";
82
+ config.debug = debug;
83
+
84
+ vad = SherpaOnnxCreateVoiceActivityDetector (&config, buffer_size_in_seconds);
85
+ if (vad == NULL)
86
+ return NULL;
87
+
88
+ self = g_new0 (GjsifySherpaShimVad, 1);
89
+ self->vad = vad;
90
+ return self;
91
+ }
92
+
93
+ void
94
+ gjsify_sherpa_shim_vad_close (GjsifySherpaShimVad *vad)
95
+ {
96
+ if (vad == NULL)
97
+ return;
98
+ if (vad->segment != NULL)
99
+ SherpaOnnxDestroySpeechSegment (vad->segment);
100
+ SherpaOnnxDestroyVoiceActivityDetector (vad->vad);
101
+ g_free (vad);
102
+ }
103
+
104
+ void
105
+ gjsify_sherpa_shim_vad_accept (GjsifySherpaShimVad *vad, const float *samples, int32_t n)
106
+ {
107
+ if (vad == NULL || samples == NULL || n <= 0)
108
+ return;
109
+ SherpaOnnxVoiceActivityDetectorAcceptWaveform (vad->vad, samples, n);
110
+ }
111
+
112
+ int32_t
113
+ gjsify_sherpa_shim_vad_is_speech (GjsifySherpaShimVad *vad)
114
+ {
115
+ return vad == NULL ? 0 : SherpaOnnxVoiceActivityDetectorDetected (vad->vad);
116
+ }
117
+
118
+ int32_t
119
+ gjsify_sherpa_shim_vad_is_empty (GjsifySherpaShimVad *vad)
120
+ {
121
+ return vad == NULL ? 1 : SherpaOnnxVoiceActivityDetectorEmpty (vad->vad);
122
+ }
123
+
124
+ void
125
+ gjsify_sherpa_shim_vad_flush (GjsifySherpaShimVad *vad)
126
+ {
127
+ if (vad != NULL)
128
+ SherpaOnnxVoiceActivityDetectorFlush (vad->vad);
129
+ }
130
+
131
+ void
132
+ gjsify_sherpa_shim_vad_clear (GjsifySherpaShimVad *vad)
133
+ {
134
+ if (vad == NULL)
135
+ return;
136
+ if (vad->segment != NULL)
137
+ {
138
+ SherpaOnnxDestroySpeechSegment (vad->segment);
139
+ vad->segment = NULL;
140
+ }
141
+ SherpaOnnxVoiceActivityDetectorClear (vad->vad);
142
+ }
143
+
144
+ void
145
+ gjsify_sherpa_shim_vad_reset (GjsifySherpaShimVad *vad)
146
+ {
147
+ if (vad == NULL)
148
+ return;
149
+ if (vad->segment != NULL)
150
+ {
151
+ SherpaOnnxDestroySpeechSegment (vad->segment);
152
+ vad->segment = NULL;
153
+ }
154
+ SherpaOnnxVoiceActivityDetectorReset (vad->vad);
155
+ }
156
+
157
+ int32_t
158
+ gjsify_sherpa_shim_vad_peek (GjsifySherpaShimVad *vad, int64_t *start)
159
+ {
160
+ if (start != NULL)
161
+ *start = -1;
162
+
163
+ if (vad == NULL)
164
+ return -1;
165
+
166
+ /* An un-popped segment from a previous peek would leak; the queue head is the
167
+ * only thing this answers about, so re-peeking means re-reading the head. */
168
+ if (vad->segment != NULL)
169
+ {
170
+ SherpaOnnxDestroySpeechSegment (vad->segment);
171
+ vad->segment = NULL;
172
+ }
173
+
174
+ if (SherpaOnnxVoiceActivityDetectorEmpty (vad->vad))
175
+ return -1;
176
+
177
+ vad->segment = SherpaOnnxVoiceActivityDetectorFront (vad->vad);
178
+ if (vad->segment == NULL)
179
+ return -1;
180
+
181
+ if (start != NULL)
182
+ *start = vad->segment->start;
183
+
184
+ return vad->segment->n;
185
+ }
186
+
187
+ void
188
+ gjsify_sherpa_shim_vad_read (GjsifySherpaShimVad *vad, float *dest, int32_t n)
189
+ {
190
+ if (vad == NULL || vad->segment == NULL || dest == NULL || n <= 0)
191
+ return;
192
+ if (n > vad->segment->n)
193
+ n = vad->segment->n;
194
+ memcpy (dest, vad->segment->samples, (size_t) n * sizeof (float));
195
+ }
196
+
197
+ void
198
+ gjsify_sherpa_shim_vad_pop (GjsifySherpaShimVad *vad)
199
+ {
200
+ if (vad == NULL)
201
+ return;
202
+ if (vad->segment != NULL)
203
+ {
204
+ SherpaOnnxDestroySpeechSegment (vad->segment);
205
+ vad->segment = NULL;
206
+ }
207
+ SherpaOnnxVoiceActivityDetectorPop (vad->vad);
208
+ }
209
+
210
+ /* ------------------------------------------------------------------------- */
211
+ /* Online recognizer */
212
+ /* ------------------------------------------------------------------------- */
213
+
214
+ struct _GjsifySherpaShimOnline
215
+ {
216
+ const SherpaOnnxOnlineRecognizer *recognizer;
217
+ };
218
+
219
+ struct _GjsifySherpaShimOnlineStream
220
+ {
221
+ const SherpaOnnxOnlineRecognizer *recognizer; /* borrowed from the owner */
222
+ const SherpaOnnxOnlineStream *stream;
223
+ };
224
+
225
+ /** Every transducer leg needs all four files; name the first one missing. */
226
+ static gboolean
227
+ transducer_files_present (const char *encoder,
228
+ const char *decoder,
229
+ const char *joiner,
230
+ const char *tokens)
231
+ {
232
+ return SherpaOnnxFileExists (encoder) && SherpaOnnxFileExists (decoder) &&
233
+ SherpaOnnxFileExists (joiner) && SherpaOnnxFileExists (tokens);
234
+ }
235
+
236
+ GjsifySherpaShimOnline *
237
+ gjsify_sherpa_shim_online_open (const char *encoder,
238
+ const char *decoder,
239
+ const char *joiner,
240
+ const char *tokens,
241
+ const char *model_type,
242
+ const char *provider,
243
+ int32_t num_threads,
244
+ const char *decoding_method,
245
+ int32_t max_active_paths,
246
+ float hotwords_score,
247
+ float blank_penalty,
248
+ int32_t enable_endpoint,
249
+ float rule1_min_trailing_silence,
250
+ float rule2_min_trailing_silence,
251
+ float rule3_min_utterance_length,
252
+ int32_t sample_rate,
253
+ int32_t feature_dim,
254
+ int32_t debug)
255
+ {
256
+ SherpaOnnxOnlineRecognizerConfig config;
257
+ const SherpaOnnxOnlineRecognizer *recognizer;
258
+ GjsifySherpaShimOnline *self;
259
+
260
+ if (encoder == NULL || decoder == NULL || joiner == NULL || tokens == NULL)
261
+ return NULL;
262
+ if (!transducer_files_present (encoder, decoder, joiner, tokens))
263
+ return NULL;
264
+
265
+ memset (&config, 0, sizeof (config));
266
+ config.feat_config.sample_rate = sample_rate;
267
+ config.feat_config.feature_dim = feature_dim;
268
+ config.model_config.transducer.encoder = encoder;
269
+ config.model_config.transducer.decoder = decoder;
270
+ config.model_config.transducer.joiner = joiner;
271
+ config.model_config.tokens = tokens;
272
+ config.model_config.num_threads = num_threads;
273
+ config.model_config.provider = provider;
274
+ config.model_config.model_type = model_type;
275
+ config.model_config.debug = debug;
276
+ config.decoding_method = decoding_method;
277
+ config.max_active_paths = max_active_paths;
278
+ config.enable_endpoint = enable_endpoint;
279
+ config.rule1_min_trailing_silence = rule1_min_trailing_silence;
280
+ config.rule2_min_trailing_silence = rule2_min_trailing_silence;
281
+ config.rule3_min_utterance_length = rule3_min_utterance_length;
282
+ config.hotwords_score = hotwords_score;
283
+ config.blank_penalty = blank_penalty;
284
+
285
+ recognizer = SherpaOnnxCreateOnlineRecognizer (&config);
286
+ if (recognizer == NULL)
287
+ return NULL;
288
+
289
+ self = g_new0 (GjsifySherpaShimOnline, 1);
290
+ self->recognizer = recognizer;
291
+ return self;
292
+ }
293
+
294
+ void
295
+ gjsify_sherpa_shim_online_close (GjsifySherpaShimOnline *recognizer)
296
+ {
297
+ if (recognizer == NULL)
298
+ return;
299
+ SherpaOnnxDestroyOnlineRecognizer (recognizer->recognizer);
300
+ g_free (recognizer);
301
+ }
302
+
303
+ GjsifySherpaShimOnlineStream *
304
+ gjsify_sherpa_shim_online_stream_open (GjsifySherpaShimOnline *recognizer,
305
+ const char *hotwords)
306
+ {
307
+ const SherpaOnnxOnlineStream *stream;
308
+ GjsifySherpaShimOnlineStream *self;
309
+
310
+ if (recognizer == NULL)
311
+ return NULL;
312
+
313
+ stream = hotwords != NULL
314
+ ? SherpaOnnxCreateOnlineStreamWithHotwords (recognizer->recognizer, hotwords)
315
+ : SherpaOnnxCreateOnlineStream (recognizer->recognizer);
316
+ if (stream == NULL)
317
+ return NULL;
318
+
319
+ self = g_new0 (GjsifySherpaShimOnlineStream, 1);
320
+ self->recognizer = recognizer->recognizer;
321
+ self->stream = stream;
322
+ return self;
323
+ }
324
+
325
+ void
326
+ gjsify_sherpa_shim_online_stream_close (GjsifySherpaShimOnlineStream *stream)
327
+ {
328
+ if (stream == NULL)
329
+ return;
330
+ SherpaOnnxDestroyOnlineStream (stream->stream);
331
+ g_free (stream);
332
+ }
333
+
334
+ void
335
+ gjsify_sherpa_shim_online_stream_accept (GjsifySherpaShimOnlineStream *stream,
336
+ int32_t sample_rate,
337
+ const float *samples,
338
+ int32_t n)
339
+ {
340
+ if (stream == NULL || samples == NULL || n <= 0)
341
+ return;
342
+ SherpaOnnxOnlineStreamAcceptWaveform (stream->stream, sample_rate, samples, n);
343
+ }
344
+
345
+ void
346
+ gjsify_sherpa_shim_online_stream_input_finished (GjsifySherpaShimOnlineStream *stream)
347
+ {
348
+ if (stream != NULL)
349
+ SherpaOnnxOnlineStreamInputFinished (stream->stream);
350
+ }
351
+
352
+ int32_t
353
+ gjsify_sherpa_shim_online_stream_is_ready (GjsifySherpaShimOnlineStream *stream)
354
+ {
355
+ if (stream == NULL)
356
+ return 0;
357
+ return SherpaOnnxIsOnlineStreamReady (stream->recognizer, stream->stream);
358
+ }
359
+
360
+ int32_t
361
+ gjsify_sherpa_shim_online_stream_is_endpoint (GjsifySherpaShimOnlineStream *stream)
362
+ {
363
+ if (stream == NULL)
364
+ return 0;
365
+ return SherpaOnnxOnlineStreamIsEndpoint (stream->recognizer, stream->stream);
366
+ }
367
+
368
+ void
369
+ gjsify_sherpa_shim_online_stream_decode (GjsifySherpaShimOnlineStream *stream)
370
+ {
371
+ if (stream != NULL)
372
+ SherpaOnnxDecodeOnlineStream (stream->recognizer, stream->stream);
373
+ }
374
+
375
+ void
376
+ gjsify_sherpa_shim_online_stream_reset (GjsifySherpaShimOnlineStream *stream)
377
+ {
378
+ if (stream != NULL)
379
+ SherpaOnnxOnlineStreamReset (stream->recognizer, stream->stream);
380
+ }
381
+
382
+ char *
383
+ gjsify_sherpa_shim_online_stream_text (GjsifySherpaShimOnlineStream *stream)
384
+ {
385
+ const SherpaOnnxOnlineRecognizerResult *result;
386
+ char *text;
387
+
388
+ if (stream == NULL)
389
+ return NULL;
390
+
391
+ result = SherpaOnnxGetOnlineStreamResult (stream->recognizer, stream->stream);
392
+ if (result == NULL)
393
+ return NULL;
394
+
395
+ text = g_strdup (result->text != NULL ? result->text : "");
396
+ SherpaOnnxDestroyOnlineRecognizerResult (result);
397
+ return text;
398
+ }
399
+
400
+ char *
401
+ gjsify_sherpa_shim_online_stream_json (GjsifySherpaShimOnlineStream *stream)
402
+ {
403
+ const char *json;
404
+ char *copy;
405
+
406
+ if (stream == NULL)
407
+ return NULL;
408
+
409
+ json = SherpaOnnxGetOnlineStreamResultAsJson (stream->recognizer, stream->stream);
410
+ if (json == NULL)
411
+ return NULL;
412
+
413
+ copy = g_strdup (json);
414
+ SherpaOnnxDestroyOnlineStreamResultJson (json);
415
+ return copy;
416
+ }
417
+
418
+ /* ------------------------------------------------------------------------- */
419
+ /* Offline recognizer */
420
+ /* ------------------------------------------------------------------------- */
421
+
422
+ struct _GjsifySherpaShimOffline
423
+ {
424
+ const SherpaOnnxOfflineRecognizer *recognizer;
425
+ };
426
+
427
+ struct _GjsifySherpaShimOfflineStream
428
+ {
429
+ const SherpaOnnxOfflineRecognizer *recognizer; /* borrowed from the owner */
430
+ const SherpaOnnxOfflineStream *stream;
431
+ };
432
+
433
+ GjsifySherpaShimOffline *
434
+ gjsify_sherpa_shim_offline_open (const char *encoder,
435
+ const char *decoder,
436
+ const char *joiner,
437
+ const char *tokens,
438
+ const char *model_type,
439
+ const char *provider,
440
+ int32_t num_threads,
441
+ const char *decoding_method,
442
+ int32_t max_active_paths,
443
+ float hotwords_score,
444
+ float blank_penalty,
445
+ int32_t sample_rate,
446
+ int32_t feature_dim,
447
+ int32_t debug)
448
+ {
449
+ SherpaOnnxOfflineRecognizerConfig config;
450
+ const SherpaOnnxOfflineRecognizer *recognizer;
451
+ GjsifySherpaShimOffline *self;
452
+
453
+ if (encoder == NULL || decoder == NULL || joiner == NULL || tokens == NULL)
454
+ return NULL;
455
+ if (!transducer_files_present (encoder, decoder, joiner, tokens))
456
+ return NULL;
457
+
458
+ memset (&config, 0, sizeof (config));
459
+ config.feat_config.sample_rate = sample_rate;
460
+ config.feat_config.feature_dim = feature_dim;
461
+ config.model_config.transducer.encoder = encoder;
462
+ config.model_config.transducer.decoder = decoder;
463
+ config.model_config.transducer.joiner = joiner;
464
+ config.model_config.tokens = tokens;
465
+ config.model_config.num_threads = num_threads;
466
+ config.model_config.provider = provider;
467
+ config.model_config.model_type = model_type;
468
+ config.model_config.debug = debug;
469
+ config.decoding_method = decoding_method;
470
+ config.max_active_paths = max_active_paths;
471
+ config.hotwords_score = hotwords_score;
472
+ config.blank_penalty = blank_penalty;
473
+
474
+ recognizer = SherpaOnnxCreateOfflineRecognizer (&config);
475
+ if (recognizer == NULL)
476
+ return NULL;
477
+
478
+ self = g_new0 (GjsifySherpaShimOffline, 1);
479
+ self->recognizer = recognizer;
480
+ return self;
481
+ }
482
+
483
+ void
484
+ gjsify_sherpa_shim_offline_close (GjsifySherpaShimOffline *recognizer)
485
+ {
486
+ if (recognizer == NULL)
487
+ return;
488
+ SherpaOnnxDestroyOfflineRecognizer (recognizer->recognizer);
489
+ g_free (recognizer);
490
+ }
491
+
492
+ GjsifySherpaShimOfflineStream *
493
+ gjsify_sherpa_shim_offline_stream_open (GjsifySherpaShimOffline *recognizer,
494
+ const char *hotwords)
495
+ {
496
+ const SherpaOnnxOfflineStream *stream;
497
+ GjsifySherpaShimOfflineStream *self;
498
+
499
+ if (recognizer == NULL)
500
+ return NULL;
501
+
502
+ stream = hotwords != NULL
503
+ ? SherpaOnnxCreateOfflineStreamWithHotwords (recognizer->recognizer, hotwords)
504
+ : SherpaOnnxCreateOfflineStream (recognizer->recognizer);
505
+ if (stream == NULL)
506
+ return NULL;
507
+
508
+ self = g_new0 (GjsifySherpaShimOfflineStream, 1);
509
+ self->recognizer = recognizer->recognizer;
510
+ self->stream = stream;
511
+ return self;
512
+ }
513
+
514
+ void
515
+ gjsify_sherpa_shim_offline_stream_close (GjsifySherpaShimOfflineStream *stream)
516
+ {
517
+ if (stream == NULL)
518
+ return;
519
+ SherpaOnnxDestroyOfflineStream (stream->stream);
520
+ g_free (stream);
521
+ }
522
+
523
+ void
524
+ gjsify_sherpa_shim_offline_stream_accept (GjsifySherpaShimOfflineStream *stream,
525
+ int32_t sample_rate,
526
+ const float *samples,
527
+ int32_t n)
528
+ {
529
+ if (stream == NULL || samples == NULL || n <= 0)
530
+ return;
531
+ SherpaOnnxAcceptWaveformOffline (stream->stream, sample_rate, samples, n);
532
+ }
533
+
534
+ void
535
+ gjsify_sherpa_shim_offline_stream_decode (GjsifySherpaShimOfflineStream *stream)
536
+ {
537
+ if (stream != NULL)
538
+ SherpaOnnxDecodeOfflineStream (stream->recognizer, stream->stream);
539
+ }
540
+
541
+ char *
542
+ gjsify_sherpa_shim_offline_stream_text (GjsifySherpaShimOfflineStream *stream)
543
+ {
544
+ const SherpaOnnxOfflineRecognizerResult *result;
545
+ char *text;
546
+
547
+ if (stream == NULL)
548
+ return NULL;
549
+
550
+ result = SherpaOnnxGetOfflineStreamResult (stream->stream);
551
+ if (result == NULL)
552
+ return NULL;
553
+
554
+ text = g_strdup (result->text != NULL ? result->text : "");
555
+ SherpaOnnxDestroyOfflineRecognizerResult (result);
556
+ return text;
557
+ }
558
+
559
+ char *
560
+ gjsify_sherpa_shim_offline_stream_json (GjsifySherpaShimOfflineStream *stream)
561
+ {
562
+ const char *json;
563
+ char *copy;
564
+
565
+ if (stream == NULL)
566
+ return NULL;
567
+
568
+ json = SherpaOnnxGetOfflineStreamResultAsJson (stream->stream);
569
+ if (json == NULL)
570
+ return NULL;
571
+
572
+ copy = g_strdup (json);
573
+ SherpaOnnxDestroyOfflineStreamResultJson (json);
574
+ return copy;
575
+ }