@mentra/bluetooth-sdk 0.1.9 → 0.1.10

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 (147) hide show
  1. package/README.md +116 -18
  2. package/android/build.gradle +289 -16
  3. package/android/gradle.properties.example +9 -0
  4. package/android/lc3Lib/build.gradle +95 -6
  5. package/android/settings.gradle +3 -0
  6. package/android/silero/LICENSE.md +17 -0
  7. package/android/silero/build.gradle +42 -0
  8. package/android/silero/proguard-rules.pro +24 -0
  9. package/android/silero/src/androidTest/assets/hello.wav +0 -0
  10. package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +79 -0
  11. package/android/silero/src/main/assets/LICENSE +21 -0
  12. package/android/silero/src/main/assets/silero_vad.onnx +0 -0
  13. package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +160 -0
  14. package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +449 -0
  15. package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +15 -0
  16. package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +14 -0
  17. package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +12 -0
  18. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +47 -0
  19. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +41 -0
  20. package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +44 -0
  21. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +6 -0
  22. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +165 -27
  23. package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +89 -21
  24. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +189 -16
  25. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceStore.kt +22 -2
  26. package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +787 -85
  27. package/android/src/main/java/com/mentra/bluetoothsdk/audio/AudioModels.kt +6 -2
  28. package/android/src/main/java/com/mentra/bluetoothsdk/camera/CameraModels.kt +123 -10
  29. package/android/src/main/java/com/mentra/bluetoothsdk/controllers/ControllerManager.kt +12 -2
  30. package/android/src/main/java/com/mentra/bluetoothsdk/controllers/R1.kt +8 -2
  31. package/android/src/main/java/com/mentra/bluetoothsdk/debug/BleTraceLogger.kt +33 -17
  32. package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +101 -0
  33. package/android/src/main/java/com/mentra/bluetoothsdk/internal/MapParsing.kt +8 -0
  34. package/android/src/main/java/com/mentra/bluetoothsdk/photoreceiver/LocalPhotoReceiverRegistry.java +108 -0
  35. package/android/src/main/java/com/mentra/bluetoothsdk/photoreceiver/LocalPhotoUploadServer.kt +399 -74
  36. package/android/src/main/java/com/mentra/bluetoothsdk/photoreceiver/MentraPhotoReceiverModule.kt +100 -17
  37. package/android/src/main/java/com/mentra/bluetoothsdk/requests/DisplayRequests.kt +17 -0
  38. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/G1.java +2 -2
  39. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/G2.kt +569 -220
  40. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Mach1.java +2 -2
  41. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.java +271 -66
  42. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraNex.kt +176 -104
  43. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraosBle.java +12753 -27036
  44. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +54 -2
  45. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Simulated.kt +16 -4
  46. package/android/src/main/java/com/mentra/bluetoothsdk/status/DeviceStatus.kt +60 -3
  47. package/android/src/main/java/com/mentra/bluetoothsdk/streaming/StreamModels.kt +12 -3
  48. package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +87 -36
  49. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +13 -4
  50. package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +229 -0
  51. package/android/src/main/java/com/mentra/bluetoothsdk/tts/TTSTools.kt +180 -0
  52. package/android/src/main/java/com/mentra/bluetoothsdk/utils/AvifExifStripper.java +573 -0
  53. package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +488 -50
  54. package/android/src/main/java/com/mentra/bluetoothsdk/utils/HeifExifTagReader.java +199 -0
  55. package/android/src/main/java/com/mentra/bluetoothsdk/utils/MessageChunkReassembler.java +18 -1
  56. package/android/src/main/java/com/mentra/bluetoothsdk/utils/MessageChunker.java +64 -18
  57. package/android/src/main/java/com/mentra/bluetoothsdk/utils/NexSGCUtils.kt +48 -39
  58. package/android/src/test/java/com/mentra/bluetoothsdk/camera/PhotoRequestTest.kt +23 -0
  59. package/android/src/test/java/com/mentra/bluetoothsdk/photoreceiver/LocalPhotoReceiverRegistryTest.java +75 -0
  60. package/android/src/test/java/com/mentra/bluetoothsdk/utils/AvifExifStripperTest.java +30 -0
  61. package/android/src/test/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadServiceTest.java +98 -0
  62. package/android/src/test/resources/avif_with_exif.avif +0 -0
  63. package/build/BluetoothSdk.types.d.ts +241 -43
  64. package/build/BluetoothSdk.types.d.ts.map +1 -1
  65. package/build/BluetoothSdk.types.js +11 -10
  66. package/build/BluetoothSdk.types.js.map +1 -1
  67. package/build/_private/BluetoothSdkModule.d.ts +36 -23
  68. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  69. package/build/_private/BluetoothSdkModule.js +30 -52
  70. package/build/_private/BluetoothSdkModule.js.map +1 -1
  71. package/build/_private/photoRequestPayload.d.ts +4 -0
  72. package/build/_private/photoRequestPayload.d.ts.map +1 -0
  73. package/build/_private/photoRequestPayload.js +25 -0
  74. package/build/_private/photoRequestPayload.js.map +1 -0
  75. package/build/index.d.ts +1 -1
  76. package/build/index.d.ts.map +1 -1
  77. package/build/index.js +23 -0
  78. package/build/index.js.map +1 -1
  79. package/build/react/useMentraBluetooth.d.ts +2 -2
  80. package/build/react/useMentraBluetooth.d.ts.map +1 -1
  81. package/build/react/useMentraBluetooth.js +3 -3
  82. package/build/react/useMentraBluetooth.js.map +1 -1
  83. package/ios/BluetoothSdkModule.swift +174 -70
  84. package/ios/LocalPhotoUploadServer.swift +370 -105
  85. package/ios/MentraBluetoothSDK.podspec +3 -1
  86. package/ios/MentraPhotoReceiverModule.swift +62 -10
  87. package/ios/Packages/CoreObjC/include/PcmConverter.h +22 -0
  88. package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +668 -26
  89. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/Info.plist +13 -9
  90. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/ios-arm64/Headers/sherpa-onnx/c-api/c-api.h +4551 -0
  91. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/ios-arm64/Headers/sherpa-onnx/c-api/cxx-api.h +1745 -0
  92. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/ios-arm64/libsherpa-onnx.a +0 -0
  93. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/ios-arm64_x86_64-simulator/Headers/sherpa-onnx/c-api/c-api.h +4551 -0
  94. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/ios-arm64_x86_64-simulator/Headers/sherpa-onnx/c-api/cxx-api.h +1745 -0
  95. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/ios-arm64_x86_64-simulator/libsherpa-onnx.a +0 -0
  96. package/ios/Source/BluetoothSdkDefaults.swift +6 -0
  97. package/ios/Source/Bridge.swift +58 -19
  98. package/ios/Source/DeviceManager.swift +149 -17
  99. package/ios/Source/DeviceStore.swift +16 -6
  100. package/ios/Source/MentraBluetoothSDK.swift +803 -47
  101. package/ios/Source/ObservableStore.swift +6 -0
  102. package/ios/Source/{Audio → audio}/AudioModels.swift +9 -7
  103. package/ios/Source/{Camera → camera}/CameraModels.swift +225 -16
  104. package/ios/Source/controllers/ControllerManager.swift +4 -3
  105. package/ios/Source/controllers/R1.swift +3 -3
  106. package/ios/Source/{Events → events}/BluetoothEvents.swift +183 -1
  107. package/ios/Source/{Internal → internal}/ValueParsing.swift +10 -1
  108. package/ios/Source/{Requests → requests}/DisplayRequests.swift +28 -4
  109. package/ios/Source/services/PhoneMic.swift +1 -0
  110. package/ios/Source/sgcs/G1.swift +2 -2
  111. package/ios/Source/sgcs/G2.swift +645 -403
  112. package/ios/Source/sgcs/Mach1.swift +2 -2
  113. package/ios/Source/sgcs/MentraLive.swift +812 -319
  114. package/ios/Source/sgcs/MentraNex.swift +412 -182
  115. package/ios/Source/sgcs/SGCManager.swift +57 -3
  116. package/ios/Source/sgcs/Simulated.swift +7 -3
  117. package/ios/Source/sgcs/mentraos_ble.pb.swift +3073 -3134
  118. package/ios/Source/status/DeviceStatus.swift +974 -0
  119. package/ios/Source/{Status → status}/WifiHotspotStatus.swift +4 -4
  120. package/ios/Source/{Streaming → streaming}/StreamModels.swift +24 -9
  121. package/ios/Source/stt/STTTools.swift +27 -12
  122. package/ios/Source/stt/SherpaOnnxTranscriber.swift +31 -8
  123. package/ios/Source/tts/TTSTools.swift +164 -0
  124. package/ios/Source/utils/JSCExperiment.swift +7 -7
  125. package/ios/Source/utils/MessageChunkReassembler.swift +20 -1
  126. package/ios/Source/utils/MessageChunker.swift +78 -21
  127. package/ios/Source/utils/TarBz2Extractor.swift +53 -2
  128. package/package.json +1 -1
  129. package/plugin/build/withAndroid.js +68 -4
  130. package/src/BluetoothSdk.types.ts +331 -59
  131. package/src/_private/BluetoothSdkModule.ts +113 -102
  132. package/src/_private/photoRequestPayload.ts +28 -0
  133. package/src/index.ts +58 -15
  134. package/src/react/useMentraBluetooth.ts +6 -4
  135. package/android/.project +0 -28
  136. package/android/lc3Lib/.project +0 -28
  137. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/Headers/cargs.h +0 -162
  138. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/Headers/sherpa-onnx/c-api/c-api.h +0 -1852
  139. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/Headers/sherpa-onnx/c-api/cxx-api.h +0 -674
  140. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/ios-arm64/sherpa-onnx.a +0 -0
  141. package/ios/Packages/SherpaOnnx/sherpa-onnx.xcframework/ios-arm64_x86_64-simulator/sherpa-onnx.a +0 -0
  142. package/ios/Source/Status/DeviceStatus.swift +0 -471
  143. /package/ios/Source/{Connection → connection}/ScanSession.swift +0 -0
  144. /package/ios/Source/{Errors → errors}/BluetoothError.swift +0 -0
  145. /package/ios/Source/{Internal → internal}/BluetoothAvailability.swift +0 -0
  146. /package/ios/Source/{Status → status}/RuntimeState.swift +0 -0
  147. /package/ios/Source/{Types → types}/DeviceModels.swift +0 -0
