react-native-sherpa-onnx 0.1.0

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.
Files changed (83) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +402 -0
  3. package/SherpaOnnx.podspec +84 -0
  4. package/android/build.gradle +193 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/cpp/CMakeLists.txt +121 -0
  7. package/android/src/main/cpp/include/sherpa-onnx/c-api/c-api.h +1918 -0
  8. package/android/src/main/cpp/include/sherpa-onnx/c-api/cxx-api.h +841 -0
  9. package/android/src/main/cpp/jni/sherpa-onnx-jni.cpp +129 -0
  10. package/android/src/main/cpp/jni/sherpa-onnx-wrapper.cpp +649 -0
  11. package/android/src/main/cpp/jni/sherpa-onnx-wrapper.h +56 -0
  12. package/android/src/main/java/com/sherpaonnx/SherpaOnnxModule.kt +316 -0
  13. package/android/src/main/java/com/sherpaonnx/SherpaOnnxPackage.kt +33 -0
  14. package/ios/Frameworks/sherpa_onnx.xcframework.zip +0 -0
  15. package/ios/SherpaOnnx.h +5 -0
  16. package/ios/SherpaOnnx.mm +293 -0
  17. package/ios/SherpaOnnx.xcconfig +19 -0
  18. package/ios/include/sherpa-onnx/c-api/c-api.h +1918 -0
  19. package/ios/include/sherpa-onnx/c-api/cxx-api.h +841 -0
  20. package/ios/sherpa-onnx-wrapper.h +57 -0
  21. package/ios/sherpa-onnx-wrapper.mm +432 -0
  22. package/lib/module/NativeSherpaOnnx.js +5 -0
  23. package/lib/module/NativeSherpaOnnx.js.map +1 -0
  24. package/lib/module/diarization/index.js +54 -0
  25. package/lib/module/diarization/index.js.map +1 -0
  26. package/lib/module/enhancement/index.js +54 -0
  27. package/lib/module/enhancement/index.js.map +1 -0
  28. package/lib/module/index.js +25 -0
  29. package/lib/module/index.js.map +1 -0
  30. package/lib/module/package.json +1 -0
  31. package/lib/module/separation/index.js +54 -0
  32. package/lib/module/separation/index.js.map +1 -0
  33. package/lib/module/stt/index.js +79 -0
  34. package/lib/module/stt/index.js.map +1 -0
  35. package/lib/module/stt/types.js +4 -0
  36. package/lib/module/stt/types.js.map +1 -0
  37. package/lib/module/tts/index.js +54 -0
  38. package/lib/module/tts/index.js.map +1 -0
  39. package/lib/module/types.js +2 -0
  40. package/lib/module/types.js.map +1 -0
  41. package/lib/module/utils.js +93 -0
  42. package/lib/module/utils.js.map +1 -0
  43. package/lib/module/vad/index.js +54 -0
  44. package/lib/module/vad/index.js.map +1 -0
  45. package/lib/typescript/package.json +1 -0
  46. package/lib/typescript/src/NativeSherpaOnnx.d.ts +39 -0
  47. package/lib/typescript/src/NativeSherpaOnnx.d.ts.map +1 -0
  48. package/lib/typescript/src/diarization/index.d.ts +49 -0
  49. package/lib/typescript/src/diarization/index.d.ts.map +1 -0
  50. package/lib/typescript/src/enhancement/index.d.ts +47 -0
  51. package/lib/typescript/src/enhancement/index.d.ts.map +1 -0
  52. package/lib/typescript/src/index.d.ts +9 -0
  53. package/lib/typescript/src/index.d.ts.map +1 -0
  54. package/lib/typescript/src/separation/index.d.ts +48 -0
  55. package/lib/typescript/src/separation/index.d.ts.map +1 -0
  56. package/lib/typescript/src/stt/index.d.ts +53 -0
  57. package/lib/typescript/src/stt/index.d.ts.map +1 -0
  58. package/lib/typescript/src/stt/types.d.ts +39 -0
  59. package/lib/typescript/src/stt/types.d.ts.map +1 -0
  60. package/lib/typescript/src/tts/index.d.ts +47 -0
  61. package/lib/typescript/src/tts/index.d.ts.map +1 -0
  62. package/lib/typescript/src/types.d.ts +59 -0
  63. package/lib/typescript/src/types.d.ts.map +1 -0
  64. package/lib/typescript/src/utils.d.ts +53 -0
  65. package/lib/typescript/src/utils.d.ts.map +1 -0
  66. package/lib/typescript/src/vad/index.d.ts +48 -0
  67. package/lib/typescript/src/vad/index.d.ts.map +1 -0
  68. package/package.json +221 -0
  69. package/scripts/copy-headers.js +184 -0
  70. package/scripts/setup-assets.js +323 -0
  71. package/scripts/setup-ios-framework.sh +282 -0
  72. package/scripts/switch-registry.js +75 -0
  73. package/src/NativeSherpaOnnx.ts +44 -0
  74. package/src/diarization/index.ts +69 -0
  75. package/src/enhancement/index.ts +67 -0
  76. package/src/index.tsx +30 -0
  77. package/src/separation/index.ts +68 -0
  78. package/src/stt/index.ts +83 -0
  79. package/src/stt/types.ts +42 -0
  80. package/src/tts/index.ts +67 -0
  81. package/src/types.ts +73 -0
  82. package/src/utils.ts +97 -0
  83. package/src/vad/index.ts +70 -0
