1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-28 13:43:54 +00:00

[audio, i2s_audio, speaker] Media Player Components PR2 (#8164)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Kevin Ahrendt
2025-02-02 20:25:41 -06:00
committed by GitHub
parent 2b711e532b
commit f6cf99384b
9 changed files with 477 additions and 61 deletions

View File

@@ -1,7 +1,6 @@
from esphome import automation
from esphome.automation import maybe_simple_id
import esphome.codegen as cg
from esphome.components import audio_dac
from esphome.components import audio, audio_dac
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_ID, CONF_VOLUME
from esphome.core import CORE
@@ -54,13 +53,15 @@ async def register_speaker(var, config):
await setup_speaker_core_(var, config)
SPEAKER_SCHEMA = cv.Schema(
SPEAKER_SCHEMA = cv.Schema.extend(audio.AUDIO_COMPONENT_SCHEMA).extend(
{
cv.Optional(CONF_AUDIO_DAC): cv.use_id(audio_dac.AudioDac),
}
)
SPEAKER_AUTOMATION_SCHEMA = maybe_simple_id({cv.GenerateID(): cv.use_id(Speaker)})
SPEAKER_AUTOMATION_SCHEMA = automation.maybe_simple_id(
{cv.GenerateID(): cv.use_id(Speaker)}
)
async def speaker_action(config, action_id, template_arg, args):

View File

@@ -9,6 +9,7 @@
#endif
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#include "esphome/components/audio/audio.h"
#ifdef USE_AUDIO_DAC
@@ -56,6 +57,10 @@ class Speaker {
// When finish() is not implemented on the platform component it should just do a normal stop.
virtual void finish() { this->stop(); }
// Pauses processing incoming audio. Needs to be implemented specifically per speaker component
virtual void set_pause_state(bool pause_state) {}
virtual bool get_pause_state() const { return false; }
virtual bool has_buffered_data() const = 0;
bool is_running() const { return this->state_ == STATE_RUNNING; }
@@ -95,6 +100,19 @@ class Speaker {
this->audio_stream_info_ = audio_stream_info;
}
audio::AudioStreamInfo &get_audio_stream_info() { return this->audio_stream_info_; }
/// Callback function for sending the duration of the audio written to the speaker since the last callback.
/// Parameters:
/// - Duration in milliseconds. Never rounded and should always be less than or equal to the actual duration.
/// - Remainder duration in microseconds. Rounded duration after subtracting the previous parameter from the actual
/// duration.
/// - Duration of remaining, unwritten audio buffered in the speaker in milliseconds.
/// - System time in microseconds when the last write was completed.
void add_audio_output_callback(std::function<void(uint32_t, uint32_t, uint32_t, uint32_t)> &&callback) {
this->audio_output_callback_.add(std::move(callback));
}
protected:
State state_{STATE_STOPPED};
audio::AudioStreamInfo audio_stream_info_;
@@ -104,6 +122,8 @@ class Speaker {
#ifdef USE_AUDIO_DAC
audio_dac::AudioDac *audio_dac_{nullptr};
#endif
CallbackManager<void(uint32_t, uint32_t, uint32_t, uint32_t)> audio_output_callback_{};
};
} // namespace speaker