@@ -0,0 +1,4551 @@
1
+ // sherpa-onnx/c-api/c-api.h
2
+ //
3
+ // Copyright (c) 2023 Xiaomi Corporation
4
+ /**
5
+ * @file c-api.h
6
+ * @brief Public C API for sherpa-onnx.
7
+ *
8
+ * This header exposes the main sherpa-onnx inference features through a stable
9
+ * C interface. It is intended for native C/C++ applications and for language
10
+ * bindings that need a C ABI.
11
+ *
12
+ * The file is organized by feature family. The major API groups are:
13
+ *
14
+ * - Utility helpers: version/build information, file checks, WAVE I/O, and a
15
+ * display helper for incremental text output
16
+ * - Streaming ASR: online recognizers, online streams, endpointing, and
17
+ * per-stream runtime options
18
+ * - Non-streaming ASR: offline recognizers, offline streams, batch decode, and
19
+ * result retrieval
20
+ * - Keyword spotting: streaming keyword detection, custom keyword streams, and
21
+ * keyword result snapshots
22
+ * - Voice activity detection: Silero/Ten VAD models, speech segment buffers,
23
+ * and detector state management
24
+ * - Text-to-speech: offline TTS model families, generation configuration, and
25
+ * generated audio helpers
26
+ * - Spoken language identification
27
+ * - Speaker embedding extraction and speaker enrollment/search/verification
28
+ * - Audio tagging
29
+ * - Offline and online punctuation restoration
30
+ * - Linear resampling
31
+ * - Offline speaker diarization
32
+ * - Offline and online speech enhancement / denoising
33
+ * - HarmonyOS-specific constructor variants
34
+ *
35
+ * Common ownership rules:
36
+ *
37
+ * - Opaque handles created by `SherpaOnnxCreate*()` functions are generally
38
+ * destroyed with a matching `SherpaOnnxDestroy*()` function
39
+ * - Snapshot/result objects returned by query functions usually need explicit
40
+ * destruction as documented on each API
41
+ * - Strings or arrays returned by helper/query functions are either:
42
+ * - statically owned by the library and must not be freed, or
43
+ * - heap-allocated for the caller and must be released with the matching
44
+ * `Free`/`Destroy` API
45
+ *
46
+ * General usage pattern:
47
+ *
48
+ * 1. Zero-initialize a config struct with `memset(&config, 0, sizeof(config))`
49
+ * 2. Fill in the required model paths and runtime options
50
+ * 3. Create the corresponding engine with `SherpaOnnxCreate*()`
51
+ * 4. Create a stream if the feature uses one
52
+ * 5. Feed audio or text, run the compute/decode API, and retrieve results
53
+ * 6. Release every returned object with the documented matching API
54
+ *
55
+ * The examples in `c-api-examples/` show complete end-to-end usage. Useful
56
+ * starting points include:
57
+ *
58
+ * - `decode-file-c-api.c` for ASR
59
+ * - `kws-c-api.c` for keyword spotting
60
+ * - `vad-whisper-c-api.c` for VAD
61
+ * - `offline-tts-c-api.c` and `kokoro-tts-en-c-api.c` for TTS
62
+ * - `speaker-identification-c-api.c` for speaker embedding and verification
63
+ * - `audio-tagging-c-api.c` for audio tagging
64
+ * - `add-punctuation-c-api.c` and `add-punctuation-online-c-api.c` for
65
+ * punctuation
66
+ * - `offline-sepaker-diarization-c-api.c` for diarization
67
+ * - `speech-enhancement-gtcrn-c-api.c` and
68
+ * `online-speech-enhancement-gtcrn-c-api.c` for speech enhancement
69
+ */
70
+
71
+ #ifndef SHERPA_ONNX_C_API_C_API_H_
72
+ #define SHERPA_ONNX_C_API_C_API_H_
73
+
74
+ #include <stdint.h>
75
+
76
+ #ifdef __cplusplus
77
+ extern "C" {
78
+ #endif
79
+
80
+ // See https://github.com/pytorch/pytorch/blob/main/c10/macros/Export.h
81
+ // We will set SHERPA_ONNX_BUILD_SHARED_LIBS and SHERPA_ONNX_BUILD_MAIN_LIB in
82
+ // CMakeLists.txt
83
+
84
+ #if defined(__GNUC__)
85
+ #pragma GCC diagnostic push
86
+ #pragma GCC diagnostic ignored "-Wattributes"
87
+ #endif
88
+
89
+ #if defined(_WIN32)
90
+ #if defined(SHERPA_ONNX_BUILD_SHARED_LIBS)
91
+ #define SHERPA_ONNX_EXPORT __declspec(dllexport)
92
+ #define SHERPA_ONNX_IMPORT __declspec(dllimport)
93
+ #else
94
+ #define SHERPA_ONNX_EXPORT
95
+ #define SHERPA_ONNX_IMPORT
96
+ #endif
97
+ #else // WIN32
98
+ #define SHERPA_ONNX_EXPORT __attribute__((visibility("default")))
99
+
100
+ #define SHERPA_ONNX_IMPORT SHERPA_ONNX_EXPORT
101
+ #endif // WIN32
102
+
103
+ #if defined(SHERPA_ONNX_BUILD_MAIN_LIB)
104
+ #define SHERPA_ONNX_API SHERPA_ONNX_EXPORT
105
+ #else
106
+ #define SHERPA_ONNX_API SHERPA_ONNX_IMPORT
107
+ #endif
108
+
109
+ #ifndef SHERPA_ONNX_DEPRECATED
110
+ #if defined(_MSC_VER)
111
+ #define SHERPA_ONNX_DEPRECATED(msg) __declspec(deprecated(msg))
112
+ #elif defined(__GNUC__) || defined(__clang__)
113
+ #define SHERPA_ONNX_DEPRECATED(msg) __attribute__((deprecated(msg)))
114
+ #else
115
+ #define SHERPA_ONNX_DEPRECATED(msg)
116
+ #endif
117
+ #endif
118
+
119
+ /**
120
+ * @brief Return the sherpa-onnx version string.
121
+ *
122
+ * The returned pointer refers to statically allocated memory owned by the
123
+ * library. Do not free it and do not modify it.
124
+ *
125
+ * @return Version string, for example `"1.12.1"`.
126
+ *
127
+ * @code
128
+ * printf("sherpa-onnx version: %s\n", SherpaOnnxGetVersionStr());
129
+ * @endcode
130
+ */
131
+ SHERPA_ONNX_API const char *SherpaOnnxGetVersionStr();
132
+
133
+ /**
134
+ * @brief Return the Git SHA1 used to build the library.
135
+ *
136
+ * The returned pointer refers to statically allocated memory owned by the
137
+ * library. Do not free it and do not modify it.
138
+ *
139
+ * @return Short Git SHA1 string, for example `"6982b86c"`.
140
+ */
141
+ SHERPA_ONNX_API const char *SherpaOnnxGetGitSha1();
142
+
143
+ /**
144
+ * @brief Return the Git build date used to build the library.
145
+ *
146
+ * The returned pointer refers to statically allocated memory owned by the
147
+ * library. Do not free it and do not modify it.
148
+ *
149
+ * @return Build date string, for example `"Fri Jun 20 11:22:52 2025"`.
150
+ */
151
+ SHERPA_ONNX_API const char *SherpaOnnxGetGitDate();
152
+
153
+ /**
154
+ * @brief Check whether a file exists.
155
+ *
156
+ * @param filename File path to test.
157
+ * @return 1 if the file exists; otherwise 0.
158
+ *
159
+ * @code
160
+ * if (!SherpaOnnxFileExists("./Obama.wav")) {
161
+ * fprintf(stderr, "Please download Obama.wav\n");
162
+ * }
163
+ * @endcode
164
+ */
165
+ SHERPA_ONNX_API int32_t SherpaOnnxFileExists(const char *filename);
166
+
167
+ /**
168
+ * @brief Configuration for a streaming transducer model.
169
+ *
170
+ * Please refer to
171
+ * https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html
172
+ * to download compatible pre-trained models.
173
+ */
174
+ typedef struct SherpaOnnxOnlineTransducerModelConfig {
175
+ /** Path to the encoder ONNX model. */
176
+ const char *encoder;
177
+ /** Path to the decoder ONNX model. */
178
+ const char *decoder;
179
+ /** Path to the joiner ONNX model. */
180
+ const char *joiner;
181
+ } SherpaOnnxOnlineTransducerModelConfig;
182
+
183
+ /**
184
+ * @brief Configuration for a streaming Paraformer model.
185
+ *
186
+ * Please visit
187
+ * https://k2-fsa.github.io/sherpa/onnx/pretrained_models/online-paraformer/index.html
188
+ * to download compatible models.
189
+ */
190
+ typedef struct SherpaOnnxOnlineParaformerModelConfig {
191
+ /** Path to the encoder ONNX model. */
192
+ const char *encoder;
193
+ /** Path to the decoder ONNX model. */
194
+ const char *decoder;
195
+ } SherpaOnnxOnlineParaformerModelConfig;
196
+
197
+ /**
198
+ * @brief Configuration for a streaming Zipformer2 CTC model.
199
+ */
200
+ typedef struct SherpaOnnxOnlineZipformer2CtcModelConfig {
201
+ /** Path to the ONNX model. */
202
+ const char *model;
203
+ } SherpaOnnxOnlineZipformer2CtcModelConfig;
204
+
205
+ /** @brief Configuration for a streaming NeMo CTC model. */
206
+ typedef struct SherpaOnnxOnlineNemoCtcModelConfig {
207
+ /** Path to the ONNX model. */
208
+ const char *model;
209
+ } SherpaOnnxOnlineNemoCtcModelConfig;
210
+
211
+ /** @brief Configuration for a streaming T-One CTC model. */
212
+ typedef struct SherpaOnnxOnlineToneCtcModelConfig {
213
+ /** Path to the ONNX model. */
214
+ const char *model;
215
+ } SherpaOnnxOnlineToneCtcModelConfig;
216
+
217
+ /**
218
+ * @brief Model configuration shared by streaming ASR recognizers.
219
+ *
220
+ * Zero-initialize this struct before use, then fill in the sub-config for the
221
+ * model family you want to use together with the shared fields such as
222
+ * @c tokens, @c provider, and @c num_threads.
223
+ *
224
+ * Exactly one model family should be configured for each recognizer. For
225
+ * example, set only one of @c transducer, @c paraformer, @c zipformer2_ctc,
226
+ * @c nemo_ctc, or @c t_one_ctc.
227
+ *
228
+ * If multiple model families are configured at the same time, the
229
+ * implementation will choose one of them, and which one is used is
230
+ * implementation-defined. Do not rely on any precedence rule.
231
+ */
232
+ typedef struct SherpaOnnxOnlineModelConfig {
233
+ /** Streaming transducer model files. */
234
+ SherpaOnnxOnlineTransducerModelConfig transducer;
235
+ /** Streaming Paraformer model files. */
236
+ SherpaOnnxOnlineParaformerModelConfig paraformer;
237
+ /** Streaming Zipformer2 CTC model files. */
238
+ SherpaOnnxOnlineZipformer2CtcModelConfig zipformer2_ctc;
239
+ /** Path to the tokens file. */
240
+ const char *tokens;
241
+ /** Number of threads used by the ONNX Runtime backend. */
242
+ int32_t num_threads;
243
+ /** Execution provider, for example "cpu", "cuda", or "coreml". */
244
+ const char *provider;
245
+ /** Non-zero to print model debug information. */
246
+ int32_t debug;
247
+ /** Optional explicit model type override. */
248
+ const char *model_type;
249
+ /**
250
+ * Modeling unit used by the tokens.
251
+ *
252
+ * Valid values include:
253
+ * - "cjkchar"
254
+ * - "bpe"
255
+ * - "cjkchar+bpe"
256
+ */
257
+ const char *modeling_unit;
258
+ /** Path to the BPE vocabulary file when BPE is used. */
259
+ const char *bpe_vocab;
260
+ /** Optional in-memory tokens data. Used instead of @c tokens when non-NULL.
261
+ */
262
+ const char *tokens_buf;
263
+ /** Size in bytes of @c tokens_buf, excluding the trailing '\0'. */
264
+ int32_t tokens_buf_size;
265
+ /** Streaming NeMo CTC model files. */
266
+ SherpaOnnxOnlineNemoCtcModelConfig nemo_ctc;
267
+ /** Streaming T-One CTC model files. */
268
+ SherpaOnnxOnlineToneCtcModelConfig t_one_ctc;
269
+ } SherpaOnnxOnlineModelConfig;
270
+
271
+ /**
272
+ * @brief Feature extraction settings for ASR.
273
+ *
274
+ * The bundled ASR models typically expect 16 kHz mono audio and 80-bin
275
+ * features.
276
+ */
277
+ typedef struct SherpaOnnxFeatureConfig {
278
+ /** Sample rate expected by the model, for example 16000. */
279
+ int32_t sample_rate;
280
+
281
+ /** Feature dimension expected by the model, for example 80. */
282
+ int32_t feature_dim;
283
+ } SherpaOnnxFeatureConfig;
284
+
285
+ /** @brief Configuration for HLG/FST-based online CTC decoding. */
286
+ typedef struct SherpaOnnxOnlineCtcFstDecoderConfig {
287
+ /** Path to the decoding graph. */
288
+ const char *graph;
289
+ /** Decoder max-active setting. */
290
+ int32_t max_active;
291
+ } SherpaOnnxOnlineCtcFstDecoderConfig;
292
+
293
+ /** @brief Configuration for homophone replacement. */
294
+ typedef struct SherpaOnnxHomophoneReplacerConfig {
295
+ /** Unused legacy field kept for ABI compatibility. */
296
+ const char *dict_dir;
297
+ /** Path to the lexicon used by the homophone replacer. */
298
+ const char *lexicon;
299
+ /** Path to the replacement rule FST file. */
300
+ const char *rule_fsts;
301
+ } SherpaOnnxHomophoneReplacerConfig;
302
+
303
+ /**
304
+ * @brief Configuration for a streaming ASR recognizer.
305
+ *
306
+ * Zero-initialize this struct before use. Then fill in @c feat_config,
307
+ * @c model_config, and any optional decoding, endpoint, or hotword settings.
308
+ *
309
+ * Example model package:
310
+ * `sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20`
311
+ *
312
+ * @code
313
+ * SherpaOnnxOnlineRecognizerConfig config;
314
+ * memset(&config, 0, sizeof(config));
315
+ *
316
+ * config.feat_config.sample_rate = 16000;
317
+ * config.feat_config.feature_dim = 80;
318
+ *
319
+ * config.model_config.transducer.encoder =
320
+ * "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/"
321
+ * "encoder-epoch-99-avg-1.int8.onnx";
322
+ * config.model_config.transducer.decoder =
323
+ * "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/"
324
+ * "decoder-epoch-99-avg-1.onnx";
325
+ * config.model_config.transducer.joiner =
326
+ * "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/"
327
+ * "joiner-epoch-99-avg-1.int8.onnx";
328
+ * config.model_config.tokens =
329
+ * "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/"
330
+ * "tokens.txt";
331
+ * config.model_config.provider = "cpu";
332
+ * config.model_config.num_threads = 1;
333
+ *
334
+ * config.decoding_method = "greedy_search";
335
+ * @endcode
336
+ */
337
+ typedef struct SherpaOnnxOnlineRecognizerConfig {
338
+ /** Feature extraction settings. */
339
+ SherpaOnnxFeatureConfig feat_config;
340
+ /** Streaming model configuration. */
341
+ SherpaOnnxOnlineModelConfig model_config;
342
+
343
+ /** Decoding method, for example "greedy_search" or "modified_beam_search". */
344
+ const char *decoding_method;
345
+
346
+ /** Number of active paths for modified beam search. */
347
+ int32_t max_active_paths;
348
+
349
+ /** Set to non-zero to enable endpoint detection. */
350
+ int32_t enable_endpoint;
351
+
352
+ /** Endpoint rule 1 trailing silence threshold in seconds. */
353
+ float rule1_min_trailing_silence;
354
+
355
+ /** Endpoint rule 2 trailing silence threshold in seconds. */
356
+ float rule2_min_trailing_silence;
357
+
358
+ /** Endpoint rule 3 utterance-length threshold in seconds. */
359
+ float rule3_min_utterance_length;
360
+
361
+ /** Path to a hotwords file. */
362
+ const char *hotwords_file;
363
+
364
+ /** Bonus score added to each hotword token during decoding. */
365
+ float hotwords_score;
366
+
367
+ /** Optional HLG/FST online CTC decoder configuration. */
368
+ SherpaOnnxOnlineCtcFstDecoderConfig ctc_fst_decoder_config;
369
+ /** Path to punctuation or text-processing rule FSTs. */
370
+ const char *rule_fsts;
371
+ /** Path to FAR archives used by text-processing rules. */
372
+ const char *rule_fars;
373
+ /** Optional blank penalty applied during decoding. */
374
+ float blank_penalty;
375
+
376
+ /** Optional in-memory hotwords text used instead of @c hotwords_file. */
377
+ const char *hotwords_buf;
378
+ /** Size in bytes of @c hotwords_buf, excluding the trailing '\0'. */
379
+ int32_t hotwords_buf_size;
380
+ /** Optional homophone replacement configuration. */
381
+ SherpaOnnxHomophoneReplacerConfig hr;
382
+ } SherpaOnnxOnlineRecognizerConfig;
383
+
384
+ /**
385
+ * @brief Incremental recognition result for a streaming ASR stream.
386
+ *
387
+ * All pointers in this struct are owned by the result object returned from
388
+ * SherpaOnnxGetOnlineStreamResult() and become invalid after
389
+ * SherpaOnnxDestroyOnlineRecognizerResult() is called.
390
+ */
391
+ typedef struct SherpaOnnxOnlineRecognizerResult {
392
+ /** Recognized text accumulated so far. */
393
+ const char *text;
394
+
395
+ /**
396
+ * Contiguous memory block containing token strings separated by '\0'.
397
+ *
398
+ * Use @c tokens_arr for convenient indexed access.
399
+ */
400
+ const char *tokens;
401
+
402
+ /** Array of @c count pointers into @c tokens. */
403
+ const char *const *tokens_arr;
404
+
405
+ /**
406
+ * Optional token timestamps in seconds.
407
+ *
408
+ * This field may be NULL when the model does not provide timestamps.
409
+ * When non-NULL, it contains @c count entries and is parallel to
410
+ * @c tokens_arr.
411
+ */
412
+ float *timestamps;
413
+
414
+ /** Number of entries in @c tokens_arr and, when available, @c timestamps. */
415
+ int32_t count;
416
+
417
+ /** JSON serialization of the result. */
418
+ const char *json;
419
+ } SherpaOnnxOnlineRecognizerResult;
420
+
421
+ /** @brief Streaming recognizer handle. */
422
+ typedef struct SherpaOnnxOnlineRecognizer SherpaOnnxOnlineRecognizer;
423
+ /** @brief Streaming decoding state for one utterance or stream. */
424
+ typedef struct SherpaOnnxOnlineStream SherpaOnnxOnlineStream;
425
+
426
+ /**
427
+ * @brief Create a streaming ASR recognizer.
428
+ *
429
+ * The returned recognizer runs locally and does not require Internet access.
430
+ *
431
+ * @param config Recognizer configuration.
432
+ * @return A recognizer handle on success, or NULL if the configuration is
433
+ * invalid. The caller owns the returned object and must free it with
434
+ * SherpaOnnxDestroyOnlineRecognizer().
435
+ *
436
+ * @code
437
+ * SherpaOnnxOnlineRecognizerConfig config;
438
+ * memset(&config, 0, sizeof(config));
439
+ * config.feat_config.sample_rate = 16000;
440
+ * config.feat_config.feature_dim = 80;
441
+ * config.model_config.transducer.encoder =
442
+ * "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/"
443
+ * "encoder-epoch-99-avg-1.int8.onnx";
444
+ * config.model_config.transducer.decoder =
445
+ * "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/"
446
+ * "decoder-epoch-99-avg-1.onnx";
447
+ * config.model_config.transducer.joiner =
448
+ * "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/"
449
+ * "joiner-epoch-99-avg-1.int8.onnx";
450
+ * config.model_config.tokens =
451
+ * "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/"
452
+ * "tokens.txt";
453
+ * config.model_config.provider = "cpu";
454
+ * config.model_config.num_threads = 1;
455
+ * config.decoding_method = "greedy_search";
456
+ *
457
+ * const SherpaOnnxOnlineRecognizer *recognizer =
458
+ * SherpaOnnxCreateOnlineRecognizer(&config);
459
+ * @endcode
460
+ */
461
+ SHERPA_ONNX_API const SherpaOnnxOnlineRecognizer *
462
+ SherpaOnnxCreateOnlineRecognizer(
463
+ const SherpaOnnxOnlineRecognizerConfig *config);
464
+
465
+ /**
466
+ * @brief Destroy a streaming recognizer.
467
+ *
468
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
469
+ *
470
+ * @code
471
+ * SherpaOnnxDestroyOnlineRecognizer(recognizer);
472
+ * recognizer = NULL;
473
+ * @endcode
474
+ */
475
+ SHERPA_ONNX_API void SherpaOnnxDestroyOnlineRecognizer(
476
+ const SherpaOnnxOnlineRecognizer *recognizer);
477
+
478
+ /**
479
+ * @brief Create a streaming ASR state object.
480
+ *
481
+ * One stream corresponds to one decoding state. Reuse the same recognizer to
482
+ * create multiple streams.
483
+ *
484
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
485
+ * @return A newly created stream. The caller owns the returned object and must
486
+ * free it with SherpaOnnxDestroyOnlineStream().
487
+ *
488
+ * @code
489
+ * const SherpaOnnxWave *wave = SherpaOnnxReadWave(
490
+ * "./sherpa-onnx-streaming-paraformer-bilingual-zh-en/test_wavs/0.wav");
491
+ * const SherpaOnnxOnlineStream *stream =
492
+ * SherpaOnnxCreateOnlineStream(recognizer);
493
+ * @endcode
494
+ */
495
+ SHERPA_ONNX_API const SherpaOnnxOnlineStream *SherpaOnnxCreateOnlineStream(
496
+ const SherpaOnnxOnlineRecognizer *recognizer);
497
+
498
+ /**
499
+ * @brief Create a streaming ASR state object with per-stream hotwords.
500
+ *
501
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
502
+ * @param hotwords Hotwords text to associate with the stream.
503
+ * @return A newly created stream. The caller owns the returned object and must
504
+ * free it with SherpaOnnxDestroyOnlineStream().
505
+ *
506
+ * @code
507
+ * const SherpaOnnxOnlineStream *stream =
508
+ * SherpaOnnxCreateOnlineStreamWithHotwords(recognizer,
509
+ * "▁HELLO ▁WORLD");
510
+ * @endcode
511
+ */
512
+ SHERPA_ONNX_API const SherpaOnnxOnlineStream *
513
+ SherpaOnnxCreateOnlineStreamWithHotwords(
514
+ const SherpaOnnxOnlineRecognizer *recognizer, const char *hotwords);
515
+
516
+ /**
517
+ * @brief Destroy a streaming ASR state object.
518
+ *
519
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream() or
520
+ * SherpaOnnxCreateOnlineStreamWithHotwords().
521
+ *
522
+ * @code
523
+ * SherpaOnnxDestroyOnlineStream(stream);
524
+ * stream = NULL;
525
+ * @endcode
526
+ */
527
+ SHERPA_ONNX_API void SherpaOnnxDestroyOnlineStream(
528
+ const SherpaOnnxOnlineStream *stream);
529
+
530
+ /**
531
+ * @brief Append audio samples to a streaming ASR stream.
532
+ *
533
+ * The input is mono floating-point PCM normalized to the range [-1, 1].
534
+ * If @p sample_rate differs from the recognizer feature sample rate,
535
+ * sherpa-onnx resamples internally.
536
+ *
537
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
538
+ * @param sample_rate Sample rate of @p samples.
539
+ * @param samples Pointer to @p n samples in the range [-1, 1].
540
+ * @param n Number of samples.
541
+ *
542
+ * @code
543
+ * int32_t start = 0;
544
+ * int32_t chunk_size = 3200; // 0.2 seconds at 16 kHz
545
+ * SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate,
546
+ * wave->samples + start, chunk_size);
547
+ * @endcode
548
+ */
549
+ SHERPA_ONNX_API void SherpaOnnxOnlineStreamAcceptWaveform(
550
+ const SherpaOnnxOnlineStream *stream, int32_t sample_rate,
551
+ const float *samples, int32_t n);
552
+
553
+ /**
554
+ * @brief Check whether a streaming ASR stream is ready to decode.
555
+ *
556
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
557
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
558
+ * @return 1 if enough frames are available for decoding; otherwise 0.
559
+ *
560
+ * @code
561
+ * if (SherpaOnnxIsOnlineStreamReady(recognizer, stream)) {
562
+ * SherpaOnnxDecodeOnlineStream(recognizer, stream);
563
+ * }
564
+ * @endcode
565
+ */
566
+ SHERPA_ONNX_API int32_t
567
+ SherpaOnnxIsOnlineStreamReady(const SherpaOnnxOnlineRecognizer *recognizer,
568
+ const SherpaOnnxOnlineStream *stream);
569
+
570
+ /**
571
+ * @brief Decode one step of a streaming ASR stream.
572
+ *
573
+ * Call this only when SherpaOnnxIsOnlineStreamReady() returns 1.
574
+ *
575
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
576
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
577
+ *
578
+ * @code
579
+ * SherpaOnnxOnlineStreamAcceptWaveform(stream, sample_rate, samples, n);
580
+ * while (SherpaOnnxIsOnlineStreamReady(recognizer, stream)) {
581
+ * SherpaOnnxDecodeOnlineStream(recognizer, stream);
582
+ * }
583
+ * @endcode
584
+ */
585
+ SHERPA_ONNX_API void SherpaOnnxDecodeOnlineStream(
586
+ const SherpaOnnxOnlineRecognizer *recognizer,
587
+ const SherpaOnnxOnlineStream *stream);
588
+
589
+ /**
590
+ * @brief Decode multiple streaming ASR streams in parallel.
591
+ *
592
+ * The caller must ensure every stream in @p streams is ready before calling
593
+ * this function.
594
+ *
595
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
596
+ * @param streams Array of @p n stream pointers.
597
+ * @param n Number of streams in @p streams.
598
+ *
599
+ * @code
600
+ * const SherpaOnnxOnlineStream *streams[2] = {stream1, stream2};
601
+ * SherpaOnnxDecodeMultipleOnlineStreams(recognizer, streams, 2);
602
+ * @endcode
603
+ */
604
+ SHERPA_ONNX_API void SherpaOnnxDecodeMultipleOnlineStreams(
605
+ const SherpaOnnxOnlineRecognizer *recognizer,
606
+ const SherpaOnnxOnlineStream **streams, int32_t n);
607
+
608
+ /**
609
+ * @brief Get the current streaming ASR result for a stream.
610
+ *
611
+ * The returned snapshot is independent from the stream state. The caller owns
612
+ * it and must free it with SherpaOnnxDestroyOnlineRecognizerResult().
613
+ *
614
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
615
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
616
+ * @return A newly allocated result snapshot.
617
+ *
618
+ * @code
619
+ * const SherpaOnnxOnlineRecognizerResult *r =
620
+ * SherpaOnnxGetOnlineStreamResult(recognizer, stream);
621
+ * printf("%s\n", r->text);
622
+ * // r->tokens_arr[i] and r->timestamps[i] are parallel when timestamps
623
+ * // are available.
624
+ * SherpaOnnxDestroyOnlineRecognizerResult(r);
625
+ * @endcode
626
+ */
627
+ SHERPA_ONNX_API const SherpaOnnxOnlineRecognizerResult *
628
+ SherpaOnnxGetOnlineStreamResult(const SherpaOnnxOnlineRecognizer *recognizer,
629
+ const SherpaOnnxOnlineStream *stream);
630
+
631
+ /**
632
+ * @brief Destroy a result returned by SherpaOnnxGetOnlineStreamResult().
633
+ *
634
+ * @param r A pointer returned by SherpaOnnxGetOnlineStreamResult().
635
+ *
636
+ * @code
637
+ * SherpaOnnxDestroyOnlineRecognizerResult(r);
638
+ * r = NULL;
639
+ * @endcode
640
+ */
641
+ SHERPA_ONNX_API void SherpaOnnxDestroyOnlineRecognizerResult(
642
+ const SherpaOnnxOnlineRecognizerResult *r);
643
+
644
+ /**
645
+ * @brief Get the current streaming ASR result as JSON.
646
+ *
647
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
648
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
649
+ * @return A newly allocated JSON string. Free it with
650
+ * SherpaOnnxDestroyOnlineStreamResultJson().
651
+ *
652
+ * @code
653
+ * const char *json =
654
+ * SherpaOnnxGetOnlineStreamResultAsJson(recognizer, stream);
655
+ * puts(json);
656
+ * SherpaOnnxDestroyOnlineStreamResultJson(json);
657
+ * @endcode
658
+ */
659
+ SHERPA_ONNX_API const char *SherpaOnnxGetOnlineStreamResultAsJson(
660
+ const SherpaOnnxOnlineRecognizer *recognizer,
661
+ const SherpaOnnxOnlineStream *stream);
662
+
663
+ /**
664
+ * @brief Free a JSON string returned by
665
+ * SherpaOnnxGetOnlineStreamResultAsJson().
666
+ *
667
+ * @param s A pointer returned by SherpaOnnxGetOnlineStreamResultAsJson().
668
+ *
669
+ * @code
670
+ * SherpaOnnxDestroyOnlineStreamResultJson(json);
671
+ * json = NULL;
672
+ * @endcode
673
+ */
674
+ SHERPA_ONNX_API void SherpaOnnxDestroyOnlineStreamResultJson(const char *s);
675
+
676
+ /**
677
+ * @brief Reset a streaming ASR stream after an endpoint or utterance boundary.
678
+ *
679
+ * This clears the decoder state for the stream so that it can be reused for a
680
+ * new utterance.
681
+ *
682
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
683
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
684
+ *
685
+ * @code
686
+ * if (SherpaOnnxOnlineStreamIsEndpoint(recognizer, stream)) {
687
+ * SherpaOnnxOnlineStreamReset(recognizer, stream);
688
+ * }
689
+ * @endcode
690
+ */
691
+ SHERPA_ONNX_API void SherpaOnnxOnlineStreamReset(
692
+ const SherpaOnnxOnlineRecognizer *recognizer,
693
+ const SherpaOnnxOnlineStream *stream);
694
+
695
+ /**
696
+ * @brief Signal end-of-input for a streaming ASR stream.
697
+ *
698
+ * After calling this function, do not append more samples to the stream.
699
+ *
700
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
701
+ *
702
+ * @code
703
+ * SherpaOnnxOnlineStreamInputFinished(stream);
704
+ * @endcode
705
+ */
706
+ SHERPA_ONNX_API void SherpaOnnxOnlineStreamInputFinished(
707
+ const SherpaOnnxOnlineStream *stream);
708
+
709
+ /**
710
+ * @brief Set a per-stream runtime option.
711
+ *
712
+ * This is a generic extension point for model-specific or runtime-specific
713
+ * options such as "is_final" for streaming Paraformer.
714
+ *
715
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
716
+ * @param key Option name.
717
+ * @param value Option value represented as text.
718
+ *
719
+ * @code
720
+ * SherpaOnnxOnlineStreamSetOption(stream, "is_final", "1");
721
+ * @endcode
722
+ */
723
+ SHERPA_ONNX_API void SherpaOnnxOnlineStreamSetOption(
724
+ const SherpaOnnxOnlineStream *stream, const char *key, const char *value);
725
+
726
+ /**
727
+ * @brief Get a per-stream runtime option.
728
+ *
729
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
730
+ * @param key Option name.
731
+ * @return The option value. The returned pointer is owned by the stream, must
732
+ * not be freed by the caller, and may be invalidated if the option is
733
+ * overwritten or the stream is destroyed.
734
+ *
735
+ * @code
736
+ * const char *value = SherpaOnnxOnlineStreamGetOption(stream, "is_final");
737
+ * @endcode
738
+ */
739
+ SHERPA_ONNX_API const char *SherpaOnnxOnlineStreamGetOption(
740
+ const SherpaOnnxOnlineStream *stream, const char *key);
741
+
742
+ /**
743
+ * @brief Check whether a per-stream runtime option exists.
744
+ *
745
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
746
+ * @param key Option name.
747
+ * @return 1 if the option exists; otherwise 0.
748
+ *
749
+ * @code
750
+ * int32_t has_option = SherpaOnnxOnlineStreamHasOption(stream, "is_final");
751
+ * @endcode
752
+ */
753
+ SHERPA_ONNX_API int32_t SherpaOnnxOnlineStreamHasOption(
754
+ const SherpaOnnxOnlineStream *stream, const char *key);
755
+
756
+ /**
757
+ * @brief Check whether endpoint detection has triggered for a stream.
758
+ *
759
+ * @param recognizer A pointer returned by SherpaOnnxCreateOnlineRecognizer().
760
+ * @param stream A pointer returned by SherpaOnnxCreateOnlineStream().
761
+ * @return 1 if an endpoint is detected; otherwise 0.
762
+ *
763
+ * @code
764
+ * if (SherpaOnnxOnlineStreamIsEndpoint(recognizer, stream)) {
765
+ * SherpaOnnxOnlineStreamReset(recognizer, stream);
766
+ * }
767
+ * @endcode
768
+ */
769
+ SHERPA_ONNX_API int32_t
770
+ SherpaOnnxOnlineStreamIsEndpoint(const SherpaOnnxOnlineRecognizer *recognizer,
771
+ const SherpaOnnxOnlineStream *stream);
772
+
773
+ /**
774
+ * @brief Helper for pretty-printing incremental recognition results.
775
+ *
776
+ * This utility is mainly used by example programs on Linux and macOS.
777
+ */
778
+ typedef struct SherpaOnnxDisplay SherpaOnnxDisplay;
779
+
780
+ /**
781
+ * @brief Create a display helper.
782
+ *
783
+ * @param max_word_per_line Maximum number of words to show per line.
784
+ * @return A newly allocated display helper. Free it with
785
+ * SherpaOnnxDestroyDisplay().
786
+ *
787
+ * @code
788
+ * const SherpaOnnxDisplay *display = SherpaOnnxCreateDisplay(50);
789
+ * @endcode
790
+ */
791
+ SHERPA_ONNX_API const SherpaOnnxDisplay *SherpaOnnxCreateDisplay(
792
+ int32_t max_word_per_line);
793
+
794
+ /**
795
+ * @brief Destroy a display helper.
796
+ *
797
+ * @param display A pointer returned by SherpaOnnxCreateDisplay().
798
+ */
799
+ SHERPA_ONNX_API void SherpaOnnxDestroyDisplay(const SherpaOnnxDisplay *display);
800
+
801
+ /**
802
+ * @brief Print one line of text using the display helper.
803
+ *
804
+ * @param display A pointer returned by SherpaOnnxCreateDisplay().
805
+ * @param idx Segment or utterance index to print.
806
+ * @param s Text to print.
807
+ *
808
+ * @code
809
+ * SherpaOnnxPrint(display, segment_id, r->text);
810
+ * @endcode
811
+ */
812
+ SHERPA_ONNX_API void SherpaOnnxPrint(const SherpaOnnxDisplay *display,
813
+ int32_t idx, const char *s);
814
+ // ============================================================
815
+ // For offline ASR (i.e., non-streaming ASR)
816
+ // ============================================================
817
+
818
+ /**
819
+ * @brief Configuration for a non-streaming transducer model.
820
+ */
821
+ typedef struct SherpaOnnxOfflineTransducerModelConfig {
822
+ /** Path to the encoder ONNX model. */
823
+ const char *encoder;
824
+ /** Path to the decoder ONNX model. */
825
+ const char *decoder;
826
+ /** Path to the joiner ONNX model. */
827
+ const char *joiner;
828
+ } SherpaOnnxOfflineTransducerModelConfig;
829
+
830
+ /** @brief Configuration for a non-streaming Paraformer model. */
831
+ typedef struct SherpaOnnxOfflineParaformerModelConfig {
832
+ /** Path to the ONNX model. */
833
+ const char *model;
834
+ } SherpaOnnxOfflineParaformerModelConfig;
835
+
836
+ /** @brief Configuration for a non-streaming NeMo CTC model. */
837
+ typedef struct SherpaOnnxOfflineNemoEncDecCtcModelConfig {
838
+ /** Path to the ONNX model. */
839
+ const char *model;
840
+ } SherpaOnnxOfflineNemoEncDecCtcModelConfig;
841
+
842
+ /**
843
+ * @brief Configuration for a non-streaming Whisper model.
844
+ */
845
+ typedef struct SherpaOnnxOfflineWhisperModelConfig {
846
+ /** Path to the encoder ONNX model. */
847
+ const char *encoder;
848
+ /** Path to the decoder ONNX model. */
849
+ const char *decoder;
850
+ /** Optional language hint, for example "en" or "zh". */
851
+ const char *language;
852
+ /** Optional Whisper task such as "transcribe" or "translate". */
853
+ const char *task;
854
+ /** Number of tail padding frames appended internally. */
855
+ int32_t tail_paddings;
856
+
857
+ /** Non-zero to enable token-level timestamps when supported by the model. */
858
+ int32_t enable_token_timestamps;
859
+
860
+ /** Non-zero to enable Whisper segment-level timestamps. */
861
+ int32_t enable_segment_timestamps;
862
+ } SherpaOnnxOfflineWhisperModelConfig;
863
+
864
+ /** @brief Configuration for a Canary model. */
865
+ typedef struct SherpaOnnxOfflineCanaryModelConfig {
866
+ /** Path to the encoder ONNX model. */
867
+ const char *encoder;
868
+ /** Path to the decoder ONNX model. */
869
+ const char *decoder;
870
+ /** Source language hint. */
871
+ const char *src_lang;
872
+ /** Target language hint. */
873
+ const char *tgt_lang;
874
+ /** Non-zero to enable punctuation and capitalization when supported. */
875
+ int32_t use_pnc;
876
+ } SherpaOnnxOfflineCanaryModelConfig;
877
+
878
+ /** @brief Configuration for a Cohere Transcribe model. */
879
+ typedef struct SherpaOnnxOfflineCohereTranscribeModelConfig {
880
+ /** Path to the encoder ONNX model. */
881
+ const char *encoder;
882
+ /** Path to the decoder ONNX model. */
883
+ const char *decoder;
884
+ /** Optional language hint, for example "en" or "zh". */
885
+ const char *language;
886
+ /** Non-zero to enable punctuation. */
887
+ int32_t use_punct;
888
+ /** Non-zero to enable inverse text normalization. */
889
+ int32_t use_itn;
890
+ } SherpaOnnxOfflineCohereTranscribeModelConfig;
891
+
892
+ /** @brief Configuration for a FireRedAsr encoder/decoder model. */
893
+ typedef struct SherpaOnnxOfflineFireRedAsrModelConfig {
894
+ /** Path to the encoder ONNX model. */
895
+ const char *encoder;
896
+ /** Path to the decoder ONNX model. */
897
+ const char *decoder;
898
+ } SherpaOnnxOfflineFireRedAsrModelConfig;
899
+
900
+ /** @brief Configuration for a FireRedAsr CTC model. */
901
+ typedef struct SherpaOnnxOfflineFireRedAsrCtcModelConfig {
902
+ /** Path to the ONNX model. */
903
+ const char *model;
904
+ } SherpaOnnxOfflineFireRedAsrCtcModelConfig;
905
+
906
+ /** @brief Configuration for a Moonshine model. */
907
+ typedef struct SherpaOnnxOfflineMoonshineModelConfig {
908
+ /** Path to the preprocessor ONNX model. */
909
+ const char *preprocessor;
910
+ /** Path to the encoder ONNX model. */
911
+ const char *encoder;
912
+ /** Path to the uncached decoder ONNX model. */
913
+ const char *uncached_decoder;
914
+ /** Path to the cached decoder ONNX model. */
915
+ const char *cached_decoder;
916
+ /** Path to the merged decoder ONNX model. */
917
+ const char *merged_decoder;
918
+ } SherpaOnnxOfflineMoonshineModelConfig;
919
+
920
+ /** @brief Configuration for a TDNN model. */
921
+ typedef struct SherpaOnnxOfflineTdnnModelConfig {
922
+ /** Path to the ONNX model. */
923
+ const char *model;
924
+ } SherpaOnnxOfflineTdnnModelConfig;
925
+
926
+ /** @brief Configuration for an offline language model. */
927
+ typedef struct SherpaOnnxOfflineLMConfig {
928
+ /** Path to the language model. */
929
+ const char *model;
930
+ /** Interpolation scale for the language model. */
931
+ float scale;
932
+ } SherpaOnnxOfflineLMConfig;
933
+
934
+ /** @brief Configuration for a SenseVoice model. */
935
+ typedef struct SherpaOnnxOfflineSenseVoiceModelConfig {
936
+ /** Path to the ONNX model. */
937
+ const char *model;
938
+ /** Optional language hint. */
939
+ const char *language;
940
+ /** Non-zero to enable inverse text normalization. */
941
+ int32_t use_itn;
942
+ } SherpaOnnxOfflineSenseVoiceModelConfig;
943
+
944
+ /** @brief Configuration for a Dolphin model. */
945
+ typedef struct SherpaOnnxOfflineDolphinModelConfig {
946
+ /** Path to the ONNX model. */
947
+ const char *model;
948
+ } SherpaOnnxOfflineDolphinModelConfig;
949
+
950
+ /** @brief Configuration for an offline Zipformer CTC model. */
951
+ typedef struct SherpaOnnxOfflineZipformerCtcModelConfig {
952
+ /** Path to the ONNX model. */
953
+ const char *model;
954
+ } SherpaOnnxOfflineZipformerCtcModelConfig;
955
+
956
+ /** @brief Configuration for an offline WeNet CTC model. */
957
+ typedef struct SherpaOnnxOfflineWenetCtcModelConfig {
958
+ /** Path to the ONNX model. */
959
+ const char *model;
960
+ } SherpaOnnxOfflineWenetCtcModelConfig;
961
+
962
+ /** @brief Configuration for an omnilingual offline CTC model. */
963
+ typedef struct SherpaOnnxOfflineOmnilingualAsrCtcModelConfig {
964
+ /** Path to the ONNX model. */
965
+ const char *model;
966
+ } SherpaOnnxOfflineOmnilingualAsrCtcModelConfig;
967
+
968
+ /** @brief Configuration for an offline FunASR Nano model. */
969
+ typedef struct SherpaOnnxOfflineFunASRNanoModelConfig {
970
+ /** Path to the encoder adaptor. */
971
+ const char *encoder_adaptor;
972
+ /** Path to the LLM ONNX model. */
973
+ const char *llm;
974
+ /** Path to the embedding model. */
975
+ const char *embedding;
976
+ /** Path to the tokenizer file. */
977
+ const char *tokenizer;
978
+ /** System prompt. */
979
+ const char *system_prompt;
980
+ /** User prompt. */
981
+ const char *user_prompt;
982
+ /** Maximum number of generated tokens. */
983
+ int32_t max_new_tokens;
984
+ /** Sampling temperature. */
985
+ float temperature;
986
+ /** Top-p sampling threshold. */
987
+ float top_p;
988
+ /** Random seed. */
989
+ int32_t seed;
990
+ /** Optional language hint. */
991
+ const char *language;
992
+ /** Non-zero to enable inverse text normalization. */
993
+ int32_t itn;
994
+ /** Optional hotwords text. */
995
+ const char *hotwords;
996
+ } SherpaOnnxOfflineFunASRNanoModelConfig;
997
+
998
+ /** @brief Configuration for an offline Qwen3-ASR model. */
999
+ typedef struct SherpaOnnxOfflineQwen3ASRModelConfig {
1000
+ /** Path to the conv-frontend ONNX model. */
1001
+ const char *conv_frontend;
1002
+ /** Path to the encoder ONNX model. */
1003
+ const char *encoder;
1004
+ /** Path to the decoder ONNX model (with KV cache). */
1005
+ const char *decoder;
1006
+ /** Path to the tokenizer directory (e.g. containing `vocab.json`). */
1007
+ const char *tokenizer;
1008
+ /** Maximum total sequence length supported by the model. */
1009
+ int32_t max_total_len;
1010
+ /** Maximum number of new tokens to generate. */
1011
+ int32_t max_new_tokens;
1012
+ /** Sampling temperature. */
1013
+ float temperature;
1014
+ /** Top-p (nucleus) sampling threshold. */
1015
+ float top_p;
1016
+ /** Random seed for reproducible sampling. */
1017
+ int32_t seed;
1018
+ /** Optional comma-separated hotwords (UTF-8, ASCII ','), e.g. @c
1019
+ * "foo,bar,baz". */
1020
+ const char *hotwords;
1021
+ } SherpaOnnxOfflineQwen3ASRModelConfig;
1022
+
1023
+ /** @brief Configuration for a MedASR CTC model. */
1024
+ typedef struct SherpaOnnxOfflineMedAsrCtcModelConfig {
1025
+ /** Path to the ONNX model. */
1026
+ const char *model;
1027
+ } SherpaOnnxOfflineMedAsrCtcModelConfig;
1028
+
1029
+ /**
1030
+ * @brief Model configuration shared by offline ASR recognizers.
1031
+ *
1032
+ * Zero-initialize this struct before use, then fill in exactly the sub-config
1033
+ * needed by the model family you want to run.
1034
+ *
1035
+ * Exactly one model family should be configured for each recognizer. For
1036
+ * example, set only one of @c transducer, @c paraformer, @c nemo_ctc,
1037
+ * @c whisper, @c tdnn, @c sense_voice, @c moonshine, @c fire_red_asr,
1038
+ * @c dolphin, @c zipformer_ctc, @c canary, @c cohere_transcribe,
1039
+ * @c wenet_ctc, @c omnilingual, @c medasr, @c funasr_nano,
1040
+ * @c fire_red_asr_ctc, or @c qwen3_asr.
1041
+ *
1042
+ * If multiple model families are configured at the same time, the
1043
+ * implementation will choose one of them, and which one is used is
1044
+ * implementation-defined. Do not rely on any precedence rule.
1045
+ */
1046
+ typedef struct SherpaOnnxOfflineModelConfig {
1047
+ /** Non-streaming transducer model files. */
1048
+ SherpaOnnxOfflineTransducerModelConfig transducer;
1049
+ /** Non-streaming Paraformer model files. */
1050
+ SherpaOnnxOfflineParaformerModelConfig paraformer;
1051
+ /** Non-streaming NeMo CTC model files. */
1052
+ SherpaOnnxOfflineNemoEncDecCtcModelConfig nemo_ctc;
1053
+ /** Whisper model files and options. */
1054
+ SherpaOnnxOfflineWhisperModelConfig whisper;
1055
+ /** TDNN model files. */
1056
+ SherpaOnnxOfflineTdnnModelConfig tdnn;
1057
+
1058
+ /** Path to the tokens file. */
1059
+ const char *tokens;
1060
+ /** Number of backend threads. */
1061
+ int32_t num_threads;
1062
+ /** Non-zero to print debug information. */
1063
+ int32_t debug;
1064
+ /** Execution provider, for example "cpu" or "cuda". */
1065
+ const char *provider;
1066
+ /** Optional explicit model type override. */
1067
+ const char *model_type;
1068
+ /** Modeling unit, such as "cjkchar", "bpe", or "cjkchar+bpe". */
1069
+ const char *modeling_unit;
1070
+ /** Path to the BPE vocabulary file when BPE is used. */
1071
+ const char *bpe_vocab;
1072
+ /** Path to the TeleSpeech CTC model. */
1073
+ const char *telespeech_ctc;
1074
+ /** SenseVoice configuration. */
1075
+ SherpaOnnxOfflineSenseVoiceModelConfig sense_voice;
1076
+ /** Moonshine configuration. */
1077
+ SherpaOnnxOfflineMoonshineModelConfig moonshine;
1078
+ /** FireRedAsr configuration. */
1079
+ SherpaOnnxOfflineFireRedAsrModelConfig fire_red_asr;
1080
+ /** Dolphin configuration. */
1081
+ SherpaOnnxOfflineDolphinModelConfig dolphin;
1082
+ /** Zipformer CTC configuration. */
1083
+ SherpaOnnxOfflineZipformerCtcModelConfig zipformer_ctc;
1084
+ /** Canary configuration. */
1085
+ SherpaOnnxOfflineCanaryModelConfig canary;
1086
+ /** WeNet CTC configuration. */
1087
+ SherpaOnnxOfflineWenetCtcModelConfig wenet_ctc;
1088
+ /** Omnilingual CTC configuration. */
1089
+ SherpaOnnxOfflineOmnilingualAsrCtcModelConfig omnilingual;
1090
+ /** MedASR configuration. */
1091
+ SherpaOnnxOfflineMedAsrCtcModelConfig medasr;
1092
+ /** FunASR Nano configuration. */
1093
+ SherpaOnnxOfflineFunASRNanoModelConfig funasr_nano;
1094
+ /** FireRedAsr CTC configuration. */
1095
+ SherpaOnnxOfflineFireRedAsrCtcModelConfig fire_red_asr_ctc;
1096
+ /** Qwen3-ASR configuration. */
1097
+ SherpaOnnxOfflineQwen3ASRModelConfig qwen3_asr;
1098
+ /** Cohere Transcribe configuration. */
1099
+ SherpaOnnxOfflineCohereTranscribeModelConfig cohere_transcribe;
1100
+ } SherpaOnnxOfflineModelConfig;
1101
+
1102
+ /**
1103
+ * @brief Configuration for a non-streaming ASR recognizer.
1104
+ *
1105
+ * Zero-initialize this struct before use.
1106
+ *
1107
+ * Example using Whisper:
1108
+ *
1109
+ * @code
1110
+ * SherpaOnnxOfflineRecognizerConfig config;
1111
+ * memset(&config, 0, sizeof(config));
1112
+ *
1113
+ * config.feat_config.sample_rate = 16000;
1114
+ * config.feat_config.feature_dim = 80;
1115
+ *
1116
+ * config.model_config.whisper.encoder =
1117
+ * "./sherpa-onnx-whisper-tiny/tiny-encoder.onnx";
1118
+ * config.model_config.whisper.decoder =
1119
+ * "./sherpa-onnx-whisper-tiny/tiny-decoder.onnx";
1120
+ * config.model_config.whisper.language = "en";
1121
+ * config.model_config.whisper.task = "transcribe";
1122
+ * config.model_config.tokens =
1123
+ * "./sherpa-onnx-whisper-tiny/tiny-tokens.txt";
1124
+ * config.model_config.provider = "cpu";
1125
+ * config.model_config.num_threads = 1;
1126
+ *
1127
+ * config.decoding_method = "greedy_search";
1128
+ * @endcode
1129
+ *
1130
+ * Example using SenseVoice:
1131
+ *
1132
+ * @code
1133
+ * config.model_config.sense_voice.model =
1134
+ * "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17-int8/model.int8.onnx";
1135
+ * config.model_config.sense_voice.language = "auto";
1136
+ * config.model_config.sense_voice.use_itn = 1;
1137
+ * config.model_config.tokens =
1138
+ * "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17-int8/tokens.txt";
1139
+ * @endcode
1140
+ *
1141
+ * Example using Parakeet TDT:
1142
+ *
1143
+ * @code
1144
+ * config.model_config.transducer.encoder =
1145
+ * "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/encoder.int8.onnx";
1146
+ * config.model_config.transducer.decoder =
1147
+ * "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/decoder.int8.onnx";
1148
+ * config.model_config.transducer.joiner =
1149
+ * "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/joiner.int8.onnx";
1150
+ * config.model_config.tokens =
1151
+ * "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/tokens.txt";
1152
+ * config.model_config.model_type = "nemo_transducer";
1153
+ * @endcode
1154
+ */
1155
+ typedef struct SherpaOnnxOfflineRecognizerConfig {
1156
+ /** Feature extraction settings. */
1157
+ SherpaOnnxFeatureConfig feat_config;
1158
+ /** Offline model configuration. */
1159
+ SherpaOnnxOfflineModelConfig model_config;
1160
+ /** Optional language model configuration. */
1161
+ SherpaOnnxOfflineLMConfig lm_config;
1162
+
1163
+ /** Decoding method, for example "greedy_search" or "modified_beam_search". */
1164
+ const char *decoding_method;
1165
+ /** Number of active paths for modified beam search. */
1166
+ int32_t max_active_paths;
1167
+
1168
+ /** Path to a hotwords file. */
1169
+ const char *hotwords_file;
1170
+
1171
+ /** Bonus score added to each hotword token. */
1172
+ float hotwords_score;
1173
+ /** Path to punctuation or text-processing rule FSTs. */
1174
+ const char *rule_fsts;
1175
+ /** Path to FAR archives used by text-processing rules. */
1176
+ const char *rule_fars;
1177
+ /** Optional blank penalty applied during decoding. */
1178
+ float blank_penalty;
1179
+
1180
+ /** Optional homophone replacement configuration. */
1181
+ SherpaOnnxHomophoneReplacerConfig hr;
1182
+ } SherpaOnnxOfflineRecognizerConfig;
1183
+
1184
+ /** @brief Non-streaming recognizer handle. */
1185
+ typedef struct SherpaOnnxOfflineRecognizer SherpaOnnxOfflineRecognizer;
1186
+
1187
+ /** @brief Non-streaming decoding state for one utterance. */
1188
+ typedef struct SherpaOnnxOfflineStream SherpaOnnxOfflineStream;
1189
+
1190
+ /**
1191
+ * @brief Create a non-streaming ASR recognizer.
1192
+ *
1193
+ * @param config Recognizer configuration.
1194
+ * @return A recognizer handle on success, or NULL if the configuration is
1195
+ * invalid. The caller owns the returned object and must free it with
1196
+ * SherpaOnnxDestroyOfflineRecognizer().
1197
+ *
1198
+ * Whisper example:
1199
+ *
1200
+ * @code
1201
+ * SherpaOnnxOfflineRecognizerConfig config;
1202
+ * memset(&config, 0, sizeof(config));
1203
+ * config.feat_config.sample_rate = 16000;
1204
+ * config.feat_config.feature_dim = 80;
1205
+ * config.model_config.whisper.encoder =
1206
+ * "./sherpa-onnx-whisper-tiny/tiny-encoder.onnx";
1207
+ * config.model_config.whisper.decoder =
1208
+ * "./sherpa-onnx-whisper-tiny/tiny-decoder.onnx";
1209
+ * config.model_config.whisper.language = "en";
1210
+ * config.model_config.whisper.task = "transcribe";
1211
+ * config.model_config.tokens =
1212
+ * "./sherpa-onnx-whisper-tiny/tiny-tokens.txt";
1213
+ * config.model_config.provider = "cpu";
1214
+ * config.model_config.num_threads = 1;
1215
+ * config.decoding_method = "greedy_search";
1216
+ *
1217
+ * const SherpaOnnxOfflineRecognizer *recognizer =
1218
+ * SherpaOnnxCreateOfflineRecognizer(&config);
1219
+ * @endcode
1220
+ *
1221
+ * SenseVoice example:
1222
+ *
1223
+ * @code
1224
+ * config.model_config.sense_voice.model =
1225
+ * "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17-int8/model.int8.onnx";
1226
+ * config.model_config.sense_voice.language = "auto";
1227
+ * config.model_config.sense_voice.use_itn = 1;
1228
+ * config.model_config.tokens =
1229
+ * "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17-int8/tokens.txt";
1230
+ * @endcode
1231
+ *
1232
+ * Parakeet TDT example:
1233
+ *
1234
+ * @code
1235
+ * config.model_config.transducer.encoder =
1236
+ * "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/encoder.int8.onnx";
1237
+ * config.model_config.transducer.decoder =
1238
+ * "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/decoder.int8.onnx";
1239
+ * config.model_config.transducer.joiner =
1240
+ * "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/joiner.int8.onnx";
1241
+ * config.model_config.tokens =
1242
+ * "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/tokens.txt";
1243
+ * config.model_config.model_type = "nemo_transducer";
1244
+ * @endcode
1245
+ */
1246
+ SHERPA_ONNX_API const SherpaOnnxOfflineRecognizer *
1247
+ SherpaOnnxCreateOfflineRecognizer(
1248
+ const SherpaOnnxOfflineRecognizerConfig *config);
1249
+
1250
+ /**
1251
+ * @brief Update the configuration of an existing offline recognizer.
1252
+ *
1253
+ * @param recognizer Recognizer handle.
1254
+ * @param config New recognizer configuration.
1255
+ *
1256
+ * @code
1257
+ * SherpaOnnxOfflineRecognizerSetConfig(recognizer, &config);
1258
+ * @endcode
1259
+ */
1260
+ SHERPA_ONNX_API void SherpaOnnxOfflineRecognizerSetConfig(
1261
+ const SherpaOnnxOfflineRecognizer *recognizer,
1262
+ const SherpaOnnxOfflineRecognizerConfig *config);
1263
+
1264
+ /**
1265
+ * @brief Destroy a non-streaming recognizer.
1266
+ *
1267
+ * @param recognizer A pointer returned by SherpaOnnxCreateOfflineRecognizer().
1268
+ *
1269
+ * @code
1270
+ * SherpaOnnxDestroyOfflineRecognizer(recognizer);
1271
+ * recognizer = NULL;
1272
+ * @endcode
1273
+ */
1274
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineRecognizer(
1275
+ const SherpaOnnxOfflineRecognizer *recognizer);
1276
+
1277
+ /**
1278
+ * @brief Create a non-streaming ASR input stream.
1279
+ *
1280
+ * @param recognizer A pointer returned by SherpaOnnxCreateOfflineRecognizer().
1281
+ * @return A newly created stream. The caller owns the returned object and must
1282
+ * free it with SherpaOnnxDestroyOfflineStream().
1283
+ *
1284
+ * @code
1285
+ * const SherpaOnnxWave *wave =
1286
+ * SherpaOnnxReadWave("./sherpa-onnx-whisper-tiny.en/test_wavs/0.wav");
1287
+ * const SherpaOnnxOfflineStream *stream =
1288
+ * SherpaOnnxCreateOfflineStream(recognizer);
1289
+ * @endcode
1290
+ */
1291
+ SHERPA_ONNX_API const SherpaOnnxOfflineStream *SherpaOnnxCreateOfflineStream(
1292
+ const SherpaOnnxOfflineRecognizer *recognizer);
1293
+
1294
+ /**
1295
+ * @brief Create a non-streaming ASR input stream with per-stream hotwords.
1296
+ *
1297
+ * @param recognizer A pointer returned by SherpaOnnxCreateOfflineRecognizer().
1298
+ * @param hotwords Hotwords text to associate with the stream.
1299
+ * @return A newly created stream. The caller owns the returned object and must
1300
+ * free it with SherpaOnnxDestroyOfflineStream().
1301
+ *
1302
+ * @code
1303
+ * const SherpaOnnxOfflineStream *stream =
1304
+ * SherpaOnnxCreateOfflineStreamWithHotwords(recognizer,
1305
+ * "▁HELLO ▁WORLD");
1306
+ * @endcode
1307
+ */
1308
+ SHERPA_ONNX_API const SherpaOnnxOfflineStream *
1309
+ SherpaOnnxCreateOfflineStreamWithHotwords(
1310
+ const SherpaOnnxOfflineRecognizer *recognizer, const char *hotwords);
1311
+
1312
+ /**
1313
+ * @brief Destroy a non-streaming ASR stream.
1314
+ *
1315
+ * @param stream A pointer returned by SherpaOnnxCreateOfflineStream() or
1316
+ * SherpaOnnxCreateOfflineStreamWithHotwords().
1317
+ *
1318
+ * @code
1319
+ * SherpaOnnxDestroyOfflineStream(stream);
1320
+ * stream = NULL;
1321
+ * @endcode
1322
+ */
1323
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineStream(
1324
+ const SherpaOnnxOfflineStream *stream);
1325
+
1326
+ /**
1327
+ * @brief Provide the full utterance to an offline ASR stream.
1328
+ *
1329
+ * The input is mono floating-point PCM normalized to the range [-1, 1].
1330
+ * If @p sample_rate differs from the recognizer feature sample rate,
1331
+ * sherpa-onnx resamples internally.
1332
+ *
1333
+ * @warning Call this function at most once for each offline stream. Offline
1334
+ * recognition expects the entire utterance in a single call.
1335
+ *
1336
+ * @param stream A pointer returned by SherpaOnnxCreateOfflineStream().
1337
+ * @param sample_rate Sample rate of @p samples.
1338
+ * @param samples Pointer to @p n samples in the range [-1, 1].
1339
+ * @param n Number of samples.
1340
+ *
1341
+ * @code
1342
+ * const SherpaOnnxWave *wave =
1343
+ * SherpaOnnxReadWave("./sherpa-onnx-whisper-tiny.en/test_wavs/0.wav");
1344
+ * const SherpaOnnxOfflineStream *stream =
1345
+ * SherpaOnnxCreateOfflineStream(recognizer);
1346
+ * SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate,
1347
+ * wave->samples, wave->num_samples);
1348
+ * SherpaOnnxDecodeOfflineStream(recognizer, stream);
1349
+ * @endcode
1350
+ */
1351
+ SHERPA_ONNX_API void SherpaOnnxAcceptWaveformOffline(
1352
+ const SherpaOnnxOfflineStream *stream, int32_t sample_rate,
1353
+ const float *samples, int32_t n);
1354
+
1355
+ /**
1356
+ * @brief Set a per-stream runtime option for offline ASR.
1357
+ *
1358
+ * @param stream A pointer returned by SherpaOnnxCreateOfflineStream().
1359
+ * @param key Option name.
1360
+ * @param value Option value represented as text.
1361
+ *
1362
+ * @code
1363
+ * SherpaOnnxOfflineStreamSetOption(stream, "language", "en");
1364
+ * @endcode
1365
+ */
1366
+ SHERPA_ONNX_API void SherpaOnnxOfflineStreamSetOption(
1367
+ const SherpaOnnxOfflineStream *stream, const char *key, const char *value);
1368
+
1369
+ /**
1370
+ * @brief Get a per-stream runtime option for offline ASR.
1371
+ *
1372
+ * @param stream A pointer returned by SherpaOnnxCreateOfflineStream().
1373
+ * @param key Option name.
1374
+ * @return The option value. The returned pointer is owned by the stream, must
1375
+ * not be freed by the caller, and may be invalidated if the option is
1376
+ * overwritten or the stream is destroyed.
1377
+ *
1378
+ * @code
1379
+ * const char *value = SherpaOnnxOfflineStreamGetOption(stream, "language");
1380
+ * @endcode
1381
+ */
1382
+ SHERPA_ONNX_API const char *SherpaOnnxOfflineStreamGetOption(
1383
+ const SherpaOnnxOfflineStream *stream, const char *key);
1384
+
1385
+ /**
1386
+ * @brief Check whether a per-stream runtime option exists.
1387
+ *
1388
+ * @param stream A pointer returned by SherpaOnnxCreateOfflineStream().
1389
+ * @param key Option name.
1390
+ * @return 1 if the option exists; otherwise 0.
1391
+ *
1392
+ * @code
1393
+ * int32_t has_language =
1394
+ * SherpaOnnxOfflineStreamHasOption(stream, "language");
1395
+ * @endcode
1396
+ */
1397
+ SHERPA_ONNX_API int32_t SherpaOnnxOfflineStreamHasOption(
1398
+ const SherpaOnnxOfflineStream *stream, const char *key);
1399
+
1400
+ /**
1401
+ * @brief Run offline ASR on one stream.
1402
+ *
1403
+ * Call this after SherpaOnnxAcceptWaveformOffline().
1404
+ *
1405
+ * @param recognizer A pointer returned by SherpaOnnxCreateOfflineRecognizer().
1406
+ * @param stream A pointer returned by SherpaOnnxCreateOfflineStream().
1407
+ *
1408
+ * @code
1409
+ * SherpaOnnxDecodeOfflineStream(recognizer, stream);
1410
+ * @endcode
1411
+ */
1412
+ SHERPA_ONNX_API void SherpaOnnxDecodeOfflineStream(
1413
+ const SherpaOnnxOfflineRecognizer *recognizer,
1414
+ const SherpaOnnxOfflineStream *stream);
1415
+
1416
+ /**
1417
+ * @brief Run offline ASR on multiple streams in parallel.
1418
+ *
1419
+ * The caller must have already provided one utterance to each stream via
1420
+ * SherpaOnnxAcceptWaveformOffline().
1421
+ *
1422
+ * @param recognizer A pointer returned by SherpaOnnxCreateOfflineRecognizer().
1423
+ * @param streams Array of @p n offline stream pointers.
1424
+ * @param n Number of streams in @p streams.
1425
+ *
1426
+ * @code
1427
+ * const SherpaOnnxOfflineStream *streams[2] = {stream1, stream2};
1428
+ * SherpaOnnxDecodeMultipleOfflineStreams(recognizer, streams, 2);
1429
+ * @endcode
1430
+ */
1431
+ SHERPA_ONNX_API void SherpaOnnxDecodeMultipleOfflineStreams(
1432
+ const SherpaOnnxOfflineRecognizer *recognizer,
1433
+ const SherpaOnnxOfflineStream **streams, int32_t n);
1434
+
1435
+ /**
1436
+ * @brief Recognition result for a non-streaming ASR stream.
1437
+ *
1438
+ * All pointers in this struct are owned by the result object returned from
1439
+ * SherpaOnnxGetOfflineStreamResult() and become invalid after
1440
+ * SherpaOnnxDestroyOfflineRecognizerResult() is called.
1441
+ */
1442
+ typedef struct SherpaOnnxOfflineRecognizerResult {
1443
+ /** Recognized text. */
1444
+ const char *text;
1445
+
1446
+ /**
1447
+ * Optional token timestamps in seconds.
1448
+ *
1449
+ * This field may be NULL when the model does not provide token timestamps.
1450
+ * When non-NULL, it contains @c count entries and is parallel to
1451
+ * @c tokens_arr.
1452
+ */
1453
+ float *timestamps;
1454
+
1455
+ /** Number of token entries in @c tokens_arr and related per-token arrays. */
1456
+ int32_t count;
1457
+
1458
+ /**
1459
+ * Contiguous memory block containing token strings separated by '\0'.
1460
+ *
1461
+ * Use @c tokens_arr for convenient indexed access.
1462
+ */
1463
+ const char *tokens;
1464
+
1465
+ /** Array of @c count pointers into @c tokens. */
1466
+ const char *const *tokens_arr;
1467
+
1468
+ /** JSON serialization of the result. */
1469
+ const char *json;
1470
+
1471
+ /** Optional recognized language label. */
1472
+ const char *lang;
1473
+
1474
+ /** Optional recognized emotion label. */
1475
+ const char *emotion;
1476
+
1477
+ /** Optional recognized event label. */
1478
+ const char *event;
1479
+
1480
+ /** Optional token durations in seconds, parallel to @c tokens_arr. */
1481
+ float *durations;
1482
+
1483
+ /** Optional token log probabilities, parallel to @c tokens_arr. */
1484
+ float *ys_log_probs;
1485
+
1486
+ /** Optional segment start times in seconds, parallel to @c segment_texts_arr.
1487
+ */
1488
+ const float *segment_timestamps;
1489
+
1490
+ /** Optional segment durations in seconds, parallel to @c segment_texts_arr.
1491
+ */
1492
+ const float *segment_durations;
1493
+
1494
+ /** Contiguous memory block containing segment texts separated by '\0'. */
1495
+ const char *segment_texts;
1496
+
1497
+ /** Array of @c segment_count pointers into @c segment_texts. */
1498
+ const char *const *segment_texts_arr;
1499
+
1500
+ /** Number of segment entries in the segment-level arrays. */
1501
+ int32_t segment_count;
1502
+ } SherpaOnnxOfflineRecognizerResult;
1503
+
1504
+ /**
1505
+ * @brief Get the recognition result for an offline ASR stream.
1506
+ *
1507
+ * Call this after SherpaOnnxDecodeOfflineStream() or
1508
+ * SherpaOnnxDecodeMultipleOfflineStreams().
1509
+ *
1510
+ * @param stream A pointer returned by SherpaOnnxCreateOfflineStream().
1511
+ * @return A newly allocated result snapshot. Free it with
1512
+ * SherpaOnnxDestroyOfflineRecognizerResult().
1513
+ *
1514
+ * @code
1515
+ * const SherpaOnnxOfflineRecognizerResult *r =
1516
+ * SherpaOnnxGetOfflineStreamResult(stream);
1517
+ * printf("%s\n", r->text);
1518
+ * if (r->timestamps) {
1519
+ * printf("First token starts at %.3f seconds\n", r->timestamps[0]);
1520
+ * }
1521
+ * SherpaOnnxDestroyOfflineRecognizerResult(r);
1522
+ * @endcode
1523
+ */
1524
+ SHERPA_ONNX_API const SherpaOnnxOfflineRecognizerResult *
1525
+ SherpaOnnxGetOfflineStreamResult(const SherpaOnnxOfflineStream *stream);
1526
+
1527
+ /**
1528
+ * @brief Destroy a result returned by SherpaOnnxGetOfflineStreamResult().
1529
+ *
1530
+ * @param r A pointer returned by SherpaOnnxGetOfflineStreamResult().
1531
+ *
1532
+ * @code
1533
+ * SherpaOnnxDestroyOfflineRecognizerResult(r);
1534
+ * r = NULL;
1535
+ * @endcode
1536
+ */
1537
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineRecognizerResult(
1538
+ const SherpaOnnxOfflineRecognizerResult *r);
1539
+
1540
+ /**
1541
+ * @brief Get the offline ASR result as JSON.
1542
+ *
1543
+ * @param stream A pointer returned by SherpaOnnxCreateOfflineStream().
1544
+ * @return A newly allocated JSON string. Free it with
1545
+ * SherpaOnnxDestroyOfflineStreamResultJson().
1546
+ *
1547
+ * @code
1548
+ * const char *json = SherpaOnnxGetOfflineStreamResultAsJson(stream);
1549
+ * puts(json);
1550
+ * SherpaOnnxDestroyOfflineStreamResultJson(json);
1551
+ * @endcode
1552
+ */
1553
+ SHERPA_ONNX_API const char *SherpaOnnxGetOfflineStreamResultAsJson(
1554
+ const SherpaOnnxOfflineStream *stream);
1555
+
1556
+ /**
1557
+ * @brief Free a JSON string returned by
1558
+ * SherpaOnnxGetOfflineStreamResultAsJson().
1559
+ *
1560
+ * @param s A pointer returned by SherpaOnnxGetOfflineStreamResultAsJson().
1561
+ *
1562
+ * @code
1563
+ * SherpaOnnxDestroyOfflineStreamResultJson(json);
1564
+ * json = NULL;
1565
+ * @endcode
1566
+ */
1567
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineStreamResultJson(const char *s);
1568
+
1569
+ // ============================================================
1570
+ // For keyword spotting
1571
+ // ============================================================
1572
+ /**
1573
+ * @brief Snapshot of the current keyword spotting result.
1574
+ *
1575
+ * Free this object with SherpaOnnxDestroyKeywordResult().
1576
+ */
1577
+ typedef struct SherpaOnnxKeywordResult {
1578
+ /**
1579
+ * Triggered keyword text.
1580
+ *
1581
+ * For English models this is usually space-separated words. For Chinese
1582
+ * models it is typically the surface form without spaces.
1583
+ */
1584
+ const char *keyword;
1585
+
1586
+ /**
1587
+ * Token sequence as a single string.
1588
+ *
1589
+ * For BPE-based models this contains the decoded BPE tokens.
1590
+ */
1591
+ const char *tokens;
1592
+
1593
+ /**
1594
+ * Token sequence as an array.
1595
+ *
1596
+ * The array length is @c count. Each string is owned by this result object.
1597
+ */
1598
+ const char *const *tokens_arr;
1599
+
1600
+ /** Number of decoded tokens in @c tokens_arr and @c timestamps. */
1601
+ int32_t count;
1602
+
1603
+ /**
1604
+ * Per-token timestamps in seconds.
1605
+ *
1606
+ * This array has @c count elements. Element @c i corresponds to
1607
+ * `tokens_arr[i]`.
1608
+ */
1609
+ float *timestamps;
1610
+
1611
+ /** Start time of the current segment in seconds. */
1612
+ float start_time;
1613
+
1614
+ /**
1615
+ * JSON representation of the result.
1616
+ *
1617
+ * The JSON includes `keyword`, `tokens`, `timestamps`, and `start_time`.
1618
+ */
1619
+ const char *json;
1620
+ } SherpaOnnxKeywordResult;
1621
+
1622
+ /**
1623
+ * @brief Configuration for keyword spotting.
1624
+ *
1625
+ * The acoustic model is configured through @c model_config. In practice this is
1626
+ * usually a streaming transducer model.
1627
+ *
1628
+ * Keyword definitions can be provided either through @c keywords_file or
1629
+ * through @c keywords_buf/@c keywords_buf_size. If both are set, the buffer is
1630
+ * used.
1631
+ *
1632
+ * Example using
1633
+ * `sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile`:
1634
+ *
1635
+ * @code
1636
+ * SherpaOnnxKeywordSpotterConfig config;
1637
+ * memset(&config, 0, sizeof(config));
1638
+ *
1639
+ * config.model_config.transducer.encoder =
1640
+ * "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile/"
1641
+ * "encoder-epoch-12-avg-2-chunk-16-left-64.int8.onnx";
1642
+ * config.model_config.transducer.decoder =
1643
+ * "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile/"
1644
+ * "decoder-epoch-12-avg-2-chunk-16-left-64.onnx";
1645
+ * config.model_config.transducer.joiner =
1646
+ * "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile/"
1647
+ * "joiner-epoch-12-avg-2-chunk-16-left-64.int8.onnx";
1648
+ * config.model_config.tokens =
1649
+ * "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile/"
1650
+ * "tokens.txt";
1651
+ * config.model_config.provider = "cpu";
1652
+ * config.model_config.num_threads = 1;
1653
+ *
1654
+ * config.keywords_file =
1655
+ * "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile/"
1656
+ * "test_wavs/test_keywords.txt";
1657
+ * config.max_active_paths = 4;
1658
+ * config.keywords_score = 3.0f;
1659
+ * config.keywords_threshold = 0.1f;
1660
+ * @endcode
1661
+ */
1662
+ typedef struct SherpaOnnxKeywordSpotterConfig {
1663
+ /** Feature extraction parameters. */
1664
+ SherpaOnnxFeatureConfig feat_config;
1665
+ /** Streaming acoustic model configuration. */
1666
+ SherpaOnnxOnlineModelConfig model_config;
1667
+ /** Maximum number of active decoding paths. */
1668
+ int32_t max_active_paths;
1669
+ /** Number of trailing blank symbols required before trigger finalization. */
1670
+ int32_t num_trailing_blanks;
1671
+ /** Bonus score applied to keywords during search. */
1672
+ float keywords_score;
1673
+ /** Detection threshold. Larger values are more conservative. */
1674
+ float keywords_threshold;
1675
+ /** Optional keyword file. */
1676
+ const char *keywords_file;
1677
+ /** Optional in-memory keyword data. If non-null, it overrides @c
1678
+ * keywords_file. */
1679
+ const char *keywords_buf;
1680
+ /** Size in bytes of @c keywords_buf, excluding any trailing `'\0'`. */
1681
+ int32_t keywords_buf_size;
1682
+ } SherpaOnnxKeywordSpotterConfig;
1683
+
1684
+ /** @brief Opaque keyword spotter handle. */
1685
+ typedef struct SherpaOnnxKeywordSpotter SherpaOnnxKeywordSpotter;
1686
+
1687
+ /**
1688
+ * @brief Create a keyword spotter.
1689
+ *
1690
+ * @param config Keyword spotter configuration.
1691
+ * @return A newly allocated keyword spotter on success, or NULL on error. Free
1692
+ * it with SherpaOnnxDestroyKeywordSpotter().
1693
+ */
1694
+ SHERPA_ONNX_API const SherpaOnnxKeywordSpotter *SherpaOnnxCreateKeywordSpotter(
1695
+ const SherpaOnnxKeywordSpotterConfig *config);
1696
+
1697
+ /**
1698
+ * @brief Destroy a keyword spotter.
1699
+ *
1700
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1701
+ */
1702
+ SHERPA_ONNX_API void SherpaOnnxDestroyKeywordSpotter(
1703
+ const SherpaOnnxKeywordSpotter *spotter);
1704
+
1705
+ /**
1706
+ * @brief Create a keyword spotting stream using the spotter's built-in keyword
1707
+ * list.
1708
+ *
1709
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1710
+ * @return A newly allocated stream. Free it with
1711
+ * SherpaOnnxDestroyOnlineStream().
1712
+ */
1713
+ SHERPA_ONNX_API const SherpaOnnxOnlineStream *SherpaOnnxCreateKeywordStream(
1714
+ const SherpaOnnxKeywordSpotter *spotter);
1715
+
1716
+ /**
1717
+ * @brief Create a keyword spotting stream with extra or replacement keywords.
1718
+ *
1719
+ * The @p keywords string uses the same textual format as the keyword files used
1720
+ * by the examples. For instance:
1721
+ *
1722
+ * @code
1723
+ * const SherpaOnnxOnlineStream *stream =
1724
+ * SherpaOnnxCreateKeywordStreamWithKeywords(
1725
+ * kws, "y ǎn y uán @演员/zh ī m íng @知名");
1726
+ * @endcode
1727
+ *
1728
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1729
+ * @param keywords Inline keyword definition string.
1730
+ * @return A newly allocated stream. Free it with
1731
+ * SherpaOnnxDestroyOnlineStream().
1732
+ */
1733
+ SHERPA_ONNX_API const SherpaOnnxOnlineStream *
1734
+ SherpaOnnxCreateKeywordStreamWithKeywords(
1735
+ const SherpaOnnxKeywordSpotter *spotter, const char *keywords);
1736
+
1737
+ /**
1738
+ * @brief Check whether a keyword stream has enough audio for decoding.
1739
+ *
1740
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1741
+ * @param stream A pointer returned by SherpaOnnxCreateKeywordStream() or
1742
+ * SherpaOnnxCreateKeywordStreamWithKeywords().
1743
+ * @return 1 if the stream is ready to decode; otherwise 0.
1744
+ */
1745
+ SHERPA_ONNX_API int32_t
1746
+ SherpaOnnxIsKeywordStreamReady(const SherpaOnnxKeywordSpotter *spotter,
1747
+ const SherpaOnnxOnlineStream *stream);
1748
+
1749
+ /**
1750
+ * @brief Decode one ready keyword stream.
1751
+ *
1752
+ * Call this only when SherpaOnnxIsKeywordStreamReady() returns 1.
1753
+ *
1754
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1755
+ * @param stream A pointer returned by SherpaOnnxCreateKeywordStream() or
1756
+ * SherpaOnnxCreateKeywordStreamWithKeywords().
1757
+ */
1758
+ SHERPA_ONNX_API void SherpaOnnxDecodeKeywordStream(
1759
+ const SherpaOnnxKeywordSpotter *spotter,
1760
+ const SherpaOnnxOnlineStream *stream);
1761
+
1762
+ /**
1763
+ * @brief Reset a keyword stream after a keyword is detected.
1764
+ *
1765
+ * The examples call this immediately after a successful trigger so the next
1766
+ * keyword can be detected independently.
1767
+ *
1768
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1769
+ * @param stream A pointer returned by SherpaOnnxCreateKeywordStream() or
1770
+ * SherpaOnnxCreateKeywordStreamWithKeywords().
1771
+ */
1772
+ SHERPA_ONNX_API void SherpaOnnxResetKeywordStream(
1773
+ const SherpaOnnxKeywordSpotter *spotter,
1774
+ const SherpaOnnxOnlineStream *stream);
1775
+
1776
+ /**
1777
+ * @brief Decode multiple ready keyword streams in parallel.
1778
+ *
1779
+ * The caller must ensure every stream in @p streams is ready before calling
1780
+ * this function.
1781
+ *
1782
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1783
+ * @param streams Array of ready streams.
1784
+ * @param n Number of elements in @p streams.
1785
+ */
1786
+ SHERPA_ONNX_API void SherpaOnnxDecodeMultipleKeywordStreams(
1787
+ const SherpaOnnxKeywordSpotter *spotter,
1788
+ const SherpaOnnxOnlineStream **streams, int32_t n);
1789
+
1790
+ /**
1791
+ * @brief Get the current keyword spotting result for a stream.
1792
+ *
1793
+ * The returned snapshot may represent either "no trigger yet" or a detected
1794
+ * keyword. A common pattern is to check whether `strlen(r->keyword) != 0`.
1795
+ *
1796
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1797
+ * @param stream A pointer returned by SherpaOnnxCreateKeywordStream() or
1798
+ * SherpaOnnxCreateKeywordStreamWithKeywords().
1799
+ * @return A newly allocated result snapshot. Free it with
1800
+ * SherpaOnnxDestroyKeywordResult().
1801
+ *
1802
+ * @code
1803
+ * const SherpaOnnxKeywordResult *r = SherpaOnnxGetKeywordResult(kws, stream);
1804
+ * if (r && r->json && strlen(r->keyword)) {
1805
+ * fprintf(stderr, "Detected keyword: %s\n", r->json);
1806
+ * SherpaOnnxResetKeywordStream(kws, stream);
1807
+ * }
1808
+ * SherpaOnnxDestroyKeywordResult(r);
1809
+ * @endcode
1810
+ */
1811
+ SHERPA_ONNX_API const SherpaOnnxKeywordResult *SherpaOnnxGetKeywordResult(
1812
+ const SherpaOnnxKeywordSpotter *spotter,
1813
+ const SherpaOnnxOnlineStream *stream);
1814
+
1815
+ /**
1816
+ * @brief Destroy a keyword result snapshot.
1817
+ *
1818
+ * @param r A pointer returned by SherpaOnnxGetKeywordResult().
1819
+ */
1820
+ SHERPA_ONNX_API void SherpaOnnxDestroyKeywordResult(
1821
+ const SherpaOnnxKeywordResult *r);
1822
+
1823
+ /**
1824
+ * @brief Get the current keyword spotting result as JSON.
1825
+ *
1826
+ * @param spotter A pointer returned by SherpaOnnxCreateKeywordSpotter().
1827
+ * @param stream A pointer returned by SherpaOnnxCreateKeywordStream() or
1828
+ * SherpaOnnxCreateKeywordStreamWithKeywords().
1829
+ * @return A newly allocated JSON string. Free it with
1830
+ * SherpaOnnxFreeKeywordResultJson().
1831
+ */
1832
+ SHERPA_ONNX_API const char *SherpaOnnxGetKeywordResultAsJson(
1833
+ const SherpaOnnxKeywordSpotter *spotter,
1834
+ const SherpaOnnxOnlineStream *stream);
1835
+
1836
+ /**
1837
+ * @brief Free a JSON string returned by SherpaOnnxGetKeywordResultAsJson().
1838
+ *
1839
+ * @param s A pointer returned by SherpaOnnxGetKeywordResultAsJson().
1840
+ */
1841
+ SHERPA_ONNX_API void SherpaOnnxFreeKeywordResultJson(const char *s);
1842
+
1843
+ // ============================================================
1844
+ // For VAD
1845
+ // ============================================================
1846
+
1847
+ /** @brief Configuration for a Silero VAD model. */
1848
+ typedef struct SherpaOnnxSileroVadModelConfig {
1849
+ /** Path to `silero_vad.onnx`. */
1850
+ const char *model;
1851
+ /** Speech probability threshold. Frames above this value are speech. */
1852
+ float threshold;
1853
+ /** Minimum silence duration in seconds used to close a speech segment. */
1854
+ float min_silence_duration;
1855
+ /** Minimum speech duration in seconds to keep a detected segment. */
1856
+ float min_speech_duration;
1857
+ /** Input window size in samples. A common value is 512. */
1858
+ int32_t window_size;
1859
+ /**
1860
+ * Maximum speech duration in seconds.
1861
+ *
1862
+ * When a segment exceeds this value, the detector temporarily uses a higher
1863
+ * threshold to encourage a split.
1864
+ */
1865
+ float max_speech_duration;
1866
+ } SherpaOnnxSileroVadModelConfig;
1867
+
1868
+ /** @brief Configuration for a Ten VAD model. */
1869
+ typedef struct SherpaOnnxTenVadModelConfig {
1870
+ /** Path to `ten-vad.onnx`. */
1871
+ const char *model;
1872
+ /** Speech probability threshold. Frames above this value are speech. */
1873
+ float threshold;
1874
+ /** Minimum silence duration in seconds used to close a speech segment. */
1875
+ float min_silence_duration;
1876
+ /** Minimum speech duration in seconds to keep a detected segment. */
1877
+ float min_speech_duration;
1878
+ /** Input window size in samples. A common value is 256. */
1879
+ int32_t window_size;
1880
+ /**
1881
+ * Maximum speech duration in seconds.
1882
+ *
1883
+ * When a segment exceeds this value, the detector temporarily uses a higher
1884
+ * threshold to encourage a split.
1885
+ */
1886
+ float max_speech_duration;
1887
+ } SherpaOnnxTenVadModelConfig;
1888
+
1889
+ /**
1890
+ * @brief Configuration shared by voice activity detectors.
1891
+ *
1892
+ * Exactly one VAD model family should be configured. Set either
1893
+ * @c silero_vad.model or @c ten_vad.model.
1894
+ *
1895
+ * If both are configured, the implementation will choose one of them, and
1896
+ * which one is used is implementation-defined. Do not rely on any precedence
1897
+ * rule.
1898
+ *
1899
+ * Example model files:
1900
+ * - `./silero_vad.onnx`
1901
+ * - `./ten-vad.onnx`
1902
+ *
1903
+ * @code
1904
+ * SherpaOnnxVadModelConfig config;
1905
+ * memset(&config, 0, sizeof(config));
1906
+ *
1907
+ * config.silero_vad.model = "./silero_vad.onnx";
1908
+ * config.silero_vad.threshold = 0.25f;
1909
+ * config.silero_vad.min_silence_duration = 0.5f;
1910
+ * config.silero_vad.min_speech_duration = 0.5f;
1911
+ * config.silero_vad.max_speech_duration = 10.0f;
1912
+ * config.silero_vad.window_size = 512;
1913
+ *
1914
+ * config.sample_rate = 16000;
1915
+ * config.num_threads = 1;
1916
+ * config.provider = "cpu";
1917
+ * config.debug = 0;
1918
+ * @endcode
1919
+ */
1920
+ typedef struct SherpaOnnxVadModelConfig {
1921
+ /** Silero VAD configuration. */
1922
+ SherpaOnnxSileroVadModelConfig silero_vad;
1923
+ /** Input sample rate expected by the detector, usually 16000. */
1924
+ int32_t sample_rate;
1925
+ /** Number of backend threads. */
1926
+ int32_t num_threads;
1927
+ /** Execution provider, for example "cpu" or "cuda". */
1928
+ const char *provider;
1929
+ /** Non-zero to print debug information. */
1930
+ int32_t debug;
1931
+ /** Ten VAD configuration. */
1932
+ SherpaOnnxTenVadModelConfig ten_vad;
1933
+ } SherpaOnnxVadModelConfig;
1934
+
1935
+ /** @brief Opaque circular-buffer handle used by helper APIs. */
1936
+ typedef struct SherpaOnnxCircularBuffer SherpaOnnxCircularBuffer;
1937
+
1938
+ /**
1939
+ * @brief Create a floating-point circular buffer.
1940
+ *
1941
+ * @param capacity Maximum number of samples the buffer can keep.
1942
+ * @return A newly allocated buffer. Free it with
1943
+ * SherpaOnnxDestroyCircularBuffer().
1944
+ *
1945
+ * @code
1946
+ * const SherpaOnnxCircularBuffer *buffer =
1947
+ * SherpaOnnxCreateCircularBuffer(16000 * 30);
1948
+ * @endcode
1949
+ */
1950
+ SHERPA_ONNX_API const SherpaOnnxCircularBuffer *SherpaOnnxCreateCircularBuffer(
1951
+ int32_t capacity);
1952
+
1953
+ /**
1954
+ * @brief Destroy a circular buffer.
1955
+ *
1956
+ * @param buffer A pointer returned by SherpaOnnxCreateCircularBuffer().
1957
+ *
1958
+ * @code
1959
+ * SherpaOnnxDestroyCircularBuffer(buffer);
1960
+ * buffer = NULL;
1961
+ * @endcode
1962
+ */
1963
+ SHERPA_ONNX_API void SherpaOnnxDestroyCircularBuffer(
1964
+ const SherpaOnnxCircularBuffer *buffer);
1965
+
1966
+ /**
1967
+ * @brief Append samples to a circular buffer.
1968
+ *
1969
+ * @param buffer A pointer returned by SherpaOnnxCreateCircularBuffer().
1970
+ * @param p Pointer to @p n samples.
1971
+ * @param n Number of samples.
1972
+ *
1973
+ * @code
1974
+ * SherpaOnnxCircularBufferPush(buffer, wave->samples, wave->num_samples);
1975
+ * @endcode
1976
+ */
1977
+ SHERPA_ONNX_API void SherpaOnnxCircularBufferPush(
1978
+ const SherpaOnnxCircularBuffer *buffer, const float *p, int32_t n);
1979
+
1980
+ /**
1981
+ * @brief Copy out a slice of samples from a circular buffer.
1982
+ *
1983
+ * @param buffer A pointer returned by SherpaOnnxCreateCircularBuffer().
1984
+ * @param start_index Absolute start index in the buffer timeline.
1985
+ * @param n Number of samples to copy.
1986
+ * @return A newly allocated array containing @p n samples. Free it with
1987
+ * SherpaOnnxCircularBufferFree().
1988
+ *
1989
+ * @code
1990
+ * const float *samples = SherpaOnnxCircularBufferGet(buffer, start, 3200);
1991
+ * SherpaOnnxCircularBufferFree(samples);
1992
+ * @endcode
1993
+ */
1994
+ SHERPA_ONNX_API const float *SherpaOnnxCircularBufferGet(
1995
+ const SherpaOnnxCircularBuffer *buffer, int32_t start_index, int32_t n);
1996
+
1997
+ /** @brief Free an array returned by SherpaOnnxCircularBufferGet(). */
1998
+ SHERPA_ONNX_API void SherpaOnnxCircularBufferFree(const float *p);
1999
+
2000
+ /**
2001
+ * @brief Drop samples from the front of a circular buffer.
2002
+ *
2003
+ * @param buffer A pointer returned by SherpaOnnxCreateCircularBuffer().
2004
+ * @param n Number of samples to remove.
2005
+ */
2006
+ SHERPA_ONNX_API void SherpaOnnxCircularBufferPop(
2007
+ const SherpaOnnxCircularBuffer *buffer, int32_t n);
2008
+
2009
+ /**
2010
+ * @brief Return the number of currently stored samples.
2011
+ *
2012
+ * @param buffer A pointer returned by SherpaOnnxCreateCircularBuffer().
2013
+ * @return Number of samples currently in the buffer.
2014
+ */
2015
+ SHERPA_ONNX_API int32_t
2016
+ SherpaOnnxCircularBufferSize(const SherpaOnnxCircularBuffer *buffer);
2017
+
2018
+ /**
2019
+ * @brief Return the current head index of the buffer timeline.
2020
+ *
2021
+ * The value is monotonically non-decreasing until
2022
+ * SherpaOnnxCircularBufferReset() is called.
2023
+ *
2024
+ * @param buffer A pointer returned by SherpaOnnxCreateCircularBuffer().
2025
+ * @return The current head index.
2026
+ */
2027
+ SHERPA_ONNX_API int32_t
2028
+ SherpaOnnxCircularBufferHead(const SherpaOnnxCircularBuffer *buffer);
2029
+
2030
+ /**
2031
+ * @brief Clear a circular buffer and reset its head index.
2032
+ *
2033
+ * @param buffer A pointer returned by SherpaOnnxCreateCircularBuffer().
2034
+ */
2035
+ SHERPA_ONNX_API void SherpaOnnxCircularBufferReset(
2036
+ const SherpaOnnxCircularBuffer *buffer);
2037
+
2038
+ /**
2039
+ * @brief One detected speech segment returned by the VAD.
2040
+ *
2041
+ * The segment owns @c samples. Free the whole object with
2042
+ * SherpaOnnxDestroySpeechSegment().
2043
+ */
2044
+ typedef struct SherpaOnnxSpeechSegment {
2045
+ /** Start index, in input samples, of this segment. */
2046
+ int32_t start;
2047
+ /** Newly allocated mono samples for this segment. */
2048
+ float *samples;
2049
+ /** Number of samples in @c samples. */
2050
+ int32_t n;
2051
+ } SherpaOnnxSpeechSegment;
2052
+
2053
+ /** @brief Opaque voice activity detector handle. */
2054
+ typedef struct SherpaOnnxVoiceActivityDetector SherpaOnnxVoiceActivityDetector;
2055
+
2056
+ /**
2057
+ * @brief Create a voice activity detector.
2058
+ *
2059
+ * Example model files are shown in `c-api-examples/vad-whisper-c-api.c`.
2060
+ *
2061
+ * @param config VAD configuration.
2062
+ * @param buffer_size_in_seconds Internal buffering capacity in seconds.
2063
+ * @return A newly allocated detector on success, or NULL on configuration
2064
+ * error. Free it with SherpaOnnxDestroyVoiceActivityDetector().
2065
+ *
2066
+ * @code
2067
+ * SherpaOnnxVadModelConfig config;
2068
+ * memset(&config, 0, sizeof(config));
2069
+ * config.silero_vad.model = "./silero_vad.onnx";
2070
+ * config.silero_vad.threshold = 0.25f;
2071
+ * config.silero_vad.min_silence_duration = 0.5f;
2072
+ * config.silero_vad.min_speech_duration = 0.5f;
2073
+ * config.silero_vad.max_speech_duration = 10.0f;
2074
+ * config.silero_vad.window_size = 512;
2075
+ * config.sample_rate = 16000;
2076
+ * config.num_threads = 1;
2077
+ *
2078
+ * const SherpaOnnxVoiceActivityDetector *vad =
2079
+ * SherpaOnnxCreateVoiceActivityDetector(&config, 30.0f);
2080
+ * @endcode
2081
+ */
2082
+ SHERPA_ONNX_API const SherpaOnnxVoiceActivityDetector *
2083
+ SherpaOnnxCreateVoiceActivityDetector(const SherpaOnnxVadModelConfig *config,
2084
+ float buffer_size_in_seconds);
2085
+
2086
+ /**
2087
+ * @brief Destroy a voice activity detector.
2088
+ *
2089
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2090
+ */
2091
+ SHERPA_ONNX_API void SherpaOnnxDestroyVoiceActivityDetector(
2092
+ const SherpaOnnxVoiceActivityDetector *p);
2093
+
2094
+ /**
2095
+ * @brief Feed audio samples to the VAD.
2096
+ *
2097
+ * Input samples are mono floating-point PCM in the range [-1, 1].
2098
+ *
2099
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2100
+ * @param samples Pointer to @p n samples.
2101
+ * @param n Number of samples.
2102
+ *
2103
+ * @code
2104
+ * SherpaOnnxVoiceActivityDetectorAcceptWaveform(vad,
2105
+ * wave->samples + i,
2106
+ * window_size);
2107
+ * @endcode
2108
+ */
2109
+ SHERPA_ONNX_API void SherpaOnnxVoiceActivityDetectorAcceptWaveform(
2110
+ const SherpaOnnxVoiceActivityDetector *p, const float *samples, int32_t n);
2111
+
2112
+ /**
2113
+ * @brief Check whether the detector currently has any completed speech segment.
2114
+ *
2115
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2116
+ * @return 1 if no completed speech segment is available; otherwise 0.
2117
+ */
2118
+ SHERPA_ONNX_API int32_t
2119
+ SherpaOnnxVoiceActivityDetectorEmpty(const SherpaOnnxVoiceActivityDetector *p);
2120
+
2121
+ /**
2122
+ * @brief Check whether the detector is currently inside speech.
2123
+ *
2124
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2125
+ * @return 1 if speech is currently detected; otherwise 0.
2126
+ */
2127
+ SHERPA_ONNX_API int32_t SherpaOnnxVoiceActivityDetectorDetected(
2128
+ const SherpaOnnxVoiceActivityDetector *p);
2129
+
2130
+ /**
2131
+ * @brief Remove the front speech segment from the detector queue.
2132
+ *
2133
+ * Call this after consuming the segment returned by
2134
+ * SherpaOnnxVoiceActivityDetectorFront().
2135
+ *
2136
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2137
+ *
2138
+ * @code
2139
+ * const SherpaOnnxSpeechSegment *segment =
2140
+ * SherpaOnnxVoiceActivityDetectorFront(vad);
2141
+ * // ... use segment ...
2142
+ * SherpaOnnxDestroySpeechSegment(segment);
2143
+ * SherpaOnnxVoiceActivityDetectorPop(vad);
2144
+ * @endcode
2145
+ */
2146
+ SHERPA_ONNX_API void SherpaOnnxVoiceActivityDetectorPop(
2147
+ const SherpaOnnxVoiceActivityDetector *p);
2148
+
2149
+ /**
2150
+ * @brief Remove all queued speech segments.
2151
+ *
2152
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2153
+ */
2154
+ SHERPA_ONNX_API void SherpaOnnxVoiceActivityDetectorClear(
2155
+ const SherpaOnnxVoiceActivityDetector *p);
2156
+
2157
+ /**
2158
+ * @brief Get the first queued speech segment.
2159
+ *
2160
+ * The returned segment is a copy owned by the caller. Free it with
2161
+ * SherpaOnnxDestroySpeechSegment().
2162
+ *
2163
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2164
+ * @return The first queued speech segment, or NULL if none is available.
2165
+ *
2166
+ * @code
2167
+ * while (!SherpaOnnxVoiceActivityDetectorEmpty(vad)) {
2168
+ * const SherpaOnnxSpeechSegment *segment =
2169
+ * SherpaOnnxVoiceActivityDetectorFront(vad);
2170
+ * printf("start=%d, samples=%d\n", segment->start, segment->n);
2171
+ * SherpaOnnxDestroySpeechSegment(segment);
2172
+ * SherpaOnnxVoiceActivityDetectorPop(vad);
2173
+ * }
2174
+ * @endcode
2175
+ */
2176
+ SHERPA_ONNX_API const SherpaOnnxSpeechSegment *
2177
+ SherpaOnnxVoiceActivityDetectorFront(const SherpaOnnxVoiceActivityDetector *p);
2178
+
2179
+ /**
2180
+ * @brief Destroy a speech segment returned by
2181
+ * SherpaOnnxVoiceActivityDetectorFront().
2182
+ *
2183
+ * @param p A pointer returned by SherpaOnnxVoiceActivityDetectorFront().
2184
+ */
2185
+ SHERPA_ONNX_API void SherpaOnnxDestroySpeechSegment(
2186
+ const SherpaOnnxSpeechSegment *p);
2187
+
2188
+ /**
2189
+ * @brief Reset a voice activity detector so it can process a new stream.
2190
+ *
2191
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2192
+ */
2193
+ SHERPA_ONNX_API void SherpaOnnxVoiceActivityDetectorReset(
2194
+ const SherpaOnnxVoiceActivityDetector *p);
2195
+
2196
+ /**
2197
+ * @brief Flush buffered tail samples and force final segmentation.
2198
+ *
2199
+ * Call this after the last chunk of input has been fed.
2200
+ *
2201
+ * @param p A pointer returned by SherpaOnnxCreateVoiceActivityDetector().
2202
+ *
2203
+ * @code
2204
+ * SherpaOnnxVoiceActivityDetectorFlush(vad);
2205
+ * @endcode
2206
+ */
2207
+ SHERPA_ONNX_API void SherpaOnnxVoiceActivityDetectorFlush(
2208
+ const SherpaOnnxVoiceActivityDetector *p);
2209
+
2210
+ // ============================================================
2211
+ // For offline Text-to-Speech (i.e., non-streaming TTS)
2212
+ // ============================================================
2213
+
2214
+ /** @brief Configuration for a VITS TTS model. */
2215
+ typedef struct SherpaOnnxOfflineTtsVitsModelConfig {
2216
+ /** Path to the VITS ONNX model, for example `./vits-ljs.onnx`. */
2217
+ const char *model;
2218
+ /** Path to the lexicon file. Ignored if @c data_dir is provided. */
2219
+ const char *lexicon;
2220
+ /** Path to the tokens file. */
2221
+ const char *tokens;
2222
+ /** Optional path to espeak-ng-data. */
2223
+ const char *data_dir;
2224
+ /** VITS noise scale. */
2225
+ float noise_scale;
2226
+ /** VITS duration noise scale. */
2227
+ float noise_scale_w;
2228
+ /** Speech rate scale. Values < 1 are slower; values > 1 are faster. */
2229
+ float length_scale;
2230
+ /** Unused legacy field kept for ABI compatibility. */
2231
+ const char *dict_dir;
2232
+ } SherpaOnnxOfflineTtsVitsModelConfig;
2233
+
2234
+ /** @brief Configuration for a Matcha TTS model. */
2235
+ typedef struct SherpaOnnxOfflineTtsMatchaModelConfig {
2236
+ /** Path to the Matcha acoustic model. */
2237
+ const char *acoustic_model;
2238
+ /** Path to the vocoder model, for example `./vocos-22khz-univ.onnx`. */
2239
+ const char *vocoder;
2240
+ /** Path to the lexicon file. */
2241
+ const char *lexicon;
2242
+ /** Path to the tokens file. */
2243
+ const char *tokens;
2244
+ /** Optional path to espeak-ng-data. */
2245
+ const char *data_dir;
2246
+ /** Matcha noise scale. */
2247
+ float noise_scale;
2248
+ /** Speech rate scale. Values < 1 are slower; values > 1 are faster. */
2249
+ float length_scale;
2250
+ /** Unused legacy field kept for ABI compatibility. */
2251
+ const char *dict_dir;
2252
+ } SherpaOnnxOfflineTtsMatchaModelConfig;
2253
+
2254
+ /** @brief Configuration for a Kokoro TTS model. */
2255
+ typedef struct SherpaOnnxOfflineTtsKokoroModelConfig {
2256
+ /** Path to the Kokoro model, for example `./kokoro-en-v0_19/model.onnx`. */
2257
+ const char *model;
2258
+ /** Path to the Kokoro voices file. */
2259
+ const char *voices;
2260
+ /** Path to the tokens file. */
2261
+ const char *tokens;
2262
+ /** Optional path to espeak-ng-data. */
2263
+ const char *data_dir;
2264
+ /** Speech rate scale. Values < 1 are slower; values > 1 are faster. */
2265
+ float length_scale;
2266
+ /** Unused legacy field kept for ABI compatibility. */
2267
+ const char *dict_dir;
2268
+ /** Optional lexicon file. */
2269
+ const char *lexicon;
2270
+ /** Optional language hint. */
2271
+ const char *lang;
2272
+ } SherpaOnnxOfflineTtsKokoroModelConfig;
2273
+
2274
+ /** @brief Configuration for a Kitten TTS model. */
2275
+ typedef struct SherpaOnnxOfflineTtsKittenModelConfig {
2276
+ /** Path to the Kitten model. */
2277
+ const char *model;
2278
+ /** Path to the Kitten voices file. */
2279
+ const char *voices;
2280
+ /** Path to the tokens file. */
2281
+ const char *tokens;
2282
+ /** Optional path to espeak-ng-data. */
2283
+ const char *data_dir;
2284
+ /** Speech rate scale. Values < 1 are slower; values > 1 are faster. */
2285
+ float length_scale;
2286
+ } SherpaOnnxOfflineTtsKittenModelConfig;
2287
+
2288
+ /** @brief Configuration for a ZipVoice TTS model. */
2289
+ typedef struct SherpaOnnxOfflineTtsZipvoiceModelConfig {
2290
+ /** Path to the tokens file. */
2291
+ const char *tokens;
2292
+ /** Path to the ZipVoice encoder model. */
2293
+ const char *encoder;
2294
+ /** Path to the ZipVoice decoder model. */
2295
+ const char *decoder;
2296
+ /** Path to the vocoder model. */
2297
+ const char *vocoder;
2298
+ /** Optional path to espeak-ng-data. */
2299
+ const char *data_dir;
2300
+ /** Path to the lexicon file. */
2301
+ const char *lexicon;
2302
+ /** Feature scaling factor. */
2303
+ float feat_scale;
2304
+ /** Time shift parameter. */
2305
+ float t_shift;
2306
+ /** Target RMS parameter. */
2307
+ float target_rms;
2308
+ /** Guidance scale parameter. */
2309
+ float guidance_scale;
2310
+ } SherpaOnnxOfflineTtsZipvoiceModelConfig;
2311
+
2312
+ /** @brief Configuration for a Pocket TTS model. */
2313
+ typedef struct SherpaOnnxOfflineTtsPocketModelConfig {
2314
+ /** Path to `lm_flow*.onnx`. */
2315
+ const char *lm_flow;
2316
+ /** Path to `lm_main*.onnx`. */
2317
+ const char *lm_main;
2318
+ /** Path to the Pocket encoder model. */
2319
+ const char *encoder;
2320
+ /** Path to the Pocket decoder model. */
2321
+ const char *decoder;
2322
+ /** Path to the text conditioner model. */
2323
+ const char *text_conditioner;
2324
+ /** Path to `vocab.json`. */
2325
+ const char *vocab_json;
2326
+ /** Path to `token_scores.json`. */
2327
+ const char *token_scores_json;
2328
+ /** Voice embedding cache capacity. */
2329
+ int32_t voice_embedding_cache_capacity;
2330
+ } SherpaOnnxOfflineTtsPocketModelConfig;
2331
+
2332
+ /** @brief Configuration for a Supertonic TTS model. */
2333
+ typedef struct SherpaOnnxOfflineTtsSupertonicModelConfig {
2334
+ /** Path to the duration predictor model. */
2335
+ const char *duration_predictor;
2336
+ /** Path to the text encoder model. */
2337
+ const char *text_encoder;
2338
+ /** Path to the vector estimator model. */
2339
+ const char *vector_estimator;
2340
+ /** Path to the vocoder model. */
2341
+ const char *vocoder;
2342
+ /** Path to `tts.json`. */
2343
+ const char *tts_json;
2344
+ /** Path to the unicode indexer file. */
2345
+ const char *unicode_indexer;
2346
+ /** Path to the voice style file. */
2347
+ const char *voice_style;
2348
+ } SherpaOnnxOfflineTtsSupertonicModelConfig;
2349
+
2350
+ /**
2351
+ * @brief Configuration shared by offline TTS models.
2352
+ *
2353
+ * Exactly one TTS model family should be configured. For example, set only one
2354
+ * of @c vits, @c matcha, @c kokoro, @c kitten, @c zipvoice, @c pocket, or
2355
+ * @c supertonic.
2356
+ *
2357
+ * If multiple model families are configured at the same time, the
2358
+ * implementation will choose one of them, and which one is used is
2359
+ * implementation-defined. Do not rely on any precedence rule.
2360
+ *
2361
+ * Concrete example model packages in this repository include:
2362
+ * - `kokoro-en-v0_19`
2363
+ * - `sherpa-onnx-pocket-tts-int8-2026-01-26`
2364
+ * - `matcha-icefall-en_US-ljspeech`
2365
+ * - `sherpa-onnx-zipvoice-distill-int8-zh-en-emilia`
2366
+ */
2367
+ typedef struct SherpaOnnxOfflineTtsModelConfig {
2368
+ /** VITS configuration. */
2369
+ SherpaOnnxOfflineTtsVitsModelConfig vits;
2370
+ /** Number of backend threads. */
2371
+ int32_t num_threads;
2372
+ /** Non-zero to print debug information. */
2373
+ int32_t debug;
2374
+ /** Execution provider, for example "cpu" or "cuda". */
2375
+ const char *provider;
2376
+ /** Matcha configuration. */
2377
+ SherpaOnnxOfflineTtsMatchaModelConfig matcha;
2378
+ /** Kokoro configuration. */
2379
+ SherpaOnnxOfflineTtsKokoroModelConfig kokoro;
2380
+ /** Kitten configuration. */
2381
+ SherpaOnnxOfflineTtsKittenModelConfig kitten;
2382
+ /** ZipVoice configuration. */
2383
+ SherpaOnnxOfflineTtsZipvoiceModelConfig zipvoice;
2384
+ /** Pocket configuration. */
2385
+ SherpaOnnxOfflineTtsPocketModelConfig pocket;
2386
+ /** Supertonic configuration. */
2387
+ SherpaOnnxOfflineTtsSupertonicModelConfig supertonic;
2388
+ } SherpaOnnxOfflineTtsModelConfig;
2389
+
2390
+ /**
2391
+ * @brief Configuration for offline text-to-speech.
2392
+ *
2393
+ * @code
2394
+ * SherpaOnnxOfflineTtsConfig config;
2395
+ * memset(&config, 0, sizeof(config));
2396
+ *
2397
+ * config.model.kokoro.model = "./kokoro-en-v0_19/model.onnx";
2398
+ * config.model.kokoro.voices = "./kokoro-en-v0_19/voices.bin";
2399
+ * config.model.kokoro.tokens = "./kokoro-en-v0_19/tokens.txt";
2400
+ * config.model.kokoro.data_dir = "./kokoro-en-v0_19/espeak-ng-data";
2401
+ * config.model.num_threads = 2;
2402
+ * config.model.provider = "cpu";
2403
+ * config.model.debug = 0;
2404
+ * config.max_num_sentences = 2;
2405
+ * @endcode
2406
+ */
2407
+ typedef struct SherpaOnnxOfflineTtsConfig {
2408
+ /** TTS model configuration. */
2409
+ SherpaOnnxOfflineTtsModelConfig model;
2410
+ /** Optional comma-separated rule FST list. */
2411
+ const char *rule_fsts;
2412
+ /** Maximum number of sentences processed per chunk. */
2413
+ int32_t max_num_sentences;
2414
+ /** Optional FAR archives used by text normalization rules. */
2415
+ const char *rule_fars;
2416
+ /** Default silence scale between sentences. */
2417
+ float silence_scale;
2418
+ } SherpaOnnxOfflineTtsConfig;
2419
+
2420
+ /**
2421
+ * @brief Generated waveform returned by TTS APIs.
2422
+ *
2423
+ * The returned structure owns @c samples. Free the whole object with
2424
+ * SherpaOnnxDestroyOfflineTtsGeneratedAudio().
2425
+ */
2426
+ typedef struct SherpaOnnxGeneratedAudio {
2427
+ /** Generated mono samples in the range [-1, 1]. */
2428
+ const float *samples;
2429
+ /** Number of samples in @c samples. */
2430
+ int32_t n;
2431
+ /** Output sample rate. */
2432
+ int32_t sample_rate;
2433
+ } SherpaOnnxGeneratedAudio;
2434
+
2435
+ /**
2436
+ * @brief Callback invoked during incremental generation.
2437
+ *
2438
+ * Return 1 to continue generation. Return 0 to stop early.
2439
+ *
2440
+ * The @p samples pointer is only valid during the callback. Copy the samples if
2441
+ * you need to keep them after the callback returns.
2442
+ */
2443
+ typedef int32_t (*SherpaOnnxGeneratedAudioCallback)(const float *samples,
2444
+ int32_t n);
2445
+
2446
+ /**
2447
+ * @brief Same as SherpaOnnxGeneratedAudioCallback but with an extra user
2448
+ * pointer.
2449
+ */
2450
+ typedef int32_t (*SherpaOnnxGeneratedAudioCallbackWithArg)(const float *samples,
2451
+ int32_t n,
2452
+ void *arg);
2453
+
2454
+ /**
2455
+ * @brief Progress callback invoked during incremental generation.
2456
+ *
2457
+ * @param samples Newly generated samples valid only during the callback.
2458
+ * @param n Number of samples in @p samples.
2459
+ * @param p Progress in the range [0, 1].
2460
+ * @return Return 1 to continue generation. Return 0 to stop early.
2461
+ */
2462
+ typedef int32_t (*SherpaOnnxGeneratedAudioProgressCallback)(
2463
+ const float *samples, int32_t n, float p);
2464
+
2465
+ /**
2466
+ * @brief Same as SherpaOnnxGeneratedAudioProgressCallback but with an extra
2467
+ * user pointer.
2468
+ */
2469
+ typedef int32_t (*SherpaOnnxGeneratedAudioProgressCallbackWithArg)(
2470
+ const float *samples, int32_t n, float p, void *arg);
2471
+
2472
+ /** @brief Opaque offline TTS handle. */
2473
+ typedef struct SherpaOnnxOfflineTts SherpaOnnxOfflineTts;
2474
+
2475
+ /**
2476
+ * @brief Create an offline TTS engine.
2477
+ *
2478
+ * @param config TTS configuration.
2479
+ * @return A newly allocated TTS engine on success, or NULL on configuration
2480
+ * error. Free it with SherpaOnnxDestroyOfflineTts().
2481
+ *
2482
+ * @code
2483
+ * SherpaOnnxOfflineTtsConfig config;
2484
+ * memset(&config, 0, sizeof(config));
2485
+ * config.model.kokoro.model = "./kokoro-en-v0_19/model.onnx";
2486
+ * config.model.kokoro.voices = "./kokoro-en-v0_19/voices.bin";
2487
+ * config.model.kokoro.tokens = "./kokoro-en-v0_19/tokens.txt";
2488
+ * config.model.kokoro.data_dir = "./kokoro-en-v0_19/espeak-ng-data";
2489
+ * config.model.num_threads = 2;
2490
+ *
2491
+ * const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&config);
2492
+ * @endcode
2493
+ */
2494
+ SHERPA_ONNX_API const SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTts(
2495
+ const SherpaOnnxOfflineTtsConfig *config);
2496
+
2497
+ /**
2498
+ * @brief Destroy an offline TTS engine.
2499
+ *
2500
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2501
+ */
2502
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineTts(
2503
+ const SherpaOnnxOfflineTts *tts);
2504
+
2505
+ /**
2506
+ * @brief Return the output sample rate of a TTS engine.
2507
+ *
2508
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2509
+ * @return Output sample rate in Hz.
2510
+ */
2511
+ SHERPA_ONNX_API int32_t
2512
+ SherpaOnnxOfflineTtsSampleRate(const SherpaOnnxOfflineTts *tts);
2513
+
2514
+ /**
2515
+ * @brief Return the number of available speaker IDs.
2516
+ *
2517
+ * Single-speaker models often return 1.
2518
+ *
2519
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2520
+ * @return Number of speakers supported by the model.
2521
+ */
2522
+ SHERPA_ONNX_API int32_t
2523
+ SherpaOnnxOfflineTtsNumSpeakers(const SherpaOnnxOfflineTts *tts);
2524
+
2525
+ /**
2526
+ * @brief Generate speech from text using the simple sid/speed interface.
2527
+ *
2528
+ * @deprecated Use SherpaOnnxOfflineTtsGenerateWithConfig() instead.
2529
+ *
2530
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2531
+ * @param text Input text.
2532
+ * @param sid Speaker ID for multi-speaker models.
2533
+ * @param speed Speech rate. Values > 1 are faster.
2534
+ * @return Generated audio, or NULL on error. Free it with
2535
+ * SherpaOnnxDestroyOfflineTtsGeneratedAudio().
2536
+ *
2537
+ * @code
2538
+ * const SherpaOnnxGeneratedAudio *audio =
2539
+ * SherpaOnnxOfflineTtsGenerate(tts, "Hello from sherpa-onnx!", 0, 1.0f);
2540
+ * SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate,
2541
+ * "./generated.wav");
2542
+ * SherpaOnnxDestroyOfflineTtsGeneratedAudio(audio);
2543
+ * @endcode
2544
+ */
2545
+ SHERPA_ONNX_API SHERPA_ONNX_DEPRECATED(
2546
+ "Use SherpaOnnxOfflineTtsGenerateWithConfig() instead") const
2547
+ SherpaOnnxGeneratedAudio *SherpaOnnxOfflineTtsGenerate(
2548
+ const SherpaOnnxOfflineTts *tts, const char *text, int32_t sid,
2549
+ float speed);
2550
+
2551
+ /**
2552
+ * @brief Generate speech and receive incremental audio chunks through a
2553
+ * callback.
2554
+ *
2555
+ * @deprecated Use SherpaOnnxOfflineTtsGenerateWithConfig() instead.
2556
+ *
2557
+ * The callback receives newly generated samples. The sample pointer is valid
2558
+ * only for the duration of the callback.
2559
+ *
2560
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2561
+ * @param text Input text.
2562
+ * @param sid Speaker ID for multi-speaker models.
2563
+ * @param speed Speech rate. Values > 1 are faster.
2564
+ * @param callback Incremental callback. Return 0 to stop generation early.
2565
+ * @return Final generated audio, or NULL on error. Free it with
2566
+ * SherpaOnnxDestroyOfflineTtsGeneratedAudio().
2567
+ */
2568
+ SHERPA_ONNX_API SHERPA_ONNX_DEPRECATED(
2569
+ "Use SherpaOnnxOfflineTtsGenerateWithConfig() instead") const
2570
+ SherpaOnnxGeneratedAudio *SherpaOnnxOfflineTtsGenerateWithCallback(
2571
+ const SherpaOnnxOfflineTts *tts, const char *text, int32_t sid,
2572
+ float speed, SherpaOnnxGeneratedAudioCallback callback);
2573
+
2574
+ /**
2575
+ * @brief Generate speech with a progress callback.
2576
+ *
2577
+ * @deprecated Use SherpaOnnxOfflineTtsGenerateWithConfig() instead.
2578
+ *
2579
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2580
+ * @param text Input text.
2581
+ * @param sid Speaker ID for multi-speaker models.
2582
+ * @param speed Speech rate. Values > 1 are faster.
2583
+ * @param callback Progress callback. Return 0 to stop generation early.
2584
+ * @return Final generated audio, or NULL on error. Free it with
2585
+ * SherpaOnnxDestroyOfflineTtsGeneratedAudio().
2586
+ *
2587
+ * @code
2588
+ * int32_t Progress(const float *samples, int32_t n, float p) {
2589
+ * fprintf(stderr, "Progress: %.2f%%\n", p * 100);
2590
+ * return 1;
2591
+ * }
2592
+ *
2593
+ * const SherpaOnnxGeneratedAudio *audio =
2594
+ * SherpaOnnxOfflineTtsGenerateWithProgressCallback(tts, text, 0, 1.0f,
2595
+ * Progress);
2596
+ * @endcode
2597
+ */
2598
+ SHERPA_ONNX_API SHERPA_ONNX_DEPRECATED(
2599
+ "Use SherpaOnnxOfflineTtsGenerateWithConfig() instead") const
2600
+ SherpaOnnxGeneratedAudio *SherpaOnnxOfflineTtsGenerateWithProgressCallback(
2601
+ const SherpaOnnxOfflineTts *tts, const char *text, int32_t sid,
2602
+ float speed, SherpaOnnxGeneratedAudioProgressCallback callback);
2603
+
2604
+ /**
2605
+ * @brief Generate speech with a progress callback that receives a user pointer.
2606
+ *
2607
+ * @deprecated Use SherpaOnnxOfflineTtsGenerateWithConfig() instead.
2608
+ *
2609
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2610
+ * @param text Input text.
2611
+ * @param sid Speaker ID for multi-speaker models.
2612
+ * @param speed Speech rate. Values > 1 are faster.
2613
+ * @param callback Progress callback with user pointer. Return 0 to stop early.
2614
+ * @param arg User pointer forwarded to @p callback.
2615
+ * @return Final generated audio, or NULL on error. Free it with
2616
+ * SherpaOnnxDestroyOfflineTtsGeneratedAudio().
2617
+ */
2618
+ SHERPA_ONNX_API SHERPA_ONNX_DEPRECATED(
2619
+ "Use SherpaOnnxOfflineTtsGenerateWithConfig() instead") const
2620
+ SherpaOnnxGeneratedAudio
2621
+ *SherpaOnnxOfflineTtsGenerateWithProgressCallbackWithArg(
2622
+ const SherpaOnnxOfflineTts *tts, const char *text, int32_t sid,
2623
+ float speed,
2624
+ SherpaOnnxGeneratedAudioProgressCallbackWithArg callback,
2625
+ void *arg);
2626
+
2627
+ /**
2628
+ * @brief Same as SherpaOnnxOfflineTtsGenerateWithCallback() but with a user
2629
+ * pointer.
2630
+ *
2631
+ * @deprecated Use SherpaOnnxOfflineTtsGenerateWithConfig() instead.
2632
+ *
2633
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2634
+ * @param text Input text.
2635
+ * @param sid Speaker ID for multi-speaker models.
2636
+ * @param speed Speech rate. Values > 1 are faster.
2637
+ * @param callback Incremental callback with user pointer.
2638
+ * @param arg User pointer forwarded to @p callback.
2639
+ * @return Final generated audio, or NULL on error. Free it with
2640
+ * SherpaOnnxDestroyOfflineTtsGeneratedAudio().
2641
+ */
2642
+ SHERPA_ONNX_API SHERPA_ONNX_DEPRECATED(
2643
+ "Use SherpaOnnxOfflineTtsGenerateWithConfig() instead") const
2644
+ SherpaOnnxGeneratedAudio *SherpaOnnxOfflineTtsGenerateWithCallbackWithArg(
2645
+ const SherpaOnnxOfflineTts *tts, const char *text, int32_t sid,
2646
+ float speed, SherpaOnnxGeneratedAudioCallbackWithArg callback,
2647
+ void *arg);
2648
+
2649
+ /**
2650
+ * @brief Deprecated ZipVoice-specific generation API.
2651
+ *
2652
+ * Use SherpaOnnxOfflineTtsGenerateWithConfig() instead.
2653
+ */
2654
+ SHERPA_ONNX_API SHERPA_ONNX_DEPRECATED(
2655
+ "Use SherpaOnnxOfflineTtsGenerateWithConfig() instead") const
2656
+ SherpaOnnxGeneratedAudio *SherpaOnnxOfflineTtsGenerateWithZipvoice(
2657
+ const SherpaOnnxOfflineTts *tts, const char *text,
2658
+ const char *prompt_text, const float *prompt_samples, int32_t n_prompt,
2659
+ int32_t prompt_sr, float speed, int32_t num_steps);
2660
+
2661
+ /**
2662
+ * @brief Generation-time parameters shared by advanced TTS APIs.
2663
+ *
2664
+ * This struct supports both simple multi-speaker synthesis and more advanced
2665
+ * zero-shot or reference-conditioned models.
2666
+ *
2667
+ * Example for Pocket TTS:
2668
+ *
2669
+ * @code
2670
+ * SherpaOnnxGenerationConfig cfg;
2671
+ * memset(&cfg, 0, sizeof(cfg));
2672
+ * cfg.speed = 1.0f;
2673
+ * cfg.reference_audio = wave->samples;
2674
+ * cfg.reference_audio_len = wave->num_samples;
2675
+ * cfg.reference_sample_rate = wave->sample_rate;
2676
+ * cfg.extra = "{\"max_reference_audio_len\": 10.0, \"seed\": 42}";
2677
+ * @endcode
2678
+ */
2679
+ typedef struct SherpaOnnxGenerationConfig {
2680
+ /** Silence scale between sentences. */
2681
+ float silence_scale;
2682
+ /** Speech rate. Used only by models that support it. */
2683
+ float speed;
2684
+ /** Speaker ID for multi-speaker models. */
2685
+ int32_t sid;
2686
+ /** Optional reference audio for zero-shot or voice-cloning models. */
2687
+ const float *reference_audio;
2688
+ /** Length of @c reference_audio in samples. */
2689
+ int32_t reference_audio_len;
2690
+ /** Sample rate of @c reference_audio. */
2691
+ int32_t reference_sample_rate;
2692
+ /** Optional reference text associated with @c reference_audio. */
2693
+ const char *reference_text;
2694
+ /** Optional number of flow-matching steps. */
2695
+ int32_t num_steps;
2696
+ /** Optional model-specific JSON string with extra key/value pairs. */
2697
+ const char *extra;
2698
+ } SherpaOnnxGenerationConfig;
2699
+
2700
+ /**
2701
+ * @brief Generate speech using the advanced configuration interface.
2702
+ *
2703
+ * This is the preferred API for new integrations. It supports callback-based
2704
+ * progress reporting and model-specific options such as reference audio.
2705
+ *
2706
+ * @param tts A pointer returned by SherpaOnnxCreateOfflineTts().
2707
+ * @param text Input text.
2708
+ * @param config Generation-time configuration.
2709
+ * @param callback Optional progress callback with user pointer. Return 0 to
2710
+ * stop early.
2711
+ * @param arg User pointer forwarded to @p callback.
2712
+ * @return Generated audio, or NULL on error. Free it with
2713
+ * SherpaOnnxDestroyOfflineTtsGeneratedAudio().
2714
+ *
2715
+ * @code
2716
+ * SherpaOnnxGenerationConfig cfg;
2717
+ * memset(&cfg, 0, sizeof(cfg));
2718
+ * cfg.sid = 0;
2719
+ * cfg.speed = 1.0f;
2720
+ * cfg.silence_scale = 0.2f;
2721
+ *
2722
+ * const SherpaOnnxGeneratedAudio *audio =
2723
+ * SherpaOnnxOfflineTtsGenerateWithConfig(tts,
2724
+ * "Today as always, men fall into two groups.",
2725
+ * &cfg, NULL, NULL);
2726
+ * @endcode
2727
+ */
2728
+ SHERPA_ONNX_API const SherpaOnnxGeneratedAudio *
2729
+ SherpaOnnxOfflineTtsGenerateWithConfig(
2730
+ const SherpaOnnxOfflineTts *tts, const char *text,
2731
+ const SherpaOnnxGenerationConfig *config,
2732
+ SherpaOnnxGeneratedAudioProgressCallbackWithArg callback, void *arg);
2733
+
2734
+ /**
2735
+ * @brief Destroy audio returned by a TTS generation API.
2736
+ *
2737
+ * @param p A pointer returned by one of the SherpaOnnxOfflineTtsGenerate*
2738
+ * functions.
2739
+ */
2740
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineTtsGeneratedAudio(
2741
+ const SherpaOnnxGeneratedAudio *p);
2742
+
2743
+ /**
2744
+ * @brief Write floating-point PCM to a mono 16-bit WAVE file.
2745
+ *
2746
+ * @param samples Pointer to @p n samples in the range [-1, 1].
2747
+ * @param n Number of samples.
2748
+ * @param sample_rate Sample rate in Hz.
2749
+ * @param filename Output filename.
2750
+ * @return 1 on success; 0 on failure.
2751
+ *
2752
+ * @code
2753
+ * SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate,
2754
+ * "./generated-kokoro-en.wav");
2755
+ * @endcode
2756
+ */
2757
+ SHERPA_ONNX_API int32_t SherpaOnnxWriteWave(const float *samples, int32_t n,
2758
+ int32_t sample_rate,
2759
+ const char *filename);
2760
+
2761
+ /**
2762
+ * @brief Return the number of bytes needed for a mono 16-bit WAVE file.
2763
+ *
2764
+ * @param n_samples Number of PCM samples.
2765
+ * @return Required buffer size in bytes.
2766
+ */
2767
+ SHERPA_ONNX_API int64_t SherpaOnnxWaveFileSize(int32_t n_samples);
2768
+
2769
+ /**
2770
+ * @brief Write a mono 16-bit WAVE file to a caller-provided buffer.
2771
+ *
2772
+ * Allocate at least SherpaOnnxWaveFileSize(@p n) bytes before calling.
2773
+ *
2774
+ * @param samples Pointer to @p n samples in the range [-1, 1].
2775
+ * @param n Number of samples.
2776
+ * @param sample_rate Sample rate in Hz.
2777
+ * @param buffer Output buffer.
2778
+ */
2779
+ SHERPA_ONNX_API void SherpaOnnxWriteWaveToBuffer(const float *samples,
2780
+ int32_t n, int32_t sample_rate,
2781
+ char *buffer);
2782
+
2783
+ /**
2784
+ * @brief Write multi-channel audio to a WAVE file (16-bit PCM).
2785
+ *
2786
+ * @param samples samples[c] is a pointer to channel c samples in [-1, 1].
2787
+ * @param n Number of samples per channel.
2788
+ * @param sample_rate Sample rate in Hz.
2789
+ * @param num_channels Number of channels.
2790
+ * @param filename Output filename.
2791
+ * @return 1 on success; 0 on failure.
2792
+ */
2793
+ SHERPA_ONNX_API int32_t SherpaOnnxWriteWaveMultiChannel(
2794
+ const float *const *samples, int32_t n, int32_t sample_rate,
2795
+ int32_t num_channels, const char *filename);
2796
+
2797
+ /**
2798
+ * @brief Decoded mono WAVE file content.
2799
+ *
2800
+ * Free this object with SherpaOnnxFreeWave().
2801
+ */
2802
+ typedef struct SherpaOnnxWave {
2803
+ /** Samples normalized to the range [-1, 1]. */
2804
+ const float *samples;
2805
+ /** Sample rate in Hz. */
2806
+ int32_t sample_rate;
2807
+ /** Number of samples. */
2808
+ int32_t num_samples;
2809
+ } SherpaOnnxWave;
2810
+
2811
+ /**
2812
+ * @brief Read a mono 16-bit PCM WAVE file.
2813
+ *
2814
+ * @param filename Input WAVE filename.
2815
+ * @return A newly allocated wave object, or NULL on error. Free it with
2816
+ * SherpaOnnxFreeWave().
2817
+ *
2818
+ * @code
2819
+ * const SherpaOnnxWave *wave = SherpaOnnxReadWave("./Obama.wav");
2820
+ * if (wave) {
2821
+ * printf("sample_rate=%d, num_samples=%d\n",
2822
+ * wave->sample_rate, wave->num_samples);
2823
+ * SherpaOnnxFreeWave(wave);
2824
+ * }
2825
+ * @endcode
2826
+ */
2827
+ SHERPA_ONNX_API const SherpaOnnxWave *SherpaOnnxReadWave(const char *filename);
2828
+
2829
+ /**
2830
+ * @brief Read a mono 16-bit PCM WAVE file from binary memory.
2831
+ *
2832
+ * @param data Pointer to the WAVE file bytes.
2833
+ * @param n Size of @p data in bytes.
2834
+ * @return A newly allocated wave object, or NULL on error. Free it with
2835
+ * SherpaOnnxFreeWave().
2836
+ */
2837
+ SHERPA_ONNX_API const SherpaOnnxWave *SherpaOnnxReadWaveFromBinaryData(
2838
+ const char *data, int32_t n);
2839
+
2840
+ /**
2841
+ * @brief Destroy a wave object returned by SherpaOnnxReadWave() or
2842
+ * SherpaOnnxReadWaveFromBinaryData().
2843
+ */
2844
+ SHERPA_ONNX_API void SherpaOnnxFreeWave(const SherpaOnnxWave *wave);
2845
+
2846
+ /**
2847
+ * @brief Decoded multi-channel WAVE file content.
2848
+ *
2849
+ * Free this object with SherpaOnnxFreeMultiChannelWave().
2850
+ */
2851
+ typedef struct SherpaOnnxMultiChannelWave {
2852
+ /** samples[c] points to channel c samples normalized to [-1, 1].
2853
+ * Note: The sample data for all channels are stored in a single contiguous
2854
+ * memory block, one channel after another.
2855
+ * */
2856
+ const float *const *samples;
2857
+ /** Number of channels. */
2858
+ int32_t num_channels;
2859
+ /** Number of samples per channel. */
2860
+ int32_t num_samples;
2861
+ /** Sample rate in Hz. */
2862
+ int32_t sample_rate;
2863
+ } SherpaOnnxMultiChannelWave;
2864
+
2865
+ /**
2866
+ * @brief Read a multi-channel 16-bit PCM WAVE file.
2867
+ *
2868
+ * @param filename Input WAVE filename.
2869
+ * @return A newly allocated multi-channel wave object, or NULL on error.
2870
+ * Free it with SherpaOnnxFreeMultiChannelWave().
2871
+ */
2872
+ SHERPA_ONNX_API const SherpaOnnxMultiChannelWave *
2873
+ SherpaOnnxReadWaveMultiChannel(const char *filename);
2874
+
2875
+ /**
2876
+ * @brief Destroy a multi-channel wave object.
2877
+ *
2878
+ * @param wave A pointer returned by SherpaOnnxReadWaveMultiChannel().
2879
+ */
2880
+ SHERPA_ONNX_API void SherpaOnnxFreeMultiChannelWave(
2881
+ const SherpaOnnxMultiChannelWave *wave);
2882
+
2883
+ // ============================================================
2884
+ // For spoken language identification
2885
+ // ============================================================
2886
+
2887
+ /**
2888
+ * @brief Whisper-based model files for spoken language identification.
2889
+ *
2890
+ * Example:
2891
+ *
2892
+ * @code
2893
+ * SherpaOnnxSpokenLanguageIdentificationWhisperConfig whisper;
2894
+ * memset(&whisper, 0, sizeof(whisper));
2895
+ * whisper.encoder = "./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx";
2896
+ * whisper.decoder = "./sherpa-onnx-whisper-tiny/tiny-decoder.int8.onnx";
2897
+ * @endcode
2898
+ */
2899
+ typedef struct SherpaOnnxSpokenLanguageIdentificationWhisperConfig {
2900
+ /** Whisper encoder model. */
2901
+ const char *encoder;
2902
+ /** Whisper decoder model. */
2903
+ const char *decoder;
2904
+ /** Optional tail padding in samples appended internally before inference. */
2905
+ int32_t tail_paddings;
2906
+ } SherpaOnnxSpokenLanguageIdentificationWhisperConfig;
2907
+
2908
+ /**
2909
+ * @brief Configuration for spoken language identification.
2910
+ *
2911
+ * The current implementation uses Whisper-based models.
2912
+ *
2913
+ * Example using `sherpa-onnx-whisper-tiny`:
2914
+ *
2915
+ * @code
2916
+ * SherpaOnnxSpokenLanguageIdentificationConfig config;
2917
+ * memset(&config, 0, sizeof(config));
2918
+ * config.whisper.encoder = "./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx";
2919
+ * config.whisper.decoder = "./sherpa-onnx-whisper-tiny/tiny-decoder.int8.onnx";
2920
+ * config.num_threads = 1;
2921
+ * config.provider = "cpu";
2922
+ * @endcode
2923
+ */
2924
+ typedef struct SherpaOnnxSpokenLanguageIdentificationConfig {
2925
+ /** Whisper model configuration. */
2926
+ SherpaOnnxSpokenLanguageIdentificationWhisperConfig whisper;
2927
+ /** Number of inference threads. */
2928
+ int32_t num_threads;
2929
+ /** Non-zero to print debug information. */
2930
+ int32_t debug;
2931
+ /** Execution provider such as `"cpu"`. */
2932
+ const char *provider;
2933
+ } SherpaOnnxSpokenLanguageIdentificationConfig;
2934
+
2935
+ /** @brief Opaque spoken-language identification handle. */
2936
+ typedef struct SherpaOnnxSpokenLanguageIdentification
2937
+ SherpaOnnxSpokenLanguageIdentification;
2938
+
2939
+ /**
2940
+ * @brief Create a spoken-language identifier.
2941
+ *
2942
+ * @param config Spoken-language identification configuration.
2943
+ * @return A newly allocated identifier on success, or NULL on error. Free it
2944
+ * with SherpaOnnxDestroySpokenLanguageIdentification().
2945
+ */
2946
+ SHERPA_ONNX_API const SherpaOnnxSpokenLanguageIdentification *
2947
+ SherpaOnnxCreateSpokenLanguageIdentification(
2948
+ const SherpaOnnxSpokenLanguageIdentificationConfig *config);
2949
+
2950
+ /**
2951
+ * @brief Destroy a spoken-language identifier.
2952
+ *
2953
+ * @param slid A pointer returned by
2954
+ * SherpaOnnxCreateSpokenLanguageIdentification().
2955
+ */
2956
+ SHERPA_ONNX_API void SherpaOnnxDestroySpokenLanguageIdentification(
2957
+ const SherpaOnnxSpokenLanguageIdentification *slid);
2958
+
2959
+ /**
2960
+ * @brief Create an offline stream for spoken-language identification.
2961
+ *
2962
+ * Feed audio to the returned stream with SherpaOnnxAcceptWaveformOffline(), and
2963
+ * then call SherpaOnnxSpokenLanguageIdentificationCompute().
2964
+ *
2965
+ * @param slid A pointer returned by
2966
+ * SherpaOnnxCreateSpokenLanguageIdentification().
2967
+ * @return A newly allocated offline stream. Free it with
2968
+ * SherpaOnnxDestroyOfflineStream().
2969
+ */
2970
+ SHERPA_ONNX_API SherpaOnnxOfflineStream *
2971
+ SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream(
2972
+ const SherpaOnnxSpokenLanguageIdentification *slid);
2973
+
2974
+ /**
2975
+ * @brief Result of spoken-language identification.
2976
+ *
2977
+ * Free this object with SherpaOnnxDestroySpokenLanguageIdentificationResult().
2978
+ */
2979
+ typedef struct SherpaOnnxSpokenLanguageIdentificationResult {
2980
+ /**
2981
+ * Predicted language code such as `"en"`, `"de"`, `"zh"`, or `"es"`.
2982
+ */
2983
+ const char *lang;
2984
+ } SherpaOnnxSpokenLanguageIdentificationResult;
2985
+
2986
+ /**
2987
+ * @brief Run spoken-language identification on an offline stream.
2988
+ *
2989
+ * Example:
2990
+ *
2991
+ * @code
2992
+ * SherpaOnnxOfflineStream *stream =
2993
+ * SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream(slid);
2994
+ * SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
2995
+ * wave->num_samples);
2996
+ * const SherpaOnnxSpokenLanguageIdentificationResult *result =
2997
+ * SherpaOnnxSpokenLanguageIdentificationCompute(slid, stream);
2998
+ * printf("lang=%s\n", result->lang);
2999
+ * SherpaOnnxDestroySpokenLanguageIdentificationResult(result);
3000
+ * SherpaOnnxDestroyOfflineStream(stream);
3001
+ * @endcode
3002
+ *
3003
+ * @param slid A pointer returned by
3004
+ * SherpaOnnxCreateSpokenLanguageIdentification().
3005
+ * @param s A pointer returned by
3006
+ * SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream().
3007
+ * @return A newly allocated result object. Free it with
3008
+ * SherpaOnnxDestroySpokenLanguageIdentificationResult().
3009
+ */
3010
+ SHERPA_ONNX_API const SherpaOnnxSpokenLanguageIdentificationResult *
3011
+ SherpaOnnxSpokenLanguageIdentificationCompute(
3012
+ const SherpaOnnxSpokenLanguageIdentification *slid,
3013
+ const SherpaOnnxOfflineStream *s);
3014
+
3015
+ /**
3016
+ * @brief Destroy a spoken-language identification result.
3017
+ *
3018
+ * @param r A pointer returned by
3019
+ * SherpaOnnxSpokenLanguageIdentificationCompute().
3020
+ */
3021
+ SHERPA_ONNX_API void SherpaOnnxDestroySpokenLanguageIdentificationResult(
3022
+ const SherpaOnnxSpokenLanguageIdentificationResult *r);
3023
+
3024
+ // ============================================================
3025
+ // For speaker embedding extraction
3026
+ // ============================================================
3027
+ /**
3028
+ * @brief Configuration for speaker embedding extraction.
3029
+ *
3030
+ * Example using
3031
+ * `3dspeaker_speech_campplus_sv_zh-cn_16k-common.onnx`:
3032
+ *
3033
+ * @code
3034
+ * SherpaOnnxSpeakerEmbeddingExtractorConfig config;
3035
+ * memset(&config, 0, sizeof(config));
3036
+ * config.model = "./3dspeaker_speech_campplus_sv_zh-cn_16k-common.onnx";
3037
+ * config.num_threads = 1;
3038
+ * config.provider = "cpu";
3039
+ * @endcode
3040
+ */
3041
+ typedef struct SherpaOnnxSpeakerEmbeddingExtractorConfig {
3042
+ /** Speaker embedding model file. */
3043
+ const char *model;
3044
+ /** Number of inference threads. */
3045
+ int32_t num_threads;
3046
+ /** Non-zero to print debug information. */
3047
+ int32_t debug;
3048
+ /** Execution provider such as `"cpu"`. */
3049
+ const char *provider;
3050
+ } SherpaOnnxSpeakerEmbeddingExtractorConfig;
3051
+
3052
+ /** @brief Opaque speaker embedding extractor handle. */
3053
+ typedef struct SherpaOnnxSpeakerEmbeddingExtractor
3054
+ SherpaOnnxSpeakerEmbeddingExtractor;
3055
+
3056
+ /**
3057
+ * @brief Create a speaker embedding extractor.
3058
+ *
3059
+ * @param config Speaker embedding extractor configuration.
3060
+ * @return A newly allocated extractor on success, or NULL on error. Free it
3061
+ * with SherpaOnnxDestroySpeakerEmbeddingExtractor().
3062
+ */
3063
+ SHERPA_ONNX_API const SherpaOnnxSpeakerEmbeddingExtractor *
3064
+ SherpaOnnxCreateSpeakerEmbeddingExtractor(
3065
+ const SherpaOnnxSpeakerEmbeddingExtractorConfig *config);
3066
+
3067
+ /**
3068
+ * @brief Destroy a speaker embedding extractor.
3069
+ *
3070
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingExtractor().
3071
+ */
3072
+ SHERPA_ONNX_API void SherpaOnnxDestroySpeakerEmbeddingExtractor(
3073
+ const SherpaOnnxSpeakerEmbeddingExtractor *p);
3074
+
3075
+ /**
3076
+ * @brief Return the embedding dimension produced by the extractor.
3077
+ *
3078
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingExtractor().
3079
+ * @return Embedding dimension.
3080
+ */
3081
+ SHERPA_ONNX_API int32_t SherpaOnnxSpeakerEmbeddingExtractorDim(
3082
+ const SherpaOnnxSpeakerEmbeddingExtractor *p);
3083
+
3084
+ /**
3085
+ * @brief Create a streaming feature buffer for embedding extraction.
3086
+ *
3087
+ * Feed samples with SherpaOnnxOnlineStreamAcceptWaveform(), then call
3088
+ * SherpaOnnxSpeakerEmbeddingExtractorIsReady() and
3089
+ * SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding().
3090
+ *
3091
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingExtractor().
3092
+ * @return A newly allocated online stream. Free it with
3093
+ * SherpaOnnxDestroyOnlineStream().
3094
+ */
3095
+ SHERPA_ONNX_API const SherpaOnnxOnlineStream *
3096
+ SherpaOnnxSpeakerEmbeddingExtractorCreateStream(
3097
+ const SherpaOnnxSpeakerEmbeddingExtractor *p);
3098
+
3099
+ /**
3100
+ * @brief Check whether enough audio has been provided to compute an embedding.
3101
+ *
3102
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingExtractor().
3103
+ * @param s A pointer returned by
3104
+ * SherpaOnnxSpeakerEmbeddingExtractorCreateStream().
3105
+ * @return 1 if the stream is ready; otherwise 0.
3106
+ */
3107
+ SHERPA_ONNX_API int32_t SherpaOnnxSpeakerEmbeddingExtractorIsReady(
3108
+ const SherpaOnnxSpeakerEmbeddingExtractor *p,
3109
+ const SherpaOnnxOnlineStream *s);
3110
+
3111
+ /**
3112
+ * @brief Compute the embedding for a stream.
3113
+ *
3114
+ * The returned vector has `SherpaOnnxSpeakerEmbeddingExtractorDim(p)` elements.
3115
+ * Free it with SherpaOnnxSpeakerEmbeddingExtractorDestroyEmbedding().
3116
+ *
3117
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingExtractor().
3118
+ * @param s A pointer returned by
3119
+ * SherpaOnnxSpeakerEmbeddingExtractorCreateStream().
3120
+ * @return A newly allocated embedding vector.
3121
+ *
3122
+ * @code
3123
+ * const SherpaOnnxOnlineStream *stream =
3124
+ * SherpaOnnxSpeakerEmbeddingExtractorCreateStream(ex);
3125
+ * SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate,
3126
+ * wave->samples, wave->num_samples);
3127
+ * SherpaOnnxOnlineStreamInputFinished(stream);
3128
+ * if (SherpaOnnxSpeakerEmbeddingExtractorIsReady(ex, stream)) {
3129
+ * const float *v =
3130
+ * SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding(ex, stream);
3131
+ * SherpaOnnxSpeakerEmbeddingExtractorDestroyEmbedding(v);
3132
+ * }
3133
+ * SherpaOnnxDestroyOnlineStream(stream);
3134
+ * @endcode
3135
+ */
3136
+ SHERPA_ONNX_API const float *
3137
+ SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding(
3138
+ const SherpaOnnxSpeakerEmbeddingExtractor *p,
3139
+ const SherpaOnnxOnlineStream *s);
3140
+
3141
+ /**
3142
+ * @brief Destroy an embedding vector returned by
3143
+ * SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding().
3144
+ *
3145
+ * @param v A pointer returned by
3146
+ * SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding().
3147
+ */
3148
+ SHERPA_ONNX_API void SherpaOnnxSpeakerEmbeddingExtractorDestroyEmbedding(
3149
+ const float *v);
3150
+
3151
+ /** @brief Opaque speaker embedding manager handle. */
3152
+ typedef struct SherpaOnnxSpeakerEmbeddingManager
3153
+ SherpaOnnxSpeakerEmbeddingManager;
3154
+
3155
+ /**
3156
+ * @brief Create a speaker embedding manager.
3157
+ *
3158
+ * The manager stores enrolled speaker embeddings and supports speaker search
3159
+ * and verification.
3160
+ *
3161
+ * @param dim Embedding dimension. This should match
3162
+ * SherpaOnnxSpeakerEmbeddingExtractorDim().
3163
+ * @return A newly allocated manager. Free it with
3164
+ * SherpaOnnxDestroySpeakerEmbeddingManager().
3165
+ */
3166
+ SHERPA_ONNX_API const SherpaOnnxSpeakerEmbeddingManager *
3167
+ SherpaOnnxCreateSpeakerEmbeddingManager(int32_t dim);
3168
+
3169
+ /**
3170
+ * @brief Destroy a speaker embedding manager.
3171
+ *
3172
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3173
+ */
3174
+ SHERPA_ONNX_API void SherpaOnnxDestroySpeakerEmbeddingManager(
3175
+ const SherpaOnnxSpeakerEmbeddingManager *p);
3176
+
3177
+ /**
3178
+ * @brief Add one enrollment embedding for a speaker.
3179
+ *
3180
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3181
+ * @param name Speaker name.
3182
+ * @param v Embedding vector with exactly `dim` elements.
3183
+ * @return 1 on success; 0 on error.
3184
+ */
3185
+ SHERPA_ONNX_API int32_t
3186
+ SherpaOnnxSpeakerEmbeddingManagerAdd(const SherpaOnnxSpeakerEmbeddingManager *p,
3187
+ const char *name, const float *v);
3188
+
3189
+ /**
3190
+ * @brief Add multiple enrollment embeddings for one speaker.
3191
+ *
3192
+ * @p v is a NULL-terminated array of embedding pointers:
3193
+ * `v[0]`, `v[1]`, ..., `v[n - 1]`, followed by `v[n] == NULL`.
3194
+ *
3195
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3196
+ * @param name Speaker name.
3197
+ * @param v NULL-terminated array of embedding pointers.
3198
+ * @return 1 on success; 0 on error.
3199
+ *
3200
+ * @code
3201
+ * const float *spk1_vec[4] = {e1, e2, e3, NULL};
3202
+ * SherpaOnnxSpeakerEmbeddingManagerAddList(manager, "fangjun", spk1_vec);
3203
+ * @endcode
3204
+ */
3205
+ SHERPA_ONNX_API int32_t SherpaOnnxSpeakerEmbeddingManagerAddList(
3206
+ const SherpaOnnxSpeakerEmbeddingManager *p, const char *name,
3207
+ const float **v);
3208
+
3209
+ /**
3210
+ * @brief Add multiple enrollment embeddings packed in one flat array.
3211
+ *
3212
+ * The input contains @p n embeddings laid out consecutively, so the total
3213
+ * array length must be `n * dim`.
3214
+ *
3215
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3216
+ * @param name Speaker name.
3217
+ * @param v Flattened embedding array.
3218
+ * @param n Number of embeddings in @p v.
3219
+ * @return 1 on success; 0 on error.
3220
+ */
3221
+ SHERPA_ONNX_API int32_t SherpaOnnxSpeakerEmbeddingManagerAddListFlattened(
3222
+ const SherpaOnnxSpeakerEmbeddingManager *p, const char *name,
3223
+ const float *v, int32_t n);
3224
+
3225
+ /**
3226
+ * @brief Remove a speaker from the manager.
3227
+ *
3228
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3229
+ * @param name Speaker name to remove.
3230
+ * @return 1 if removed; otherwise 0. Returns 0 if the speaker does not exist.
3231
+ */
3232
+ SHERPA_ONNX_API int32_t SherpaOnnxSpeakerEmbeddingManagerRemove(
3233
+ const SherpaOnnxSpeakerEmbeddingManager *p, const char *name);
3234
+
3235
+ /**
3236
+ * @brief Search for the best matching enrolled speaker.
3237
+ *
3238
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3239
+ * @param v Query embedding vector.
3240
+ * @param threshold Minimum similarity threshold in the range [0, 1].
3241
+ * @return A newly allocated speaker name on match, or NULL if no speaker
3242
+ * passes the threshold. Free the returned name with
3243
+ * SherpaOnnxSpeakerEmbeddingManagerFreeSearch().
3244
+ */
3245
+ SHERPA_ONNX_API const char *SherpaOnnxSpeakerEmbeddingManagerSearch(
3246
+ const SherpaOnnxSpeakerEmbeddingManager *p, const float *v,
3247
+ float threshold);
3248
+
3249
+ /**
3250
+ * @brief Free a string returned by SherpaOnnxSpeakerEmbeddingManagerSearch().
3251
+ *
3252
+ * @param name A pointer returned by
3253
+ * SherpaOnnxSpeakerEmbeddingManagerSearch().
3254
+ */
3255
+ SHERPA_ONNX_API void SherpaOnnxSpeakerEmbeddingManagerFreeSearch(
3256
+ const char *name);
3257
+
3258
+ /**
3259
+ * @brief One speaker match returned by the best-matches API.
3260
+ */
3261
+ typedef struct SherpaOnnxSpeakerEmbeddingManagerSpeakerMatch {
3262
+ /** Similarity score. Larger means more similar. */
3263
+ float score;
3264
+ /** Speaker name. */
3265
+ const char *name;
3266
+ } SherpaOnnxSpeakerEmbeddingManagerSpeakerMatch;
3267
+
3268
+ /**
3269
+ * @brief Collection of best speaker matches.
3270
+ *
3271
+ * Free this object with SherpaOnnxSpeakerEmbeddingManagerFreeBestMatches().
3272
+ */
3273
+ typedef struct SherpaOnnxSpeakerEmbeddingManagerBestMatchesResult {
3274
+ /** Pointer to an array of @c count matches. */
3275
+ const SherpaOnnxSpeakerEmbeddingManagerSpeakerMatch *matches;
3276
+ /** Number of valid entries in @c matches. */
3277
+ int32_t count;
3278
+ } SherpaOnnxSpeakerEmbeddingManagerBestMatchesResult;
3279
+
3280
+ /**
3281
+ * @brief Return up to @p n best matches above a similarity threshold.
3282
+ *
3283
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3284
+ * @param v Query embedding vector.
3285
+ * @param threshold Minimum similarity threshold in the range [0, 1].
3286
+ * @param n Maximum number of matches to return.
3287
+ * @return A newly allocated result object, or NULL if no matches are found.
3288
+ * Free it with SherpaOnnxSpeakerEmbeddingManagerFreeBestMatches().
3289
+ */
3290
+ SHERPA_ONNX_API const SherpaOnnxSpeakerEmbeddingManagerBestMatchesResult *
3291
+ SherpaOnnxSpeakerEmbeddingManagerGetBestMatches(
3292
+ const SherpaOnnxSpeakerEmbeddingManager *p, const float *v, float threshold,
3293
+ int32_t n);
3294
+
3295
+ /**
3296
+ * @brief Destroy a best-matches result.
3297
+ *
3298
+ * @param r A pointer returned by
3299
+ * SherpaOnnxSpeakerEmbeddingManagerGetBestMatches().
3300
+ */
3301
+ SHERPA_ONNX_API void SherpaOnnxSpeakerEmbeddingManagerFreeBestMatches(
3302
+ const SherpaOnnxSpeakerEmbeddingManagerBestMatchesResult *r);
3303
+
3304
+ /**
3305
+ * @brief Verify whether a query embedding matches a named speaker.
3306
+ *
3307
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3308
+ * @param name Speaker name to compare against.
3309
+ * @param v Query embedding vector.
3310
+ * @param threshold Minimum similarity threshold in the range [0, 1].
3311
+ * @return 1 if the speaker matches; otherwise 0.
3312
+ */
3313
+ SHERPA_ONNX_API int32_t SherpaOnnxSpeakerEmbeddingManagerVerify(
3314
+ const SherpaOnnxSpeakerEmbeddingManager *p, const char *name,
3315
+ const float *v, float threshold);
3316
+
3317
+ /**
3318
+ * @brief Check whether a speaker is enrolled.
3319
+ *
3320
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3321
+ * @param name Speaker name.
3322
+ * @return 1 if the speaker exists; otherwise 0.
3323
+ */
3324
+ SHERPA_ONNX_API int32_t SherpaOnnxSpeakerEmbeddingManagerContains(
3325
+ const SherpaOnnxSpeakerEmbeddingManager *p, const char *name);
3326
+
3327
+ /**
3328
+ * @brief Return the number of enrolled speakers.
3329
+ *
3330
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3331
+ * @return Number of enrolled speakers.
3332
+ */
3333
+ SHERPA_ONNX_API int32_t SherpaOnnxSpeakerEmbeddingManagerNumSpeakers(
3334
+ const SherpaOnnxSpeakerEmbeddingManager *p);
3335
+
3336
+ /**
3337
+ * @brief Return all enrolled speaker names.
3338
+ *
3339
+ * The returned array is NULL-terminated. If no speakers are enrolled, the
3340
+ * returned array still exists and its first element is NULL.
3341
+ *
3342
+ * @param p A pointer returned by SherpaOnnxCreateSpeakerEmbeddingManager().
3343
+ * @return A newly allocated NULL-terminated array of speaker names. Free it
3344
+ * with SherpaOnnxSpeakerEmbeddingManagerFreeAllSpeakers().
3345
+ */
3346
+ SHERPA_ONNX_API const char *const *
3347
+ SherpaOnnxSpeakerEmbeddingManagerGetAllSpeakers(
3348
+ const SherpaOnnxSpeakerEmbeddingManager *p);
3349
+
3350
+ /**
3351
+ * @brief Free an array returned by
3352
+ * SherpaOnnxSpeakerEmbeddingManagerGetAllSpeakers().
3353
+ *
3354
+ * @param names A pointer returned by
3355
+ * SherpaOnnxSpeakerEmbeddingManagerGetAllSpeakers().
3356
+ */
3357
+ SHERPA_ONNX_API void SherpaOnnxSpeakerEmbeddingManagerFreeAllSpeakers(
3358
+ const char *const *names);
3359
+
3360
+ // ============================================================
3361
+ // For audio tagging
3362
+ // ============================================================
3363
+ /** @brief Zipformer audio-tagging model configuration. */
3364
+ typedef struct SherpaOnnxOfflineZipformerAudioTaggingModelConfig {
3365
+ /** Model filename. */
3366
+ const char *model;
3367
+ } SherpaOnnxOfflineZipformerAudioTaggingModelConfig;
3368
+
3369
+ /**
3370
+ * @brief Audio-tagging model configuration.
3371
+ *
3372
+ * Configure exactly one model family. If multiple model families are provided,
3373
+ * one of them will be used and the choice is implementation-defined.
3374
+ *
3375
+ * Example using
3376
+ * `sherpa-onnx-zipformer-audio-tagging-2024-04-09`:
3377
+ *
3378
+ * @code
3379
+ * SherpaOnnxAudioTaggingModelConfig model;
3380
+ * memset(&model, 0, sizeof(model));
3381
+ * model.zipformer.model =
3382
+ * "./sherpa-onnx-zipformer-audio-tagging-2024-04-09/model.int8.onnx";
3383
+ * model.num_threads = 1;
3384
+ * model.provider = "cpu";
3385
+ * @endcode
3386
+ */
3387
+ typedef struct SherpaOnnxAudioTaggingModelConfig {
3388
+ /** Zipformer model configuration. */
3389
+ SherpaOnnxOfflineZipformerAudioTaggingModelConfig zipformer;
3390
+ /** Alternative CED model file. */
3391
+ const char *ced;
3392
+ /** Number of inference threads. */
3393
+ int32_t num_threads;
3394
+ /** Non-zero to print debug information. */
3395
+ int32_t debug;
3396
+ /** Execution provider such as `"cpu"`. */
3397
+ const char *provider;
3398
+ } SherpaOnnxAudioTaggingModelConfig;
3399
+
3400
+ /**
3401
+ * @brief Configuration for audio tagging.
3402
+ *
3403
+ * @code
3404
+ * SherpaOnnxAudioTaggingConfig config;
3405
+ * memset(&config, 0, sizeof(config));
3406
+ * config.model.zipformer.model =
3407
+ * "./sherpa-onnx-zipformer-audio-tagging-2024-04-09/model.int8.onnx";
3408
+ * config.model.num_threads = 1;
3409
+ * config.model.provider = "cpu";
3410
+ * config.labels =
3411
+ * "./sherpa-onnx-zipformer-audio-tagging-2024-04-09/class_labels_indices.csv";
3412
+ * config.top_k = 5;
3413
+ * @endcode
3414
+ */
3415
+ typedef struct SherpaOnnxAudioTaggingConfig {
3416
+ /** Acoustic model configuration. */
3417
+ SherpaOnnxAudioTaggingModelConfig model;
3418
+ /** CSV file containing class labels. */
3419
+ const char *labels;
3420
+ /** Default number of results to return when `top_k == -1` at inference time.
3421
+ */
3422
+ int32_t top_k;
3423
+ } SherpaOnnxAudioTaggingConfig;
3424
+
3425
+ /**
3426
+ * @brief One audio-tagging prediction.
3427
+ */
3428
+ typedef struct SherpaOnnxAudioEvent {
3429
+ /** Event label. */
3430
+ const char *name;
3431
+ /** Integer label index. */
3432
+ int32_t index;
3433
+ /** Probability or confidence score. */
3434
+ float prob;
3435
+ } SherpaOnnxAudioEvent;
3436
+
3437
+ /** @brief Opaque audio tagger handle. */
3438
+ typedef struct SherpaOnnxAudioTagging SherpaOnnxAudioTagging;
3439
+
3440
+ /**
3441
+ * @brief Create an audio tagger.
3442
+ *
3443
+ * @param config Audio-tagging configuration.
3444
+ * @return A newly allocated audio tagger on success, or NULL on error. Free it
3445
+ * with SherpaOnnxDestroyAudioTagging().
3446
+ */
3447
+ SHERPA_ONNX_API const SherpaOnnxAudioTagging *SherpaOnnxCreateAudioTagging(
3448
+ const SherpaOnnxAudioTaggingConfig *config);
3449
+
3450
+ /**
3451
+ * @brief Destroy an audio tagger.
3452
+ *
3453
+ * @param tagger A pointer returned by SherpaOnnxCreateAudioTagging().
3454
+ */
3455
+ SHERPA_ONNX_API void SherpaOnnxDestroyAudioTagging(
3456
+ const SherpaOnnxAudioTagging *tagger);
3457
+
3458
+ /**
3459
+ * @brief Create an offline stream for audio tagging.
3460
+ *
3461
+ * @param tagger A pointer returned by SherpaOnnxCreateAudioTagging().
3462
+ * @return A newly allocated offline stream. Free it with
3463
+ * SherpaOnnxDestroyOfflineStream().
3464
+ */
3465
+ SHERPA_ONNX_API const SherpaOnnxOfflineStream *
3466
+ SherpaOnnxAudioTaggingCreateOfflineStream(const SherpaOnnxAudioTagging *tagger);
3467
+
3468
+ /**
3469
+ * @brief Run audio tagging on an offline stream.
3470
+ *
3471
+ * The returned array is NULL-terminated. If @p top_k is -1, the value stored in
3472
+ * `config.top_k` is used instead.
3473
+ *
3474
+ * @param tagger A pointer returned by SherpaOnnxCreateAudioTagging().
3475
+ * @param s A pointer returned by SherpaOnnxAudioTaggingCreateOfflineStream().
3476
+ * @param top_k Number of top results to return, or -1 to use the configured
3477
+ * default.
3478
+ * @return A newly allocated NULL-terminated array of result pointers ordered by
3479
+ * descending probability. Free it with
3480
+ * SherpaOnnxAudioTaggingFreeResults().
3481
+ *
3482
+ * @code
3483
+ * const SherpaOnnxAudioEvent *const *results =
3484
+ * SherpaOnnxAudioTaggingCompute(tagger, stream, 5);
3485
+ * for (int32_t i = 0; results[i] != NULL; ++i) {
3486
+ * printf("%d %.3f %s\n", results[i]->index, results[i]->prob,
3487
+ * results[i]->name);
3488
+ * }
3489
+ * SherpaOnnxAudioTaggingFreeResults(results);
3490
+ * @endcode
3491
+ */
3492
+ SHERPA_ONNX_API const SherpaOnnxAudioEvent *const *
3493
+ SherpaOnnxAudioTaggingCompute(const SherpaOnnxAudioTagging *tagger,
3494
+ const SherpaOnnxOfflineStream *s, int32_t top_k);
3495
+
3496
+ /**
3497
+ * @brief Destroy results returned by SherpaOnnxAudioTaggingCompute().
3498
+ *
3499
+ * @param p A pointer returned by SherpaOnnxAudioTaggingCompute().
3500
+ */
3501
+ SHERPA_ONNX_API void SherpaOnnxAudioTaggingFreeResults(
3502
+ const SherpaOnnxAudioEvent *const *p);
3503
+
3504
+ // ============================================================
3505
+ // For punctuation
3506
+ // ============================================================
3507
+
3508
+ /**
3509
+ * @brief Offline punctuation model configuration.
3510
+ *
3511
+ * Example:
3512
+ *
3513
+ * @code
3514
+ * SherpaOnnxOfflinePunctuationModelConfig model;
3515
+ * memset(&model, 0, sizeof(model));
3516
+ * model.ct_transformer =
3517
+ * "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx";
3518
+ * model.num_threads = 1;
3519
+ * model.provider = "cpu";
3520
+ * @endcode
3521
+ */
3522
+ typedef struct SherpaOnnxOfflinePunctuationModelConfig {
3523
+ /** Offline punctuation model file. */
3524
+ const char *ct_transformer;
3525
+ /** Number of inference threads. */
3526
+ int32_t num_threads;
3527
+ /** Non-zero to print debug information. */
3528
+ int32_t debug;
3529
+ /** Execution provider such as `"cpu"`. */
3530
+ const char *provider;
3531
+ } SherpaOnnxOfflinePunctuationModelConfig;
3532
+
3533
+ /** @brief Configuration for offline punctuation. */
3534
+ typedef struct SherpaOnnxOfflinePunctuationConfig {
3535
+ /** Model configuration. */
3536
+ SherpaOnnxOfflinePunctuationModelConfig model;
3537
+ } SherpaOnnxOfflinePunctuationConfig;
3538
+
3539
+ /** @brief Opaque offline punctuation handle. */
3540
+ typedef struct SherpaOnnxOfflinePunctuation SherpaOnnxOfflinePunctuation;
3541
+
3542
+ /**
3543
+ * @brief Create an offline punctuation processor.
3544
+ *
3545
+ * @param config Offline punctuation configuration.
3546
+ * @return A newly allocated punctuation processor on success, or NULL on
3547
+ * error. Free it with SherpaOnnxDestroyOfflinePunctuation().
3548
+ */
3549
+ SHERPA_ONNX_API const SherpaOnnxOfflinePunctuation *
3550
+ SherpaOnnxCreateOfflinePunctuation(
3551
+ const SherpaOnnxOfflinePunctuationConfig *config);
3552
+
3553
+ /**
3554
+ * @brief Destroy an offline punctuation processor.
3555
+ *
3556
+ * @param punct A pointer returned by SherpaOnnxCreateOfflinePunctuation().
3557
+ */
3558
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflinePunctuation(
3559
+ const SherpaOnnxOfflinePunctuation *punct);
3560
+
3561
+ /**
3562
+ * @brief Add punctuation to a complete input text.
3563
+ *
3564
+ * @param punct A pointer returned by SherpaOnnxCreateOfflinePunctuation().
3565
+ * @param text Input text without punctuation.
3566
+ * @return A newly allocated punctuated string. Free it with
3567
+ * SherpaOfflinePunctuationFreeText().
3568
+ */
3569
+ SHERPA_ONNX_API const char *SherpaOfflinePunctuationAddPunct(
3570
+ const SherpaOnnxOfflinePunctuation *punct, const char *text);
3571
+
3572
+ /**
3573
+ * @brief Free a string returned by SherpaOfflinePunctuationAddPunct().
3574
+ *
3575
+ * @param text A pointer returned by SherpaOfflinePunctuationAddPunct().
3576
+ */
3577
+ SHERPA_ONNX_API void SherpaOfflinePunctuationFreeText(const char *text);
3578
+
3579
+ /**
3580
+ * @brief Online punctuation model configuration.
3581
+ *
3582
+ * Example using `sherpa-onnx-online-punct-en-2024-08-06`:
3583
+ *
3584
+ * @code
3585
+ * SherpaOnnxOnlinePunctuationModelConfig model;
3586
+ * memset(&model, 0, sizeof(model));
3587
+ * model.cnn_bilstm =
3588
+ * "./sherpa-onnx-online-punct-en-2024-08-06/model.int8.onnx"; model.bpe_vocab =
3589
+ * "./sherpa-onnx-online-punct-en-2024-08-06/bpe.vocab"; model.num_threads = 1;
3590
+ * model.provider = "cpu";
3591
+ * @endcode
3592
+ */
3593
+ typedef struct SherpaOnnxOnlinePunctuationModelConfig {
3594
+ /** Online punctuation model file. */
3595
+ const char *cnn_bilstm;
3596
+ /** BPE vocabulary used by the model. */
3597
+ const char *bpe_vocab;
3598
+ /** Number of inference threads. */
3599
+ int32_t num_threads;
3600
+ /** Non-zero to print debug information. */
3601
+ int32_t debug;
3602
+ /** Execution provider such as `"cpu"`. */
3603
+ const char *provider;
3604
+ } SherpaOnnxOnlinePunctuationModelConfig;
3605
+
3606
+ /** @brief Configuration for online punctuation. */
3607
+ typedef struct SherpaOnnxOnlinePunctuationConfig {
3608
+ /** Model configuration. */
3609
+ SherpaOnnxOnlinePunctuationModelConfig model;
3610
+ } SherpaOnnxOnlinePunctuationConfig;
3611
+
3612
+ /** @brief Opaque online punctuation handle. */
3613
+ typedef struct SherpaOnnxOnlinePunctuation SherpaOnnxOnlinePunctuation;
3614
+
3615
+ /**
3616
+ * @brief Create an online punctuation processor.
3617
+ *
3618
+ * @param config Online punctuation configuration.
3619
+ * @return A newly allocated punctuation processor on success, or NULL on
3620
+ * error. Free it with SherpaOnnxDestroyOnlinePunctuation().
3621
+ */
3622
+ SHERPA_ONNX_API const SherpaOnnxOnlinePunctuation *
3623
+ SherpaOnnxCreateOnlinePunctuation(
3624
+ const SherpaOnnxOnlinePunctuationConfig *config);
3625
+
3626
+ /**
3627
+ * @brief Destroy an online punctuation processor.
3628
+ *
3629
+ * @param punctuation A pointer returned by SherpaOnnxCreateOnlinePunctuation().
3630
+ */
3631
+ SHERPA_ONNX_API void SherpaOnnxDestroyOnlinePunctuation(
3632
+ const SherpaOnnxOnlinePunctuation *punctuation);
3633
+
3634
+ /**
3635
+ * @brief Add punctuation to one text chunk using the online punctuation model.
3636
+ *
3637
+ * @param punctuation A pointer returned by SherpaOnnxCreateOnlinePunctuation().
3638
+ * @param text Input text chunk.
3639
+ * @return A newly allocated punctuated string. Free it with
3640
+ * SherpaOnnxOnlinePunctuationFreeText().
3641
+ *
3642
+ * @code
3643
+ * const char *out =
3644
+ * SherpaOnnxOnlinePunctuationAddPunct(punct,
3645
+ * "how are you i am fine thank you");
3646
+ * printf("%s\n", out);
3647
+ * SherpaOnnxOnlinePunctuationFreeText(out);
3648
+ * @endcode
3649
+ */
3650
+ SHERPA_ONNX_API const char *SherpaOnnxOnlinePunctuationAddPunct(
3651
+ const SherpaOnnxOnlinePunctuation *punctuation, const char *text);
3652
+
3653
+ /**
3654
+ * @brief Free a string returned by SherpaOnnxOnlinePunctuationAddPunct().
3655
+ *
3656
+ * @param text A pointer returned by SherpaOnnxOnlinePunctuationAddPunct().
3657
+ */
3658
+ SHERPA_ONNX_API void SherpaOnnxOnlinePunctuationFreeText(const char *text);
3659
+
3660
+ // For resampling
3661
+ /** @brief Opaque linear resampler handle. */
3662
+ typedef struct SherpaOnnxLinearResampler SherpaOnnxLinearResampler;
3663
+
3664
+ /**
3665
+ * @brief Create a linear resampler.
3666
+ *
3667
+ * A common choice is:
3668
+ *
3669
+ * @code
3670
+ * float min_freq = samp_rate_in_hz < samp_rate_out_hz ? samp_rate_in_hz
3671
+ * : samp_rate_out_hz;
3672
+ * float filter_cutoff_hz = 0.99f * 0.5f * min_freq;
3673
+ * int32_t num_zeros = 6;
3674
+ * @endcode
3675
+ *
3676
+ * @param samp_rate_in_hz Input sample rate in Hz.
3677
+ * @param samp_rate_out_hz Output sample rate in Hz.
3678
+ * @param filter_cutoff_hz Low-pass cutoff frequency in Hz.
3679
+ * @param num_zeros Low-pass filter width control parameter.
3680
+ * @return A newly allocated resampler. Free it with
3681
+ * SherpaOnnxDestroyLinearResampler().
3682
+ */
3683
+ SHERPA_ONNX_API const SherpaOnnxLinearResampler *
3684
+ SherpaOnnxCreateLinearResampler(int32_t samp_rate_in_hz,
3685
+ int32_t samp_rate_out_hz,
3686
+ float filter_cutoff_hz, int32_t num_zeros);
3687
+
3688
+ /**
3689
+ * @brief Destroy a linear resampler.
3690
+ *
3691
+ * @param p A pointer returned by SherpaOnnxCreateLinearResampler().
3692
+ */
3693
+ SHERPA_ONNX_API void SherpaOnnxDestroyLinearResampler(
3694
+ const SherpaOnnxLinearResampler *p);
3695
+
3696
+ /**
3697
+ * @brief Reset a linear resampler to its initial state.
3698
+ *
3699
+ * @param p A pointer returned by SherpaOnnxCreateLinearResampler().
3700
+ */
3701
+ SHERPA_ONNX_API void SherpaOnnxLinearResamplerReset(
3702
+ const SherpaOnnxLinearResampler *p);
3703
+
3704
+ /**
3705
+ * @brief Output chunk returned by SherpaOnnxLinearResamplerResample().
3706
+ *
3707
+ * Free this object with SherpaOnnxLinearResamplerResampleFree().
3708
+ */
3709
+ typedef struct SherpaOnnxResampleOut {
3710
+ /** Output samples. */
3711
+ const float *samples;
3712
+ /** Number of output samples. */
3713
+ int32_t n;
3714
+ } SherpaOnnxResampleOut;
3715
+
3716
+ /**
3717
+ * @brief Resample one chunk of input audio.
3718
+ *
3719
+ * Set @p flush to 1 for the final chunk so buffered samples are emitted.
3720
+ *
3721
+ * @param p A pointer returned by SherpaOnnxCreateLinearResampler().
3722
+ * @param input Input sample array.
3723
+ * @param input_dim Number of input samples.
3724
+ * @param flush 1 if this is the final chunk; otherwise 0.
3725
+ * @return A newly allocated output chunk. Free it with
3726
+ * SherpaOnnxLinearResamplerResampleFree().
3727
+ */
3728
+ SHERPA_ONNX_API const SherpaOnnxResampleOut *SherpaOnnxLinearResamplerResample(
3729
+ const SherpaOnnxLinearResampler *p, const float *input, int32_t input_dim,
3730
+ int32_t flush);
3731
+
3732
+ /**
3733
+ * @brief Destroy a resampler output chunk.
3734
+ *
3735
+ * @param p A pointer returned by SherpaOnnxLinearResamplerResample().
3736
+ */
3737
+ SHERPA_ONNX_API void SherpaOnnxLinearResamplerResampleFree(
3738
+ const SherpaOnnxResampleOut *p);
3739
+
3740
+ /**
3741
+ * @brief Return the resampler input sample rate.
3742
+ *
3743
+ * @param p A pointer returned by SherpaOnnxCreateLinearResampler().
3744
+ * @return Input sample rate in Hz.
3745
+ */
3746
+ SHERPA_ONNX_API int32_t SherpaOnnxLinearResamplerResampleGetInputSampleRate(
3747
+ const SherpaOnnxLinearResampler *p);
3748
+
3749
+ /**
3750
+ * @brief Return the resampler output sample rate.
3751
+ *
3752
+ * @param p A pointer returned by SherpaOnnxCreateLinearResampler().
3753
+ * @return Output sample rate in Hz.
3754
+ */
3755
+ SHERPA_ONNX_API int32_t SherpaOnnxLinearResamplerResampleGetOutputSampleRate(
3756
+ const SherpaOnnxLinearResampler *p);
3757
+
3758
+ // =========================================================================
3759
+ // For offline speaker diarization (i.e., non-streaming speaker diarization)
3760
+ // =========================================================================
3761
+ /** @brief Pyannote speaker-segmentation model configuration. */
3762
+ typedef struct SherpaOnnxOfflineSpeakerSegmentationPyannoteModelConfig {
3763
+ /** Segmentation model filename. */
3764
+ const char *model;
3765
+ } SherpaOnnxOfflineSpeakerSegmentationPyannoteModelConfig;
3766
+
3767
+ /**
3768
+ * @brief Segmentation model configuration for offline speaker diarization.
3769
+ *
3770
+ * Configure exactly one model family. If multiple model families are provided,
3771
+ * one is chosen and the choice is implementation-defined.
3772
+ */
3773
+ typedef struct SherpaOnnxOfflineSpeakerSegmentationModelConfig {
3774
+ /** Pyannote segmentation model configuration. */
3775
+ SherpaOnnxOfflineSpeakerSegmentationPyannoteModelConfig pyannote;
3776
+ /** Number of inference threads. */
3777
+ int32_t num_threads;
3778
+ /** Non-zero to print debug information. */
3779
+ int32_t debug;
3780
+ /** Execution provider such as `"cpu"`. */
3781
+ const char *provider;
3782
+ } SherpaOnnxOfflineSpeakerSegmentationModelConfig;
3783
+
3784
+ /**
3785
+ * @brief Fast clustering configuration.
3786
+ *
3787
+ * If @c num_clusters is greater than 0, @c threshold is ignored. When the
3788
+ * number of speakers is known in advance, setting @c num_clusters is strongly
3789
+ * recommended.
3790
+ */
3791
+ typedef struct SherpaOnnxFastClusteringConfig {
3792
+ /** Known number of speakers. If > 0, threshold-based clustering is bypassed.
3793
+ */
3794
+ int32_t num_clusters;
3795
+ /** Distance threshold used when the number of speakers is unknown. */
3796
+ float threshold;
3797
+ } SherpaOnnxFastClusteringConfig;
3798
+
3799
+ /**
3800
+ * @brief Configuration for offline speaker diarization.
3801
+ *
3802
+ * Example based on `offline-sepaker-diarization-c-api.c`:
3803
+ *
3804
+ * @code
3805
+ * SherpaOnnxOfflineSpeakerDiarizationConfig config;
3806
+ * memset(&config, 0, sizeof(config));
3807
+ * config.segmentation.pyannote.model =
3808
+ * "./sherpa-onnx-pyannote-segmentation-3-0/model.onnx";
3809
+ * config.embedding.model =
3810
+ * "./3dspeaker_speech_eres2net_base_sv_zh-cn_3dspeaker_16k.onnx";
3811
+ * config.clustering.num_clusters = 4;
3812
+ * @endcode
3813
+ */
3814
+ typedef struct SherpaOnnxOfflineSpeakerDiarizationConfig {
3815
+ /** Speaker segmentation model configuration. */
3816
+ SherpaOnnxOfflineSpeakerSegmentationModelConfig segmentation;
3817
+ /** Speaker embedding extractor configuration. */
3818
+ SherpaOnnxSpeakerEmbeddingExtractorConfig embedding;
3819
+ /** Clustering configuration. */
3820
+ SherpaOnnxFastClusteringConfig clustering;
3821
+ /** Segments shorter than this duration in seconds are discarded. */
3822
+ float min_duration_on;
3823
+ /** Small gaps shorter than this duration in seconds may be merged. */
3824
+ float min_duration_off;
3825
+ } SherpaOnnxOfflineSpeakerDiarizationConfig;
3826
+
3827
+ /** @brief Opaque offline speaker diarization handle. */
3828
+ typedef struct SherpaOnnxOfflineSpeakerDiarization
3829
+ SherpaOnnxOfflineSpeakerDiarization;
3830
+
3831
+ /**
3832
+ * @brief Create an offline speaker diarization pipeline.
3833
+ *
3834
+ * @param config Offline speaker diarization configuration.
3835
+ * @return A newly allocated diarizer on success, or NULL on error. Free it
3836
+ * with SherpaOnnxDestroyOfflineSpeakerDiarization().
3837
+ */
3838
+ SHERPA_ONNX_API const SherpaOnnxOfflineSpeakerDiarization *
3839
+ SherpaOnnxCreateOfflineSpeakerDiarization(
3840
+ const SherpaOnnxOfflineSpeakerDiarizationConfig *config);
3841
+
3842
+ /**
3843
+ * @brief Destroy an offline speaker diarizer.
3844
+ *
3845
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeakerDiarization().
3846
+ */
3847
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineSpeakerDiarization(
3848
+ const SherpaOnnxOfflineSpeakerDiarization *sd);
3849
+
3850
+ /**
3851
+ * @brief Return the expected input sample rate.
3852
+ *
3853
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeakerDiarization().
3854
+ * @return Required input sample rate in Hz.
3855
+ */
3856
+ SHERPA_ONNX_API int32_t SherpaOnnxOfflineSpeakerDiarizationGetSampleRate(
3857
+ const SherpaOnnxOfflineSpeakerDiarization *sd);
3858
+
3859
+ /**
3860
+ * @brief Update clustering-related settings of an existing diarizer.
3861
+ *
3862
+ * Only `config->clustering` is used. Other fields are ignored.
3863
+ *
3864
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeakerDiarization().
3865
+ * @param config Configuration whose `clustering` field will be applied.
3866
+ */
3867
+ SHERPA_ONNX_API void SherpaOnnxOfflineSpeakerDiarizationSetConfig(
3868
+ const SherpaOnnxOfflineSpeakerDiarization *sd,
3869
+ const SherpaOnnxOfflineSpeakerDiarizationConfig *config);
3870
+
3871
+ /** @brief Opaque offline speaker diarization result. */
3872
+ typedef struct SherpaOnnxOfflineSpeakerDiarizationResult
3873
+ SherpaOnnxOfflineSpeakerDiarizationResult;
3874
+
3875
+ /**
3876
+ * @brief One diarization segment.
3877
+ */
3878
+ typedef struct SherpaOnnxOfflineSpeakerDiarizationSegment {
3879
+ /** Segment start time in seconds. */
3880
+ float start;
3881
+ /** Segment end time in seconds. */
3882
+ float end;
3883
+ /** Speaker label, typically an integer cluster ID. */
3884
+ int32_t speaker;
3885
+ } SherpaOnnxOfflineSpeakerDiarizationSegment;
3886
+
3887
+ /**
3888
+ * @brief Return the number of speakers in a diarization result.
3889
+ *
3890
+ * @param r A pointer returned by one of the
3891
+ * SherpaOnnxOfflineSpeakerDiarizationProcess*() functions.
3892
+ * @return Number of speaker clusters.
3893
+ */
3894
+ SHERPA_ONNX_API int32_t SherpaOnnxOfflineSpeakerDiarizationResultGetNumSpeakers(
3895
+ const SherpaOnnxOfflineSpeakerDiarizationResult *r);
3896
+
3897
+ /**
3898
+ * @brief Return the number of diarization segments.
3899
+ *
3900
+ * @param r A pointer returned by one of the
3901
+ * SherpaOnnxOfflineSpeakerDiarizationProcess*() functions.
3902
+ * @return Number of segments.
3903
+ */
3904
+ SHERPA_ONNX_API int32_t SherpaOnnxOfflineSpeakerDiarizationResultGetNumSegments(
3905
+ const SherpaOnnxOfflineSpeakerDiarizationResult *r);
3906
+
3907
+ /**
3908
+ * @brief Return segments sorted by start time.
3909
+ *
3910
+ * The returned array contains exactly
3911
+ * SherpaOnnxOfflineSpeakerDiarizationResultGetNumSegments() entries.
3912
+ *
3913
+ * @param r A pointer returned by one of the
3914
+ * SherpaOnnxOfflineSpeakerDiarizationProcess*() functions.
3915
+ * @return A newly allocated segment array. Free it with
3916
+ * SherpaOnnxOfflineSpeakerDiarizationDestroySegment().
3917
+ */
3918
+ SHERPA_ONNX_API const SherpaOnnxOfflineSpeakerDiarizationSegment *
3919
+ SherpaOnnxOfflineSpeakerDiarizationResultSortByStartTime(
3920
+ const SherpaOnnxOfflineSpeakerDiarizationResult *r);
3921
+
3922
+ /**
3923
+ * @brief Destroy a segment array returned by
3924
+ * SherpaOnnxOfflineSpeakerDiarizationResultSortByStartTime().
3925
+ *
3926
+ * @param s A pointer returned by
3927
+ * SherpaOnnxOfflineSpeakerDiarizationResultSortByStartTime().
3928
+ */
3929
+ SHERPA_ONNX_API void SherpaOnnxOfflineSpeakerDiarizationDestroySegment(
3930
+ const SherpaOnnxOfflineSpeakerDiarizationSegment *s);
3931
+
3932
+ /**
3933
+ * @brief Progress callback for offline speaker diarization.
3934
+ *
3935
+ * The current implementation reports progress but ignores the callback's
3936
+ * return value.
3937
+ */
3938
+ typedef int32_t (*SherpaOnnxOfflineSpeakerDiarizationProgressCallback)(
3939
+ int32_t num_processed_chunks, int32_t num_total_chunks, void *arg);
3940
+
3941
+ /**
3942
+ * @brief Same as SherpaOnnxOfflineSpeakerDiarizationProgressCallback but
3943
+ * without a user pointer.
3944
+ */
3945
+ typedef int32_t (*SherpaOnnxOfflineSpeakerDiarizationProgressCallbackNoArg)(
3946
+ int32_t num_processed_chunks, int32_t num_total_chunks);
3947
+
3948
+ /**
3949
+ * @brief Run offline speaker diarization.
3950
+ *
3951
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeakerDiarization().
3952
+ * @param samples Input mono PCM samples normalized to [-1, 1].
3953
+ * @param n Number of input samples.
3954
+ * @return A newly allocated diarization result. Free it with
3955
+ * SherpaOnnxOfflineSpeakerDiarizationDestroyResult().
3956
+ */
3957
+ SHERPA_ONNX_API const SherpaOnnxOfflineSpeakerDiarizationResult *
3958
+ SherpaOnnxOfflineSpeakerDiarizationProcess(
3959
+ const SherpaOnnxOfflineSpeakerDiarization *sd, const float *samples,
3960
+ int32_t n);
3961
+
3962
+ /**
3963
+ * @brief Run offline speaker diarization with a progress callback.
3964
+ *
3965
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeakerDiarization().
3966
+ * @param samples Input mono PCM samples normalized to [-1, 1].
3967
+ * @param n Number of input samples.
3968
+ * @param callback Progress callback.
3969
+ * @param arg User pointer forwarded to @p callback.
3970
+ * @return A newly allocated diarization result. Free it with
3971
+ * SherpaOnnxOfflineSpeakerDiarizationDestroyResult().
3972
+ *
3973
+ * @code
3974
+ * static int32_t ProgressCallback(int32_t done, int32_t total, void *arg) {
3975
+ * fprintf(stderr, "progress %.2f%%\n", 100.0f * done / total);
3976
+ * return 0;
3977
+ * }
3978
+ * @endcode
3979
+ */
3980
+ SHERPA_ONNX_API const SherpaOnnxOfflineSpeakerDiarizationResult *
3981
+ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallback(
3982
+ const SherpaOnnxOfflineSpeakerDiarization *sd, const float *samples,
3983
+ int32_t n, SherpaOnnxOfflineSpeakerDiarizationProgressCallback callback,
3984
+ void *arg);
3985
+
3986
+ /**
3987
+ * @brief Run offline speaker diarization with a progress callback that has no
3988
+ * user pointer.
3989
+ *
3990
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeakerDiarization().
3991
+ * @param samples Input mono PCM samples normalized to [-1, 1].
3992
+ * @param n Number of input samples.
3993
+ * @param callback Progress callback.
3994
+ * @return A newly allocated diarization result. Free it with
3995
+ * SherpaOnnxOfflineSpeakerDiarizationDestroyResult().
3996
+ */
3997
+ SHERPA_ONNX_API const SherpaOnnxOfflineSpeakerDiarizationResult *
3998
+ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg(
3999
+ const SherpaOnnxOfflineSpeakerDiarization *sd, const float *samples,
4000
+ int32_t n,
4001
+ SherpaOnnxOfflineSpeakerDiarizationProgressCallbackNoArg callback);
4002
+
4003
+ /**
4004
+ * @brief Destroy a diarization result.
4005
+ *
4006
+ * @param r A pointer returned by one of the
4007
+ * SherpaOnnxOfflineSpeakerDiarizationProcess*() functions.
4008
+ */
4009
+ SHERPA_ONNX_API void SherpaOnnxOfflineSpeakerDiarizationDestroyResult(
4010
+ const SherpaOnnxOfflineSpeakerDiarizationResult *r);
4011
+
4012
+ // =========================================================================
4013
+ // For offline speech enhancement
4014
+ // =========================================================================
4015
+ /** @brief GTCRN offline denoiser model configuration. */
4016
+ typedef struct SherpaOnnxOfflineSpeechDenoiserGtcrnModelConfig {
4017
+ /** Model filename. */
4018
+ const char *model;
4019
+ } SherpaOnnxOfflineSpeechDenoiserGtcrnModelConfig;
4020
+
4021
+ /** @brief DPDFNet offline denoiser model configuration. */
4022
+ typedef struct SherpaOnnxOfflineSpeechDenoiserDpdfNetModelConfig {
4023
+ /** Model filename. */
4024
+ const char *model;
4025
+ } SherpaOnnxOfflineSpeechDenoiserDpdfNetModelConfig;
4026
+
4027
+ /**
4028
+ * @brief Speech denoiser model configuration shared by offline and online APIs.
4029
+ *
4030
+ * Configure exactly one model family. If multiple model families are provided,
4031
+ * one is chosen and the choice is implementation-defined.
4032
+ */
4033
+ typedef struct SherpaOnnxOfflineSpeechDenoiserModelConfig {
4034
+ /** GTCRN model configuration. */
4035
+ SherpaOnnxOfflineSpeechDenoiserGtcrnModelConfig gtcrn;
4036
+ /** Number of inference threads. */
4037
+ int32_t num_threads;
4038
+ /** Non-zero to print debug information. */
4039
+ int32_t debug;
4040
+ /** Execution provider such as `"cpu"`. */
4041
+ const char *provider;
4042
+ /** DPDFNet model configuration. */
4043
+ SherpaOnnxOfflineSpeechDenoiserDpdfNetModelConfig dpdfnet;
4044
+ } SherpaOnnxOfflineSpeechDenoiserModelConfig;
4045
+
4046
+ /** @brief Configuration for offline speech denoising. */
4047
+ typedef struct SherpaOnnxOfflineSpeechDenoiserConfig {
4048
+ /** Model configuration. */
4049
+ SherpaOnnxOfflineSpeechDenoiserModelConfig model;
4050
+ } SherpaOnnxOfflineSpeechDenoiserConfig;
4051
+
4052
+ /** @brief Opaque offline speech denoiser handle. */
4053
+ typedef struct SherpaOnnxOfflineSpeechDenoiser SherpaOnnxOfflineSpeechDenoiser;
4054
+
4055
+ /**
4056
+ * @brief Create an offline speech denoiser.
4057
+ *
4058
+ * Example using `gtcrn_simple.onnx`:
4059
+ *
4060
+ * @code
4061
+ * SherpaOnnxOfflineSpeechDenoiserConfig config;
4062
+ * memset(&config, 0, sizeof(config));
4063
+ * config.model.gtcrn.model = "./gtcrn_simple.onnx";
4064
+ * @endcode
4065
+ *
4066
+ * @param config Offline denoiser configuration.
4067
+ * @return A newly allocated denoiser on success, or NULL on error. Free it
4068
+ * with SherpaOnnxDestroyOfflineSpeechDenoiser().
4069
+ */
4070
+ SHERPA_ONNX_API const SherpaOnnxOfflineSpeechDenoiser *
4071
+ SherpaOnnxCreateOfflineSpeechDenoiser(
4072
+ const SherpaOnnxOfflineSpeechDenoiserConfig *config);
4073
+
4074
+ /**
4075
+ * @brief Destroy an offline speech denoiser.
4076
+ *
4077
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeechDenoiser().
4078
+ */
4079
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineSpeechDenoiser(
4080
+ const SherpaOnnxOfflineSpeechDenoiser *sd);
4081
+
4082
+ /**
4083
+ * @brief Return the expected sample rate for the denoiser.
4084
+ *
4085
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeechDenoiser().
4086
+ * @return Required input sample rate in Hz.
4087
+ */
4088
+ SHERPA_ONNX_API int32_t SherpaOnnxOfflineSpeechDenoiserGetSampleRate(
4089
+ const SherpaOnnxOfflineSpeechDenoiser *sd);
4090
+
4091
+ /**
4092
+ * @brief Denoised audio returned by offline or online speech enhancement APIs.
4093
+ *
4094
+ * Free this object with SherpaOnnxDestroyDenoisedAudio().
4095
+ */
4096
+ typedef struct SherpaOnnxDenoisedAudio {
4097
+ /** Output samples in the range [-1, 1]. */
4098
+ const float *samples;
4099
+ /** Number of output samples. */
4100
+ int32_t n;
4101
+ /** Output sample rate in Hz. */
4102
+ int32_t sample_rate;
4103
+ } SherpaOnnxDenoisedAudio;
4104
+
4105
+ /**
4106
+ * @brief Run offline speech denoising on a complete waveform.
4107
+ *
4108
+ * @param sd A pointer returned by SherpaOnnxCreateOfflineSpeechDenoiser().
4109
+ * @param samples Input mono PCM samples normalized to [-1, 1].
4110
+ * @param n Number of input samples.
4111
+ * @param sample_rate Input sample rate in Hz.
4112
+ * @return A newly allocated denoised waveform. Free it with
4113
+ * SherpaOnnxDestroyDenoisedAudio().
4114
+ *
4115
+ * @code
4116
+ * const SherpaOnnxDenoisedAudio *denoised =
4117
+ * SherpaOnnxOfflineSpeechDenoiserRun(sd, wave->samples, wave->num_samples,
4118
+ * wave->sample_rate);
4119
+ * SherpaOnnxWriteWave(denoised->samples, denoised->n, denoised->sample_rate,
4120
+ * "./enhanced.wav");
4121
+ * SherpaOnnxDestroyDenoisedAudio(denoised);
4122
+ * @endcode
4123
+ */
4124
+ SHERPA_ONNX_API const SherpaOnnxDenoisedAudio *
4125
+ SherpaOnnxOfflineSpeechDenoiserRun(const SherpaOnnxOfflineSpeechDenoiser *sd,
4126
+ const float *samples, int32_t n,
4127
+ int32_t sample_rate);
4128
+
4129
+ /**
4130
+ * @brief Destroy denoised audio returned by a speech enhancement API.
4131
+ *
4132
+ * @param p A pointer returned by SherpaOnnxOfflineSpeechDenoiserRun(),
4133
+ * SherpaOnnxOnlineSpeechDenoiserRun(), or
4134
+ * SherpaOnnxOnlineSpeechDenoiserFlush().
4135
+ */
4136
+ SHERPA_ONNX_API void SherpaOnnxDestroyDenoisedAudio(
4137
+ const SherpaOnnxDenoisedAudio *p);
4138
+
4139
+ // =========================================================================
4140
+ // For streaming speech enhancement
4141
+ // =========================================================================
4142
+ /** @brief Configuration for streaming speech denoising. */
4143
+ typedef struct SherpaOnnxOnlineSpeechDenoiserConfig {
4144
+ /** Model configuration. */
4145
+ SherpaOnnxOfflineSpeechDenoiserModelConfig model;
4146
+ } SherpaOnnxOnlineSpeechDenoiserConfig;
4147
+
4148
+ /** @brief Opaque online speech denoiser handle. */
4149
+ typedef struct SherpaOnnxOnlineSpeechDenoiser SherpaOnnxOnlineSpeechDenoiser;
4150
+
4151
+ /**
4152
+ * @brief Create an online speech denoiser.
4153
+ *
4154
+ * @param config Online denoiser configuration.
4155
+ * @return A newly allocated denoiser on success, or NULL on error. Free it
4156
+ * with SherpaOnnxDestroyOnlineSpeechDenoiser().
4157
+ */
4158
+ SHERPA_ONNX_API const SherpaOnnxOnlineSpeechDenoiser *
4159
+ SherpaOnnxCreateOnlineSpeechDenoiser(
4160
+ const SherpaOnnxOnlineSpeechDenoiserConfig *config);
4161
+
4162
+ /**
4163
+ * @brief Destroy an online speech denoiser.
4164
+ *
4165
+ * @param sd A pointer returned by SherpaOnnxCreateOnlineSpeechDenoiser().
4166
+ */
4167
+ SHERPA_ONNX_API void SherpaOnnxDestroyOnlineSpeechDenoiser(
4168
+ const SherpaOnnxOnlineSpeechDenoiser *sd);
4169
+
4170
+ /**
4171
+ * @brief Return the expected input sample rate for the online denoiser.
4172
+ *
4173
+ * @param sd A pointer returned by SherpaOnnxCreateOnlineSpeechDenoiser().
4174
+ * @return Required input sample rate in Hz.
4175
+ */
4176
+ SHERPA_ONNX_API int32_t SherpaOnnxOnlineSpeechDenoiserGetSampleRate(
4177
+ const SherpaOnnxOnlineSpeechDenoiser *sd);
4178
+
4179
+ /**
4180
+ * @brief Return the recommended chunk size in samples for streaming input.
4181
+ *
4182
+ * Example programs feed audio to the online denoiser in this chunk size.
4183
+ *
4184
+ * @param sd A pointer returned by SherpaOnnxCreateOnlineSpeechDenoiser().
4185
+ * @return Frame shift in samples.
4186
+ */
4187
+ SHERPA_ONNX_API int32_t SherpaOnnxOnlineSpeechDenoiserGetFrameShiftInSamples(
4188
+ const SherpaOnnxOnlineSpeechDenoiser *sd);
4189
+
4190
+ /**
4191
+ * @brief Process one chunk of streaming audio.
4192
+ *
4193
+ * This function is not thread-safe. It may return NULL when not enough input
4194
+ * has been accumulated to produce denoised output yet.
4195
+ *
4196
+ * @param sd A pointer returned by SherpaOnnxCreateOnlineSpeechDenoiser().
4197
+ * @param samples Input chunk normalized to [-1, 1].
4198
+ * @param n Number of input samples.
4199
+ * @param sample_rate Input sample rate in Hz.
4200
+ * @return A newly allocated denoised chunk, or NULL if no output is available
4201
+ * yet. Free non-NULL results with SherpaOnnxDestroyDenoisedAudio().
4202
+ */
4203
+ SHERPA_ONNX_API const SherpaOnnxDenoisedAudio *
4204
+ SherpaOnnxOnlineSpeechDenoiserRun(const SherpaOnnxOnlineSpeechDenoiser *sd,
4205
+ const float *samples, int32_t n,
4206
+ int32_t sample_rate);
4207
+
4208
+ /**
4209
+ * @brief Flush buffered samples and reset the online denoiser.
4210
+ *
4211
+ * This also resets the denoiser so it can be reused for a new utterance.
4212
+ *
4213
+ * @param sd A pointer returned by SherpaOnnxCreateOnlineSpeechDenoiser().
4214
+ * @return A newly allocated denoised chunk, or NULL if no buffered output
4215
+ * remains. Free non-NULL results with SherpaOnnxDestroyDenoisedAudio().
4216
+ */
4217
+ SHERPA_ONNX_API const SherpaOnnxDenoisedAudio *
4218
+ SherpaOnnxOnlineSpeechDenoiserFlush(const SherpaOnnxOnlineSpeechDenoiser *sd);
4219
+
4220
+ /**
4221
+ * @brief Reset an online denoiser so it can process a new stream.
4222
+ *
4223
+ * @param sd A pointer returned by SherpaOnnxCreateOnlineSpeechDenoiser().
4224
+ */
4225
+ SHERPA_ONNX_API void SherpaOnnxOnlineSpeechDenoiserReset(
4226
+ const SherpaOnnxOnlineSpeechDenoiser *sd);
4227
+
4228
+ // =========================================================================
4229
+ // Source separation
4230
+ // =========================================================================
4231
+
4232
+ /** @brief Spleeter source-separation model configuration. */
4233
+ typedef struct SherpaOnnxOfflineSourceSeparationSpleeterModelConfig {
4234
+ /** Path to the vocals ONNX model. */
4235
+ const char *vocals;
4236
+ /** Path to the accompaniment ONNX model. */
4237
+ const char *accompaniment;
4238
+ } SherpaOnnxOfflineSourceSeparationSpleeterModelConfig;
4239
+
4240
+ /** @brief UVR (MDX-Net) source-separation model configuration. */
4241
+ typedef struct SherpaOnnxOfflineSourceSeparationUvrModelConfig {
4242
+ /** Path to the UVR ONNX model. */
4243
+ const char *model;
4244
+ } SherpaOnnxOfflineSourceSeparationUvrModelConfig;
4245
+
4246
+ /** @brief Source-separation model configuration. */
4247
+ typedef struct SherpaOnnxOfflineSourceSeparationModelConfig {
4248
+ SherpaOnnxOfflineSourceSeparationSpleeterModelConfig spleeter;
4249
+ SherpaOnnxOfflineSourceSeparationUvrModelConfig uvr;
4250
+ int32_t num_threads;
4251
+ int32_t debug;
4252
+ const char *provider;
4253
+ } SherpaOnnxOfflineSourceSeparationModelConfig;
4254
+
4255
+ /** @brief Top-level source-separation configuration. */
4256
+ typedef struct SherpaOnnxOfflineSourceSeparationConfig {
4257
+ SherpaOnnxOfflineSourceSeparationModelConfig model;
4258
+ } SherpaOnnxOfflineSourceSeparationConfig;
4259
+
4260
+ /** @brief Opaque source-separation engine handle. */
4261
+ typedef struct SherpaOnnxOfflineSourceSeparation
4262
+ SherpaOnnxOfflineSourceSeparation;
4263
+
4264
+ /**
4265
+ * @brief Create a source-separation engine.
4266
+ *
4267
+ * @param config Source-separation configuration.
4268
+ * @return A newly allocated engine on success, or NULL on error. Free it
4269
+ * with SherpaOnnxDestroyOfflineSourceSeparation().
4270
+ */
4271
+ SHERPA_ONNX_API const SherpaOnnxOfflineSourceSeparation *
4272
+ SherpaOnnxCreateOfflineSourceSeparation(
4273
+ const SherpaOnnxOfflineSourceSeparationConfig *config);
4274
+
4275
+ /**
4276
+ * @brief Destroy a source-separation engine.
4277
+ *
4278
+ * @param ss A pointer returned by SherpaOnnxCreateOfflineSourceSeparation().
4279
+ */
4280
+ SHERPA_ONNX_API void SherpaOnnxDestroyOfflineSourceSeparation(
4281
+ const SherpaOnnxOfflineSourceSeparation *ss);
4282
+
4283
+ /**
4284
+ * @brief Return the output sample rate of the source-separation engine.
4285
+ *
4286
+ * @param ss A pointer returned by SherpaOnnxCreateOfflineSourceSeparation().
4287
+ * @return Output sample rate in Hz.
4288
+ */
4289
+ SHERPA_ONNX_API int32_t SherpaOnnxOfflineSourceSeparationGetOutputSampleRate(
4290
+ const SherpaOnnxOfflineSourceSeparation *ss);
4291
+
4292
+ /**
4293
+ * @brief Return the number of stems produced by the engine.
4294
+ *
4295
+ * For Spleeter 2-stems this returns 2 (vocals + accompaniment).
4296
+ *
4297
+ * @param ss A pointer returned by SherpaOnnxCreateOfflineSourceSeparation().
4298
+ * @return Number of output stems.
4299
+ */
4300
+ SHERPA_ONNX_API int32_t SherpaOnnxOfflineSourceSeparationGetNumberOfStems(
4301
+ const SherpaOnnxOfflineSourceSeparation *ss);
4302
+
4303
+ /** @brief A single stem (one output track) with one or more channels. */
4304
+ typedef struct SherpaOnnxSourceSeparationStem {
4305
+ /** samples[c] points to the heap-allocated sample array for channel c. */
4306
+ float **samples;
4307
+ /** Number of channels in this stem. */
4308
+ int32_t num_channels;
4309
+ /** Number of samples per channel. */
4310
+ int32_t n;
4311
+ } SherpaOnnxSourceSeparationStem;
4312
+
4313
+ /** @brief Output of a source-separation run. */
4314
+ typedef struct SherpaOnnxSourceSeparationOutput {
4315
+ /** Heap-allocated array of stems (length num_stems). */
4316
+ const SherpaOnnxSourceSeparationStem *stems;
4317
+ /** Number of stems. */
4318
+ int32_t num_stems;
4319
+ /** Sample rate of every stem in Hz. */
4320
+ int32_t sample_rate;
4321
+ } SherpaOnnxSourceSeparationOutput;
4322
+
4323
+ /**
4324
+ * @brief Run source separation on multi-channel audio.
4325
+ *
4326
+ * All input channels must have the same number of samples.
4327
+ *
4328
+ * @param ss A pointer returned by
4329
+ * SherpaOnnxCreateOfflineSourceSeparation().
4330
+ * @param samples samples[c] is a float array for channel c, values in
4331
+ * [-1, 1].
4332
+ * @param num_channels Number of input channels.
4333
+ * @param num_samples Number of samples per channel (all channels must have
4334
+ * the same length).
4335
+ * @param sample_rate Input sample rate in Hz.
4336
+ * @return A newly allocated output on success, or NULL on error. Free it
4337
+ * with SherpaOnnxDestroySourceSeparationOutput().
4338
+ */
4339
+ SHERPA_ONNX_API const SherpaOnnxSourceSeparationOutput *
4340
+ SherpaOnnxOfflineSourceSeparationProcess(
4341
+ const SherpaOnnxOfflineSourceSeparation *ss, const float *const *samples,
4342
+ int32_t num_channels, int32_t num_samples, int32_t sample_rate);
4343
+
4344
+ /**
4345
+ * @brief Destroy the output of a source-separation run.
4346
+ *
4347
+ * @param p A pointer returned by SherpaOnnxOfflineSourceSeparationProcess().
4348
+ */
4349
+ SHERPA_ONNX_API void SherpaOnnxDestroySourceSeparationOutput(
4350
+ const SherpaOnnxSourceSeparationOutput *p);
4351
+
4352
+ #ifdef __OHOS__
4353
+
4354
+ /**
4355
+ * @brief HarmonyOS native resource manager type.
4356
+ *
4357
+ * Pass the resource manager provided by the HarmonyOS application runtime when
4358
+ * using the `*OHOS()` constructors below.
4359
+ */
4360
+ typedef struct NativeResourceManager NativeResourceManager;
4361
+
4362
+ /**
4363
+ * @brief Create an offline speech denoiser on HarmonyOS.
4364
+ *
4365
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateOfflineSpeechDenoiser().
4366
+ *
4367
+ * @param config Offline denoiser configuration.
4368
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4369
+ * @return A newly allocated denoiser, or NULL on error. Free it with
4370
+ * SherpaOnnxDestroyOfflineSpeechDenoiser().
4371
+ */
4372
+ SHERPA_ONNX_API const SherpaOnnxOfflineSpeechDenoiser *
4373
+ SherpaOnnxCreateOfflineSpeechDenoiserOHOS(
4374
+ const SherpaOnnxOfflineSpeechDenoiserConfig *config,
4375
+ NativeResourceManager *mgr);
4376
+
4377
+ /**
4378
+ * @brief Create an online speech denoiser on HarmonyOS.
4379
+ *
4380
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateOnlineSpeechDenoiser().
4381
+ *
4382
+ * @param config Online denoiser configuration.
4383
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4384
+ * @return A newly allocated denoiser, or NULL on error. Free it with
4385
+ * SherpaOnnxDestroyOnlineSpeechDenoiser().
4386
+ */
4387
+ SHERPA_ONNX_API const SherpaOnnxOnlineSpeechDenoiser *
4388
+ SherpaOnnxCreateOnlineSpeechDenoiserOHOS(
4389
+ const SherpaOnnxOnlineSpeechDenoiserConfig *config,
4390
+ NativeResourceManager *mgr);
4391
+
4392
+ /**
4393
+ * @brief Create an online recognizer on HarmonyOS.
4394
+ *
4395
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateOnlineRecognizer().
4396
+ *
4397
+ * @param config Recognizer configuration.
4398
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4399
+ * @return A newly allocated recognizer, or NULL on error. Free it with
4400
+ * SherpaOnnxDestroyOnlineRecognizer().
4401
+ */
4402
+ SHERPA_ONNX_API const SherpaOnnxOnlineRecognizer *
4403
+ SherpaOnnxCreateOnlineRecognizerOHOS(
4404
+ const SherpaOnnxOnlineRecognizerConfig *config, NativeResourceManager *mgr);
4405
+
4406
+ /**
4407
+ * @brief Create an offline recognizer on HarmonyOS.
4408
+ *
4409
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateOfflineRecognizer().
4410
+ *
4411
+ * @param config Recognizer configuration.
4412
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4413
+ * @return A newly allocated recognizer, or NULL on error. Free it with
4414
+ * SherpaOnnxDestroyOfflineRecognizer().
4415
+ */
4416
+ SHERPA_ONNX_API const SherpaOnnxOfflineRecognizer *
4417
+ SherpaOnnxCreateOfflineRecognizerOHOS(
4418
+ const SherpaOnnxOfflineRecognizerConfig *config,
4419
+ NativeResourceManager *mgr);
4420
+
4421
+ /**
4422
+ * @brief Create a voice activity detector on HarmonyOS.
4423
+ *
4424
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateVoiceActivityDetector().
4425
+ *
4426
+ * @param config VAD model configuration.
4427
+ * @param buffer_size_in_seconds Internal buffer duration in seconds.
4428
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4429
+ * @return A newly allocated VAD instance, or NULL on error. Free it with
4430
+ * SherpaOnnxDestroyVoiceActivityDetector().
4431
+ */
4432
+ SHERPA_ONNX_API const SherpaOnnxVoiceActivityDetector *
4433
+ SherpaOnnxCreateVoiceActivityDetectorOHOS(
4434
+ const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
4435
+ NativeResourceManager *mgr);
4436
+
4437
+ /**
4438
+ * @brief Create an offline TTS engine on HarmonyOS.
4439
+ *
4440
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateOfflineTts().
4441
+ *
4442
+ * @param config Offline TTS configuration.
4443
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4444
+ * @return A newly allocated TTS engine, or NULL on error. Free it with
4445
+ * SherpaOnnxDestroyOfflineTts().
4446
+ */
4447
+ SHERPA_ONNX_API const SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTtsOHOS(
4448
+ const SherpaOnnxOfflineTtsConfig *config, NativeResourceManager *mgr);
4449
+
4450
+ /**
4451
+ * @brief Create an offline punctuation processor on HarmonyOS.
4452
+ *
4453
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateOfflinePunctuation().
4454
+ *
4455
+ * @param config Offline punctuation configuration.
4456
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4457
+ * @return A newly allocated punctuation processor, or NULL on error. Free it
4458
+ * with SherpaOnnxDestroyOfflinePunctuation().
4459
+ */
4460
+ SHERPA_ONNX_API const SherpaOnnxOfflinePunctuation *
4461
+ SherpaOnnxCreateOfflinePunctuationOHOS(
4462
+ const SherpaOnnxOfflinePunctuationConfig *config,
4463
+ NativeResourceManager *mgr);
4464
+
4465
+ /**
4466
+ * @brief Create an online punctuation processor on HarmonyOS.
4467
+ *
4468
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateOnlinePunctuation().
4469
+ *
4470
+ * @param config Online punctuation configuration.
4471
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4472
+ * @return A newly allocated punctuation processor, or NULL on error. Free it
4473
+ * with SherpaOnnxDestroyOnlinePunctuation().
4474
+ */
4475
+ SHERPA_ONNX_API const SherpaOnnxOnlinePunctuation *
4476
+ SherpaOnnxCreateOnlinePunctuationOHOS(
4477
+ const SherpaOnnxOnlinePunctuationConfig *config,
4478
+ NativeResourceManager *mgr);
4479
+
4480
+ /**
4481
+ * @brief Create a speaker embedding extractor on HarmonyOS.
4482
+ *
4483
+ * This is the HarmonyOS counterpart of
4484
+ * SherpaOnnxCreateSpeakerEmbeddingExtractor().
4485
+ *
4486
+ * @param config Speaker embedding extractor configuration.
4487
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4488
+ * @return A newly allocated extractor, or NULL on error. Free it with
4489
+ * SherpaOnnxDestroySpeakerEmbeddingExtractor().
4490
+ */
4491
+ SHERPA_ONNX_API const SherpaOnnxSpeakerEmbeddingExtractor *
4492
+ SherpaOnnxCreateSpeakerEmbeddingExtractorOHOS(
4493
+ const SherpaOnnxSpeakerEmbeddingExtractorConfig *config,
4494
+ NativeResourceManager *mgr);
4495
+
4496
+ /**
4497
+ * @brief Create a keyword spotter on HarmonyOS.
4498
+ *
4499
+ * This is the HarmonyOS counterpart of SherpaOnnxCreateKeywordSpotter().
4500
+ *
4501
+ * @param config Keyword spotter configuration.
4502
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4503
+ * @return A newly allocated keyword spotter, or NULL on error. Free it with
4504
+ * SherpaOnnxDestroyKeywordSpotter().
4505
+ */
4506
+ SHERPA_ONNX_API const SherpaOnnxKeywordSpotter *
4507
+ SherpaOnnxCreateKeywordSpotterOHOS(const SherpaOnnxKeywordSpotterConfig *config,
4508
+ NativeResourceManager *mgr);
4509
+
4510
+ /**
4511
+ * @brief Create an offline speaker diarizer on HarmonyOS.
4512
+ *
4513
+ * This is the HarmonyOS counterpart of
4514
+ * SherpaOnnxCreateOfflineSpeakerDiarization().
4515
+ *
4516
+ * @param config Offline speaker diarization configuration.
4517
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4518
+ * @return A newly allocated diarizer, or NULL on error. Free it with
4519
+ * SherpaOnnxDestroyOfflineSpeakerDiarization().
4520
+ */
4521
+ SHERPA_ONNX_API const SherpaOnnxOfflineSpeakerDiarization *
4522
+ SherpaOnnxCreateOfflineSpeakerDiarizationOHOS(
4523
+ const SherpaOnnxOfflineSpeakerDiarizationConfig *config,
4524
+ NativeResourceManager *mgr);
4525
+
4526
+ /**
4527
+ * @brief Create a source separation engine on HarmonyOS.
4528
+ *
4529
+ * This is the HarmonyOS counterpart of
4530
+ * SherpaOnnxCreateOfflineSourceSeparation().
4531
+ *
4532
+ * @param config Source separation configuration.
4533
+ * @param mgr HarmonyOS resource manager used to resolve bundled assets.
4534
+ * @return A newly allocated source separation engine, or NULL on error. Free it
4535
+ * with SherpaOnnxDestroyOfflineSourceSeparation().
4536
+ */
4537
+ SHERPA_ONNX_API const SherpaOnnxOfflineSourceSeparation *
4538
+ SherpaOnnxCreateOfflineSourceSeparationOHOS(
4539
+ const SherpaOnnxOfflineSourceSeparationConfig *config,
4540
+ NativeResourceManager *mgr);
4541
+ #endif
4542
+
4543
+ #if defined(__GNUC__)
4544
+ #pragma GCC diagnostic pop
4545
+ #endif
4546
+
4547
+ #ifdef __cplusplus
4548
+ } /* extern "C" */
4549
+ #endif
4550
+
4551
+ #endif // SHERPA_ONNX_C_API_C_API_H_