@@ -24,6 +24,7 @@ OnlineASR::OnlineASR(const ASR *asr) : asr_(asr) {
2424}
2525
2626void OnlineASR::insertAudioChunk (std::span<const float > audio) {
27+ std::lock_guard<std::mutex> lock (audioBufferMutex_);
2728 audioBuffer_.insert (audioBuffer_.end (), audio.begin (), audio.end ());
2829}
2930
@@ -32,7 +33,10 @@ bool OnlineASR::isReady() const {
3233}
3334
3435ProcessResult OnlineASR::process (const DecodingOptions &options) {
36+ std::unique_lock<std::mutex> lock (audioBufferMutex_);
37+
3538 std::vector<Segment> transcriptions = asr_->transcribe (audioBuffer_, options);
39+ lock.unlock ();
3640
3741 if (transcriptions.empty ()) {
3842 return {.committed = {}, .nonCommitted = {}};
@@ -57,9 +61,7 @@ ProcessResult OnlineASR::process(const DecodingOptions &options) {
5761 const float newEnd = hypothesisBuffer_.fresh_ .back ().end ;
5862 float shift = 0 .F ;
5963 for (size_t i = 0 ; i < hypothesisBuffer_.fresh_ .size (); i++) {
60- const float originalStart = hypothesisBuffer_.fresh_ [i].start ;
6164 const float originalEnd = hypothesisBuffer_.fresh_ [i].end ;
62- const std::string &wordContent = hypothesisBuffer_.fresh_ [i].content ;
6365
6466 if (i < hypothesisBuffer_.hypothesis_ .size () &&
6567 utils::equalsIgnoreCase (hypothesisBuffer_.fresh_ [i].content ,
@@ -104,6 +106,7 @@ ProcessResult OnlineASR::process(const DecodingOptions &options) {
104106
105107 // Since Whisper does not accept waveforms longer than 30 seconds, we need
106108 // to cut the audio at some safe point.
109+ lock.lock ();
107110 const float audioDuration =
108111 static_cast <float >(audioBuffer_.size ()) / constants::kSamplingRate ;
109112 if (audioDuration > params::kStreamChunkThreshold ) {
@@ -126,6 +129,7 @@ ProcessResult OnlineASR::process(const DecodingOptions &options) {
126129 audioBuffer_.begin () + nSamplesToErase);
127130 bufferTimeOffset_ += eraseDuration;
128131 }
132+ lock.unlock ();
129133
130134 return {.committed = move_to_vector (committed),
131135 .nonCommitted = move_to_vector (nonCommitted)};
@@ -140,6 +144,8 @@ std::vector<Word> OnlineASR::finish() {
140144}
141145
142146void OnlineASR::reset () {
147+ std::lock_guard<std::mutex> lock (audioBufferMutex_);
148+
143149 hypothesisBuffer_.reset ();
144150 bufferTimeOffset_ = 0 .f ;
145151
0 commit comments