SherpaOnnxVadAsr: Offload runSecondPass to background thread for improved 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.
This commit is contained in:
committed by
GitHub
parent
a3d63130ba
commit
6613828d86
@@ -19,6 +19,11 @@ import com.k2fsa.sherpa.onnx.Vad
|
|||||||
import com.k2fsa.sherpa.onnx.getFeatureConfig
|
import com.k2fsa.sherpa.onnx.getFeatureConfig
|
||||||
import com.k2fsa.sherpa.onnx.getOfflineModelConfig
|
import com.k2fsa.sherpa.onnx.getOfflineModelConfig
|
||||||
import com.k2fsa.sherpa.onnx.getVadModelConfig
|
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
|
import kotlin.concurrent.thread
|
||||||
|
|
||||||
|
|
||||||
@@ -166,6 +171,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
val bufferSize = 512 // in samples
|
val bufferSize = 512 // in samples
|
||||||
val buffer = ShortArray(bufferSize)
|
val buffer = ShortArray(bufferSize)
|
||||||
|
val coroutineScope = CoroutineScope(Dispatchers.IO)
|
||||||
|
|
||||||
|
|
||||||
while (isRecording) {
|
while (isRecording) {
|
||||||
val ret = audioRecord?.read(buffer, 0, buffer.size)
|
val ret = audioRecord?.read(buffer, 0, buffer.size)
|
||||||
@@ -175,11 +182,15 @@ class MainActivity : AppCompatActivity() {
|
|||||||
vad.acceptWaveform(samples)
|
vad.acceptWaveform(samples)
|
||||||
while(!vad.empty()) {
|
while(!vad.empty()) {
|
||||||
var segment = vad.front()
|
var segment = vad.front()
|
||||||
val text = runSecondPass(segment.samples)
|
coroutineScope.launch {
|
||||||
|
val text = runSecondPass(segment.samples)
|
||||||
if (text.isNotBlank()) {
|
if (text.isNotBlank()) {
|
||||||
lastText = "${lastText}\n${idx}: ${text}"
|
withContext(Dispatchers.Main) {
|
||||||
idx += 1
|
lastText = "${lastText}\n${idx}: ${text}"
|
||||||
|
idx += 1
|
||||||
|
textView.text = lastText.lowercase()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vad.pop();
|
vad.pop();
|
||||||
@@ -192,6 +203,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up the coroutine scope when done
|
||||||
|
coroutineScope.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initOfflineRecognizer() {
|
private fun initOfflineRecognizer() {
|
||||||
|
|||||||
Reference in New Issue
Block a user