mirror of
https://github.com/esphome/esphome.git
synced 2025-07-28 14:16:40 +00:00
[microphone] simplify mute handling to avoid unnecessary copies (#9303)
This commit is contained in:
parent
1ef7b2d64f
commit
547c7d6dc8
@ -5,17 +5,15 @@ namespace microphone {
|
||||
|
||||
void Microphone::add_data_callback(std::function<void(const std::vector<uint8_t> &)> &&data_callback) {
|
||||
std::function<void(const std::vector<uint8_t> &)> mute_handled_callback =
|
||||
[this, data_callback](const std::vector<uint8_t> &data) { data_callback(this->silence_audio_(data)); };
|
||||
[this, data_callback](const std::vector<uint8_t> &data) {
|
||||
if (this->mute_state_) {
|
||||
data_callback(std::vector<uint8_t>(data.size(), 0));
|
||||
} else {
|
||||
data_callback(data);
|
||||
};
|
||||
};
|
||||
this->data_callbacks_.add(std::move(mute_handled_callback));
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Microphone::silence_audio_(std::vector<uint8_t> data) {
|
||||
if (this->mute_state_) {
|
||||
std::memset((void *) data.data(), 0, data.size());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
} // namespace microphone
|
||||
} // namespace esphome
|
||||
|
@ -33,8 +33,6 @@ class Microphone {
|
||||
audio::AudioStreamInfo get_audio_stream_info() { return this->audio_stream_info_; }
|
||||
|
||||
protected:
|
||||
std::vector<uint8_t> silence_audio_(std::vector<uint8_t> data);
|
||||
|
||||
State state_{STATE_STOPPED};
|
||||
bool mute_state_{false};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user