优化后的代码

This commit is contained in:
林若思 2026-01-03 10:24:24 +08:00
parent 956dd78c1b
commit 0261c5ccaa

View File

@ -110,6 +110,7 @@ class VoiceController(
VoiceState.WAIT_SPEECH_COOLDOWN -> {
if (now >= speechEnableAtMs) {
waitSpeechFailStartMs = 0L // ⭐ 必须清
state = VoiceState.WAIT_SPEECH
waitSpeechStartMs = now // ⭐ 关键:开始等人说话
}
@ -171,12 +172,8 @@ class VoiceController(
VoiceState.UPLOADING -> return
// ⭐ 关键:只要不是纯等待唤醒,一律打断
VoiceState.RECORDING,
VoiceState.WAIT_SPEECH,
VoiceState.WAIT_SPEECH_COOLDOWN,
VoiceState.PLAYING_BACKEND -> {
Log.d(TAG, "⚠ WakeWord interrupt state=$state")
stopBackendAudio?.invoke()
enterWakeup(interrupt = true)
}
@ -185,21 +182,15 @@ class VoiceController(
}
}
private fun enterWakeup(interrupt: Boolean) {
waitSpeechFailStartMs = 0L // ⭐ 唤醒即新会话
waitSpeechStartMs = 0L
if (interrupt) {
Log.d(TAG, "🛑 Interrupt current speech / recording")
audioBuffer.clear()
preBuffer.clear() // ⭐ 防止把旧唤醒词带进去
vadManager.reset()
resetEnergyStat()
vadStarted = false
silenceStartMs = 0L
waitSpeechStartMs = 0L // ⭐
waitSpeechFailStartMs = 0L // ⭐
}
inKwsObserve = true
@ -209,7 +200,6 @@ class VoiceController(
onWakeup()
}
/* ================= VAD START ================= */
private fun onVadStart() {
@ -220,6 +210,7 @@ class VoiceController(
vadStarted = true
recordingStartMs = System.currentTimeMillis()
silenceStartMs = 0L
waitSpeechFailStartMs = 0L // ⭐ 新一轮有效说话
waitSpeechStartMs = 0L // ⭐ 清掉“等待说话”超时
resetEnergyStat()