ESPHome 2026.6.0-dev
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 : 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 bool cached_pause_{false};
63
65
66 // Index of the previously-active output we're waiting on to fully stop before
67 // starting the new one. -1 means no pending start. Set by switch_to_output()
68 // when switching mid-playback; cleared by loop() once the old output reports
69 // is_stopped(). Required because shared-bus drivers (e.g. two i2s_audio
70 // speakers on one i2s_bus) reject start() until the previous user releases.
71 std::atomic<int8_t> pending_start_prev_idx_{-1};
72
73 private:
75 // Index into outputs_, always within [0, outputs_.size()). Defaults to the first
76 // configured output; updated by switch_to_output().
77 std::atomic<int8_t> active_output_idx_{0};
78};
79
80template<typename... Ts> class SwitchOutputAction : public Action<Ts...> {
81 public:
82 explicit SwitchOutputAction(Router *parent) : parent_(parent) {}
84 void play(const Ts &...x) override { this->parent_->switch_to_output(this->target_.value(x...)); }
85
86 protected:
88};
89
90} // namespace esphome::router
91
92#endif // USE_ESP32
virtual void play(const Ts &...x)=0
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:529
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 dump_config() 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:43
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5