Skip to content

Commit 8590cea

Browse files
committed
bug-fix: protect acquiring buffer by different workers
1 parent 22a84b7 commit 8590cea

4 files changed

Lines changed: 29 additions & 15 deletions

File tree

apps/speech/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"metro-config": "^0.81.0",
2020
"react": "19.1.0",
2121
"react-native": "0.81.5",
22-
"react-native-audio-api": "0.11.3",
22+
"react-native-audio-api": "0.11.1",
2323
"react-native-device-info": "^14.0.4",
2424
"react-native-executorch": "workspace:*",
2525
"react-native-reanimated": "~4.1.1",

packages/react-native-executorch/common/rnexecutorch/models/speech_to_text/stream/OnlineASRProcessor.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ using namespace types;
1010
OnlineASRProcessor::OnlineASRProcessor(const ASR *asr) : asr(asr) {}
1111

1212
void OnlineASRProcessor::insertAudioChunk(std::span<const float> audio) {
13+
std::scoped_lock<std::mutex> lock(audioMutex);
1314
audioBuffer.insert(audioBuffer.end(), audio.begin(), audio.end());
1415
}
1516

1617
ProcessResult OnlineASRProcessor::processIter(const DecodingOptions &options) {
17-
std::vector<Segment> res = asr->transcribe(audioBuffer, options);
18+
std::vector<float> snapshot;
19+
20+
{
21+
std::scoped_lock<std::mutex> lock(audioMutex);
22+
snapshot = audioBuffer;
23+
}
24+
25+
std::vector<Segment> res = asr->transcribe(snapshot, options);
1826

1927
std::vector<Word> tsw;
2028
for (const auto &segment : res) {
@@ -28,20 +36,19 @@ ProcessResult OnlineASRProcessor::processIter(const DecodingOptions &options) {
2836
this->committed.insert(this->committed.end(), flushed.begin(), flushed.end());
2937

3038
constexpr int32_t chunkThresholdSec = 15;
31-
if (static_cast<float>(audioBuffer.size()) /
32-
OnlineASRProcessor::kSamplingRate >
39+
if (static_cast<float>(snapshot.size()) / OnlineASRProcessor::kSamplingRate >
3340
chunkThresholdSec) {
3441
chunkCompletedSegment(res);
3542
}
3643

37-
auto move_to_vector = [](auto& container) {
38-
return std::vector<Word>(std::make_move_iterator(container.begin()),
39-
std::make_move_iterator(container.end()));
44+
auto move_to_vector = [](auto &container) {
45+
return std::vector<Word>(std::make_move_iterator(container.begin()),
46+
std::make_move_iterator(container.end()));
4047
};
4148

4249
std::deque<Word> nonCommittedWords = this->hypothesisBuffer.complete();
4350

44-
return { move_to_vector(flushed), move_to_vector(nonCommittedWords) };
51+
return {move_to_vector(flushed), move_to_vector(nonCommittedWords)};
4552
}
4653

4754
void OnlineASRProcessor::chunkCompletedSegment(std::span<const Segment> res) {
@@ -74,6 +81,7 @@ void OnlineASRProcessor::chunkAt(float time) {
7481
auto startIndex =
7582
static_cast<size_t>(cutSeconds * OnlineASRProcessor::kSamplingRate);
7683

84+
std::scoped_lock<std::mutex> lock(audioMutex);
7785
if (startIndex < audioBuffer.size()) {
7886
audioBuffer.erase(audioBuffer.begin(), audioBuffer.begin() + startIndex);
7987
} else {
@@ -88,8 +96,11 @@ std::vector<Word> OnlineASRProcessor::finish() {
8896
std::vector<Word> buffer(std::make_move_iterator(bufferDeq.begin()),
8997
std::make_move_iterator(bufferDeq.end()));
9098

91-
this->bufferTimeOffset += static_cast<float>(audioBuffer.size()) /
92-
OnlineASRProcessor::kSamplingRate;
99+
{
100+
std::scoped_lock<std::mutex> lock(audioMutex);
101+
this->bufferTimeOffset += static_cast<float>(audioBuffer.size()) /
102+
OnlineASRProcessor::kSamplingRate;
103+
}
93104
return buffer;
94105
}
95106

packages/react-native-executorch/common/rnexecutorch/models/speech_to_text/stream/OnlineASRProcessor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "rnexecutorch/models/speech_to_text/types/ProcessResult.h"
66
#include "rnexecutorch/models/speech_to_text/types/Word.h"
77

8+
#include <mutex>
9+
810
namespace rnexecutorch::models::speech_to_text::stream {
911

1012
class OnlineASRProcessor {
@@ -16,6 +18,7 @@ class OnlineASRProcessor {
1618
std::vector<types::Word> finish();
1719

1820
std::vector<float> audioBuffer;
21+
mutable std::mutex audioMutex;
1922

2023
private:
2124
const asr::ASR *asr;

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12847,17 +12847,17 @@ __metadata:
1284712847
languageName: node
1284812848
linkType: hard
1284912849

12850-
"react-native-audio-api@npm:0.11.3":
12851-
version: 0.11.3
12852-
resolution: "react-native-audio-api@npm:0.11.3"
12850+
"react-native-audio-api@npm:0.11.1":
12851+
version: 0.11.1
12852+
resolution: "react-native-audio-api@npm:0.11.1"
1285312853
dependencies:
1285412854
semver: "npm:^7.7.3"
1285512855
peerDependencies:
1285612856
react: "*"
1285712857
react-native: "*"
1285812858
bin:
1285912859
setup-rn-audio-api-web: scripts/setup-rn-audio-api-web.js
12860-
checksum: 10/0c973061a81196f93cb19eecf3c6126deaaaa28ad89c7830cdcfc833faad594fb275b733d12729aa11676e0d571c493006fdef4bf243a79dcb142666e87bcf0b
12860+
checksum: 10/3e062c479ec76015fe068af3b03928b591c11fc6873b030b7756ab6e6eb2a17499801d061002997121fbfb746040d7039266d81196c3ae05ee8b489b07a97047
1286112861
languageName: node
1286212862
linkType: hard
1286312863

@@ -14100,7 +14100,7 @@ __metadata:
1410014100
metro-config: "npm:^0.81.0"
1410114101
react: "npm:19.1.0"
1410214102
react-native: "npm:0.81.5"
14103-
react-native-audio-api: "npm:0.11.3"
14103+
react-native-audio-api: "npm:0.11.1"
1410414104
react-native-device-info: "npm:^14.0.4"
1410514105
react-native-executorch: "workspace:*"
1410614106
react-native-reanimated: "npm:~4.1.1"

0 commit comments

Comments
 (0)