From 073dd3995eed1476432e22ca55097743b6fc822e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 15 Mar 2026 22:59:19 -1000 Subject: [PATCH] [microphone] Remove unnecessary std::function wrapping in callbacks Pass wrapping lambdas directly to CallbackManager::add() instead of first wrapping in std::function. The lambdas capture [this, callback] (two pointers) so they hit the heap path either way, but this avoids double-wrapping and the extra std::function overhead. Also fix "deallaction" typo. --- esphome/components/microphone/microphone.h | 16 +++++------- .../components/microphone/microphone_source.h | 26 +++++++++---------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/esphome/components/microphone/microphone.h b/esphome/components/microphone/microphone.h index 57025564ac..f7093d3699 100644 --- a/esphome/components/microphone/microphone.h +++ b/esphome/components/microphone/microphone.h @@ -23,15 +23,13 @@ class Microphone { virtual void start() = 0; virtual void stop() = 0; template void add_data_callback(F &&data_callback) { - std::function &)> mute_handled_callback = - [this, data_callback](const std::vector &data) { - if (this->mute_state_) { - data_callback(std::vector(data.size(), 0)); - } else { - data_callback(data); - }; - }; - this->data_callbacks_.add(std::move(mute_handled_callback)); + this->data_callbacks_.add([this, data_callback](const std::vector &data) { + if (this->mute_state_) { + data_callback(std::vector(data.size(), 0)); + } else { + data_callback(data); + } + }); } bool is_running() const { return this->state_ == STATE_RUNNING; } diff --git a/esphome/components/microphone/microphone_source.h b/esphome/components/microphone/microphone_source.h index eb45bb2bed..8947369376 100644 --- a/esphome/components/microphone/microphone_source.h +++ b/esphome/components/microphone/microphone_source.h @@ -48,21 +48,19 @@ class MicrophoneSource { void add_channel(uint8_t channel) { this->channels_.set(channel); } template void add_data_callback(F &&data_callback) { - std::function &)> filtered_callback = - [this, data_callback](const std::vector &data) { - if (this->enabled_ || this->passive_) { - if (this->processed_samples_.use_count() == 0) { - // Create vector if its unused - this->processed_samples_ = std::make_shared>(); - } + this->mic_->add_data_callback([this, data_callback](const std::vector &data) { + if (this->enabled_ || this->passive_) { + if (this->processed_samples_.use_count() == 0) { + // Create vector if its unused + this->processed_samples_ = std::make_shared>(); + } - // Take temporary ownership of samples vector to avoid deallaction before the callback finishes - std::shared_ptr> output_samples = this->processed_samples_; - this->process_audio_(data, *output_samples); - data_callback(*output_samples); - } - }; - this->mic_->add_data_callback(std::move(filtered_callback)); + // Take temporary ownership of samples vector to avoid deallocation before the callback finishes + std::shared_ptr> output_samples = this->processed_samples_; + this->process_audio_(data, *output_samples); + data_callback(*output_samples); + } + }); } void set_gain_factor(int32_t gain_factor) { this->gain_factor_ = clamp(gain_factor, 1, MAX_GAIN_FACTOR); }