ESPHome 2026.9.0
Loading...
Searching...
No Matches
router_speaker.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
9
10#include <freertos/FreeRTOS.h>
11
12#include <atomic>
13
14namespace esphome::router {
15
16class Router final : public Component, public speaker::Speaker {
17 public:
18 float get_setup_priority() const override { return setup_priority::DATA; }
19
20 void setup() override;
21 void loop() override;
22 void dump_config() override;
23
24 size_t play(const uint8_t *data, size_t length) override { return this->play(data, length, 0); }
25 size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) override;
26
27 void start() override;
28 void stop() override;
29 void finish() override;
30
31 bool has_buffered_data() const override;
32
33 void set_pause_state(bool pause_state) override;
34 bool get_pause_state() const override { return this->cached_pause_; }
35
36 void set_volume(float volume) override;
37 float get_volume() override { return this->volume_; }
38
39 void set_mute_state(bool mute_state) override;
40 bool get_mute_state() override { return this->mute_state_; }
41
42 // Allocate the output list to its final size. Must be called before add_output().
43 void set_output_count(size_t count) { this->outputs_.init(count); }
44 void add_output(speaker::Speaker *spk) { this->outputs_.push_back(spk); }
45
49
50 // Always valid: active_output_idx_ stays within [0, outputs_.size()) and at least
51 // two outputs are required (validated in Python), so this never returns null.
53 return this->outputs_[this->active_output_idx_.load(std::memory_order_relaxed)];
54 }
55
56 protected:
57 // Frames written to the active output but not yet played: incremented in play() and decremented
58 // (clamped at zero) by the active output's audio_output_callback. Mirrors mixer_speaker's
59 // frames_in_pipeline_.
60 std::atomic<uint32_t> frames_in_pipeline_{0};
61
62 // Set when entering STATE_STARTING; used to time out a start the output never acts on
64
65 bool cached_pause_{false};
66
68
69 // Index of the previously-active output we're waiting on to fully stop before
70 // starting the new one. -1 means no pending start. Set by switch_to_output()
71 // when switching mid-playback; cleared by loop() once the old output reports
72 // is_stopped(). Required because shared-bus drivers (e.g. two i2s_audio
73 // speakers on one i2s_bus) reject start() until the previous user releases.
74 std::atomic<int8_t> pending_start_prev_idx_{-1};
75
76 private:
78 // Index into outputs_, always within [0, outputs_.size()). Defaults to the first
79 // configured output; updated by switch_to_output().
80 std::atomic<int8_t> active_output_idx_{0};
81};
82
83template<typename... Ts> class SwitchOutputAction final : public Action<Ts...> {
84 public:
85 explicit SwitchOutputAction(Router *parent) : parent_(parent) {}
87 void play(const Ts &...x) override { this->parent_->switch_to_output(this->target_.value(x...)); }
88
89 protected:
91};
92
93} // namespace esphome::router
94
95#endif // USE_ESP32
virtual void play(const Ts &...x)=0
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:558
void set_mute_state(bool mute_state) override
size_t play(const uint8_t *data, size_t length) override
void set_output_count(size_t count)
speaker::Speaker * get_active_output() const
float get_setup_priority() const override
bool get_mute_state() override
bool get_pause_state() const override
void set_pause_state(bool pause_state) override
void set_volume(float volume) override
void add_output(speaker::Speaker *spk)
bool has_buffered_data() const override
std::atomic< uint32_t > frames_in_pipeline_
bool switch_to_output(speaker::Speaker *target)
Switch the active output to the given speaker.
float get_volume() override
std::atomic< int8_t > pending_start_prev_idx_
TEMPLATABLE_VALUE(speaker::Speaker *, target) void play(const Ts &...x) override
constexpr float DATA
For components that import data from directly connected sensors like DHT.
Definition component.h:45
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5