wangzhibo
2026-08-26 70481429367ca00144b24f118cbfdfb28b02967d
xiaozhi-esp32-2.2.6/main/audio/wake_words/custom_wake_word.cc
@@ -2,6 +2,7 @@
#include "audio_service.h"
#include "system_info.h"
#include "assets.h"
#include "boards/sening-xiaozhi-s3/system.h"
#include <esp_log.h>
#include <esp_mn_iface.h>
@@ -89,13 +90,29 @@
    if (models_list == nullptr) {
        language_ = "cn";
        models_ = esp_srmodel_init("model");
#ifdef CONFIG_CUSTOM_WAKE_WORD
#if defined(BOARD_CUSTOM_WAKE_WORD_PINYIN) && defined(BOARD_CUSTOM_WAKE_WORD_DISPLAY) && defined(BOARD_CUSTOM_WAKE_WORD_THRESHOLD_PERCENT)
        threshold_ = BOARD_CUSTOM_WAKE_WORD_THRESHOLD_PERCENT / 100.0f;
        commands_.push_back({BOARD_CUSTOM_WAKE_WORD_PINYIN, BOARD_CUSTOM_WAKE_WORD_DISPLAY, "wake"});
#elif defined(CONFIG_CUSTOM_WAKE_WORD)
        threshold_ = CONFIG_CUSTOM_WAKE_WORD_THRESHOLD / 100.0f;
        commands_.push_back({CONFIG_CUSTOM_WAKE_WORD, CONFIG_CUSTOM_WAKE_WORD_DISPLAY, "wake"});
#endif
        commands_.push_back({OFFLINE_COMMAND_WORD_PINYIN, OFFLINE_COMMAND_WORD, "offline_command"});
    } else {
        models_ = models_list;
        ParseWakenetModelConfig();
#if defined(BOARD_CUSTOM_WAKE_WORD_PINYIN) && defined(BOARD_CUSTOM_WAKE_WORD_DISPLAY) && defined(BOARD_CUSTOM_WAKE_WORD_THRESHOLD_PERCENT)
        threshold_ = BOARD_CUSTOM_WAKE_WORD_THRESHOLD_PERCENT / 100.0f;
        for (auto it = commands_.begin(); it != commands_.end();) {
            if (it->action == "wake") {
                it = commands_.erase(it);
            } else {
                ++it;
            }
        }
        commands_.push_back({BOARD_CUSTOM_WAKE_WORD_PINYIN, BOARD_CUSTOM_WAKE_WORD_DISPLAY, "wake"});
#endif
        commands_.push_back({OFFLINE_COMMAND_WORD_PINYIN, OFFLINE_COMMAND_WORD, "offline_command"});
    }
    if (models_ == nullptr || models_->num == -1) {
@@ -130,6 +147,10 @@
void CustomWakeWord::OnWakeWordDetected(std::function<void(const std::string& wake_word)> callback) {
    wake_word_detected_callback_ = callback;
}
void CustomWakeWord::OnOfflineCommandDetected(std::function<void(const std::string& command)> callback) {
    offline_command_detected_callback_ = callback;
}
void CustomWakeWord::Start() {
@@ -180,10 +201,14 @@
                    last_detected_wake_word_ = command.text;
                    running_ = false;
                    input_buffer_.clear();
                    if (wake_word_detected_callback_) {
                        wake_word_detected_callback_(last_detected_wake_word_);
                    }
                } else if (command.action == "offline_command") {
                    if (offline_command_detected_callback_) {
                        offline_command_detected_callback_(command.text);
                    }
                }
            }
            multinet_->clean(multinet_model_data_);