Fix keyword spotting. (#1689)

Reset the stream right after detecting a keyword
This commit is contained in:
Fangjun Kuang
2025-01-20 16:41:10 +08:00
committed by GitHub
parent b943341fb1
commit 8b989a851c
43 changed files with 813 additions and 293 deletions

View File

@@ -151,24 +151,27 @@ class MainActivity : AppCompatActivity() {
stream.acceptWaveform(samples, sampleRate = sampleRateInHz)
while (kws.isReady(stream)) {
kws.decode(stream)
}
val text = kws.getResult(stream).keyword
val text = kws.getResult(stream).keyword
var textToDisplay = lastText
var textToDisplay = lastText
if (text.isNotBlank()) {
if (lastText.isBlank()) {
textToDisplay = "$idx: $text"
} else {
textToDisplay = "$idx: $text\n$lastText"
if (text.isNotBlank()) {
// Remember to reset the stream right after detecting a keyword
kws.reset(stream)
if (lastText.isBlank()) {
textToDisplay = "$idx: $text"
} else {
textToDisplay = "$idx: $text\n$lastText"
}
lastText = "$idx: $text\n$lastText"
idx += 1
}
lastText = "$idx: $text\n$lastText"
idx += 1
}
runOnUiThread {
textView.text = textToDisplay
runOnUiThread {
textView.text = textToDisplay
}
}
}
}