mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	[microphone] simplify mute handling to avoid unnecessary copies (#9303)
This commit is contained in:
		| @@ -5,17 +5,15 @@ 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) { | ||||||
|  |         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)); |   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 microphone | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -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}; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user