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