From 220e4fab27b7f419b3e89e2f2d28ae789aac09d7 Mon Sep 17 00:00:00 2001
From: wangzhibo <wangzhibo@shsening.com>
Date: 星期四, 08 十月 2026 16:29:55 +0800
Subject: [PATCH] 5种场景模式家长端ok

---
 xiaozhi-esp32-2.2.6/main/audio/wake_words/custom_wake_word.cc |   34 ++++++++++++++++++++++++++++++----
 1 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/xiaozhi-esp32-2.2.6/main/audio/wake_words/custom_wake_word.cc b/xiaozhi-esp32-2.2.6/main/audio/wake_words/custom_wake_word.cc
index 7fb298f..b8f678f 100644
--- a/xiaozhi-esp32-2.2.6/main/audio/wake_words/custom_wake_word.cc
+++ b/xiaozhi-esp32-2.2.6/main/audio/wake_words/custom_wake_word.cc
@@ -9,6 +9,7 @@
 #include <esp_mn_models.h>
 #include <esp_mn_speech_commands.h>
 #include <cJSON.h>
+#include <chrono>
 
 #define TAG "CustomWakeWord"
 
@@ -250,12 +251,28 @@
 }
 
 void CustomWakeWord::StoreWakeWordData(const std::vector<int16_t>& data) {
-    // store audio data to wake_word_pcm_
-    wake_word_pcm_.push_back(data);
+    std::vector<int16_t> chunk;
+    chunk.reserve(2048);
+    chunk.assign(data.begin(), data.end());
+    wake_word_pcm_.push_back(std::move(chunk));
     // keep about 2 seconds of data, detect duration is 30ms (sample_rate == 16000, chunksize == 512)
     while (wake_word_pcm_.size() > 2000 / 30) {
         wake_word_pcm_.pop_front();
     }
+}
+
+void CustomWakeWord::ClearWakeBuffers() {
+    std::lock_guard<std::mutex> lock(wake_word_mutex_);
+    wake_word_pcm_.clear();
+    wake_word_opus_.clear();
+}
+
+void CustomWakeWord::AbortWakeWordEncode() {
+    std::lock_guard<std::mutex> lock(wake_word_mutex_);
+    wake_word_pcm_.clear();
+    wake_word_opus_.clear();
+    wake_word_opus_.emplace_back();
+    wake_word_cv_.notify_all();
 }
 
 void CustomWakeWord::EncodeWakeWordData() {
@@ -333,13 +350,22 @@
         }
         vTaskDelete(NULL);
     }, "encode_wake_word", stack_size, this, 2, wake_word_encode_task_stack_, wake_word_encode_task_buffer_);
+    if (wake_word_encode_task_ == nullptr) {
+        ESP_LOGE(TAG, "Failed to create encode_wake_word task");
+        std::lock_guard<std::mutex> lock(wake_word_mutex_);
+        wake_word_opus_.push_back(std::vector<uint8_t>());
+        wake_word_cv_.notify_all();
+    }
 }
 
 bool CustomWakeWord::GetWakeWordOpus(std::vector<uint8_t>& opus) {
     std::unique_lock<std::mutex> lock(wake_word_mutex_);
-    wake_word_cv_.wait(lock, [this]() {
+    if (!wake_word_cv_.wait_for(lock, std::chrono::milliseconds(2000), [this]() {
         return !wake_word_opus_.empty();
-    });
+    })) {
+        ESP_LOGW(TAG, "wake opus wait timeout");
+        return false;
+    }
     opus.swap(wake_word_opus_.front());
     wake_word_opus_.pop_front();
     return !opus.empty();

--
Gitblit v1.9.1