react-native-sherpa-onnx 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +232 -236
- package/SherpaOnnx.podspec +68 -64
- package/android/build.gradle +182 -192
- package/android/codegen.gradle +57 -0
- package/android/prebuilt-download.gradle +428 -0
- package/android/prebuilt-versions.gradle +43 -0
- package/android/proguard-rules.pro +10 -0
- package/android/src/main/assets/testModels/add_mul_add.onnx +28 -0
- package/android/src/main/assets/testModels/nnapi_internal_uint8_support.onnx +0 -0
- package/android/src/main/assets/testModels/qnn_multi_ctx_embed.onnx +0 -0
- package/android/src/main/cpp/CMakeLists.txt +166 -129
- package/android/src/main/cpp/CMakePresets.json +54 -0
- package/android/src/main/cpp/crypto/sha256.cpp +174 -0
- package/android/src/main/cpp/crypto/sha256.h +16 -0
- package/android/src/main/cpp/jni/archive/sherpa-onnx-archive-helper.cpp +404 -0
- package/android/src/main/cpp/jni/archive/sherpa-onnx-archive-helper.h +56 -0
- package/android/src/main/cpp/jni/archive/sherpa-onnx-archive-jni.cpp +181 -0
- package/android/src/main/cpp/jni/audio/sherpa-onnx-audio-convert-jni.cpp +888 -0
- package/{ios → android/src/main/cpp/jni/model_detect}/sherpa-onnx-common.h +18 -18
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-detect-jni-common.cpp +86 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-detect-jni-common.h +20 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-helper.cpp +423 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-helper.h +55 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-stt.cpp +399 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-tts.cpp +238 -0
- package/{ios → android/src/main/cpp/jni/model_detect}/sherpa-onnx-model-detect.h +122 -89
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-stt-wrapper.cpp +99 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-stt-wrapper.h +16 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-tts-wrapper.cpp +78 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-tts-wrapper.h +16 -0
- package/android/src/main/cpp/jni/module/sherpa-onnx-module-jni.cpp +190 -0
- package/android/src/main/cpp/jni/tts/sherpa-onnx-tts-zipvoice-jni.cpp +301 -0
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxArchiveHelper.kt +94 -0
- package/android/src/main/java/com/sherpaonnx/{SherpaOnnxCoreHelper.kt → SherpaOnnxAssetHelper.kt} +350 -236
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxModule.kt +791 -483
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxSttHelper.kt +699 -109
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxTtsHelper.kt +1123 -668
- package/android/src/main/java/com/sherpaonnx/ZipvoiceTtsWrapper.kt +187 -0
- package/ios/SherpaOnnx+Assets.h +11 -0
- package/ios/SherpaOnnx+Assets.mm +325 -0
- package/ios/SherpaOnnx+STT.mm +455 -118
- package/ios/SherpaOnnx+TTS.mm +1101 -712
- package/ios/SherpaOnnx.h +17 -6
- package/ios/SherpaOnnx.mm +206 -311
- package/ios/SherpaOnnx.xcconfig +19 -19
- package/ios/SherpaOnnxCoreMLHelper.swift +24 -0
- package/ios/archive/sherpa-onnx-archive-helper.h +21 -0
- package/ios/archive/sherpa-onnx-archive-helper.mm +296 -0
- package/ios/libarchive_darwin_config.h +153 -0
- package/{android/src/main/cpp/jni → ios/model_detect}/sherpa-onnx-common.h +18 -18
- package/ios/model_detect/sherpa-onnx-model-detect-helper.h +49 -0
- package/ios/model_detect/sherpa-onnx-model-detect-helper.mm +210 -0
- package/ios/model_detect/sherpa-onnx-model-detect-stt.mm +344 -0
- package/ios/model_detect/sherpa-onnx-model-detect-tts.mm +201 -0
- package/{android/src/main/cpp/jni → ios/model_detect}/sherpa-onnx-model-detect.h +117 -89
- package/ios/scripts/patch-libarchive-includes.sh +61 -0
- package/ios/scripts/setup-ios-libarchive.sh +98 -0
- package/ios/stt/sherpa-onnx-stt-wrapper.h +129 -0
- package/ios/stt/sherpa-onnx-stt-wrapper.mm +523 -0
- package/ios/{sherpa-onnx-tts-wrapper.h → tts/sherpa-onnx-tts-wrapper.h} +90 -85
- package/ios/{sherpa-onnx-tts-wrapper.mm → tts/sherpa-onnx-tts-wrapper.mm} +376 -345
- package/lib/module/NativeSherpaOnnx.js +3 -0
- package/lib/module/NativeSherpaOnnx.js.map +1 -1
- package/lib/module/audio/index.js +22 -0
- package/lib/module/audio/index.js.map +1 -0
- package/lib/module/diarization/index.js +1 -1
- package/lib/module/diarization/index.js.map +1 -1
- package/lib/module/download/ModelDownloadManager.js +918 -0
- package/lib/module/download/ModelDownloadManager.js.map +1 -0
- package/lib/module/download/extractTarBz2.js +53 -0
- package/lib/module/download/extractTarBz2.js.map +1 -0
- package/lib/module/download/index.js +6 -0
- package/lib/module/download/index.js.map +1 -0
- package/lib/module/download/validation.js +178 -0
- package/lib/module/download/validation.js.map +1 -0
- package/lib/module/enhancement/index.js +1 -1
- package/lib/module/enhancement/index.js.map +1 -1
- package/lib/module/index.js +41 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/separation/index.js +1 -1
- package/lib/module/separation/index.js.map +1 -1
- package/lib/module/stt/index.js +127 -60
- package/lib/module/stt/index.js.map +1 -1
- package/lib/module/stt/sttModelLanguages.js +512 -0
- package/lib/module/stt/sttModelLanguages.js.map +1 -0
- package/lib/module/stt/types.js +53 -1
- package/lib/module/stt/types.js.map +1 -1
- package/lib/module/tts/index.js +216 -289
- package/lib/module/tts/index.js.map +1 -1
- package/lib/module/tts/types.js +86 -1
- package/lib/module/tts/types.js.map +1 -1
- package/lib/module/types.js.map +1 -1
- package/lib/module/utils.js +86 -73
- package/lib/module/utils.js.map +1 -1
- package/lib/module/vad/index.js +1 -1
- package/lib/module/vad/index.js.map +1 -1
- package/lib/typescript/src/NativeSherpaOnnx.d.ts +192 -38
- package/lib/typescript/src/NativeSherpaOnnx.d.ts.map +1 -1
- package/lib/typescript/src/audio/index.d.ts +13 -0
- package/lib/typescript/src/audio/index.d.ts.map +1 -0
- package/lib/typescript/src/diarization/index.d.ts +3 -2
- package/lib/typescript/src/diarization/index.d.ts.map +1 -1
- package/lib/typescript/src/download/ModelDownloadManager.d.ts +108 -0
- package/lib/typescript/src/download/ModelDownloadManager.d.ts.map +1 -0
- package/lib/typescript/src/download/extractTarBz2.d.ts +14 -0
- package/lib/typescript/src/download/extractTarBz2.d.ts.map +1 -0
- package/lib/typescript/src/download/index.d.ts +7 -0
- package/lib/typescript/src/download/index.d.ts.map +1 -0
- package/lib/typescript/src/download/validation.d.ts +57 -0
- package/lib/typescript/src/download/validation.d.ts.map +1 -0
- package/lib/typescript/src/enhancement/index.d.ts +3 -2
- package/lib/typescript/src/enhancement/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +26 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/separation/index.d.ts +3 -2
- package/lib/typescript/src/separation/index.d.ts.map +1 -1
- package/lib/typescript/src/stt/index.d.ts +31 -43
- package/lib/typescript/src/stt/index.d.ts.map +1 -1
- package/lib/typescript/src/stt/sttModelLanguages.d.ts +52 -0
- package/lib/typescript/src/stt/sttModelLanguages.d.ts.map +1 -0
- package/lib/typescript/src/stt/types.d.ts +196 -9
- package/lib/typescript/src/stt/types.d.ts.map +1 -1
- package/lib/typescript/src/tts/index.d.ts +25 -211
- package/lib/typescript/src/tts/index.d.ts.map +1 -1
- package/lib/typescript/src/tts/types.d.ts +148 -25
- package/lib/typescript/src/tts/types.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +0 -32
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/utils.d.ts +28 -13
- package/lib/typescript/src/utils.d.ts.map +1 -1
- package/lib/typescript/src/vad/index.d.ts +3 -2
- package/lib/typescript/src/vad/index.d.ts.map +1 -1
- package/package.json +250 -222
- package/scripts/check-qnn-support.sh +78 -0
- package/scripts/setup-ios-framework.sh +379 -282
- package/src/NativeSherpaOnnx.ts +474 -251
- package/src/audio/index.ts +32 -0
- package/src/diarization/index.ts +4 -2
- package/src/download/ModelDownloadManager.ts +1325 -0
- package/src/download/extractTarBz2.ts +78 -0
- package/src/download/index.ts +43 -0
- package/src/download/validation.ts +279 -0
- package/src/enhancement/index.ts +4 -2
- package/src/index.tsx +78 -27
- package/src/separation/index.ts +4 -2
- package/src/stt/index.ts +249 -89
- package/src/stt/sttModelLanguages.ts +237 -0
- package/src/stt/types.ts +263 -9
- package/src/tts/index.ts +470 -458
- package/src/tts/types.ts +373 -218
- package/src/types.ts +0 -44
- package/src/utils.ts +145 -131
- package/src/vad/index.ts +4 -2
- package/third_party/ffmpeg_prebuilt/ANDROID_RELEASE_TAG +1 -0
- package/third_party/libarchive_prebuilt/ANDROID_RELEASE_TAG +1 -0
- package/third_party/libarchive_prebuilt/IOS_RELEASE_TAG +1 -0
- package/third_party/sherpa-onnx-prebuilt/ANDROID_RELEASE_TAG +1 -0
- package/third_party/sherpa-onnx-prebuilt/IOS_RELEASE_TAG +1 -0
- package/android/src/main/cpp/include/sherpa-onnx/c-api/c-api.h +0 -1918
- package/android/src/main/cpp/include/sherpa-onnx/c-api/cxx-api.h +0 -841
- package/android/src/main/cpp/jni/sherpa-onnx-model-detect.cpp +0 -541
- package/android/src/main/cpp/jni/sherpa-onnx-stt-jni.cpp +0 -336
- package/android/src/main/cpp/jni/sherpa-onnx-stt-wrapper.cpp +0 -222
- package/android/src/main/cpp/jni/sherpa-onnx-stt-wrapper.h +0 -68
- package/android/src/main/cpp/jni/sherpa-onnx-tts-jni.cpp +0 -823
- package/android/src/main/cpp/jni/sherpa-onnx-tts-wrapper.cpp +0 -387
- package/android/src/main/cpp/jni/sherpa-onnx-tts-wrapper.h +0 -147
- package/ios/Frameworks/sherpa_onnx.xcframework.zip +0 -0
- package/ios/include/sherpa-onnx/c-api/c-api.h +0 -1918
- package/ios/include/sherpa-onnx/c-api/cxx-api.h +0 -841
- package/ios/sherpa-onnx-model-detect.mm +0 -441
- package/ios/sherpa-onnx-stt-wrapper.h +0 -48
- package/ios/sherpa-onnx-stt-wrapper.mm +0 -201
- package/scripts/copy-headers.js +0 -184
- package/scripts/setup-assets.js +0 -323
package/src/tts/index.ts
CHANGED
|
@@ -1,458 +1,470 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import SherpaOnnx from '../NativeSherpaOnnx';
|
|
3
|
-
import type {
|
|
4
|
-
TTSInitializeOptions,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
noiseScale =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
throw
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
)
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
*
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
*
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
1
|
+
import { DeviceEventEmitter } from 'react-native';
|
|
2
|
+
import SherpaOnnx from '../NativeSherpaOnnx';
|
|
3
|
+
import type {
|
|
4
|
+
TTSInitializeOptions,
|
|
5
|
+
TTSModelType,
|
|
6
|
+
TtsModelOptions,
|
|
7
|
+
TtsUpdateOptions,
|
|
8
|
+
TtsGenerationOptions,
|
|
9
|
+
GeneratedAudio,
|
|
10
|
+
GeneratedAudioWithTimestamps,
|
|
11
|
+
TTSModelInfo,
|
|
12
|
+
TtsEngine,
|
|
13
|
+
TtsStreamChunk,
|
|
14
|
+
TtsStreamEnd,
|
|
15
|
+
TtsStreamError,
|
|
16
|
+
TtsStreamHandlers,
|
|
17
|
+
} from './types';
|
|
18
|
+
import type { ModelPathConfig } from '../types';
|
|
19
|
+
import { resolveModelPath } from '../utils';
|
|
20
|
+
|
|
21
|
+
let ttsInstanceCounter = 0;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Flatten model-specific options for the given model type to native init/update params.
|
|
25
|
+
* When modelType is 'auto' or missing, returns undefined for all (native uses defaults).
|
|
26
|
+
*/
|
|
27
|
+
function flattenTtsModelOptionsForNative(
|
|
28
|
+
modelType: TTSModelType | undefined,
|
|
29
|
+
modelOptions: TtsModelOptions | undefined
|
|
30
|
+
): {
|
|
31
|
+
noiseScale: number | undefined;
|
|
32
|
+
noiseScaleW: number | undefined;
|
|
33
|
+
lengthScale: number | undefined;
|
|
34
|
+
} {
|
|
35
|
+
if (
|
|
36
|
+
!modelOptions ||
|
|
37
|
+
!modelType ||
|
|
38
|
+
modelType === 'auto' ||
|
|
39
|
+
modelType === 'zipvoice' // Zipvoice does not use noise/length scale; native uses its own defaults
|
|
40
|
+
)
|
|
41
|
+
return {
|
|
42
|
+
noiseScale: undefined,
|
|
43
|
+
noiseScaleW: undefined,
|
|
44
|
+
lengthScale: undefined,
|
|
45
|
+
};
|
|
46
|
+
const block =
|
|
47
|
+
modelType === 'vits'
|
|
48
|
+
? modelOptions.vits
|
|
49
|
+
: modelType === 'matcha'
|
|
50
|
+
? modelOptions.matcha
|
|
51
|
+
: modelType === 'kokoro'
|
|
52
|
+
? modelOptions.kokoro
|
|
53
|
+
: modelType === 'kitten'
|
|
54
|
+
? modelOptions.kitten
|
|
55
|
+
: modelType === 'pocket'
|
|
56
|
+
? modelOptions.pocket
|
|
57
|
+
: undefined;
|
|
58
|
+
if (!block)
|
|
59
|
+
return {
|
|
60
|
+
noiseScale: undefined,
|
|
61
|
+
noiseScaleW: undefined,
|
|
62
|
+
lengthScale: undefined,
|
|
63
|
+
};
|
|
64
|
+
const out: {
|
|
65
|
+
noiseScale: number | undefined;
|
|
66
|
+
noiseScaleW: number | undefined;
|
|
67
|
+
lengthScale: number | undefined;
|
|
68
|
+
} = {
|
|
69
|
+
noiseScale: undefined,
|
|
70
|
+
noiseScaleW: undefined,
|
|
71
|
+
lengthScale: undefined,
|
|
72
|
+
};
|
|
73
|
+
const n = block as {
|
|
74
|
+
noiseScale?: number;
|
|
75
|
+
noiseScaleW?: number;
|
|
76
|
+
lengthScale?: number;
|
|
77
|
+
};
|
|
78
|
+
if (n.noiseScale !== undefined && typeof n.noiseScale === 'number')
|
|
79
|
+
out.noiseScale = n.noiseScale;
|
|
80
|
+
if (n.noiseScaleW !== undefined && typeof n.noiseScaleW === 'number')
|
|
81
|
+
out.noiseScaleW = n.noiseScaleW;
|
|
82
|
+
if (n.lengthScale !== undefined && typeof n.lengthScale === 'number')
|
|
83
|
+
out.lengthScale = n.lengthScale;
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Detect TTS model type and structure without initializing the engine.
|
|
89
|
+
* Uses the same native file-based detection as createTTS. Stateless; no instance required.
|
|
90
|
+
*
|
|
91
|
+
* @param modelPath - Model path configuration (asset, file, or auto)
|
|
92
|
+
* @param options - Optional modelType (default: 'auto')
|
|
93
|
+
* @returns Object with success, detectedModels (array of { type, modelDir }), and modelType (primary detected type)
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const result = await detectTtsModel({ type: 'asset', path: 'models/vits-piper-en' });
|
|
97
|
+
* if (result.success) console.log('Detected type:', result.modelType, result.detectedModels);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export async function detectTtsModel(
|
|
101
|
+
modelPath: ModelPathConfig,
|
|
102
|
+
options?: { modelType?: TTSModelType }
|
|
103
|
+
): Promise<{
|
|
104
|
+
success: boolean;
|
|
105
|
+
detectedModels: Array<{ type: string; modelDir: string }>;
|
|
106
|
+
modelType?: string;
|
|
107
|
+
}> {
|
|
108
|
+
const resolvedPath = await resolveModelPath(modelPath);
|
|
109
|
+
return SherpaOnnx.detectTtsModel(resolvedPath, options?.modelType);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Convert TtsGenerationOptions to a flat object for the native bridge.
|
|
114
|
+
* Flattens referenceAudio { samples, sampleRate } to referenceAudio array + referenceSampleRate.
|
|
115
|
+
*/
|
|
116
|
+
function toNativeTtsOptions(
|
|
117
|
+
options?: TtsGenerationOptions
|
|
118
|
+
): Record<string, unknown> {
|
|
119
|
+
if (options == null) return {};
|
|
120
|
+
const out: Record<string, unknown> = {};
|
|
121
|
+
if (options.sid !== undefined) out.sid = options.sid;
|
|
122
|
+
if (options.speed !== undefined) out.speed = options.speed;
|
|
123
|
+
if (options.silenceScale !== undefined)
|
|
124
|
+
out.silenceScale = options.silenceScale;
|
|
125
|
+
if (options.referenceAudio != null) {
|
|
126
|
+
out.referenceAudio = options.referenceAudio.samples;
|
|
127
|
+
out.referenceSampleRate = options.referenceAudio.sampleRate;
|
|
128
|
+
}
|
|
129
|
+
if (options.referenceText !== undefined)
|
|
130
|
+
out.referenceText = options.referenceText;
|
|
131
|
+
if (options.numSteps !== undefined) out.numSteps = options.numSteps;
|
|
132
|
+
if (options.extra != null && Object.keys(options.extra).length > 0)
|
|
133
|
+
out.extra = options.extra;
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// TTS stream events are sent from native via sendEventWithName; use DeviceEventEmitter
|
|
138
|
+
// so we don't need NativeEventEmitter (which expects addListener/removeListeners on the module).
|
|
139
|
+
/**
|
|
140
|
+
* Create a TTS engine instance. Call destroy() on the returned engine when done to free native resources.
|
|
141
|
+
*
|
|
142
|
+
* @param options - TTS initialization options or model path configuration
|
|
143
|
+
* @returns Promise resolving to a TtsEngine instance
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const tts = await createTTS({
|
|
147
|
+
* modelPath: { type: 'asset', path: 'models/vits-piper-en' },
|
|
148
|
+
* modelType: 'vits',
|
|
149
|
+
* modelOptions: { vits: { noiseScale: 0.667 } },
|
|
150
|
+
* });
|
|
151
|
+
* const audio = await tts.generateSpeech('Hello world');
|
|
152
|
+
* await tts.destroy();
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
export async function createTTS(
|
|
156
|
+
options: TTSInitializeOptions | ModelPathConfig
|
|
157
|
+
): Promise<TtsEngine> {
|
|
158
|
+
const instanceId = `tts_${++ttsInstanceCounter}`;
|
|
159
|
+
|
|
160
|
+
let modelPath: ModelPathConfig;
|
|
161
|
+
let modelType: TTSModelType | undefined;
|
|
162
|
+
let provider: string | undefined;
|
|
163
|
+
let numThreads: number | undefined;
|
|
164
|
+
let debug: boolean | undefined;
|
|
165
|
+
let modelOptions: TtsModelOptions | undefined;
|
|
166
|
+
let ruleFsts: string | undefined;
|
|
167
|
+
let ruleFars: string | undefined;
|
|
168
|
+
let maxNumSentences: number | undefined;
|
|
169
|
+
let silenceScale: number | undefined;
|
|
170
|
+
|
|
171
|
+
if ('modelPath' in options) {
|
|
172
|
+
modelPath = options.modelPath;
|
|
173
|
+
modelType = options.modelType;
|
|
174
|
+
provider = options.provider;
|
|
175
|
+
numThreads = options.numThreads;
|
|
176
|
+
debug = options.debug;
|
|
177
|
+
modelOptions = options.modelOptions;
|
|
178
|
+
ruleFsts = options.ruleFsts;
|
|
179
|
+
ruleFars = options.ruleFars;
|
|
180
|
+
maxNumSentences = options.maxNumSentences;
|
|
181
|
+
silenceScale = options.silenceScale;
|
|
182
|
+
} else {
|
|
183
|
+
modelPath = options;
|
|
184
|
+
modelType = undefined;
|
|
185
|
+
provider = undefined;
|
|
186
|
+
numThreads = undefined;
|
|
187
|
+
debug = undefined;
|
|
188
|
+
modelOptions = undefined;
|
|
189
|
+
ruleFsts = undefined;
|
|
190
|
+
ruleFars = undefined;
|
|
191
|
+
maxNumSentences = undefined;
|
|
192
|
+
silenceScale = undefined;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const flat = flattenTtsModelOptionsForNative(modelType, modelOptions);
|
|
196
|
+
const resolvedPath = await resolveModelPath(modelPath);
|
|
197
|
+
|
|
198
|
+
const result = await SherpaOnnx.initializeTts(
|
|
199
|
+
instanceId,
|
|
200
|
+
resolvedPath,
|
|
201
|
+
modelType ?? 'auto',
|
|
202
|
+
numThreads ?? 2,
|
|
203
|
+
debug ?? false,
|
|
204
|
+
flat.noiseScale,
|
|
205
|
+
flat.noiseScaleW,
|
|
206
|
+
flat.lengthScale,
|
|
207
|
+
ruleFsts,
|
|
208
|
+
ruleFars,
|
|
209
|
+
maxNumSentences,
|
|
210
|
+
silenceScale,
|
|
211
|
+
provider
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
if (!result.success) {
|
|
215
|
+
throw new Error(
|
|
216
|
+
`TTS initialization failed: ${JSON.stringify(
|
|
217
|
+
result.detectedModels ?? []
|
|
218
|
+
)}`
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const firstDetected = result.detectedModels?.[0];
|
|
223
|
+
const effectiveModelType: TTSModelType | undefined =
|
|
224
|
+
modelType && modelType !== 'auto'
|
|
225
|
+
? modelType
|
|
226
|
+
: (firstDetected?.type as TTSModelType);
|
|
227
|
+
|
|
228
|
+
let destroyed = false;
|
|
229
|
+
|
|
230
|
+
const guard = () => {
|
|
231
|
+
if (destroyed) {
|
|
232
|
+
throw new Error(
|
|
233
|
+
`TTS instance ${instanceId} has been destroyed; cannot call methods on it.`
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const engine: TtsEngine = {
|
|
239
|
+
get instanceId() {
|
|
240
|
+
return instanceId;
|
|
241
|
+
},
|
|
242
|
+
|
|
243
|
+
async generateSpeech(
|
|
244
|
+
text: string,
|
|
245
|
+
opts?: TtsGenerationOptions
|
|
246
|
+
): Promise<GeneratedAudio> {
|
|
247
|
+
guard();
|
|
248
|
+
return SherpaOnnx.generateTts(instanceId, text, toNativeTtsOptions(opts));
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
async generateSpeechWithTimestamps(
|
|
252
|
+
text: string,
|
|
253
|
+
opts?: TtsGenerationOptions
|
|
254
|
+
): Promise<GeneratedAudioWithTimestamps> {
|
|
255
|
+
guard();
|
|
256
|
+
return SherpaOnnx.generateTtsWithTimestamps(
|
|
257
|
+
instanceId,
|
|
258
|
+
text,
|
|
259
|
+
toNativeTtsOptions(opts)
|
|
260
|
+
);
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
async generateSpeechStream(
|
|
264
|
+
text: string,
|
|
265
|
+
opts: TtsGenerationOptions | undefined,
|
|
266
|
+
handlers: TtsStreamHandlers
|
|
267
|
+
): Promise<() => void> {
|
|
268
|
+
guard();
|
|
269
|
+
const subscriptions = [
|
|
270
|
+
DeviceEventEmitter.addListener('ttsStreamChunk', (event: unknown) => {
|
|
271
|
+
const e = event as TtsStreamChunk;
|
|
272
|
+
if (e.instanceId != null && e.instanceId !== instanceId) return;
|
|
273
|
+
handlers.onChunk?.(e);
|
|
274
|
+
}),
|
|
275
|
+
DeviceEventEmitter.addListener('ttsStreamEnd', (event: unknown) => {
|
|
276
|
+
const e = event as TtsStreamEnd;
|
|
277
|
+
if (e.instanceId != null && e.instanceId !== instanceId) return;
|
|
278
|
+
handlers.onEnd?.(e);
|
|
279
|
+
}),
|
|
280
|
+
DeviceEventEmitter.addListener('ttsStreamError', (event: unknown) => {
|
|
281
|
+
const e = event as TtsStreamError;
|
|
282
|
+
if (e.instanceId != null && e.instanceId !== instanceId) return;
|
|
283
|
+
handlers.onError?.(e);
|
|
284
|
+
}),
|
|
285
|
+
];
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
await SherpaOnnx.generateTtsStream(
|
|
289
|
+
instanceId,
|
|
290
|
+
text,
|
|
291
|
+
toNativeTtsOptions(opts)
|
|
292
|
+
);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
subscriptions.forEach((sub) => sub.remove());
|
|
295
|
+
throw error;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return () => {
|
|
299
|
+
subscriptions.forEach((sub) => sub.remove());
|
|
300
|
+
};
|
|
301
|
+
},
|
|
302
|
+
|
|
303
|
+
async cancelSpeechStream(): Promise<void> {
|
|
304
|
+
guard();
|
|
305
|
+
return SherpaOnnx.cancelTtsStream(instanceId);
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
async startPcmPlayer(sampleRate: number, channels: number): Promise<void> {
|
|
309
|
+
guard();
|
|
310
|
+
return SherpaOnnx.startTtsPcmPlayer(instanceId, sampleRate, channels);
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
async writePcmChunk(samples: number[]): Promise<void> {
|
|
314
|
+
guard();
|
|
315
|
+
return SherpaOnnx.writeTtsPcmChunk(instanceId, samples);
|
|
316
|
+
},
|
|
317
|
+
|
|
318
|
+
async stopPcmPlayer(): Promise<void> {
|
|
319
|
+
guard();
|
|
320
|
+
return SherpaOnnx.stopTtsPcmPlayer(instanceId);
|
|
321
|
+
},
|
|
322
|
+
|
|
323
|
+
async updateParams(opts: TtsUpdateOptions): Promise<{
|
|
324
|
+
success: boolean;
|
|
325
|
+
detectedModels: Array<{ type: string; modelDir: string }>;
|
|
326
|
+
}> {
|
|
327
|
+
guard();
|
|
328
|
+
const effectiveModelTypeForUpdate =
|
|
329
|
+
opts.modelType && opts.modelType !== 'auto'
|
|
330
|
+
? opts.modelType
|
|
331
|
+
: effectiveModelType;
|
|
332
|
+
const flatOpts = flattenTtsModelOptionsForNative(
|
|
333
|
+
effectiveModelTypeForUpdate,
|
|
334
|
+
opts.modelOptions
|
|
335
|
+
);
|
|
336
|
+
const noiseArg =
|
|
337
|
+
flatOpts.noiseScale === undefined ? Number.NaN : flatOpts.noiseScale;
|
|
338
|
+
const noiseWArg =
|
|
339
|
+
flatOpts.noiseScaleW === undefined ? Number.NaN : flatOpts.noiseScaleW;
|
|
340
|
+
const lengthArg =
|
|
341
|
+
flatOpts.lengthScale === undefined ? Number.NaN : flatOpts.lengthScale;
|
|
342
|
+
return SherpaOnnx.updateTtsParams(
|
|
343
|
+
instanceId,
|
|
344
|
+
noiseArg,
|
|
345
|
+
noiseWArg,
|
|
346
|
+
lengthArg
|
|
347
|
+
);
|
|
348
|
+
},
|
|
349
|
+
|
|
350
|
+
async getModelInfo(): Promise<TTSModelInfo> {
|
|
351
|
+
guard();
|
|
352
|
+
const [sampleRate, numSpeakers] = await Promise.all([
|
|
353
|
+
SherpaOnnx.getTtsSampleRate(instanceId),
|
|
354
|
+
SherpaOnnx.getTtsNumSpeakers(instanceId),
|
|
355
|
+
]);
|
|
356
|
+
return { sampleRate, numSpeakers };
|
|
357
|
+
},
|
|
358
|
+
|
|
359
|
+
async getSampleRate(): Promise<number> {
|
|
360
|
+
guard();
|
|
361
|
+
return SherpaOnnx.getTtsSampleRate(instanceId);
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
async getNumSpeakers(): Promise<number> {
|
|
365
|
+
guard();
|
|
366
|
+
return SherpaOnnx.getTtsNumSpeakers(instanceId);
|
|
367
|
+
},
|
|
368
|
+
|
|
369
|
+
async destroy(): Promise<void> {
|
|
370
|
+
if (destroyed) return;
|
|
371
|
+
destroyed = true;
|
|
372
|
+
await SherpaOnnx.unloadTts(instanceId);
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
return engine;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// ========== Module-level utilities (stateless, no instance required) ==========
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Save generated TTS audio to a WAV file.
|
|
383
|
+
*/
|
|
384
|
+
export function saveAudioToFile(
|
|
385
|
+
audio: GeneratedAudio,
|
|
386
|
+
filePath: string
|
|
387
|
+
): Promise<string> {
|
|
388
|
+
return SherpaOnnx.saveTtsAudioToFile(
|
|
389
|
+
audio.samples,
|
|
390
|
+
audio.sampleRate,
|
|
391
|
+
filePath
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Save generated TTS audio to a WAV file via Android SAF content URI.
|
|
397
|
+
*/
|
|
398
|
+
export function saveAudioToContentUri(
|
|
399
|
+
audio: GeneratedAudio,
|
|
400
|
+
directoryUri: string,
|
|
401
|
+
filename: string
|
|
402
|
+
): Promise<string> {
|
|
403
|
+
return SherpaOnnx.saveTtsAudioToContentUri(
|
|
404
|
+
audio.samples,
|
|
405
|
+
audio.sampleRate,
|
|
406
|
+
directoryUri,
|
|
407
|
+
filename
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Save a text file via Android SAF content URI.
|
|
413
|
+
*/
|
|
414
|
+
export function saveTextToContentUri(
|
|
415
|
+
text: string,
|
|
416
|
+
directoryUri: string,
|
|
417
|
+
filename: string,
|
|
418
|
+
mimeType = 'text/plain'
|
|
419
|
+
): Promise<string> {
|
|
420
|
+
return SherpaOnnx.saveTtsTextToContentUri(
|
|
421
|
+
text,
|
|
422
|
+
directoryUri,
|
|
423
|
+
filename,
|
|
424
|
+
mimeType
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Copy a SAF content URI to a cache file for local playback (Android only).
|
|
430
|
+
*/
|
|
431
|
+
export function copyContentUriToCache(
|
|
432
|
+
fileUri: string,
|
|
433
|
+
filename: string
|
|
434
|
+
): Promise<string> {
|
|
435
|
+
return SherpaOnnx.copyTtsContentUriToCache(fileUri, filename);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Share a TTS audio file (file path or content URI).
|
|
440
|
+
*/
|
|
441
|
+
export function shareAudioFile(
|
|
442
|
+
fileUri: string,
|
|
443
|
+
mimeType = 'audio/wav'
|
|
444
|
+
): Promise<void> {
|
|
445
|
+
return SherpaOnnx.shareTtsAudio(fileUri, mimeType);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// Export types and runtime type list
|
|
449
|
+
export type {
|
|
450
|
+
TTSInitializeOptions,
|
|
451
|
+
TTSModelType,
|
|
452
|
+
TtsModelOptions,
|
|
453
|
+
TtsVitsModelOptions,
|
|
454
|
+
TtsMatchaModelOptions,
|
|
455
|
+
TtsKokoroModelOptions,
|
|
456
|
+
TtsKittenModelOptions,
|
|
457
|
+
TtsPocketModelOptions,
|
|
458
|
+
TtsUpdateOptions,
|
|
459
|
+
TtsGenerationOptions,
|
|
460
|
+
GeneratedAudio,
|
|
461
|
+
GeneratedAudioWithTimestamps,
|
|
462
|
+
TtsSubtitleItem,
|
|
463
|
+
TTSModelInfo,
|
|
464
|
+
TtsEngine,
|
|
465
|
+
TtsStreamHandlers,
|
|
466
|
+
TtsStreamChunk,
|
|
467
|
+
TtsStreamEnd,
|
|
468
|
+
TtsStreamError,
|
|
469
|
+
} from './types';
|
|
470
|
+
export { TTS_MODEL_TYPES } from './types';
|