ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
speaker.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <vector>
6
7#ifdef USE_ESP32
8#include <freertos/FreeRTOS.h>
9#endif
10
13
15#ifdef USE_AUDIO_DAC
17#endif
18
19namespace esphome::speaker {
20
22static constexpr float SILENT_VOLUME_THRESHOLD = 0.001f;
23
30
31class Speaker {
32 public:
33#ifdef USE_ESP32
40 virtual size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) {
41 return this->play(data, length);
42 };
43#endif
44
51 virtual size_t play(const uint8_t *data, size_t length) = 0;
52
53 size_t play(const std::vector<uint8_t> &data) { return this->play(data.data(), data.size()); }
54
55 virtual void start() = 0;
56 virtual void stop() = 0;
57 // In compare between *STOP()* and *FINISH()*; *FINISH()* will stop after emptying the play buffer,
58 // while *STOP()* will break directly.
59 // When finish() is not implemented on the platform component it should just do a normal stop.
60 virtual void finish() { this->stop(); }
61
62 // Pauses processing incoming audio. Needs to be implemented specifically per speaker component
63 virtual void set_pause_state(bool pause_state) {}
64 virtual bool get_pause_state() const { return false; }
65
66 virtual bool has_buffered_data() const = 0;
67
68 bool is_running() const { return this->state_ == STATE_RUNNING; }
69 bool is_stopped() const { return this->state_ == STATE_STOPPED; }
70
71 // Volume and mute are independent: changing one never alters the other's stored state. Volume control is
72 // handled by a configured audio dac component. Individual speaker components can override and implement in
73 // software if an audio dac isn't available.
74 virtual void set_volume(float volume) {
75 this->volume_ = volume;
76#ifdef USE_AUDIO_DAC
77 if (this->audio_dac_ != nullptr) {
78 this->audio_dac_->set_volume(volume);
80 }
81#endif
82 };
83 virtual float get_volume() { return this->volume_; }
84
85 virtual void set_mute_state(bool mute_state) {
86 this->mute_state_ = mute_state;
87#ifdef USE_AUDIO_DAC
89#endif
90 }
91 virtual bool get_mute_state() { return this->mute_state_; }
92
93#ifdef USE_AUDIO_DAC
94 void set_audio_dac(audio_dac::AudioDac *audio_dac) { this->audio_dac_ = audio_dac; }
95#endif
96
97 void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info) {
98 this->audio_stream_info_ = audio_stream_info;
99 }
100
102
107 template<typename F> void add_audio_output_callback(F &&callback) {
108 this->audio_output_callback_.add(std::forward<F>(callback));
109 }
110
111 protected:
114 bool is_silent_() const { return this->mute_state_ || this->volume_ < SILENT_VOLUME_THRESHOLD; }
115
116#ifdef USE_AUDIO_DAC
119 if (this->audio_dac_ == nullptr)
120 return;
121 if (this->is_silent_()) {
122 this->audio_dac_->set_mute_on();
123 } else {
124 this->audio_dac_->set_mute_off();
125 }
126 }
127#endif
128
130 bool mute_state_{false};
132 float volume_{1.0f};
133
134#ifdef USE_AUDIO_DAC
136#endif
137
139};
140
141} // namespace esphome::speaker
virtual bool set_mute_off()=0
virtual bool set_volume(float volume)=0
virtual bool set_mute_on()=0
virtual size_t play(const uint8_t *data, size_t length)=0
Plays the provided audio data.
bool is_running() const
Definition speaker.h:68
virtual void set_volume(float volume)
Definition speaker.h:74
size_t play(const std::vector< uint8_t > &data)
Definition speaker.h:53
void set_audio_dac(audio_dac::AudioDac *audio_dac)
Definition speaker.h:94
virtual float get_volume()
Definition speaker.h:83
virtual bool get_pause_state() const
Definition speaker.h:64
audio_dac::AudioDac * audio_dac_
Definition speaker.h:135
virtual void set_pause_state(bool pause_state)
Definition speaker.h:63
CallbackManager< void(uint32_t, int64_t)> audio_output_callback_
Definition speaker.h:138
void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info)
Definition speaker.h:97
audio::AudioStreamInfo & get_audio_stream_info()
Definition speaker.h:101
void add_audio_output_callback(F &&callback)
Callback function for sending the duration of the audio written to the speaker since the last callbac...
Definition speaker.h:107
virtual bool get_mute_state()
Definition speaker.h:91
virtual void set_mute_state(bool mute_state)
Definition speaker.h:85
virtual bool has_buffered_data() const =0
audio::AudioStreamInfo audio_stream_info_
Definition speaker.h:131
virtual void start()=0
virtual void finish()
Definition speaker.h:60
bool is_stopped() const
Definition speaker.h:69
bool is_silent_() const
Whether the output should be silent: muted, or the volume is effectively zero.
Definition speaker.h:114
void apply_audio_dac_mute_()
Uses the audio dac's mute as the silence mechanism, since a dac's minimum volume is often audible.
Definition speaker.h:118
virtual void stop()=0
static void uint32_t
uint16_t length
Definition tt21100.cpp:0