mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[microphone] Add software mute and fix wrong type for automations (#8667)
This commit is contained in:
		| @@ -32,6 +32,12 @@ CaptureAction = microphone_ns.class_( | |||||||
| StopCaptureAction = microphone_ns.class_( | StopCaptureAction = microphone_ns.class_( | ||||||
|     "StopCaptureAction", automation.Action, cg.Parented.template(Microphone) |     "StopCaptureAction", automation.Action, cg.Parented.template(Microphone) | ||||||
| ) | ) | ||||||
|  | MuteAction = microphone_ns.class_( | ||||||
|  |     "MuteAction", automation.Action, cg.Parented.template(Microphone) | ||||||
|  | ) | ||||||
|  | UnmuteAction = microphone_ns.class_( | ||||||
|  |     "UnmuteAction", automation.Action, cg.Parented.template(Microphone) | ||||||
|  | ) | ||||||
|  |  | ||||||
|  |  | ||||||
| DataTrigger = microphone_ns.class_( | DataTrigger = microphone_ns.class_( | ||||||
| @@ -42,15 +48,15 @@ DataTrigger = microphone_ns.class_( | |||||||
| IsCapturingCondition = microphone_ns.class_( | IsCapturingCondition = microphone_ns.class_( | ||||||
|     "IsCapturingCondition", automation.Condition |     "IsCapturingCondition", automation.Condition | ||||||
| ) | ) | ||||||
|  | IsMutedCondition = microphone_ns.class_("IsMutedCondition", automation.Condition) | ||||||
|  |  | ||||||
|  |  | ||||||
| async def setup_microphone_core_(var, config): | async def setup_microphone_core_(var, config): | ||||||
|     for conf in config.get(CONF_ON_DATA, []): |     for conf in config.get(CONF_ON_DATA, []): | ||||||
|         trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) |         trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) | ||||||
|         # Future PR will change the vector type to uint8 |  | ||||||
|         await automation.build_automation( |         await automation.build_automation( | ||||||
|             trigger, |             trigger, | ||||||
|             [(cg.std_vector.template(cg.int16).operator("ref").operator("const"), "x")], |             [(cg.std_vector.template(cg.uint8).operator("ref").operator("const"), "x")], | ||||||
|             conf, |             conf, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
| @@ -186,9 +192,19 @@ automation.register_action( | |||||||
|     "microphone.stop_capture", StopCaptureAction, MICROPHONE_ACTION_SCHEMA |     "microphone.stop_capture", StopCaptureAction, MICROPHONE_ACTION_SCHEMA | ||||||
| )(microphone_action) | )(microphone_action) | ||||||
|  |  | ||||||
|  | automation.register_action("microphone.mute", MuteAction, MICROPHONE_ACTION_SCHEMA)( | ||||||
|  |     microphone_action | ||||||
|  | ) | ||||||
|  | automation.register_action("microphone.unmute", UnmuteAction, MICROPHONE_ACTION_SCHEMA)( | ||||||
|  |     microphone_action | ||||||
|  | ) | ||||||
|  |  | ||||||
| automation.register_condition( | automation.register_condition( | ||||||
|     "microphone.is_capturing", IsCapturingCondition, MICROPHONE_ACTION_SCHEMA |     "microphone.is_capturing", IsCapturingCondition, MICROPHONE_ACTION_SCHEMA | ||||||
| )(microphone_action) | )(microphone_action) | ||||||
|  | automation.register_condition( | ||||||
|  |     "microphone.is_muted", IsMutedCondition, MICROPHONE_ACTION_SCHEMA | ||||||
|  | )(microphone_action) | ||||||
|  |  | ||||||
|  |  | ||||||
| @coroutine_with_priority(100.0) | @coroutine_with_priority(100.0) | ||||||
|   | |||||||
| @@ -16,6 +16,13 @@ template<typename... Ts> class StopCaptureAction : public Action<Ts...>, public | |||||||
|   void play(Ts... x) override { this->parent_->stop(); } |   void play(Ts... x) override { this->parent_->stop(); } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | template<typename... Ts> class MuteAction : public Action<Ts...>, public Parented<Microphone> { | ||||||
|  |   void play(Ts... x) override { this->parent_->set_mute_state(true); } | ||||||
|  | }; | ||||||
|  | template<typename... Ts> class UnmuteAction : public Action<Ts...>, public Parented<Microphone> { | ||||||
|  |   void play(Ts... x) override { this->parent_->set_mute_state(false); } | ||||||
|  | }; | ||||||
|  |  | ||||||
| class DataTrigger : public Trigger<const std::vector<uint8_t> &> { | class DataTrigger : public Trigger<const std::vector<uint8_t> &> { | ||||||
|  public: |  public: | ||||||
|   explicit DataTrigger(Microphone *mic) { |   explicit DataTrigger(Microphone *mic) { | ||||||
| @@ -28,5 +35,10 @@ template<typename... Ts> class IsCapturingCondition : public Condition<Ts...>, p | |||||||
|   bool check(Ts... x) override { return this->parent_->is_running(); } |   bool check(Ts... x) override { return this->parent_->is_running(); } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | template<typename... Ts> class IsMutedCondition : public Condition<Ts...>, public Parented<Microphone> { | ||||||
|  |  public: | ||||||
|  |   bool check(Ts... x) override { return this->parent_->get_mute_state(); } | ||||||
|  | }; | ||||||
|  |  | ||||||
| }  // namespace microphone | }  // namespace microphone | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
							
								
								
									
										21
									
								
								esphome/components/microphone/microphone.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								esphome/components/microphone/microphone.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | |||||||
|  | #include "microphone.h" | ||||||
|  |  | ||||||
|  | namespace esphome { | ||||||
|  | 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_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 | ||||||
| @@ -22,17 +22,21 @@ class Microphone { | |||||||
|  public: |  public: | ||||||
|   virtual void start() = 0; |   virtual void start() = 0; | ||||||
|   virtual void stop() = 0; |   virtual void stop() = 0; | ||||||
|   void add_data_callback(std::function<void(const std::vector<uint8_t> &)> &&data_callback) { |   void add_data_callback(std::function<void(const std::vector<uint8_t> &)> &&data_callback); | ||||||
|     this->data_callbacks_.add(std::move(data_callback)); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   bool is_running() const { return this->state_ == STATE_RUNNING; } |   bool is_running() const { return this->state_ == STATE_RUNNING; } | ||||||
|   bool is_stopped() const { return this->state_ == STATE_STOPPED; } |   bool is_stopped() const { return this->state_ == STATE_STOPPED; } | ||||||
|  |  | ||||||
|  |   void set_mute_state(bool is_muted) { this->mute_state_ = is_muted; } | ||||||
|  |   bool get_mute_state() { return this->mute_state_; } | ||||||
|  |  | ||||||
|   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}; | ||||||
|  |  | ||||||
|   audio::AudioStreamInfo audio_stream_info_; |   audio::AudioStreamInfo audio_stream_info_; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -10,3 +10,11 @@ microphone: | |||||||
|     adc_type: external |     adc_type: external | ||||||
|     pdm: false |     pdm: false | ||||||
|     mclk_multiple: 384 |     mclk_multiple: 384 | ||||||
|  |     on_data: | ||||||
|  |       - if: | ||||||
|  |           condition: | ||||||
|  |             - microphone.is_muted: | ||||||
|  |           then: | ||||||
|  |             - microphone.unmute: | ||||||
|  |           else: | ||||||
|  |             - microphone.mute: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user