mirror of
https://github.com/esphome/esphome.git
synced 2025-10-27 21:23:48 +00:00
[speaker, i2s_audio] Support audio_dac component, mute actions, and improved logging (#7664)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
from esphome import automation
|
||||
from esphome.automation import maybe_simple_id
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import audio_dac
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_DATA, CONF_ID, CONF_VOLUME
|
||||
from esphome.core import CORE
|
||||
from esphome.coroutine import coroutine_with_priority
|
||||
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
CODEOWNERS = ["@jesserockz", "@kahrendt"]
|
||||
|
||||
IS_PLATFORM_COMPONENT = True
|
||||
|
||||
CONF_AUDIO_DAC = "audio_dac"
|
||||
|
||||
speaker_ns = cg.esphome_ns.namespace("speaker")
|
||||
|
||||
Speaker = speaker_ns.class_("Speaker")
|
||||
@@ -26,6 +29,12 @@ FinishAction = speaker_ns.class_(
|
||||
VolumeSetAction = speaker_ns.class_(
|
||||
"VolumeSetAction", automation.Action, cg.Parented.template(Speaker)
|
||||
)
|
||||
MuteOnAction = speaker_ns.class_(
|
||||
"MuteOnAction", automation.Action, cg.Parented.template(Speaker)
|
||||
)
|
||||
MuteOffAction = speaker_ns.class_(
|
||||
"MuteOffAction", automation.Action, cg.Parented.template(Speaker)
|
||||
)
|
||||
|
||||
|
||||
IsPlayingCondition = speaker_ns.class_("IsPlayingCondition", automation.Condition)
|
||||
@@ -33,7 +42,9 @@ IsStoppedCondition = speaker_ns.class_("IsStoppedCondition", automation.Conditio
|
||||
|
||||
|
||||
async def setup_speaker_core_(var, config):
|
||||
pass
|
||||
if audio_dac_config := config.get(CONF_AUDIO_DAC):
|
||||
aud_dac = await cg.get_variable(audio_dac_config)
|
||||
cg.add(var.set_audio_dac(aud_dac))
|
||||
|
||||
|
||||
async def register_speaker(var, config):
|
||||
@@ -42,8 +53,11 @@ async def register_speaker(var, config):
|
||||
await setup_speaker_core_(var, config)
|
||||
|
||||
|
||||
SPEAKER_SCHEMA = cv.Schema({})
|
||||
|
||||
SPEAKER_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Optional(CONF_AUDIO_DAC): cv.use_id(audio_dac.AudioDac),
|
||||
}
|
||||
)
|
||||
|
||||
SPEAKER_AUTOMATION_SCHEMA = maybe_simple_id({cv.GenerateID(): cv.use_id(Speaker)})
|
||||
|
||||
@@ -113,6 +127,15 @@ async def speaker_volume_set_action(config, action_id, template_arg, args):
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"speaker.mute_off", MuteOffAction, SPEAKER_AUTOMATION_SCHEMA
|
||||
)
|
||||
@automation.register_action("speaker.mute_on", MuteOnAction, SPEAKER_AUTOMATION_SCHEMA)
|
||||
async def speaker_mute_action_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
|
||||
@coroutine_with_priority(100.0)
|
||||
async def to_code(config):
|
||||
cg.add_global(speaker_ns.using)
|
||||
|
||||
@@ -39,6 +39,26 @@ template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Pa
|
||||
void play(Ts... x) override { this->parent_->set_volume(this->volume_.value(x...)); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class MuteOnAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit MuteOnAction(Speaker *speaker) : speaker_(speaker) {}
|
||||
|
||||
void play(Ts... x) override { this->speaker_->set_mute_state(true); }
|
||||
|
||||
protected:
|
||||
Speaker *speaker_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class MuteOffAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit MuteOffAction(Speaker *speaker) : speaker_(speaker) {}
|
||||
|
||||
void play(Ts... x) override { this->speaker_->set_mute_state(false); }
|
||||
|
||||
protected:
|
||||
Speaker *speaker_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Speaker> {
|
||||
public:
|
||||
void play(Ts... x) override { this->parent_->stop(); }
|
||||
|
||||
@@ -8,7 +8,12 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#endif
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#include "esphome/components/audio/audio.h"
|
||||
#ifdef USE_AUDIO_DAC
|
||||
#include "esphome/components/audio_dac/audio_dac.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
namespace speaker {
|
||||
@@ -56,9 +61,35 @@ class Speaker {
|
||||
bool is_running() const { return this->state_ == STATE_RUNNING; }
|
||||
bool is_stopped() const { return this->state_ == STATE_STOPPED; }
|
||||
|
||||
// Volume control must be implemented by each speaker component, otherwise it will have no effect.
|
||||
virtual void set_volume(float volume) { this->volume_ = volume; };
|
||||
virtual float get_volume() { return this->volume_; }
|
||||
// Volume control is handled by a configured audio dac component. Individual speaker components can
|
||||
// override and implement in software if an audio dac isn't available.
|
||||
virtual void set_volume(float volume) {
|
||||
this->volume_ = volume;
|
||||
#ifdef USE_AUDIO_DAC
|
||||
if (this->audio_dac_ != nullptr) {
|
||||
this->audio_dac_->set_volume(volume);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
float get_volume() { return this->volume_; }
|
||||
|
||||
virtual void set_mute_state(bool mute_state) {
|
||||
this->mute_state_ = mute_state;
|
||||
#ifdef USE_AUDIO_DAC
|
||||
if (this->audio_dac_) {
|
||||
if (mute_state) {
|
||||
this->audio_dac_->set_mute_on();
|
||||
} else {
|
||||
this->audio_dac_->set_mute_off();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
bool get_mute_state() { return this->mute_state_; }
|
||||
|
||||
#ifdef USE_AUDIO_DAC
|
||||
void set_audio_dac(audio_dac::AudioDac *audio_dac) { this->audio_dac_ = audio_dac; }
|
||||
#endif
|
||||
|
||||
void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info) {
|
||||
this->audio_stream_info_ = audio_stream_info;
|
||||
@@ -68,6 +99,11 @@ class Speaker {
|
||||
State state_{STATE_STOPPED};
|
||||
audio::AudioStreamInfo audio_stream_info_;
|
||||
float volume_{1.0f};
|
||||
bool mute_state_{false};
|
||||
|
||||
#ifdef USE_AUDIO_DAC
|
||||
audio_dac::AudioDac *audio_dac_{nullptr};
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace speaker
|
||||
|
||||
Reference in New Issue
Block a user