forked from xiaozhi/xiaozhi-esp32
fix CPU usage of esp32c3 with ml307
This commit is contained in:
@@ -410,15 +410,18 @@ void Application::Start() {
|
|||||||
auto codec = board.GetAudioCodec();
|
auto codec = board.GetAudioCodec();
|
||||||
opus_decoder_ = std::make_unique<OpusDecoderWrapper>(codec->output_sample_rate(), 1, OPUS_FRAME_DURATION_MS);
|
opus_decoder_ = std::make_unique<OpusDecoderWrapper>(codec->output_sample_rate(), 1, OPUS_FRAME_DURATION_MS);
|
||||||
opus_encoder_ = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
|
opus_encoder_ = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
|
||||||
|
opus_encoder_->SetComplexity(0);
|
||||||
if (aec_mode_ != kAecOff) {
|
if (aec_mode_ != kAecOff) {
|
||||||
ESP_LOGI(TAG, "AEC mode: %d, setting opus encoder complexity to 0", aec_mode_);
|
ESP_LOGI(TAG, "AEC mode: %d, setting opus encoder complexity to 0", aec_mode_);
|
||||||
opus_encoder_->SetComplexity(0);
|
opus_encoder_->SetComplexity(0);
|
||||||
} else if (board.GetBoardType() == "ml307") {
|
|
||||||
ESP_LOGI(TAG, "ML307 board detected, setting opus encoder complexity to 5");
|
|
||||||
opus_encoder_->SetComplexity(5);
|
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "WiFi board detected, setting opus encoder complexity to 0");
|
#if CONFIG_USE_AUDIO_PROCESSOR
|
||||||
|
ESP_LOGI(TAG, "Audio processor detected, setting opus encoder complexity to 5");
|
||||||
|
opus_encoder_->SetComplexity(5);
|
||||||
|
#else
|
||||||
|
ESP_LOGI(TAG, "Audio processor not detected, setting opus encoder complexity to 0");
|
||||||
opus_encoder_->SetComplexity(0);
|
opus_encoder_->SetComplexity(0);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (codec->input_sample_rate() != 16000) {
|
if (codec->input_sample_rate() != 16000) {
|
||||||
@@ -826,7 +829,7 @@ void Application::OnAudioOutput() {
|
|||||||
SetDecodeSampleRate(packet.sample_rate, packet.frame_duration);
|
SetDecodeSampleRate(packet.sample_rate, packet.frame_duration);
|
||||||
|
|
||||||
busy_decoding_audio_ = true;
|
busy_decoding_audio_ = true;
|
||||||
background_task_->Schedule([this, codec, packet = std::move(packet)]() mutable {
|
if (!background_task_->Schedule([this, codec, packet = std::move(packet)]() mutable {
|
||||||
busy_decoding_audio_ = false;
|
busy_decoding_audio_ = false;
|
||||||
if (aborted_) {
|
if (aborted_) {
|
||||||
return;
|
return;
|
||||||
@@ -849,7 +852,9 @@ void Application::OnAudioOutput() {
|
|||||||
timestamp_queue_.push_back(packet.timestamp);
|
timestamp_queue_.push_back(packet.timestamp);
|
||||||
#endif
|
#endif
|
||||||
last_output_time_ = std::chrono::steady_clock::now();
|
last_output_time_ = std::chrono::steady_clock::now();
|
||||||
});
|
})) {
|
||||||
|
busy_decoding_audio_ = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::OnAudioInput() {
|
void Application::OnAudioInput() {
|
||||||
|
|||||||
@@ -18,12 +18,16 @@ BackgroundTask::~BackgroundTask() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BackgroundTask::Schedule(std::function<void()> callback) {
|
bool BackgroundTask::Schedule(std::function<void()> callback) {
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
if (waiting_for_completion_ > 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (active_tasks_ >= 30) {
|
if (active_tasks_ >= 30) {
|
||||||
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
|
||||||
if (free_sram < 10000) {
|
if (free_sram < 10000) {
|
||||||
ESP_LOGW(TAG, "active_tasks_ == %u, free_sram == %u", active_tasks_.load(), free_sram);
|
ESP_LOGW(TAG, "active_tasks_ == %d, free_sram == %u", active_tasks_, free_sram);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
active_tasks_++;
|
active_tasks_++;
|
||||||
@@ -38,13 +42,16 @@ void BackgroundTask::Schedule(std::function<void()> callback) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
condition_variable_.notify_all();
|
condition_variable_.notify_all();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BackgroundTask::WaitForCompletion() {
|
void BackgroundTask::WaitForCompletion() {
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
|
waiting_for_completion_++;
|
||||||
condition_variable_.wait(lock, [this]() {
|
condition_variable_.wait(lock, [this]() {
|
||||||
return background_tasks_.empty() && active_tasks_ == 0;
|
return background_tasks_.empty() && active_tasks_ == 0;
|
||||||
});
|
});
|
||||||
|
waiting_for_completion_--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BackgroundTask::BackgroundTaskLoop() {
|
void BackgroundTask::BackgroundTaskLoop() {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public:
|
|||||||
BackgroundTask(uint32_t stack_size = 4096 * 2);
|
BackgroundTask(uint32_t stack_size = 4096 * 2);
|
||||||
~BackgroundTask();
|
~BackgroundTask();
|
||||||
|
|
||||||
void Schedule(std::function<void()> callback);
|
bool Schedule(std::function<void()> callback);
|
||||||
void WaitForCompletion();
|
void WaitForCompletion();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -21,7 +21,8 @@ private:
|
|||||||
std::list<std::function<void()>> background_tasks_;
|
std::list<std::function<void()>> background_tasks_;
|
||||||
std::condition_variable condition_variable_;
|
std::condition_variable condition_variable_;
|
||||||
TaskHandle_t background_task_handle_ = nullptr;
|
TaskHandle_t background_task_handle_ = nullptr;
|
||||||
std::atomic<size_t> active_tasks_{0};
|
int active_tasks_ = 0;
|
||||||
|
int waiting_for_completion_ = 0;
|
||||||
|
|
||||||
void BackgroundTaskLoop();
|
void BackgroundTaskLoop();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -46,13 +46,17 @@ void Ml307Board::WaitForNetworkReady() {
|
|||||||
auto& application = Application::GetInstance();
|
auto& application = Application::GetInstance();
|
||||||
auto display = Board::GetInstance().GetDisplay();
|
auto display = Board::GetInstance().GetDisplay();
|
||||||
display->SetStatus(Lang::Strings::REGISTERING_NETWORK);
|
display->SetStatus(Lang::Strings::REGISTERING_NETWORK);
|
||||||
int result = modem_.WaitForNetworkReady();
|
|
||||||
if (result == -1) {
|
while (true) {
|
||||||
application.Alert(Lang::Strings::ERROR, Lang::Strings::PIN_ERROR, "sad", Lang::Sounds::P3_ERR_PIN);
|
int result = modem_.WaitForNetworkReady();
|
||||||
return;
|
if (result == -1) {
|
||||||
} else if (result == -2) {
|
application.Alert(Lang::Strings::ERROR, Lang::Strings::PIN_ERROR, "sad", Lang::Sounds::P3_ERR_PIN);
|
||||||
application.Alert(Lang::Strings::ERROR, Lang::Strings::REG_ERROR, "sad", Lang::Sounds::P3_ERR_REG);
|
} else if (result == -2) {
|
||||||
return;
|
application.Alert(Lang::Strings::ERROR, Lang::Strings::REG_ERROR, "sad", Lang::Sounds::P3_ERR_REG);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(10000));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the ML307 modem information
|
// Print the ML307 modem information
|
||||||
|
|||||||
Reference in New Issue
Block a user