react-native-sherpa-onnx 0.3.0 → 0.3.2
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 +21 -7
- package/SherpaOnnx.podspec +1 -1
- package/android/build.gradle +35 -26
- package/android/prebuilt-download.gradle +27 -14
- package/android/src/main/cpp/CMakeLists.txt +51 -17
- package/android/src/main/cpp/jni/archive/sherpa-onnx-archive-helper.cpp +14 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-helper.cpp +16 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-helper.h +3 -0
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect-stt.cpp +19 -2
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-model-detect.h +2 -1
- package/android/src/main/cpp/jni/model_detect/sherpa-onnx-stt-wrapper.cpp +1 -0
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxModule.kt +114 -8
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxOnlineSttHelper.kt +535 -0
- package/android/src/main/java/com/sherpaonnx/SherpaOnnxTtsHelper.kt +10 -10
- package/ios/SherpaOnnx+OnlineSTT.mm +365 -0
- package/ios/SherpaOnnx+TTS.mm +35 -9
- package/ios/SherpaOnnx.mm +6 -0
- package/ios/model_detect/sherpa-onnx-model-detect-helper.h +3 -0
- package/ios/model_detect/sherpa-onnx-model-detect-helper.mm +16 -0
- package/ios/model_detect/sherpa-onnx-model-detect-stt.mm +19 -2
- package/ios/model_detect/sherpa-onnx-model-detect.h +2 -1
- package/ios/online_stt/sherpa-onnx-online-stt-wrapper.h +85 -0
- package/ios/online_stt/sherpa-onnx-online-stt-wrapper.mm +270 -0
- package/lib/module/NativeSherpaOnnx.js.map +1 -1
- package/lib/module/index.js +2 -2
- package/lib/module/stt/index.js +4 -0
- package/lib/module/stt/index.js.map +1 -1
- package/lib/module/stt/streaming.js +257 -0
- package/lib/module/stt/streaming.js.map +1 -0
- package/lib/module/stt/streamingTypes.js +38 -0
- package/lib/module/stt/streamingTypes.js.map +1 -0
- package/lib/module/tts/index.js +4 -43
- package/lib/module/tts/index.js.map +1 -1
- package/lib/module/tts/streaming.js +220 -0
- package/lib/module/tts/streaming.js.map +1 -0
- package/lib/module/tts/streamingTypes.js +4 -0
- package/lib/module/tts/streamingTypes.js.map +1 -0
- package/lib/module/tts/types.js +8 -1
- package/lib/module/tts/types.js.map +1 -1
- package/lib/typescript/src/NativeSherpaOnnx.d.ts +66 -1
- package/lib/typescript/src/NativeSherpaOnnx.d.ts.map +1 -1
- package/lib/typescript/src/stt/index.d.ts +3 -0
- package/lib/typescript/src/stt/index.d.ts.map +1 -1
- package/lib/typescript/src/stt/streaming.d.ts +42 -0
- package/lib/typescript/src/stt/streaming.d.ts.map +1 -0
- package/lib/typescript/src/stt/streamingTypes.d.ts +122 -0
- package/lib/typescript/src/stt/streamingTypes.d.ts.map +1 -0
- package/lib/typescript/src/tts/index.d.ts +3 -1
- package/lib/typescript/src/tts/index.d.ts.map +1 -1
- package/lib/typescript/src/tts/streaming.d.ts +24 -0
- package/lib/typescript/src/tts/streaming.d.ts.map +1 -0
- package/lib/typescript/src/tts/streamingTypes.d.ts +27 -0
- package/lib/typescript/src/tts/streamingTypes.d.ts.map +1 -0
- package/lib/typescript/src/tts/types.d.ts +19 -6
- package/lib/typescript/src/tts/types.d.ts.map +1 -1
- package/package.json +1 -2
- package/src/NativeSherpaOnnx.ts +95 -0
- package/src/index.tsx +2 -2
- package/src/stt/index.ts +17 -0
- package/src/stt/streaming.ts +361 -0
- package/src/stt/streamingTypes.ts +151 -0
- package/src/tts/index.ts +6 -66
- package/src/tts/streaming.ts +336 -0
- package/src/tts/streamingTypes.ts +54 -0
- package/src/tts/types.ts +20 -10
- package/android/codegen.gradle +0 -57
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sherpa-onnx-online-stt-wrapper.h
|
|
3
|
+
*
|
|
4
|
+
* Purpose: Wraps sherpa-onnx C++ OnlineRecognizer for iOS streaming STT.
|
|
5
|
+
* Manages recognizer instances and streams; scans model directory for paths.
|
|
6
|
+
* Used by SherpaOnnx+OnlineSTT.mm.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#ifndef SHERPA_ONNX_ONLINE_STT_WRAPPER_H
|
|
10
|
+
#define SHERPA_ONNX_ONLINE_STT_WRAPPER_H
|
|
11
|
+
|
|
12
|
+
#include <memory>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <unordered_map>
|
|
15
|
+
#include <vector>
|
|
16
|
+
|
|
17
|
+
namespace sherpaonnx {
|
|
18
|
+
|
|
19
|
+
struct OnlineSttInitResult {
|
|
20
|
+
bool success = false;
|
|
21
|
+
std::string error;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
struct OnlineSttStreamResult {
|
|
25
|
+
std::string text;
|
|
26
|
+
std::vector<std::string> tokens;
|
|
27
|
+
std::vector<float> timestamps;
|
|
28
|
+
bool isEndpoint = false;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Wrapper for sherpa-onnx OnlineRecognizer (streaming STT).
|
|
33
|
+
* One wrapper per instanceId; multiple streams per instance.
|
|
34
|
+
*/
|
|
35
|
+
class OnlineSttWrapper {
|
|
36
|
+
public:
|
|
37
|
+
OnlineSttWrapper();
|
|
38
|
+
~OnlineSttWrapper();
|
|
39
|
+
|
|
40
|
+
OnlineSttInitResult initialize(
|
|
41
|
+
const std::string& modelDir,
|
|
42
|
+
const std::string& modelType,
|
|
43
|
+
bool enableEndpoint,
|
|
44
|
+
const std::string& decodingMethod,
|
|
45
|
+
int32_t maxActivePaths,
|
|
46
|
+
const std::string& hotwordsFile,
|
|
47
|
+
float hotwordsScore,
|
|
48
|
+
int32_t numThreads,
|
|
49
|
+
const std::string& provider,
|
|
50
|
+
const std::string& ruleFsts,
|
|
51
|
+
const std::string& ruleFars,
|
|
52
|
+
float blankPenalty,
|
|
53
|
+
bool debug,
|
|
54
|
+
bool rule1MustContainNonSilence,
|
|
55
|
+
float rule1MinTrailingSilence,
|
|
56
|
+
float rule1MinUtteranceLength,
|
|
57
|
+
bool rule2MustContainNonSilence,
|
|
58
|
+
float rule2MinTrailingSilence,
|
|
59
|
+
float rule2MinUtteranceLength,
|
|
60
|
+
bool rule3MustContainNonSilence,
|
|
61
|
+
float rule3MinTrailingSilence,
|
|
62
|
+
float rule3MinUtteranceLength
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
bool createStream(const std::string& streamId, const std::string& hotwords);
|
|
66
|
+
void acceptWaveform(const std::string& streamId, int32_t sampleRate, const float* samples, size_t n);
|
|
67
|
+
void inputFinished(const std::string& streamId);
|
|
68
|
+
void decode(const std::string& streamId);
|
|
69
|
+
bool isReady(const std::string& streamId);
|
|
70
|
+
OnlineSttStreamResult getResult(const std::string& streamId);
|
|
71
|
+
bool isEndpoint(const std::string& streamId);
|
|
72
|
+
void resetStream(const std::string& streamId);
|
|
73
|
+
void releaseStream(const std::string& streamId);
|
|
74
|
+
void unload();
|
|
75
|
+
|
|
76
|
+
bool isInitialized() const;
|
|
77
|
+
|
|
78
|
+
private:
|
|
79
|
+
struct Impl;
|
|
80
|
+
std::unique_ptr<Impl> pImpl;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
} // namespace sherpaonnx
|
|
84
|
+
|
|
85
|
+
#endif // SHERPA_ONNX_ONLINE_STT_WRAPPER_H
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sherpa-onnx-online-stt-wrapper.mm
|
|
3
|
+
*
|
|
4
|
+
* Purpose: Wraps sherpa-onnx C++ OnlineRecognizer for iOS streaming STT.
|
|
5
|
+
* Scans model directory, builds config, manages recognizer and streams.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
#include "sherpa-onnx-online-stt-wrapper.h"
|
|
9
|
+
#include "sherpa-onnx-model-detect-helper.h"
|
|
10
|
+
|
|
11
|
+
#include "sherpa-onnx/c-api/cxx-api.h"
|
|
12
|
+
|
|
13
|
+
#include <algorithm>
|
|
14
|
+
#include <cstring>
|
|
15
|
+
#include <string>
|
|
16
|
+
#include <utility>
|
|
17
|
+
|
|
18
|
+
#ifdef __APPLE__
|
|
19
|
+
#import <Foundation/Foundation.h>
|
|
20
|
+
#define LOGO(fmt, ...) NSLog(@"OnlineSttWrapper: " fmt, ##__VA_ARGS__)
|
|
21
|
+
#define LOGE(fmt, ...) NSLog(@"OnlineSttWrapper ERROR: " fmt, ##__VA_ARGS__)
|
|
22
|
+
#else
|
|
23
|
+
#define LOGO(...)
|
|
24
|
+
#define LOGE(...)
|
|
25
|
+
#endif
|
|
26
|
+
|
|
27
|
+
namespace sherpaonnx {
|
|
28
|
+
|
|
29
|
+
using namespace model_detect;
|
|
30
|
+
|
|
31
|
+
namespace {
|
|
32
|
+
|
|
33
|
+
/** Scan model directory for online model type; returns paths map (encoder, decoder, joiner, tokens or model, tokens). */
|
|
34
|
+
std::unordered_map<std::string, std::string> scanOnlineModelPaths(const std::string& modelDir, const std::string& modelType) {
|
|
35
|
+
std::unordered_map<std::string, std::string> out;
|
|
36
|
+
const int kMaxDepth = 4;
|
|
37
|
+
std::vector<FileEntry> files = ListFilesRecursive(modelDir, kMaxDepth);
|
|
38
|
+
|
|
39
|
+
auto firstOnnx = [&files](const std::vector<std::string>& tokens) -> std::string {
|
|
40
|
+
return FindOnnxByAnyToken(files, tokens, std::nullopt);
|
|
41
|
+
};
|
|
42
|
+
std::string tokensPath = FindFileEndingWith(files, "tokens.txt");
|
|
43
|
+
|
|
44
|
+
if (modelType == "transducer") {
|
|
45
|
+
std::string enc = firstOnnx({"encoder"});
|
|
46
|
+
std::string dec = firstOnnx({"decoder"});
|
|
47
|
+
std::string join = firstOnnx({"joiner"});
|
|
48
|
+
if (enc.empty() || dec.empty() || join.empty()) {
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
out["encoder"] = enc;
|
|
52
|
+
out["decoder"] = dec;
|
|
53
|
+
out["joiner"] = join;
|
|
54
|
+
out["tokens"] = tokensPath;
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
if (modelType == "paraformer") {
|
|
58
|
+
std::string enc = firstOnnx({"encoder"});
|
|
59
|
+
std::string dec = firstOnnx({"decoder"});
|
|
60
|
+
if (enc.empty() || dec.empty()) return {};
|
|
61
|
+
out["encoder"] = enc;
|
|
62
|
+
out["decoder"] = dec;
|
|
63
|
+
out["tokens"] = tokensPath;
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
if (modelType == "zipformer2_ctc" || modelType == "nemo_ctc" || modelType == "tone_ctc") {
|
|
67
|
+
std::string modelPath = firstOnnx({"model"});
|
|
68
|
+
if (modelPath.empty()) {
|
|
69
|
+
std::vector<std::string> exclude = {"encoder", "decoder", "joiner", "vocoder", "acoustic", "embedding", "llm"};
|
|
70
|
+
modelPath = FindLargestOnnxExcludingTokens(files, exclude);
|
|
71
|
+
}
|
|
72
|
+
if (modelPath.empty()) return {};
|
|
73
|
+
out["model"] = modelPath;
|
|
74
|
+
out["tokens"] = tokensPath;
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
return {};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
} // namespace
|
|
81
|
+
|
|
82
|
+
struct OnlineSttWrapper::Impl {
|
|
83
|
+
std::unique_ptr<sherpa_onnx::cxx::OnlineRecognizer> recognizer;
|
|
84
|
+
std::unordered_map<std::string, sherpa_onnx::cxx::OnlineStream> streams;
|
|
85
|
+
bool initialized = false;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
OnlineSttWrapper::OnlineSttWrapper() : pImpl(std::make_unique<Impl>()) {}
|
|
89
|
+
|
|
90
|
+
OnlineSttWrapper::~OnlineSttWrapper() {
|
|
91
|
+
unload();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
OnlineSttInitResult OnlineSttWrapper::initialize(
|
|
95
|
+
const std::string& modelDir,
|
|
96
|
+
const std::string& modelType,
|
|
97
|
+
bool enableEndpoint,
|
|
98
|
+
const std::string& decodingMethod,
|
|
99
|
+
int32_t maxActivePaths,
|
|
100
|
+
const std::string& hotwordsFile,
|
|
101
|
+
float hotwordsScore,
|
|
102
|
+
int32_t numThreads,
|
|
103
|
+
const std::string& provider,
|
|
104
|
+
const std::string& ruleFsts,
|
|
105
|
+
const std::string& ruleFars,
|
|
106
|
+
float blankPenalty,
|
|
107
|
+
bool debug,
|
|
108
|
+
// NOTE: rule*MustContainNonSilence, rule1/2MinUtteranceLength, and
|
|
109
|
+
// rule3MinTrailingSilence are not exposed by the sherpa-onnx C++ CXX API
|
|
110
|
+
// (cxx-api.h). Only rule1_min_trailing_silence, rule2_min_trailing_silence,
|
|
111
|
+
// and rule3_min_utterance_length are supported on iOS.
|
|
112
|
+
bool /* rule1MustContainNonSilence */,
|
|
113
|
+
float rule1MinTrailingSilence,
|
|
114
|
+
float /* rule1MinUtteranceLength */,
|
|
115
|
+
bool /* rule2MustContainNonSilence */,
|
|
116
|
+
float rule2MinTrailingSilence,
|
|
117
|
+
float /* rule2MinUtteranceLength */,
|
|
118
|
+
bool /* rule3MustContainNonSilence */,
|
|
119
|
+
float /* rule3MinTrailingSilence */,
|
|
120
|
+
float rule3MinUtteranceLength
|
|
121
|
+
) {
|
|
122
|
+
OnlineSttInitResult result;
|
|
123
|
+
if (pImpl->initialized) {
|
|
124
|
+
result.error = "Already initialized";
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
if (!FileExists(modelDir) || !IsDirectory(modelDir)) {
|
|
128
|
+
result.error = "Model directory does not exist or is not a directory: " + modelDir;
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
auto paths = scanOnlineModelPaths(modelDir, modelType);
|
|
133
|
+
if (paths.empty()) {
|
|
134
|
+
result.error = "Unsupported or invalid online STT model type or missing files: " + modelType;
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
sherpa_onnx::cxx::OnlineRecognizerConfig config;
|
|
139
|
+
config.feat_config.sample_rate = 16000;
|
|
140
|
+
config.feat_config.feature_dim = 80;
|
|
141
|
+
config.decoding_method = decodingMethod.empty() ? "greedy_search" : decodingMethod;
|
|
142
|
+
config.max_active_paths = maxActivePaths;
|
|
143
|
+
config.enable_endpoint = enableEndpoint;
|
|
144
|
+
config.rule1_min_trailing_silence = rule1MinTrailingSilence > 0 ? rule1MinTrailingSilence : 2.4f;
|
|
145
|
+
config.rule2_min_trailing_silence = rule2MinTrailingSilence > 0 ? rule2MinTrailingSilence : 1.4f;
|
|
146
|
+
config.rule3_min_utterance_length = rule3MinUtteranceLength > 0 ? rule3MinUtteranceLength : 20.f;
|
|
147
|
+
config.hotwords_file = hotwordsFile;
|
|
148
|
+
config.hotwords_score = hotwordsScore;
|
|
149
|
+
config.rule_fsts = ruleFsts;
|
|
150
|
+
config.rule_fars = ruleFars;
|
|
151
|
+
config.blank_penalty = blankPenalty;
|
|
152
|
+
config.model_config.num_threads = numThreads <= 0 ? 1 : numThreads;
|
|
153
|
+
config.model_config.provider = provider.empty() ? "cpu" : provider;
|
|
154
|
+
config.model_config.debug = debug;
|
|
155
|
+
config.model_config.tokens = paths.count("tokens") ? paths["tokens"] : "";
|
|
156
|
+
|
|
157
|
+
if (modelType == "transducer") {
|
|
158
|
+
config.model_config.transducer.encoder = paths["encoder"];
|
|
159
|
+
config.model_config.transducer.decoder = paths["decoder"];
|
|
160
|
+
config.model_config.transducer.joiner = paths["joiner"];
|
|
161
|
+
config.model_config.model_type = "zipformer";
|
|
162
|
+
} else if (modelType == "paraformer") {
|
|
163
|
+
config.model_config.paraformer.encoder = paths["encoder"];
|
|
164
|
+
config.model_config.paraformer.decoder = paths["decoder"];
|
|
165
|
+
config.model_config.model_type = "paraformer";
|
|
166
|
+
} else if (modelType == "zipformer2_ctc") {
|
|
167
|
+
config.model_config.zipformer2_ctc.model = paths["model"];
|
|
168
|
+
config.model_config.model_type = "zipformer2";
|
|
169
|
+
} else if (modelType == "nemo_ctc") {
|
|
170
|
+
config.model_config.nemo_ctc.model = paths["model"];
|
|
171
|
+
config.model_config.model_type = "nemo_ctc";
|
|
172
|
+
} else if (modelType == "tone_ctc") {
|
|
173
|
+
config.model_config.t_one_ctc.model = paths["model"];
|
|
174
|
+
config.model_config.model_type = "t_one";
|
|
175
|
+
} else {
|
|
176
|
+
result.error = "Unsupported online STT model type: " + modelType;
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
sherpa_onnx::cxx::OnlineRecognizer rec = sherpa_onnx::cxx::OnlineRecognizer::Create(config);
|
|
182
|
+
pImpl->recognizer = std::make_unique<sherpa_onnx::cxx::OnlineRecognizer>(std::move(rec));
|
|
183
|
+
pImpl->initialized = true;
|
|
184
|
+
result.success = true;
|
|
185
|
+
} catch (const std::exception& e) {
|
|
186
|
+
result.error = std::string("OnlineRecognizer Create failed: ") + e.what();
|
|
187
|
+
LOGE("%s", result.error.c_str());
|
|
188
|
+
} catch (...) {
|
|
189
|
+
result.error = "OnlineRecognizer Create failed: unknown error";
|
|
190
|
+
LOGE("%s", result.error.c_str());
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
bool OnlineSttWrapper::createStream(const std::string& streamId, const std::string& hotwords) {
|
|
196
|
+
if (!pImpl->initialized || !pImpl->recognizer) return false;
|
|
197
|
+
if (pImpl->streams.count(streamId)) return false;
|
|
198
|
+
try {
|
|
199
|
+
sherpa_onnx::cxx::OnlineStream stream = hotwords.empty()
|
|
200
|
+
? pImpl->recognizer->CreateStream()
|
|
201
|
+
: pImpl->recognizer->CreateStream(hotwords);
|
|
202
|
+
pImpl->streams.emplace(streamId, std::move(stream));
|
|
203
|
+
return true;
|
|
204
|
+
} catch (...) {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
void OnlineSttWrapper::acceptWaveform(const std::string& streamId, int32_t sampleRate, const float* samples, size_t n) {
|
|
210
|
+
auto it = pImpl->streams.find(streamId);
|
|
211
|
+
if (it == pImpl->streams.end()) return;
|
|
212
|
+
it->second.AcceptWaveform(sampleRate, samples, static_cast<int32_t>(n));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
void OnlineSttWrapper::inputFinished(const std::string& streamId) {
|
|
216
|
+
auto it = pImpl->streams.find(streamId);
|
|
217
|
+
if (it == pImpl->streams.end()) return;
|
|
218
|
+
it->second.InputFinished();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
void OnlineSttWrapper::decode(const std::string& streamId) {
|
|
222
|
+
auto it = pImpl->streams.find(streamId);
|
|
223
|
+
if (it == pImpl->streams.end() || !pImpl->recognizer) return;
|
|
224
|
+
pImpl->recognizer->Decode(&it->second);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
bool OnlineSttWrapper::isReady(const std::string& streamId) {
|
|
228
|
+
auto it = pImpl->streams.find(streamId);
|
|
229
|
+
if (it == pImpl->streams.end() || !pImpl->recognizer) return false;
|
|
230
|
+
return pImpl->recognizer->IsReady(&it->second);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
OnlineSttStreamResult OnlineSttWrapper::getResult(const std::string& streamId) {
|
|
234
|
+
OnlineSttStreamResult r;
|
|
235
|
+
auto it = pImpl->streams.find(streamId);
|
|
236
|
+
if (it == pImpl->streams.end() || !pImpl->recognizer) return r;
|
|
237
|
+
sherpa_onnx::cxx::OnlineRecognizerResult res = pImpl->recognizer->GetResult(&it->second);
|
|
238
|
+
r.text = res.text;
|
|
239
|
+
r.tokens = res.tokens;
|
|
240
|
+
r.timestamps = res.timestamps;
|
|
241
|
+
return r;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
bool OnlineSttWrapper::isEndpoint(const std::string& streamId) {
|
|
245
|
+
auto it = pImpl->streams.find(streamId);
|
|
246
|
+
if (it == pImpl->streams.end() || !pImpl->recognizer) return false;
|
|
247
|
+
return pImpl->recognizer->IsEndpoint(&it->second);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
void OnlineSttWrapper::resetStream(const std::string& streamId) {
|
|
251
|
+
auto it = pImpl->streams.find(streamId);
|
|
252
|
+
if (it == pImpl->streams.end() || !pImpl->recognizer) return;
|
|
253
|
+
pImpl->recognizer->Reset(&it->second);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
void OnlineSttWrapper::releaseStream(const std::string& streamId) {
|
|
257
|
+
pImpl->streams.erase(streamId);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
void OnlineSttWrapper::unload() {
|
|
261
|
+
pImpl->streams.clear();
|
|
262
|
+
pImpl->recognizer.reset();
|
|
263
|
+
pImpl->initialized = false;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
bool OnlineSttWrapper::isInitialized() const {
|
|
267
|
+
return pImpl->initialized && pImpl->recognizer != nullptr;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
} // namespace sherpaonnx
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeSherpaOnnx.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAA0B,cAAc;;AAEpE;;
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeSherpaOnnx.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAA0B,cAAc;;AAEpE;;AAsjBA,eAAeA,mBAAmB,CAACC,YAAY,CAAO,YAAY,CAAC","ignoreList":[]}
|
package/lib/module/index.js
CHANGED
|
@@ -7,8 +7,8 @@ import SherpaOnnx from "./NativeSherpaOnnx.js";
|
|
|
7
7
|
export { assetModelPath, autoModelPath, fileModelPath, getAssetPackPath, getDefaultModelPath, getPlayAssetDeliveryModelsPath, listAssetModels, listModelsAtPath, resolveModelPath } from "./utils.js";
|
|
8
8
|
|
|
9
9
|
// Note: Feature-specific exports are available via subpath imports:
|
|
10
|
-
// - import { ... } from 'react-native-sherpa-onnx/stt'
|
|
11
|
-
// - import { ... } from 'react-native-sherpa-onnx/tts'
|
|
10
|
+
// - import { createSTT, createStreamingSTT, ... } from 'react-native-sherpa-onnx/stt'
|
|
11
|
+
// - import { createTTS, ... } from 'react-native-sherpa-onnx/tts'
|
|
12
12
|
// - import { ... } from 'react-native-sherpa-onnx/download'
|
|
13
13
|
// - import { ... } from 'react-native-sherpa-onnx/vad' (planned)
|
|
14
14
|
// - import { ... } from 'react-native-sherpa-onnx/diarization' (planned)
|
package/lib/module/stt/index.js
CHANGED
|
@@ -142,6 +142,10 @@ export async function createSTT(options) {
|
|
|
142
142
|
return engine;
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
+
// Streaming (online) STT
|
|
146
|
+
export { createStreamingSTT, mapDetectedToOnlineType, getOnlineTypeOrNull } from "./streaming.js";
|
|
147
|
+
export { ONLINE_STT_MODEL_TYPES } from "./streamingTypes.js";
|
|
148
|
+
|
|
145
149
|
// Export types and runtime type list
|
|
146
150
|
|
|
147
151
|
export { STT_MODEL_TYPES, STT_HOTWORDS_MODEL_TYPES, sttSupportsHotwords } from "./types.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["SherpaOnnx","resolveModelPath","sttInstanceCounter","normalizeSttResult","raw","text","tokens","Array","isArray","timestamps","lang","emotion","event","durations","detectSttModel","modelPath","options","resolvedPath","preferInt8","modelType","createSTT","instanceId","hotwordsFile","hotwordsScore","numThreads","provider","ruleFsts","ruleFars","dither","modelOptions","modelingUnit","bpeVocab","undefined","debug","result","initializeStt","success","Error","JSON","stringify","detectedModels","destroyed","guard","engine","transcribeFile","filePath","transcribeSamples","samples","sampleRate","setConfig","config","map","decodingMethod","maxActivePaths","blankPenalty","setSttConfig","destroy","unloadStt","STT_MODEL_TYPES","STT_HOTWORDS_MODEL_TYPES","sttSupportsHotwords","getWhisperLanguages","WHISPER_LANGUAGES","getSenseVoiceLanguages","SENSEVOICE_LANGUAGES","getCanaryLanguages","CANARY_LANGUAGES","getFunasrNanoLanguages","FUNASR_NANO_LANGUAGES","getFunasrMltNanoLanguages","FUNASR_MLT_NANO_LANGUAGES"],"sourceRoot":"../../../src","sources":["stt/index.ts"],"mappings":";;AAAA,OAAOA,UAAU,MAAM,wBAAqB;AAU5C,SAASC,gBAAgB,QAAQ,aAAU;AAE3C,IAAIC,kBAAkB,GAAG,CAAC;AAE1B,SAASC,kBAAkBA,CAACC,GAQ3B,EAAwB;EACvB,OAAO;IACLC,IAAI,EAAE,OAAOD,GAAG,CAACC,IAAI,KAAK,QAAQ,GAAGD,GAAG,CAACC,IAAI,GAAG,EAAE;IAClDC,MAAM,EAAEC,KAAK,CAACC,OAAO,CAACJ,GAAG,CAACE,MAAM,CAAC,GAAIF,GAAG,CAACE,MAAM,GAAgB,EAAE;IACjEG,UAAU,EAAEF,KAAK,CAACC,OAAO,CAACJ,GAAG,CAACK,UAAU,CAAC,GACpCL,GAAG,CAACK,UAAU,GACf,EAAE;IACNC,IAAI,EAAE,OAAON,GAAG,CAACM,IAAI,KAAK,QAAQ,GAAGN,GAAG,CAACM,IAAI,GAAG,EAAE;IAClDC,OAAO,EAAE,OAAOP,GAAG,CAACO,OAAO,KAAK,QAAQ,GAAGP,GAAG,CAACO,OAAO,GAAG,EAAE;IAC3DC,KAAK,EAAE,OAAOR,GAAG,CAACQ,KAAK,KAAK,QAAQ,GAAGR,GAAG,CAACQ,KAAK,GAAG,EAAE;IACrDC,SAAS,EAAEN,KAAK,CAACC,OAAO,CAACJ,GAAG,CAACS,SAAS,CAAC,GAAIT,GAAG,CAACS,SAAS,GAAgB;EAC1E,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,eAAeC,cAAcA,CAClCC,SAA0B,EAC1BC,OAA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CAAC;IACjD,CAAC;IAED,MAAMK,OAAOA,CAAA,EAAkB;MAC7B,IAAIf,SAAS,EAAE;MACfA,SAAS,GAAG,IAAI;MAChB,MAAMzC,UAAU,CAACyD,SAAS,CAACpC,UAAU,CAAC;IACxC;EACF,CAAC;EAED,OAAOsB,MAAM;AACf;;AAEA;;AAUA,
|
|
1
|
+
{"version":3,"names":["SherpaOnnx","resolveModelPath","sttInstanceCounter","normalizeSttResult","raw","text","tokens","Array","isArray","timestamps","lang","emotion","event","durations","detectSttModel","modelPath","options","resolvedPath","preferInt8","modelType","createSTT","instanceId","hotwordsFile","hotwordsScore","numThreads","provider","ruleFsts","ruleFars","dither","modelOptions","modelingUnit","bpeVocab","undefined","debug","result","initializeStt","success","Error","JSON","stringify","detectedModels","destroyed","guard","engine","transcribeFile","filePath","transcribeSamples","samples","sampleRate","setConfig","config","map","decodingMethod","maxActivePaths","blankPenalty","setSttConfig","destroy","unloadStt","createStreamingSTT","mapDetectedToOnlineType","getOnlineTypeOrNull","ONLINE_STT_MODEL_TYPES","STT_MODEL_TYPES","STT_HOTWORDS_MODEL_TYPES","sttSupportsHotwords","getWhisperLanguages","WHISPER_LANGUAGES","getSenseVoiceLanguages","SENSEVOICE_LANGUAGES","getCanaryLanguages","CANARY_LANGUAGES","getFunasrNanoLanguages","FUNASR_NANO_LANGUAGES","getFunasrMltNanoLanguages","FUNASR_MLT_NANO_LANGUAGES"],"sourceRoot":"../../../src","sources":["stt/index.ts"],"mappings":";;AAAA,OAAOA,UAAU,MAAM,wBAAqB;AAU5C,SAASC,gBAAgB,QAAQ,aAAU;AAE3C,IAAIC,kBAAkB,GAAG,CAAC;AAE1B,SAASC,kBAAkBA,CAACC,GAQ3B,EAAwB;EACvB,OAAO;IACLC,IAAI,EAAE,OAAOD,GAAG,CAACC,IAAI,KAAK,QAAQ,GAAGD,GAAG,CAACC,IAAI,GAAG,EAAE;IAClDC,MAAM,EAAEC,KAAK,CAACC,OAAO,CAACJ,GAAG,CAACE,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wBAAqB","ignoreList":[]}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import SherpaOnnx from "../NativeSherpaOnnx.js";
|
|
4
|
+
import { resolveModelPath } from "../utils.js";
|
|
5
|
+
let streamingSttInstanceCounter = 0;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Map detected STT model type (from detectSttModel) to an online (streaming) model type.
|
|
9
|
+
* Throws if the detected type has no streaming support.
|
|
10
|
+
*/
|
|
11
|
+
export function mapDetectedToOnlineType(detectedType) {
|
|
12
|
+
const t = detectedType ?? '';
|
|
13
|
+
switch (t) {
|
|
14
|
+
case 'transducer':
|
|
15
|
+
return 'transducer';
|
|
16
|
+
case 'paraformer':
|
|
17
|
+
return 'paraformer';
|
|
18
|
+
case 'nemo_ctc':
|
|
19
|
+
return 'nemo_ctc';
|
|
20
|
+
case 'zipformer_ctc':
|
|
21
|
+
case 'ctc':
|
|
22
|
+
return 'zipformer2_ctc';
|
|
23
|
+
case 'tone_ctc':
|
|
24
|
+
return 'tone_ctc';
|
|
25
|
+
default:
|
|
26
|
+
throw new Error(`Model type "${t}" is not supported for streaming STT. Use createSTT() for offline recognition, or pass a supported modelType: transducer, paraformer, zipformer2_ctc, nemo_ctc, tone_ctc.`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the online (streaming) model type for a detected STT model type, or null if streaming is not supported.
|
|
32
|
+
* Use this to check whether the current model can be used with createStreamingSTT() (e.g. for live transcription).
|
|
33
|
+
*/
|
|
34
|
+
export function getOnlineTypeOrNull(detectedType) {
|
|
35
|
+
try {
|
|
36
|
+
return mapDetectedToOnlineType(detectedType);
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
let sttStreamCounter = 0;
|
|
42
|
+
function normalizeStreamingResult(raw) {
|
|
43
|
+
return {
|
|
44
|
+
text: typeof raw.text === 'string' ? raw.text : '',
|
|
45
|
+
tokens: Array.isArray(raw.tokens) ? raw.tokens : [],
|
|
46
|
+
timestamps: Array.isArray(raw.timestamps) ? raw.timestamps : []
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Flatten StreamingSttInitOptions to native initializeOnlineStt parameters.
|
|
52
|
+
* EndpointConfig (rule1, rule2, rule3) is expanded to 9 flat params.
|
|
53
|
+
*/
|
|
54
|
+
function flattenInitOptionsForNative(options) {
|
|
55
|
+
const ep = options.endpointConfig;
|
|
56
|
+
return {
|
|
57
|
+
modelDir: '',
|
|
58
|
+
// filled by caller after resolveModelPath
|
|
59
|
+
modelType: options.modelType,
|
|
60
|
+
enableEndpoint: options.enableEndpoint ?? true,
|
|
61
|
+
decodingMethod: options.decodingMethod ?? 'greedy_search',
|
|
62
|
+
maxActivePaths: options.maxActivePaths ?? 4,
|
|
63
|
+
hotwordsFile: options.hotwordsFile,
|
|
64
|
+
hotwordsScore: options.hotwordsScore,
|
|
65
|
+
numThreads: options.numThreads,
|
|
66
|
+
provider: options.provider,
|
|
67
|
+
ruleFsts: options.ruleFsts,
|
|
68
|
+
ruleFars: options.ruleFars,
|
|
69
|
+
blankPenalty: options.blankPenalty,
|
|
70
|
+
debug: options.debug,
|
|
71
|
+
rule1MustContainNonSilence: ep?.rule1?.mustContainNonSilence,
|
|
72
|
+
rule1MinTrailingSilence: ep?.rule1?.minTrailingSilence,
|
|
73
|
+
rule1MinUtteranceLength: ep?.rule1?.minUtteranceLength,
|
|
74
|
+
rule2MustContainNonSilence: ep?.rule2?.mustContainNonSilence,
|
|
75
|
+
rule2MinTrailingSilence: ep?.rule2?.minTrailingSilence,
|
|
76
|
+
rule2MinUtteranceLength: ep?.rule2?.minUtteranceLength,
|
|
77
|
+
rule3MustContainNonSilence: ep?.rule3?.mustContainNonSilence,
|
|
78
|
+
rule3MinTrailingSilence: ep?.rule3?.minTrailingSilence,
|
|
79
|
+
rule3MinUtteranceLength: ep?.rule3?.minUtteranceLength
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Create a streaming (online) STT engine. Use this for real-time recognition with
|
|
85
|
+
* partial results and endpoint detection. Call destroy() when done.
|
|
86
|
+
*
|
|
87
|
+
* @param options - Streaming STT init options (modelPath required; modelType optional, use 'auto' to detect from directory)
|
|
88
|
+
* @returns Promise resolving to a StreamingSttEngine
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* // With explicit model type
|
|
92
|
+
* const engine = await createStreamingSTT({
|
|
93
|
+
* modelPath: { type: 'asset', path: 'models/streaming-zipformer-en' },
|
|
94
|
+
* modelType: 'transducer',
|
|
95
|
+
* });
|
|
96
|
+
* // With auto-detection
|
|
97
|
+
* const engine = await createStreamingSTT({
|
|
98
|
+
* modelPath: { type: 'asset', path: 'models/sherpa-onnx-streaming-t-one-russian-2025-09-08' },
|
|
99
|
+
* modelType: 'auto',
|
|
100
|
+
* });
|
|
101
|
+
* const stream = await engine.createStream();
|
|
102
|
+
* await stream.acceptWaveform(samples, 16000);
|
|
103
|
+
* if (await stream.isReady()) {
|
|
104
|
+
* await stream.decode();
|
|
105
|
+
* const result = await stream.getResult();
|
|
106
|
+
* console.log(result.text);
|
|
107
|
+
* }
|
|
108
|
+
* await stream.release();
|
|
109
|
+
* await engine.destroy();
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export async function createStreamingSTT(options) {
|
|
113
|
+
const instanceId = `streaming_stt_${++streamingSttInstanceCounter}`;
|
|
114
|
+
const resolvedPath = await resolveModelPath(options.modelPath);
|
|
115
|
+
let effectiveModelType;
|
|
116
|
+
if (options.modelType === 'auto' || options.modelType === undefined) {
|
|
117
|
+
const detectResult = await SherpaOnnx.detectSttModel(resolvedPath, undefined, undefined);
|
|
118
|
+
if (!detectResult.success) {
|
|
119
|
+
const errMsg = 'error' in detectResult && typeof detectResult.error === 'string' ? detectResult.error : 'Unknown error';
|
|
120
|
+
throw new Error(`Streaming STT auto-detection failed for ${resolvedPath}. ${errMsg}`);
|
|
121
|
+
}
|
|
122
|
+
effectiveModelType = mapDetectedToOnlineType(detectResult.modelType);
|
|
123
|
+
} else {
|
|
124
|
+
effectiveModelType = options.modelType;
|
|
125
|
+
}
|
|
126
|
+
const optionsWithResolvedType = {
|
|
127
|
+
...options,
|
|
128
|
+
modelType: effectiveModelType
|
|
129
|
+
};
|
|
130
|
+
const flat = flattenInitOptionsForNative(optionsWithResolvedType);
|
|
131
|
+
flat.modelDir = resolvedPath;
|
|
132
|
+
|
|
133
|
+
// Build options with only defined values (no undefined) to avoid iOS TurboModule marshalling crash when options contain undefined.
|
|
134
|
+
const nativeOptions = {
|
|
135
|
+
modelDir: flat.modelDir,
|
|
136
|
+
modelType: flat.modelType,
|
|
137
|
+
enableEndpoint: flat.enableEndpoint,
|
|
138
|
+
decodingMethod: flat.decodingMethod,
|
|
139
|
+
maxActivePaths: flat.maxActivePaths
|
|
140
|
+
};
|
|
141
|
+
if (flat.hotwordsFile !== undefined) nativeOptions.hotwordsFile = flat.hotwordsFile;
|
|
142
|
+
if (flat.hotwordsScore !== undefined) nativeOptions.hotwordsScore = flat.hotwordsScore;
|
|
143
|
+
if (flat.numThreads !== undefined) nativeOptions.numThreads = flat.numThreads;
|
|
144
|
+
if (flat.provider !== undefined) nativeOptions.provider = flat.provider;
|
|
145
|
+
if (flat.ruleFsts !== undefined) nativeOptions.ruleFsts = flat.ruleFsts;
|
|
146
|
+
if (flat.ruleFars !== undefined) nativeOptions.ruleFars = flat.ruleFars;
|
|
147
|
+
if (flat.blankPenalty !== undefined) nativeOptions.blankPenalty = flat.blankPenalty;
|
|
148
|
+
if (flat.debug !== undefined) nativeOptions.debug = flat.debug;
|
|
149
|
+
if (flat.rule1MustContainNonSilence !== undefined) nativeOptions.rule1MustContainNonSilence = flat.rule1MustContainNonSilence;
|
|
150
|
+
if (flat.rule1MinTrailingSilence !== undefined) nativeOptions.rule1MinTrailingSilence = flat.rule1MinTrailingSilence;
|
|
151
|
+
if (flat.rule1MinUtteranceLength !== undefined) nativeOptions.rule1MinUtteranceLength = flat.rule1MinUtteranceLength;
|
|
152
|
+
if (flat.rule2MustContainNonSilence !== undefined) nativeOptions.rule2MustContainNonSilence = flat.rule2MustContainNonSilence;
|
|
153
|
+
if (flat.rule2MinTrailingSilence !== undefined) nativeOptions.rule2MinTrailingSilence = flat.rule2MinTrailingSilence;
|
|
154
|
+
if (flat.rule2MinUtteranceLength !== undefined) nativeOptions.rule2MinUtteranceLength = flat.rule2MinUtteranceLength;
|
|
155
|
+
if (flat.rule3MustContainNonSilence !== undefined) nativeOptions.rule3MustContainNonSilence = flat.rule3MustContainNonSilence;
|
|
156
|
+
if (flat.rule3MinTrailingSilence !== undefined) nativeOptions.rule3MinTrailingSilence = flat.rule3MinTrailingSilence;
|
|
157
|
+
if (flat.rule3MinUtteranceLength !== undefined) nativeOptions.rule3MinUtteranceLength = flat.rule3MinUtteranceLength;
|
|
158
|
+
const result = await SherpaOnnx.initializeOnlineSttWithOptions(instanceId, nativeOptions);
|
|
159
|
+
if (!result.success) {
|
|
160
|
+
throw new Error(`Streaming STT initialization failed for ${instanceId}`);
|
|
161
|
+
}
|
|
162
|
+
const enableInputNormalization = options.enableInputNormalization !== false;
|
|
163
|
+
let destroyed = false;
|
|
164
|
+
const guard = () => {
|
|
165
|
+
if (destroyed) {
|
|
166
|
+
throw new Error(`Streaming STT engine ${instanceId} has been destroyed; cannot call methods on it.`);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
const engine = {
|
|
170
|
+
get instanceId() {
|
|
171
|
+
return instanceId;
|
|
172
|
+
},
|
|
173
|
+
async createStream(hotwords) {
|
|
174
|
+
guard();
|
|
175
|
+
const streamId = `stt_stream_${++sttStreamCounter}`;
|
|
176
|
+
await SherpaOnnx.createSttStream(instanceId, streamId, hotwords);
|
|
177
|
+
let released = false;
|
|
178
|
+
const streamGuard = () => {
|
|
179
|
+
if (destroyed) {
|
|
180
|
+
throw new Error(`Streaming STT engine ${instanceId} has been destroyed.`);
|
|
181
|
+
}
|
|
182
|
+
if (released) {
|
|
183
|
+
throw new Error(`Stream ${streamId} has been released; cannot call methods on it.`);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
const stream = {
|
|
187
|
+
get streamId() {
|
|
188
|
+
return streamId;
|
|
189
|
+
},
|
|
190
|
+
async acceptWaveform(samples, sampleRate) {
|
|
191
|
+
streamGuard();
|
|
192
|
+
await SherpaOnnx.acceptSttWaveform(streamId, samples, sampleRate);
|
|
193
|
+
},
|
|
194
|
+
async inputFinished() {
|
|
195
|
+
streamGuard();
|
|
196
|
+
await SherpaOnnx.sttStreamInputFinished(streamId);
|
|
197
|
+
},
|
|
198
|
+
async decode() {
|
|
199
|
+
streamGuard();
|
|
200
|
+
await SherpaOnnx.decodeSttStream(streamId);
|
|
201
|
+
},
|
|
202
|
+
async isReady() {
|
|
203
|
+
streamGuard();
|
|
204
|
+
return SherpaOnnx.isSttStreamReady(streamId);
|
|
205
|
+
},
|
|
206
|
+
async getResult() {
|
|
207
|
+
streamGuard();
|
|
208
|
+
const raw = await SherpaOnnx.getSttStreamResult(streamId);
|
|
209
|
+
return normalizeStreamingResult(raw);
|
|
210
|
+
},
|
|
211
|
+
async isEndpoint() {
|
|
212
|
+
streamGuard();
|
|
213
|
+
return SherpaOnnx.isSttStreamEndpoint(streamId);
|
|
214
|
+
},
|
|
215
|
+
async reset() {
|
|
216
|
+
streamGuard();
|
|
217
|
+
await SherpaOnnx.resetSttStream(streamId);
|
|
218
|
+
},
|
|
219
|
+
async release() {
|
|
220
|
+
if (released) return;
|
|
221
|
+
released = true;
|
|
222
|
+
await SherpaOnnx.releaseSttStream(streamId);
|
|
223
|
+
},
|
|
224
|
+
async processAudioChunk(samples, sampleRate) {
|
|
225
|
+
streamGuard();
|
|
226
|
+
let toSend = samples;
|
|
227
|
+
if (enableInputNormalization && samples.length > 0) {
|
|
228
|
+
let maxAbs = 1e-10;
|
|
229
|
+
for (let i = 0; i < samples.length; i++) {
|
|
230
|
+
const abs = Math.abs(samples[i]);
|
|
231
|
+
if (abs > maxAbs) maxAbs = abs;
|
|
232
|
+
}
|
|
233
|
+
const scale = maxAbs < 0.01 ? 80 : Math.min(80, 0.8 / maxAbs);
|
|
234
|
+
toSend = new Array(samples.length);
|
|
235
|
+
for (let i = 0; i < samples.length; i++) {
|
|
236
|
+
const v = samples[i] * scale;
|
|
237
|
+
toSend[i] = v < -1 ? -1 : v > 1 ? 1 : v;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const raw = await SherpaOnnx.processSttAudioChunk(streamId, toSend, sampleRate);
|
|
241
|
+
return {
|
|
242
|
+
result: normalizeStreamingResult(raw),
|
|
243
|
+
isEndpoint: Boolean(raw.isEndpoint)
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
return stream;
|
|
248
|
+
},
|
|
249
|
+
async destroy() {
|
|
250
|
+
if (destroyed) return;
|
|
251
|
+
destroyed = true;
|
|
252
|
+
await SherpaOnnx.unloadOnlineStt(instanceId);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
return engine;
|
|
256
|
+
}
|
|
257
|
+
//# sourceMappingURL=streaming.js.map
|