@@ -12,14 +12,20 @@ namespace rnexecutorch::models::speech_to_text {
1212SpeechToText::SpeechToText (const std::string &modelName,
1313 const std::string &modelSource,
1414 const std::string &tokenizerSource,
15+ const std::string &vadSource,
1516 std::shared_ptr<react::CallInvoker> callInvoker)
1617 : callInvoker_(std::move(callInvoker)) {
1718 // Switch between the ASR implementations based on model name
1819 if (modelName == " whisper" ) {
20+ if (!vadSource.empty ()) {
21+ vad_ = std::make_unique<VoiceActivityDetection>(vadSource, callInvoker_);
22+ }
23+
1924 transcriber_ = std::make_unique<whisper::ASR >(modelSource, tokenizerSource,
2025 callInvoker_);
2126 streamer_ = std::make_unique<whisper::stream::OnlineASR>(
22- static_cast <const whisper::ASR *>(transcriber_.get ()));
27+ static_cast <const whisper::ASR *>(transcriber_.get ()),
28+ static_cast <const VoiceActivityDetection *>(vad_.get ()));
2329 } else {
2430 throw rnexecutorch::RnExecutorchError (
2531 rnexecutorch::RnExecutorchErrorCode::InvalidConfig,
@@ -116,12 +122,19 @@ TranscriptionResult wordsToResult(const std::vector<Word> &words,
116122
117123void SpeechToText::stream (std::shared_ptr<jsi::Function> callback,
118124 std::string languageOption, bool verbose,
119- uint32_t timeout) {
125+ uint32_t timeout, bool useVAD,
126+ uint32_t vadDetectionMargin) {
120127 if (isStreaming_) {
121128 throw RnExecutorchError (RnExecutorchErrorCode::StreamingInProgress,
122129 " Streaming is already in progress!" );
123130 }
124131
132+ // VAD must be initialized for this option to work.
133+ if (useVAD && !vad_) {
134+ throw RnExecutorchError (RnExecutorchErrorCode::ModuleNotLoaded,
135+ " Attempting to use VAD but it's not initialized!" );
136+ }
137+
125138 auto nativeCallback =
126139 [this , callback](const TranscriptionResult &committed,
127140 const TranscriptionResult &nonCommitted, bool isDone) {
@@ -139,18 +152,24 @@ void SpeechToText::stream(std::shared_ptr<jsi::Function> callback,
139152 };
140153
141154 isStreaming_ = true ;
142- DecodingOptions options (languageOption, verbose);
155+ StreamingOptions options (languageOption, verbose, useVAD, vadDetectionMargin );
143156
144157 while (isStreaming_) {
145158 if (readyToProcess_ && streamer_->isReady ()) {
146159 ProcessResult res = streamer_->process (options);
147160
148- TranscriptionResult committedRes =
149- wordsToResult (res.committed , languageOption, verbose);
150- TranscriptionResult nonCommittedRes =
151- wordsToResult (res.nonCommitted , languageOption, verbose);
161+ // This is not only about optimization, but also about ensuring
162+ // correctness when VAD is used. Otherwise we run into the vanishing text
163+ // issue.
164+ if (!res.committed .empty () || !res.nonCommitted .empty ()) {
165+ TranscriptionResult committedRes =
166+ wordsToResult (res.committed , languageOption, verbose);
167+ TranscriptionResult nonCommittedRes =
168+ wordsToResult (res.nonCommitted , languageOption, verbose);
169+
170+ nativeCallback (committedRes, nonCommittedRes, false );
171+ }
152172
153- nativeCallback (committedRes, nonCommittedRes, false );
154173 readyToProcess_ = false ;
155174 }
156175
@@ -166,6 +185,9 @@ void SpeechToText::stream(std::shared_ptr<jsi::Function> callback,
166185 [this ] { return !isStreaming_.load (); });
167186 }
168187
188+ // Do not use VAD on final processing to flush and commit everything properly.
189+ options.useVAD = false ;
190+
169191 std::vector<Word> finalWords = streamer_->finish (options);
170192 TranscriptionResult finalRes =
171193 wordsToResult (finalWords, languageOption, verbose);
0 commit comments