@@ -0,0 +1,841 @@
1
+ // sherpa-onnx/c-api/cxx-api.h
2
+ //
3
+ // Copyright (c) 2024 Xiaomi Corporation
4
+
5
+ // C++ Wrapper of the C API for sherpa-onnx
6
+ #ifndef SHERPA_ONNX_C_API_CXX_API_H_
7
+ #define SHERPA_ONNX_C_API_CXX_API_H_
8
+
9
+ #include <memory>
10
+ #include <string>
11
+ #include <vector>
12
+
13
+ #include "sherpa-onnx/c-api/c-api.h"
14
+
15
+ namespace sherpa_onnx::cxx {
16
+
17
+ // ============================================================================
18
+ // Streaming ASR
19
+ // ============================================================================
20
+ struct OnlineTransducerModelConfig {
21
+ std::string encoder;
22
+ std::string decoder;
23
+ std::string joiner;
24
+ };
25
+
26
+ struct OnlineParaformerModelConfig {
27
+ std::string encoder;
28
+ std::string decoder;
29
+ };
30
+
31
+ struct OnlineZipformer2CtcModelConfig {
32
+ std::string model;
33
+ };
34
+
35
+ struct OnlineNemoCtcModelConfig {
36
+ std::string model;
37
+ };
38
+
39
+ struct OnlineToneCtcModelConfig {
40
+ std::string model;
41
+ };
42
+
43
+ struct OnlineModelConfig {
44
+ OnlineTransducerModelConfig transducer;
45
+ OnlineParaformerModelConfig paraformer;
46
+ OnlineZipformer2CtcModelConfig zipformer2_ctc;
47
+ OnlineNemoCtcModelConfig nemo_ctc;
48
+ OnlineToneCtcModelConfig t_one_ctc;
49
+ std::string tokens;
50
+ int32_t num_threads = 1;
51
+ std::string provider = "cpu";
52
+ bool debug = false;
53
+ std::string model_type;
54
+ std::string modeling_unit = "cjkchar";
55
+ std::string bpe_vocab;
56
+ std::string tokens_buf;
57
+ };
58
+
59
+ struct FeatureConfig {
60
+ int32_t sample_rate = 16000;
61
+ int32_t feature_dim = 80;
62
+ };
63
+
64
+ struct OnlineCtcFstDecoderConfig {
65
+ std::string graph;
66
+ int32_t max_active = 3000;
67
+ };
68
+
69
+ struct HomophoneReplacerConfig {
70
+ std::string dict_dir; // unused
71
+ std::string lexicon;
72
+ std::string rule_fsts;
73
+ };
74
+
75
+ struct OnlineRecognizerConfig {
76
+ FeatureConfig feat_config;
77
+ OnlineModelConfig model_config;
78
+
79
+ std::string decoding_method = "greedy_search";
80
+
81
+ int32_t max_active_paths = 4;
82
+
83
+ bool enable_endpoint = false;
84
+
85
+ float rule1_min_trailing_silence = 2.4;
86
+
87
+ float rule2_min_trailing_silence = 1.2;
88
+
89
+ float rule3_min_utterance_length = 20;
90
+
91
+ std::string hotwords_file;
92
+
93
+ float hotwords_score = 1.5;
94
+
95
+ OnlineCtcFstDecoderConfig ctc_fst_decoder_config;
96
+ std::string rule_fsts;
97
+ std::string rule_fars;
98
+ float blank_penalty = 0;
99
+
100
+ std::string hotwords_buf;
101
+ HomophoneReplacerConfig hr;
102
+ };
103
+
104
+ struct OnlineRecognizerResult {
105
+ std::string text;
106
+ std::vector<std::string> tokens;
107
+ std::vector<float> timestamps;
108
+ std::string json;
109
+ };
110
+
111
+ struct Wave {
112
+ std::vector<float> samples;
113
+ int32_t sample_rate;
114
+ };
115
+
116
+ SHERPA_ONNX_API Wave ReadWave(const std::string &filename);
117
+
118
+ // Return true on success;
119
+ // Return false on failure
120
+ SHERPA_ONNX_API bool WriteWave(const std::string &filename, const Wave &wave);
121
+
122
+ template <typename Derived, typename T>
123
+ class SHERPA_ONNX_API MoveOnly {
124
+ public:
125
+ MoveOnly() = default;
126
+ explicit MoveOnly(const T *p) : p_(p) {}
127
+
128
+ ~MoveOnly() { Destroy(); }
129
+
130
+ MoveOnly(const MoveOnly &) = delete;
131
+
132
+ MoveOnly &operator=(const MoveOnly &) = delete;
133
+
134
+ MoveOnly(MoveOnly &&other) : p_(other.Release()) {}
135
+
136
+ MoveOnly &operator=(MoveOnly &&other) {
137
+ if (&other == this) {
138
+ return *this;
139
+ }
140
+
141
+ Destroy();
142
+
143
+ p_ = other.Release();
144
+
145
+ return *this;
146
+ }
147
+
148
+ const T *Get() const { return p_; }
149
+
150
+ const T *Release() {
151
+ const T *p = p_;
152
+ p_ = nullptr;
153
+ return p;
154
+ }
155
+
156
+ private:
157
+ void Destroy() {
158
+ if (p_ == nullptr) {
159
+ return;
160
+ }
161
+
162
+ static_cast<Derived *>(this)->Destroy(p_);
163
+
164
+ p_ = nullptr;
165
+ }
166
+
167
+ protected:
168
+ const T *p_ = nullptr;
169
+ };
170
+
171
+ class SHERPA_ONNX_API OnlineStream
172
+ : public MoveOnly<OnlineStream, SherpaOnnxOnlineStream> {
173
+ public:
174
+ explicit OnlineStream(const SherpaOnnxOnlineStream *p);
175
+
176
+ void AcceptWaveform(int32_t sample_rate, const float *samples,
177
+ int32_t n) const;
178
+
179
+ void InputFinished() const;
180
+
181
+ void Destroy(const SherpaOnnxOnlineStream *p) const;
182
+ };
183
+
184
+ class SHERPA_ONNX_API OnlineRecognizer
185
+ : public MoveOnly<OnlineRecognizer, SherpaOnnxOnlineRecognizer> {
186
+ public:
187
+ static OnlineRecognizer Create(const OnlineRecognizerConfig &config);
188
+
189
+ void Destroy(const SherpaOnnxOnlineRecognizer *p) const;
190
+
191
+ OnlineStream CreateStream() const;
192
+
193
+ OnlineStream CreateStream(const std::string &hotwords) const;
194
+
195
+ bool IsReady(const OnlineStream *s) const;
196
+
197
+ void Decode(const OnlineStream *s) const;
198
+
199
+ void Decode(const OnlineStream *ss, int32_t n) const;
200
+
201
+ OnlineRecognizerResult GetResult(const OnlineStream *s) const;
202
+
203
+ void Reset(const OnlineStream *s) const;
204
+
205
+ bool IsEndpoint(const OnlineStream *s) const;
206
+
207
+ private:
208
+ explicit OnlineRecognizer(const SherpaOnnxOnlineRecognizer *p);
209
+ };
210
+
211
+ // ============================================================================
212
+ // Non-streaming ASR
213
+ // ============================================================================
214
+ struct SHERPA_ONNX_API OfflineTransducerModelConfig {
215
+ std::string encoder;
216
+ std::string decoder;
217
+ std::string joiner;
218
+ };
219
+
220
+ struct SHERPA_ONNX_API OfflineParaformerModelConfig {
221
+ std::string model;
222
+ };
223
+
224
+ struct SHERPA_ONNX_API OfflineNemoEncDecCtcModelConfig {
225
+ std::string model;
226
+ };
227
+
228
+ struct SHERPA_ONNX_API OfflineWhisperModelConfig {
229
+ std::string encoder;
230
+ std::string decoder;
231
+ std::string language;
232
+ std::string task = "transcribe";
233
+ int32_t tail_paddings = -1;
234
+ };
235
+
236
+ struct SHERPA_ONNX_API OfflineCanaryModelConfig {
237
+ std::string encoder;
238
+ std::string decoder;
239
+ std::string src_lang;
240
+ std::string tgt_lang;
241
+ bool use_pnc = true;
242
+ };
243
+
244
+ struct SHERPA_ONNX_API OfflineFireRedAsrModelConfig {
245
+ std::string encoder;
246
+ std::string decoder;
247
+ };
248
+
249
+ struct SHERPA_ONNX_API OfflineTdnnModelConfig {
250
+ std::string model;
251
+ };
252
+
253
+ struct SHERPA_ONNX_API OfflineSenseVoiceModelConfig {
254
+ std::string model;
255
+ std::string language;
256
+ bool use_itn = false;
257
+ };
258
+
259
+ struct SHERPA_ONNX_API OfflineDolphinModelConfig {
260
+ std::string model;
261
+ };
262
+
263
+ struct SHERPA_ONNX_API OfflineZipformerCtcModelConfig {
264
+ std::string model;
265
+ };
266
+
267
+ struct SHERPA_ONNX_API OfflineWenetCtcModelConfig {
268
+ std::string model;
269
+ };
270
+
271
+ struct SHERPA_ONNX_API OfflineOmnilingualAsrCtcModelConfig {
272
+ std::string model;
273
+ };
274
+
275
+ struct SHERPA_ONNX_API OfflineMedAsrCtcModelConfig {
276
+ std::string model;
277
+ };
278
+
279
+ struct SHERPA_ONNX_API OfflineMoonshineModelConfig {
280
+ std::string preprocessor;
281
+ std::string encoder;
282
+ std::string uncached_decoder;
283
+ std::string cached_decoder;
284
+ };
285
+
286
+ struct SHERPA_ONNX_API OfflineFunASRNanoModelConfig {
287
+ std::string encoder_adaptor;
288
+ std::string llm;
289
+ std::string embedding;
290
+ std::string tokenizer;
291
+ std::string system_prompt = "You are a helpful assistant.";
292
+ std::string user_prompt = "语音转写:";
293
+ int32_t max_new_tokens = 512;
294
+ float temperature = 1e-6f;
295
+ float top_p = 0.8f;
296
+ int32_t seed = 42;
297
+ };
298
+
299
+ struct SHERPA_ONNX_API OfflineModelConfig {
300
+ OfflineTransducerModelConfig transducer;
301
+ OfflineParaformerModelConfig paraformer;
302
+ OfflineNemoEncDecCtcModelConfig nemo_ctc;
303
+ OfflineWhisperModelConfig whisper;
304
+ OfflineTdnnModelConfig tdnn;
305
+
306
+ std::string tokens;
307
+ int32_t num_threads = 1;
308
+ bool debug = false;
309
+ std::string provider = "cpu";
310
+ std::string model_type;
311
+ std::string modeling_unit = "cjkchar";
312
+ std::string bpe_vocab;
313
+ std::string telespeech_ctc;
314
+ OfflineSenseVoiceModelConfig sense_voice;
315
+ OfflineMoonshineModelConfig moonshine;
316
+ OfflineFireRedAsrModelConfig fire_red_asr;
317
+ OfflineDolphinModelConfig dolphin;
318
+ OfflineZipformerCtcModelConfig zipformer_ctc;
319
+ OfflineCanaryModelConfig canary;
320
+ OfflineWenetCtcModelConfig wenet_ctc;
321
+ OfflineOmnilingualAsrCtcModelConfig omnilingual;
322
+ OfflineMedAsrCtcModelConfig medasr;
323
+ OfflineFunASRNanoModelConfig funasr_nano;
324
+ };
325
+
326
+ struct SHERPA_ONNX_API OfflineLMConfig {
327
+ std::string model;
328
+ float scale = 1.0;
329
+ };
330
+
331
+ struct SHERPA_ONNX_API OfflineRecognizerConfig {
332
+ FeatureConfig feat_config;
333
+ OfflineModelConfig model_config;
334
+ OfflineLMConfig lm_config;
335
+
336
+ std::string decoding_method = "greedy_search";
337
+ int32_t max_active_paths = 4;
338
+
339
+ std::string hotwords_file;
340
+
341
+ float hotwords_score = 1.5;
342
+ std::string rule_fsts;
343
+ std::string rule_fars;
344
+ float blank_penalty = 0;
345
+ HomophoneReplacerConfig hr;
346
+ };
347
+
348
+ struct SHERPA_ONNX_API OfflineRecognizerResult {
349
+ std::string text;
350
+ std::vector<float> timestamps;
351
+ std::vector<std::string> tokens;
352
+ std::string json;
353
+ std::string lang;
354
+ std::string emotion;
355
+ std::string event;
356
+
357
+ // non-empty only for TDT models
358
+ std::vector<float> durations;
359
+ };
360
+
361
+ class SHERPA_ONNX_API OfflineStream
362
+ : public MoveOnly<OfflineStream, SherpaOnnxOfflineStream> {
363
+ public:
364
+ explicit OfflineStream(const SherpaOnnxOfflineStream *p);
365
+
366
+ void AcceptWaveform(int32_t sample_rate, const float *samples,
367
+ int32_t n) const;
368
+
369
+ void Destroy(const SherpaOnnxOfflineStream *p) const;
370
+ };
371
+
372
+ class SHERPA_ONNX_API OfflineRecognizer
373
+ : public MoveOnly<OfflineRecognizer, SherpaOnnxOfflineRecognizer> {
374
+ public:
375
+ static OfflineRecognizer Create(const OfflineRecognizerConfig &config);
376
+
377
+ void Destroy(const SherpaOnnxOfflineRecognizer *p) const;
378
+
379
+ OfflineStream CreateStream() const;
380
+
381
+ OfflineStream CreateStream(const std::string &hotwords) const;
382
+
383
+ void Decode(const OfflineStream *s) const;
384
+
385
+ void Decode(const OfflineStream *ss, int32_t n) const;
386
+
387
+ OfflineRecognizerResult GetResult(const OfflineStream *s) const;
388
+
389
+ // For unreal engine, please use this function
390
+ // See also https://github.com/k2-fsa/sherpa-onnx/discussions/1960
391
+ std::shared_ptr<OfflineRecognizerResult> GetResultPtr(
392
+ const OfflineStream *s) const;
393
+
394
+ void SetConfig(const OfflineRecognizerConfig &config) const;
395
+
396
+ private:
397
+ explicit OfflineRecognizer(const SherpaOnnxOfflineRecognizer *p);
398
+ };
399
+
400
+ // ============================================================================
401
+ // Non-streaming TTS
402
+ // ============================================================================
403
+ struct OfflineTtsVitsModelConfig {
404
+ std::string model;
405
+ std::string lexicon;
406
+ std::string tokens;
407
+ std::string data_dir;
408
+ std::string dict_dir; // unused
409
+
410
+ float noise_scale = 0.667;
411
+ float noise_scale_w = 0.8;
412
+ float length_scale = 1.0; // < 1, faster in speed; > 1, slower in speed
413
+ };
414
+
415
+ struct OfflineTtsMatchaModelConfig {
416
+ std::string acoustic_model;
417
+ std::string vocoder;
418
+ std::string lexicon;
419
+ std::string tokens;
420
+ std::string data_dir;
421
+ std::string dict_dir; // unused
422
+
423
+ float noise_scale = 0.667;
424
+ float length_scale = 1.0; // < 1, faster in speed; > 1, slower in speed
425
+ };
426
+
427
+ struct OfflineTtsKokoroModelConfig {
428
+ std::string model;
429
+ std::string voices;
430
+ std::string tokens;
431
+ std::string data_dir;
432
+ std::string dict_dir; // unused
433
+ std::string lexicon;
434
+ std::string lang;
435
+
436
+ float length_scale = 1.0; // < 1, faster in speed; > 1, slower in speed
437
+ };
438
+
439
+ struct OfflineTtsKittenModelConfig {
440
+ std::string model;
441
+ std::string voices;
442
+ std::string tokens;
443
+ std::string data_dir;
444
+
445
+ float length_scale = 1.0; // < 1, faster in speed; > 1, slower in speed
446
+ };
447
+
448
+ struct OfflineTtsZipvoiceModelConfig {
449
+ std::string tokens;
450
+ std::string encoder;
451
+ std::string decoder;
452
+ std::string vocoder;
453
+ std::string data_dir;
454
+ std::string lexicon;
455
+
456
+ float feat_scale = 0.1;
457
+ float t_shift = 0.5;
458
+ float target_rms = 0.1;
459
+ float guidance_scale = 1.0;
460
+ };
461
+
462
+ struct OfflineTtsModelConfig {
463
+ OfflineTtsVitsModelConfig vits;
464
+ OfflineTtsMatchaModelConfig matcha;
465
+ OfflineTtsKokoroModelConfig kokoro;
466
+ OfflineTtsKittenModelConfig kitten;
467
+ OfflineTtsZipvoiceModelConfig zipvoice;
468
+
469
+ int32_t num_threads = 1;
470
+ bool debug = false;
471
+ std::string provider = "cpu";
472
+ };
473
+
474
+ struct OfflineTtsConfig {
475
+ OfflineTtsModelConfig model;
476
+ std::string rule_fsts;
477
+ std::string rule_fars;
478
+ int32_t max_num_sentences = 1;
479
+ float silence_scale = 0.2;
480
+ };
481
+
482
+ struct GeneratedAudio {
483
+ std::vector<float> samples; // in the range [-1, 1]
484
+ int32_t sample_rate;
485
+ };
486
+
487
+ // Return 1 to continue generating
488
+ // Return 0 to stop generating
489
+ using OfflineTtsCallback = int32_t (*)(const float *samples,
490
+ int32_t num_samples, float progress,
491
+ void *arg);
492
+
493
+ class SHERPA_ONNX_API OfflineTts
494
+ : public MoveOnly<OfflineTts, SherpaOnnxOfflineTts> {
495
+ public:
496
+ static OfflineTts Create(const OfflineTtsConfig &config);
497
+
498
+ void Destroy(const SherpaOnnxOfflineTts *p) const;
499
+
500
+ // Return the sample rate of the generated audio
501
+ int32_t SampleRate() const;
502
+
503
+ // Number of supported speakers.
504
+ // If it supports only a single speaker, then it return 0 or 1.
505
+ int32_t NumSpeakers() const;
506
+
507
+ // @param text A string containing words separated by spaces
508
+ // @param sid Speaker ID. Used only for multi-speaker models, e.g., models
509
+ // trained using the VCTK dataset. It is not used for
510
+ // single-speaker models, e.g., models trained using the ljspeech
511
+ // dataset.
512
+ // @param speed The speed for the generated speech. E.g., 2 means 2x faster.
513
+ // @param callback If not NULL, it is called whenever config.max_num_sentences
514
+ // sentences have been processed. The callback is called in
515
+ // the current thread.
516
+ GeneratedAudio Generate(const std::string &text, int32_t sid = 0,
517
+ float speed = 1.0,
518
+ OfflineTtsCallback callback = nullptr,
519
+ void *arg = nullptr) const;
520
+
521
+ // Like Generate, but return a smart pointer.
522
+ //
523
+ // See also https://github.com/k2-fsa/sherpa-onnx/issues/2347
524
+ std::shared_ptr<GeneratedAudio> Generate2(
525
+ const std::string &text, int32_t sid = 0, float speed = 1.0,
526
+ OfflineTtsCallback callback = nullptr, void *arg = nullptr) const;
527
+
528
+ private:
529
+ explicit OfflineTts(const SherpaOnnxOfflineTts *p);
530
+ };
531
+
532
+ // ============================================================
533
+ // For Keyword Spotter
534
+ // ============================================================
535
+
536
+ struct KeywordResult {
537
+ std::string keyword;
538
+ std::vector<std::string> tokens;
539
+ std::vector<float> timestamps;
540
+ float start_time;
541
+ std::string json;
542
+ };
543
+
544
+ struct KeywordSpotterConfig {
545
+ FeatureConfig feat_config;
546
+ OnlineModelConfig model_config;
547
+ int32_t max_active_paths = 4;
548
+ int32_t num_trailing_blanks = 1;
549
+ float keywords_score = 1.0f;
550
+ float keywords_threshold = 0.25f;
551
+ std::string keywords_file;
552
+ };
553
+
554
+ class SHERPA_ONNX_API KeywordSpotter
555
+ : public MoveOnly<KeywordSpotter, SherpaOnnxKeywordSpotter> {
556
+ public:
557
+ static KeywordSpotter Create(const KeywordSpotterConfig &config);
558
+
559
+ void Destroy(const SherpaOnnxKeywordSpotter *p) const;
560
+
561
+ OnlineStream CreateStream() const;
562
+
563
+ OnlineStream CreateStream(const std::string &keywords) const;
564
+
565
+ bool IsReady(const OnlineStream *s) const;
566
+
567
+ void Decode(const OnlineStream *s) const;
568
+
569
+ void Decode(const OnlineStream *ss, int32_t n) const;
570
+
571
+ void Reset(const OnlineStream *s) const;
572
+
573
+ KeywordResult GetResult(const OnlineStream *s) const;
574
+
575
+ private:
576
+ explicit KeywordSpotter(const SherpaOnnxKeywordSpotter *p);
577
+ };
578
+
579
+ struct OfflineSpeechDenoiserGtcrnModelConfig {
580
+ std::string model;
581
+ };
582
+
583
+ struct OfflineSpeechDenoiserModelConfig {
584
+ OfflineSpeechDenoiserGtcrnModelConfig gtcrn;
585
+ int32_t num_threads = 1;
586
+ int32_t debug = false;
587
+ std::string provider = "cpu";
588
+ };
589
+
590
+ struct OfflineSpeechDenoiserConfig {
591
+ OfflineSpeechDenoiserModelConfig model;
592
+ };
593
+
594
+ struct DenoisedAudio {
595
+ std::vector<float> samples; // in the range [-1, 1]
596
+ int32_t sample_rate;
597
+ };
598
+
599
+ class SHERPA_ONNX_API OfflineSpeechDenoiser
600
+ : public MoveOnly<OfflineSpeechDenoiser, SherpaOnnxOfflineSpeechDenoiser> {
601
+ public:
602
+ static OfflineSpeechDenoiser Create(
603
+ const OfflineSpeechDenoiserConfig &config);
604
+
605
+ void Destroy(const SherpaOnnxOfflineSpeechDenoiser *p) const;
606
+
607
+ DenoisedAudio Run(const float *samples, int32_t n, int32_t sample_rate) const;
608
+
609
+ int32_t GetSampleRate() const;
610
+
611
+ private:
612
+ explicit OfflineSpeechDenoiser(const SherpaOnnxOfflineSpeechDenoiser *p);
613
+ };
614
+
615
+ // ==============================
616
+ // VAD
617
+ // ==============================
618
+
619
+ struct SileroVadModelConfig {
620
+ std::string model;
621
+ float threshold = 0.5;
622
+ float min_silence_duration = 0.5;
623
+ float min_speech_duration = 0.25;
624
+ int32_t window_size = 512;
625
+ float max_speech_duration = 20;
626
+ };
627
+
628
+ struct TenVadModelConfig {
629
+ std::string model;
630
+ float threshold = 0.5;
631
+ float min_silence_duration = 0.5;
632
+ float min_speech_duration = 0.25;
633
+ int32_t window_size = 256;
634
+ float max_speech_duration = 20;
635
+ };
636
+
637
+ struct VadModelConfig {
638
+ SileroVadModelConfig silero_vad;
639
+ TenVadModelConfig ten_vad;
640
+
641
+ int32_t sample_rate = 16000;
642
+ int32_t num_threads = 1;
643
+ std::string provider = "cpu";
644
+ bool debug = false;
645
+ };
646
+
647
+ struct SpeechSegment {
648
+ int32_t start;
649
+ std::vector<float> samples;
650
+ };
651
+
652
+ class SHERPA_ONNX_API CircularBuffer
653
+ : public MoveOnly<CircularBuffer, SherpaOnnxCircularBuffer> {
654
+ public:
655
+ static CircularBuffer Create(int32_t capacity);
656
+
657
+ void Destroy(const SherpaOnnxCircularBuffer *p) const;
658
+
659
+ void Push(const float *p, int32_t n) const;
660
+
661
+ std::vector<float> Get(int32_t start_index, int32_t n) const;
662
+
663
+ void Pop(int32_t n) const;
664
+
665
+ int32_t Size() const;
666
+
667
+ int32_t Head() const;
668
+
669
+ void Reset() const;
670
+
671
+ private:
672
+ explicit CircularBuffer(const SherpaOnnxCircularBuffer *p);
673
+ };
674
+
675
+ class SHERPA_ONNX_API VoiceActivityDetector
676
+ : public MoveOnly<VoiceActivityDetector, SherpaOnnxVoiceActivityDetector> {
677
+ public:
678
+ static VoiceActivityDetector Create(const VadModelConfig &config,
679
+ float buffer_size_in_seconds);
680
+
681
+ void Destroy(const SherpaOnnxVoiceActivityDetector *p) const;
682
+
683
+ void AcceptWaveform(const float *samples, int32_t n) const;
684
+
685
+ bool IsEmpty() const;
686
+
687
+ bool IsDetected() const;
688
+
689
+ void Pop() const;
690
+
691
+ void Clear() const;
692
+
693
+ SpeechSegment Front() const;
694
+
695
+ // For unreal engine, please use this function
696
+ // See also https://github.com/k2-fsa/sherpa-onnx/discussions/1960
697
+ std::shared_ptr<SpeechSegment> FrontPtr() const;
698
+
699
+ void Reset() const;
700
+
701
+ void Flush() const;
702
+
703
+ private:
704
+ explicit VoiceActivityDetector(const SherpaOnnxVoiceActivityDetector *p);
705
+ };
706
+
707
+ class SHERPA_ONNX_API LinearResampler
708
+ : public MoveOnly<LinearResampler, SherpaOnnxLinearResampler> {
709
+ public:
710
+ LinearResampler() = default;
711
+ static LinearResampler Create(int32_t samp_rate_in_hz,
712
+ int32_t samp_rate_out_hz,
713
+ float filter_cutoff_hz, int32_t num_zeros);
714
+
715
+ void Destroy(const SherpaOnnxLinearResampler *p) const;
716
+
717
+ void Reset() const;
718
+
719
+ std::vector<float> Resample(const float *input, int32_t input_dim,
720
+ bool flush) const;
721
+
722
+ int32_t GetInputSamplingRate() const;
723
+ int32_t GetOutputSamplingRate() const;
724
+
725
+ private:
726
+ explicit LinearResampler(const SherpaOnnxLinearResampler *p);
727
+ };
728
+
729
+ SHERPA_ONNX_API std::string GetVersionStr();
730
+ SHERPA_ONNX_API std::string GetGitSha1();
731
+ SHERPA_ONNX_API std::string GetGitDate();
732
+ SHERPA_ONNX_API bool FileExists(const std::string &filename);
733
+
734
+ // ============================================================================
735
+ // Offline Punctuation
736
+ // ============================================================================
737
+ struct OfflinePunctuationModelConfig {
738
+ std::string ct_transformer;
739
+ int32_t num_threads = 1;
740
+ bool debug = false;
741
+ std::string provider = "cpu";
742
+ };
743
+
744
+ struct OfflinePunctuationConfig {
745
+ OfflinePunctuationModelConfig model;
746
+ };
747
+
748
+ class SHERPA_ONNX_API OfflinePunctuation
749
+ : public MoveOnly<OfflinePunctuation, SherpaOnnxOfflinePunctuation> {
750
+ public:
751
+ static OfflinePunctuation Create(const OfflinePunctuationConfig &config);
752
+
753
+ void Destroy(const SherpaOnnxOfflinePunctuation *p) const;
754
+
755
+ // Add punctuations to the input text and return it.
756
+ std::string AddPunctuation(const std::string &text) const;
757
+
758
+ private:
759
+ explicit OfflinePunctuation(const SherpaOnnxOfflinePunctuation *p);
760
+ };
761
+
762
+ // ============================================================================
763
+ // Online Punctuation
764
+ // ============================================================================
765
+ struct OnlinePunctuationModelConfig {
766
+ std::string cnn_bilstm;
767
+ std::string bpe_vocab;
768
+ int32_t num_threads = 1;
769
+ bool debug = false;
770
+ std::string provider = "cpu";
771
+ };
772
+
773
+ struct OnlinePunctuationConfig {
774
+ OnlinePunctuationModelConfig model;
775
+ };
776
+
777
+ class SHERPA_ONNX_API OnlinePunctuation
778
+ : public MoveOnly<OnlinePunctuation, SherpaOnnxOnlinePunctuation> {
779
+ public:
780
+ static OnlinePunctuation Create(const OnlinePunctuationConfig &config);
781
+
782
+ void Destroy(const SherpaOnnxOnlinePunctuation *p) const;
783
+
784
+ // Add punctuations to the input text and return it.
785
+ std::string AddPunctuation(const std::string &text) const;
786
+
787
+ private:
788
+ explicit OnlinePunctuation(const SherpaOnnxOnlinePunctuation *p);
789
+ };
790
+
791
+ // ============================================================================
792
+ // Audio tagging
793
+ // ============================================================================
794
+ struct OfflineZipformerAudioTaggingModelConfig {
795
+ std::string model;
796
+ };
797
+
798
+ struct AudioTaggingModelConfig {
799
+ OfflineZipformerAudioTaggingModelConfig zipformer;
800
+ std::string ced;
801
+ int32_t num_threads = 1;
802
+ bool debug = false; // true to print debug information of the model
803
+ std::string provider = "cpu";
804
+ };
805
+
806
+ struct AudioTaggingConfig {
807
+ AudioTaggingModelConfig model;
808
+ std::string labels;
809
+ int32_t top_k = 5;
810
+ };
811
+
812
+ struct AudioEvent {
813
+ std::string name;
814
+ int32_t index;
815
+ float prob;
816
+ };
817
+
818
+ class SHERPA_ONNX_API AudioTagging
819
+ : public MoveOnly<AudioTagging, SherpaOnnxAudioTagging> {
820
+ public:
821
+ static AudioTagging Create(const AudioTaggingConfig &config);
822
+
823
+ void Destroy(const SherpaOnnxAudioTagging *p) const;
824
+
825
+ OfflineStream CreateStream() const;
826
+ // when top_k is -1, it uses the top_k from config.top_k
827
+ // when top_k is > 0, config.top_k is ignored
828
+ std::vector<AudioEvent> Compute(const OfflineStream *s, int32_t top_k = -1);
829
+
830
+ // For unreal engine, please use this function
831
+ // See also https://github.com/k2-fsa/sherpa-onnx/discussions/1960
832
+ std::shared_ptr<std::vector<AudioEvent>> ComputePtr(const OfflineStream *s,
833
+ int32_t top_k = -1);
834
+
835
+ private:
836
+ explicit AudioTagging(const SherpaOnnxAudioTagging *p);
837
+ };
838
+
839
+ } // namespace sherpa_onnx::cxx
840
+
841
+ #endif // SHERPA_ONNX_C_API_CXX_API_H_