From 9e96f0f027f4feead66fd40ad70e0fc991de0d06 Mon Sep 17 00:00:00 2001 From: Terrence Date: Wed, 4 Feb 2026 12:28:59 +0800 Subject: [PATCH] Refactor listening mode handling and wake word detection configuration - Replace direct mode setting logic with a new GetDefaultListeningMode method for improved clarity and maintainability. - Update HandleToggleChatEvent, HandleWakeWordDetectedEvent, and ContinueWakeWordInvoke to utilize the new method for determining listening mode. - Introduce Kconfig option WAKE_WORD_DETECTION_IN_LISTENING to enable or disable wake word detection during listening mode, enhancing configurability. --- main/Kconfig.projbuild | 10 ++++++++++ main/application.cc | 27 ++++++++++++++++----------- main/application.h | 1 + 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 47b23553..79995d3e 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -680,6 +680,16 @@ config SEND_WAKE_WORD_DATA help Send wake word data to the server as the first message of the conversation and wait for response +config WAKE_WORD_DETECTION_IN_LISTENING + bool "Enable Wake Word Detection in Listening Mode" + default n + depends on USE_AFE_WAKE_WORD || USE_CUSTOM_WAKE_WORD + help + Enable wake word detection while in listening mode. + When enabled, the device can detect wake word during listening, + which allows interrupting the current conversation. + When disabled (default), wake word detection is turned off during listening. + config USE_AUDIO_PROCESSOR bool "Enable Audio Noise Reduction" default y diff --git a/main/application.cc b/main/application.cc index 6106b4e0..9d544c70 100644 --- a/main/application.cc +++ b/main/application.cc @@ -691,7 +691,7 @@ void Application::HandleToggleChatEvent() { } if (state == kDeviceStateIdle) { - ListeningMode mode = aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime; + ListeningMode mode = GetDefaultListeningMode(); if (!protocol_->IsAudioChannelOpened()) { SetDeviceState(kDeviceStateConnecting); // Schedule to let the state change be processed first (UI update) @@ -801,8 +801,7 @@ void Application::HandleWakeWordDetectedEvent() { while (audio_service_.PopPacketFromSendQueue()); if (state == kDeviceStateListening) { - auto mode = aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime; - protocol_->SendStartListening(mode); + protocol_->SendStartListening(GetDefaultListeningMode()); audio_service_.ResetDecoder(); audio_service_.PlaySound(Lang::Sounds::OGG_POPUP); // Re-enable wake word detection as it was stopped by the detection itself @@ -810,7 +809,7 @@ void Application::HandleWakeWordDetectedEvent() { } else { // Play popup sound and start listening again play_popup_on_listening_ = true; - SetListeningMode(aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime); + SetListeningMode(GetDefaultListeningMode()); } } else if (state == kDeviceStateActivating) { // Restart the activation check if the wake word is detected during activation @@ -842,12 +841,12 @@ void Application::ContinueWakeWordInvoke(const std::string& wake_word) { // Set flag to play popup sound after state changes to listening play_popup_on_listening_ = true; - SetListeningMode(aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime); + SetListeningMode(GetDefaultListeningMode()); #else // Set flag to play popup sound after state changes to listening // (PlaySound here would be cleared by ResetDecoder in EnableVoiceProcessing) play_popup_on_listening_ = true; - SetListeningMode(aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime); + SetListeningMode(GetDefaultListeningMode()); #endif } @@ -891,11 +890,13 @@ void Application::HandleStateChangedEvent() { audio_service_.EnableVoiceProcessing(true); } - // TODO: Should use a Kconfig option to enable/disable wake word detection in listening mode - if (true) { - // Always ensure wake word detection state in listening - audio_service_.EnableWakeWordDetection(audio_service_.IsAfeWakeWord()); - } +#ifdef CONFIG_WAKE_WORD_DETECTION_IN_LISTENING + // Enable wake word detection in listening mode (configured via Kconfig) + audio_service_.EnableWakeWordDetection(audio_service_.IsAfeWakeWord()); +#else + // Disable wake word detection in listening mode + audio_service_.EnableWakeWordDetection(false); +#endif // Play popup sound after ResetDecoder (in EnableVoiceProcessing) has been called if (play_popup_on_listening_) { @@ -944,6 +945,10 @@ void Application::SetListeningMode(ListeningMode mode) { SetDeviceState(kDeviceStateListening); } +ListeningMode Application::GetDefaultListeningMode() const { + return aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime; +} + void Application::Reboot() { ESP_LOGI(TAG, "Rebooting..."); // Disconnect the audio channel diff --git a/main/application.h b/main/application.h index bcb81112..7ca7af4f 100644 --- a/main/application.h +++ b/main/application.h @@ -165,6 +165,7 @@ private: void InitializeProtocol(); void ShowActivationCode(const std::string& code, const std::string& message); void SetListeningMode(ListeningMode mode); + ListeningMode GetDefaultListeningMode() const; // State change handler called by state machine void OnStateChanged(DeviceState old_state, DeviceState new_state);