支持分别关闭降噪和唤醒功能

This commit is contained in:
Terrence
2025-03-04 05:30:35 +08:00
parent 778e4f433f
commit c60f134093
7 changed files with 60 additions and 35 deletions

View File

@@ -124,8 +124,11 @@ elseif(CONFIG_CONNECTION_TYPE_WEBSOCKET)
list(APPEND SOURCES "protocols/websocket_protocol.cc") list(APPEND SOURCES "protocols/websocket_protocol.cc")
endif() endif()
if(CONFIG_USE_AUDIO_PROCESSING) if(CONFIG_USE_AUDIO_PROCESSOR)
list(APPEND SOURCES "audio_processing/audio_processor.cc" "audio_processing/wake_word_detect.cc") list(APPEND SOURCES "audio_processing/audio_processor.cc")
endif()
if(CONFIG_USE_WAKE_WORD_DETECT)
list(APPEND SOURCES "audio_processing/wake_word_detect.cc")
endif() endif()
# 根据Kconfig选择语言目录 # 根据Kconfig选择语言目录

View File

@@ -180,10 +180,18 @@ choice DISPLAY_LCD_TYPE
bool "自定义屏幕参数" bool "自定义屏幕参数"
endchoice endchoice
config USE_AUDIO_PROCESSING config USE_AUDIO_PROCESSOR
bool "启用语音唤醒与音频处理" bool "启用音频降噪、增益处理"
default y default y
depends on IDF_TARGET_ESP32S3 && USE_AFE depends on IDF_TARGET_ESP32S3 && USE_AFE
help help
需要 ESP32 S3 与 AFE 支持 需要 ESP32 S3 与 AFE 支持
config USE_WAKE_WORD_DETECT
bool "启用唤醒词检测"
default y
depends on IDF_TARGET_ESP32S3 && USE_AFE
help
需要 ESP32 S3 与 AFE 支持
endmenu endmenu

View File

