mirror of
https://github.com/78/xiaozhi-esp32.git
synced 2026-02-27 14:26:36 +00:00
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.
This commit is contained in:
@@ -680,6 +680,16 @@ config SEND_WAKE_WORD_DATA
|
|||||||
help
|
help
|
||||||
Send wake word data to the server as the first message of the conversation and wait for response
|
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
|
config USE_AUDIO_PROCESSOR
|
||||||
bool "Enable Audio Noise Reduction"
|
bool "Enable Audio Noise Reduction"
|
||||||
default y
|
default y
|
||||||
|
|||||||
@@ -691,7 +691,7 @@ void Application::HandleToggleChatEvent() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (state == kDeviceStateIdle) {
|
if (state == kDeviceStateIdle) {
|
||||||
ListeningMode mode = aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime;
|
ListeningMode mode = GetDefaultListeningMode();
|
||||||
if (!protocol_->IsAudioChannelOpened()) {
|
if (!protocol_->IsAudioChannelOpened()) {
|
||||||
SetDeviceState(kDeviceStateConnecting);
|
SetDeviceState(kDeviceStateConnecting);
|
||||||
// Schedule to let the state change be processed first (UI update)
|
// Schedule to let the state change be processed first (UI update)
|
||||||
@@ -801,8 +801,7 @@ void Application::HandleWakeWordDetectedEvent() {
|
|||||||
while (audio_service_.PopPacketFromSendQueue());
|
while (audio_service_.PopPacketFromSendQueue());
|
||||||
|
|
||||||
if (state == kDeviceStateListening) {
|
if (state == kDeviceStateListening) {
|
||||||
auto mode = aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime;
|
protocol_->SendStartListening(GetDefaultListeningMode());
|
||||||
protocol_->SendStartListening(mode);
|
|
||||||
audio_service_.ResetDecoder();
|
audio_service_.ResetDecoder();
|
||||||
audio_service_.PlaySound(Lang::Sounds::OGG_POPUP);
|
audio_service_.PlaySound(Lang::Sounds::OGG_POPUP);
|
||||||
// Re-enable wake word detection as it was stopped by the detection itself
|
// Re-enable wake word detection as it was stopped by the detection itself
|
||||||
@@ -810,7 +809,7 @@ void Application::HandleWakeWordDetectedEvent() {
|
|||||||
} else {
|
} else {
|
||||||
// Play popup sound and start listening again
|
// Play popup sound and start listening again
|
||||||
play_popup_on_listening_ = true;
|
play_popup_on_listening_ = true;
|
||||||
SetListeningMode(aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime);
|
SetListeningMode(GetDefaultListeningMode());
|
||||||
}
|
}
|
||||||
} else if (state == kDeviceStateActivating) {
|
} else if (state == kDeviceStateActivating) {
|
||||||
// Restart the activation check if the wake word is detected during activation
|
// 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
|
// Set flag to play popup sound after state changes to listening
|
||||||
play_popup_on_listening_ = true;
|
play_popup_on_listening_ = true;
|
||||||
SetListeningMode(aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime);
|
SetListeningMode(GetDefaultListeningMode());
|
||||||
#else
|
#else
|
||||||
// Set flag to play popup sound after state changes to listening
|
// Set flag to play popup sound after state changes to listening
|
||||||
// (PlaySound here would be cleared by ResetDecoder in EnableVoiceProcessing)
|
// (PlaySound here would be cleared by ResetDecoder in EnableVoiceProcessing)
|
||||||
play_popup_on_listening_ = true;
|
play_popup_on_listening_ = true;
|
||||||
SetListeningMode(aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime);
|
SetListeningMode(GetDefaultListeningMode());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -891,11 +890,13 @@ void Application::HandleStateChangedEvent() {
|
|||||||
audio_service_.EnableVoiceProcessing(true);
|
audio_service_.EnableVoiceProcessing(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Should use a Kconfig option to enable/disable wake word detection in listening mode
|
#ifdef CONFIG_WAKE_WORD_DETECTION_IN_LISTENING
|
||||||
if (true) {
|
// Enable wake word detection in listening mode (configured via Kconfig)
|
||||||
// Always ensure wake word detection state in listening
|
audio_service_.EnableWakeWordDetection(audio_service_.IsAfeWakeWord());
|
||||||
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
|
// Play popup sound after ResetDecoder (in EnableVoiceProcessing) has been called
|
||||||
if (play_popup_on_listening_) {
|
if (play_popup_on_listening_) {
|
||||||
@@ -944,6 +945,10 @@ void Application::SetListeningMode(ListeningMode mode) {
|
|||||||
SetDeviceState(kDeviceStateListening);
|
SetDeviceState(kDeviceStateListening);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ListeningMode Application::GetDefaultListeningMode() const {
|
||||||
|
return aec_mode_ == kAecOff ? kListeningModeAutoStop : kListeningModeRealtime;
|
||||||
|
}
|
||||||
|
|
||||||
void Application::Reboot() {
|
void Application::Reboot() {
|
||||||
ESP_LOGI(TAG, "Rebooting...");
|
ESP_LOGI(TAG, "Rebooting...");
|
||||||
// Disconnect the audio channel
|
// Disconnect the audio channel
|
||||||
|
|||||||
@@ -165,6 +165,7 @@ private:
|
|||||||
void InitializeProtocol();
|
void InitializeProtocol();
|
||||||
void ShowActivationCode(const std::string& code, const std::string& message);
|
void ShowActivationCode(const std::string& code, const std::string& message);
|
||||||
void SetListeningMode(ListeningMode mode);
|
void SetListeningMode(ListeningMode mode);
|
||||||
|
ListeningMode GetDefaultListeningMode() const;
|
||||||
|
|
||||||
// State change handler called by state machine
|
// State change handler called by state machine
|
||||||
void OnStateChanged(DeviceState old_state, DeviceState new_state);
|
void OnStateChanged(DeviceState old_state, DeviceState new_state);
|
||||||
|
|||||||
Reference in New Issue
Block a user