Skip to content

Commit

Permalink
SherpaOnnxVadAsr: Offload runSecondPass to background thread for impr…
Browse files Browse the repository at this point in the history
…oved real-time audio processing (#1638)

This change ensures that the main audio processing loop is not blocked by
long-running operations in `runSecondPass`, improving responsiveness and
reducing the risk of missing parts of input speech.
  • Loading branch information
rominf authored Dec 24, 2024
1 parent a3d6313 commit 6613828
Showing 1 changed file with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import com.k2fsa.sherpa.onnx.Vad
import com.k2fsa.sherpa.onnx.getFeatureConfig
import com.k2fsa.sherpa.onnx.getOfflineModelConfig
import com.k2fsa.sherpa.onnx.getVadModelConfig
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.concurrent.thread


Expand Down Expand Up @@ -166,6 +171,8 @@ class MainActivity : AppCompatActivity() {

val bufferSize = 512 // in samples
val buffer = ShortArray(bufferSize)
val coroutineScope = CoroutineScope(Dispatchers.IO)


while (isRecording) {
val ret = audioRecord?.read(buffer, 0, buffer.size)
Expand All @@ -175,11 +182,15 @@ class MainActivity : AppCompatActivity() {
vad.acceptWaveform(samples)
while(!vad.empty()) {
var segment = vad.front()
val text = runSecondPass(segment.samples)

if (text.isNotBlank()) {
lastText = "${lastText}\n${idx}: ${text}"
idx += 1
coroutineScope.launch {
val text = runSecondPass(segment.samples)
if (text.isNotBlank()) {
withContext(Dispatchers.Main) {
lastText = "${lastText}\n${idx}: ${text}"
idx += 1
textView.text = lastText.lowercase()
}
}
}

vad.pop();
Expand All @@ -192,6 +203,9 @@ class MainActivity : AppCompatActivity() {
}
}
}

// Clean up the coroutine scope when done
coroutineScope.cancel()
}

private fun initOfflineRecognizer() {
Expand Down

0 comments on commit 6613828

Please sign in to comment.