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/processors/afe_audio_processor.cc |   87 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 71 insertions(+), 16 deletions(-)

diff --git a/xiaozhi-esp32-2.2.6/main/audio/processors/afe_audio_processor.cc b/xiaozhi-esp32-2.2.6/main/audio/processors/afe_audio_processor.cc
index 15e9cc5..b53816e 100644
--- a/xiaozhi-esp32-2.2.6/main/audio/processors/afe_audio_processor.cc
+++ b/xiaozhi-esp32-2.2.6/main/audio/processors/afe_audio_processor.cc
@@ -12,13 +12,37 @@
 
 void AfeAudioProcessor::Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) {
     codec_ = codec;
+    models_list_ = models_list;
+    frame_duration_ms_ = frame_duration_ms;
     frame_samples_ = frame_duration_ms * 16000 / 1000;
 
     // Pre-allocate output buffer capacity
     output_buffer_.reserve(frame_samples_);
 
-    int ref_num = codec_->input_reference() ? 1 : 0;
+    if (!CreateAfe()) {
+        ESP_LOGE(TAG, "Failed to create AFE runtime");
+        return;
+    }
 
+    if (!processor_task_started_) {
+        xTaskCreate([](void* arg) {
+            auto this_ = (AfeAudioProcessor*)arg;
+            this_->AudioProcessorTask();
+            vTaskDelete(NULL);
+        }, "audio_communication", 4096, this, 3, NULL);
+        processor_task_started_ = true;
+    }
+}
+
+bool AfeAudioProcessor::CreateAfe() {
+    if (afe_data_ != nullptr) {
+        return true;
+    }
+    if (codec_ == nullptr) {
+        return false;
+    }
+
+    int ref_num = codec_->input_reference() ? 1 : 0;
     std::string input_format;
     for (int i = 0; i < codec_->input_channels() - ref_num; i++) {
         input_format.push_back('M');
@@ -28,15 +52,15 @@
     }
 
     srmodel_list_t *models;
-    if (models_list == nullptr) {
+    if (models_list_ == nullptr) {
         models = esp_srmodel_init("model");
     } else {
-        models = models_list;
+        models = models_list_;
     }
 
     char* ns_model_name = esp_srmodel_filter(models, ESP_NSNET_PREFIX, NULL);
     char* vad_model_name = esp_srmodel_filter(models, ESP_VADN_PREFIX, NULL);
-    
+
     afe_config_t* afe_config = afe_config_init(input_format.c_str(), NULL, AFE_TYPE_VC, AFE_MODE_HIGH_PERF);
     afe_config->aec_mode = AEC_MODE_VOIP_HIGH_PERF;
     afe_config->vad_mode = VAD_MODE_0;
@@ -66,12 +90,31 @@
 
     afe_iface_ = esp_afe_handle_from_config(afe_config);
     afe_data_ = afe_iface_->create_from_config(afe_config);
-    
-    xTaskCreate([](void* arg) {
-        auto this_ = (AfeAudioProcessor*)arg;
-        this_->AudioProcessorTask();
-        vTaskDelete(NULL);
-    }, "audio_communication", 4096, this, 3, NULL);
+    if (afe_data_ == nullptr) {
+        ESP_LOGE(TAG, "create_from_config failed");
+        return false;
+    }
+    ESP_LOGI(TAG, "AFE runtime created");
+    return true;
+}
+
+void AfeAudioProcessor::ReleaseRuntime() {
+    Stop();
+    if (afe_data_ == nullptr) {
+        return;
+    }
+    vTaskDelay(pdMS_TO_TICKS(120));
+    std::lock_guard<std::mutex> lock(input_buffer_mutex_);
+    if (afe_data_ != nullptr && afe_iface_ != nullptr) {
+        afe_iface_->destroy(afe_data_);
+        afe_data_ = nullptr;
+    }
+    input_buffer_.clear();
+    ESP_LOGI(TAG, "AFE runtime released");
+}
+
+bool AfeAudioProcessor::EnsureRuntime() {
+    return CreateAfe();
 }
 
 AfeAudioProcessor::~AfeAudioProcessor() {
@@ -133,21 +176,30 @@
 }
 
 void AfeAudioProcessor::AudioProcessorTask() {
-    auto fetch_size = afe_iface_->get_fetch_chunksize(afe_data_);
-    auto feed_size = afe_iface_->get_feed_chunksize(afe_data_);
-    ESP_LOGI(TAG, "Audio communication task started, feed size: %d fetch size: %d",
-        feed_size, fetch_size);
+    ESP_LOGI(TAG, "Audio communication task started");
 
     while (true) {
         xEventGroupWaitBits(event_group_, PROCESSOR_RUNNING, pdFALSE, pdTRUE, portMAX_DELAY);
 
-        auto res = afe_iface_->fetch_with_delay(afe_data_, portMAX_DELAY);
+        esp_afe_sr_data_t* afe_data = nullptr;
+        const esp_afe_sr_iface_t* afe_iface = nullptr;
+        {
+            std::lock_guard<std::mutex> lock(input_buffer_mutex_);
+            afe_data = afe_data_;
+            afe_iface = afe_iface_;
+        }
+        if (afe_data == nullptr || afe_iface == nullptr) {
+            vTaskDelay(pdMS_TO_TICKS(20));
+            continue;
+        }
+
+        auto res = afe_iface->fetch_with_delay(afe_data, pdMS_TO_TICKS(80));
         if ((xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING) == 0) {
             continue;
         }
         if (res == nullptr || res->ret_value == ESP_FAIL) {
             if (res != nullptr) {
-                ESP_LOGI(TAG, "Error code: %d", res->ret_value);
+                ESP_LOGD(TAG, "Error code: %d", res->ret_value);
             }
             continue;
         }
@@ -187,6 +239,9 @@
 }
 
 void AfeAudioProcessor::EnableDeviceAec(bool enable) {
+    if (afe_data_ == nullptr || afe_iface_ == nullptr) {
+        return;
+    }
     if (enable) {
 #if CONFIG_USE_DEVICE_AEC
         afe_iface_->disable_vad(afe_data_);

--
Gitblit v1.9.1