@@ -100,7 +100,7 @@ void Application::CheckNewVersion() {
auto& board = Board::GetInstance(); auto& board = Board::GetInstance();
board.SetPowerSaveMode(false); board.SetPowerSaveMode(false);
#if CONFIG_USE_AUDIO_PROCESSING #if CONFIG_USE_WAKE_WORD_DETECT
wake_word_detect_.StopDetection(); wake_word_detect_.StopDetection();
#endif #endif
// 预先关闭音频输出,避免升级过程有音频操作 // 预先关闭音频输出,避免升级过程有音频操作
@@ -470,8 +470,7 @@ void Application::Start() {
vTaskDelete(NULL); vTaskDelete(NULL);
}, "check_new_version", 4096 * 2, this, 1, nullptr); }, "check_new_version", 4096 * 2, this, 1, nullptr);
#if CONFIG_USE_AUDIO_PROCESSOR
#if CONFIG_USE_AUDIO_PROCESSING
audio_processor_.Initialize(codec->input_channels(), codec->input_reference()); audio_processor_.Initialize(codec->input_channels(), codec->input_reference());
audio_processor_.OnOutput([this](std::vector<int16_t>&& data) { audio_processor_.OnOutput([this](std::vector<int16_t>&& data) {
background_task_->Schedule([this, data = std::move(data)]() mutable { background_task_->Schedule([this, data = std::move(data)]() mutable {
@@ -482,7 +481,9 @@ void Application::Start() {
}); });
}); });
}); });
#endif
#if CONFIG_USE_WAKE_WORD_DETECT
wake_word_detect_.Initialize(codec->input_channels(), codec->input_reference()); wake_word_detect_.Initialize(codec->input_channels(), codec->input_reference());
wake_word_detect_.OnVadStateChange([this](bool speaking) { wake_word_detect_.OnVadStateChange([this](bool speaking) {
Schedule([this, speaking]() { Schedule([this, speaking]() {
@@ -683,13 +684,15 @@ void Application::InputAudio() {
} }
} }
#if CONFIG_USE_AUDIO_PROCESSING #if CONFIG_USE_WAKE_WORD_DETECT
if (audio_processor_.IsRunning()) {
audio_processor_.Input(data);
}
if (wake_word_detect_.IsDetectionRunning()) { if (wake_word_detect_.IsDetectionRunning()) {
wake_word_detect_.Feed(data); wake_word_detect_.Feed(data);
} }
#endif
#if CONFIG_USE_AUDIO_PROCESSOR
if (audio_processor_.IsRunning()) {
audio_processor_.Input(data);
}
#else #else
if (device_state_ == kDeviceStateListening) { if (device_state_ == kDeviceStateListening) {
background_task_->Schedule([this, data = std::move(data)]() mutable { background_task_->Schedule([this, data = std::move(data)]() mutable {
@@ -730,7 +733,7 @@ void Application::SetDeviceState(DeviceState state) {
case kDeviceStateIdle: case kDeviceStateIdle:
display->SetStatus(Lang::Strings::STANDBY); display->SetStatus(Lang::Strings::STANDBY);
display->SetEmotion("neutral"); display->SetEmotion("neutral");
#ifdef CONFIG_USE_AUDIO_PROCESSING #if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Stop(); audio_processor_.Stop();
#endif #endif
break; break;
@@ -744,7 +747,7 @@ void Application::SetDeviceState(DeviceState state) {
display->SetEmotion("neutral"); display->SetEmotion("neutral");
ResetDecoder(); ResetDecoder();
opus_encoder_->ResetState(); opus_encoder_->ResetState();
#if CONFIG_USE_AUDIO_PROCESSING #if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Start(); audio_processor_.Start();
#endif #endif
UpdateIotStates(); UpdateIotStates();
@@ -757,7 +760,7 @@ void Application::SetDeviceState(DeviceState state) {
display->SetStatus(Lang::Strings::SPEAKING); display->SetStatus(Lang::Strings::SPEAKING);
ResetDecoder(); ResetDecoder();
codec->EnableOutput(true); codec->EnableOutput(true);
#if CONFIG_USE_AUDIO_PROCESSING #if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Stop(); audio_processor_.Stop();
#endif #endif
break; break;

View File

@@ -18,8 +18,10 @@
#include "ota.h" #include "ota.h"
#include "background_task.h" #include "background_task.h"
#if CONFIG_USE_AUDIO_PROCESSING #if CONFIG_USE_WAKE_WORD_DETECT
#include "wake_word_detect.h" #include "wake_word_detect.h"
#endif
#if CONFIG_USE_AUDIO_PROCESSOR
#include "audio_processor.h" #include "audio_processor.h"
#endif #endif
@@ -72,8 +74,10 @@ private:
Application(); Application();
~Application(); ~Application();
#if CONFIG_USE_AUDIO_PROCESSING #if CONFIG_USE_WAKE_WORD_DETECT
WakeWordDetect wake_word_detect_; WakeWordDetect wake_word_detect_;
#endif
#if CONFIG_USE_AUDIO_PROCESSOR
AudioProcessor audio_processor_; AudioProcessor audio_processor_;
#endif #endif
Ota ota_; Ota ota_;

View File

@@ -5,7 +5,7 @@
"name": "atoms3-echo-base", "name": "atoms3-echo-base",
"sdkconfig_append": [ "sdkconfig_append": [
"CONFIG_SPIRAM=n", "CONFIG_SPIRAM=n",
"CONFIG_USE_AUDIO_PROCESSING=n", "CONFIG_USE_AFE=n",
"CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y", "CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y",
"CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions_8M.csv\"" "CONFIG_PARTITION_TABLE_CUSTOM_FILENAME=\"partitions_8M.csv\""
] ]

View File

@@ -0,0 +1,9 @@
{
"target": "esp32s3",
"builds": [
{
"name": "tudouzi",
"sdkconfig_append": ["CONFIG_USE_WAKE_WORD_DETECT=n"]
}
]
}

View File

@@ -10,7 +10,6 @@
#include "assets/lang_config.h" #include "assets/lang_config.h"
#include <esp_log.h> #include <esp_log.h>
#include <esp_spiffs.h>
#include <driver/gpio.h> #include <driver/gpio.h>
#include <driver/i2c_master.h> #include <driver/i2c_master.h>
#include <esp_timer.h> #include <esp_timer.h>
@@ -28,8 +27,8 @@ private:
Button boot_button_; Button boot_button_;
Button volume_up_button_; Button volume_up_button_;
Button volume_down_button_; Button volume_down_button_;
uint8_t _data_buffer[2];
esp_timer_handle_t power_save_timer_ = nullptr; esp_timer_handle_t power_save_timer_ = nullptr;
bool show_low_power_warning_ = false;
void InitializePowerSaveTimer() { void InitializePowerSaveTimer() {
esp_timer_create_args_t power_save_timer_args = { esp_timer_create_args_t power_save_timer_args = {
@@ -50,29 +49,29 @@ private:
// 电池放电模式下,如果待机超过一定时间,则自动关机 // 电池放电模式下,如果待机超过一定时间,则自动关机
const int seconds_to_shutdown = 600; const int seconds_to_shutdown = 600;
static int seconds = 0; static int seconds = 0;
if (Application::GetInstance().GetDeviceState() != kDeviceStateIdle) { auto& app = Application::GetInstance();
if (app.GetDeviceState() != kDeviceStateIdle) {
seconds = 0; seconds = 0;
return; return;
} }
if (!axp2101_->IsDischarging()) { if (!axp2101_->IsDischarging()) {
seconds = 0; seconds = 0;
if (show_low_power_warning_) {
app.DismissAlert();
show_low_power_warning_ = false;
}
return; return;
} }
// 电量低于 10% 时,显示低电量警告
if (seconds >= seconds_to_shutdown) { if (axp2101_->GetBatteryLevel() <= 10 && !show_low_power_warning_) {
axp2101_->PowerOff(); app.Alert(Lang::Strings::WARNING, Lang::Strings::BATTERY_LOW, "sad", Lang::Sounds::P3_VIBRATION);
show_low_power_warning_ = true;
} }
}
void MountStorage() { seconds++;
// Mount the storage partition if (seconds >= seconds_to_shutdown) {
esp_vfs_spiffs_conf_t conf = { // axp2101_->PowerOff();
.base_path = "/storage", }
.partition_label = "storage",
.max_files = 5,
.format_if_mount_failed = true,
};
esp_vfs_spiffs_register(&conf);
} }
void Enable4GModule() { void Enable4GModule() {
@@ -175,7 +174,6 @@ public:
InitializeCodecI2c(); InitializeCodecI2c();
axp2101_ = new Axp2101(codec_i2c_bus_, AXP2101_I2C_ADDR); axp2101_ = new Axp2101(codec_i2c_bus_, AXP2101_I2C_ADDR);
MountStorage();
Enable4GModule(); Enable4GModule();
InitializeButtons(); InitializeButtons();