mirror of
https://github.com/esphome/esphome.git
synced 2025-07-28 14:16:40 +00:00
[microphone] Add software mute and fix wrong type for automations (#8667)
This commit is contained in:
parent
ced7ae1d7a
commit
db97440b04
@ -32,6 +32,12 @@ CaptureAction = microphone_ns.class_(
|
||||
StopCaptureAction = microphone_ns.class_(
|
||||
"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_(
|
||||
@ -42,15 +48,15 @@ DataTrigger = microphone_ns.class_(
|
||||
IsCapturingCondition = microphone_ns.class_(
|
||||
"IsCapturingCondition", automation.Condition
|
||||
)
|
||||
IsMutedCondition = microphone_ns.class_("IsMutedCondition", automation.Condition)
|
||||
|
||||
|
||||
async def setup_microphone_core_(var, config):
|
||||
for conf in config.get(CONF_ON_DATA, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
# Future PR will change the vector type to uint8
|
||||
await automation.build_automation(
|
||||
trigger,
|
||||
[(cg.std_vector.template(cg.int16).operator("ref").operator("const"), "x")],
|
||||
[(cg.std_vector.template(cg.uint8).operator("ref").operator("const"), "x")],
|
||||
conf,
|
||||
)
|
||||
|
||||
@ -186,9 +192,19 @@ automation.register_action(
|
||||
"microphone.stop_capture", StopCaptureAction, MICROPHONE_ACTION_SCHEMA
|
||||
)(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(
|
||||
"microphone.is_capturing", IsCapturingCondition, MICROPHONE_ACTION_SCHEMA
|
||||
)(microphone_action)
|
||||
automation.register_condition(
|
||||
"microphone.is_muted", IsMutedCondition, MICROPHONE_ACTION_SCHEMA
|
||||
)(microphone_action)
|
||||
|
||||
|
||||
@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(); }
|
||||
};
|
||||
|
||||
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> &> {
|
||||
public:
|
||||
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(); }
|
||||
};
|
||||
|
||||
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 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:
|
||||
virtual void start() = 0;
|
||||
virtual void stop() = 0;
|
||||
void add_data_callback(std::function<void(const std::vector<uint8_t> &)> &&data_callback) {
|
||||
this->data_callbacks_.add(std::move(data_callback));
|
||||
}
|
||||
void add_data_callback(std::function<void(const std::vector<uint8_t> &)> &&data_callback);
|
||||
|
||||
bool is_running() const { return this->state_ == STATE_RUNNING; }
|
||||
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_; }
|
||||
|
||||
protected:
|
||||
std::vector<uint8_t> silence_audio_(std::vector<uint8_t> data);
|
||||
|
||||
State state_{STATE_STOPPED};
|
||||
bool mute_state_{false};
|
||||
|
||||
audio::AudioStreamInfo audio_stream_info_;
|
||||
|
||||
|
@ -10,3 +10,11 @@ microphone:
|
||||
adc_type: external
|
||||
pdm: false
|
||||
mclk_multiple: 384
|
||||
on_data:
|
||||
- if:
|
||||
condition:
|
||||
- microphone.is_muted:
|
||||
then:
|
||||
- microphone.unmute:
|
||||
else:
|
||||
- microphone.mute:
|
||||
|
Loading…
x
Reference in New Issue
Block a user