ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
i2s_audio_speaker.cpp
Go to the documentation of this file.
1#include "i2s_audio_speaker.h"
2
3#ifdef USE_ESP32
4
5#include <driver/gpio.h>
6#include <driver/i2s_std.h>
7
10
12#include "esphome/core/hal.h"
13#include "esphome/core/log.h"
14
15#include "esp_timer.h"
16
17// esp-audio-libs
18#include <gain.h>
19
20namespace esphome::i2s_audio {
21
22static const char *const TAG = "i2s_audio.speaker";
23
24// Software volume control maps the user-facing [0.0, 1.0] range to a Q31 scale factor.
25// Volumes in (0.0, 1.0) map linearly to a dB reduction in [-49.0, 0.0] dB.
26static constexpr float SOFTWARE_VOLUME_MIN_DB = -49.0f;
27
29 this->event_group_ = xEventGroupCreate();
30
31 if (this->event_group_ == nullptr) {
32 ESP_LOGE(TAG, "Event group creation failed");
33 this->mark_failed();
34 return;
35 }
36
37 // Initialize volume control. When audio_dac is configured, this sets the DAC volume.
38 // When no audio_dac is configured, this initializes software volume control.
39 this->set_volume(this->volume_);
40}
41
43 ESP_LOGCONFIG(TAG,
44 "Speaker:\n"
45 " Pin: %d\n"
46 " Buffer duration: %" PRIu32,
47 static_cast<int8_t>(this->dout_pin_), this->buffer_duration_ms_);
48 if (this->timeout_.has_value()) {
49 ESP_LOGCONFIG(TAG, " Timeout: %" PRIu32 " ms", this->timeout_.value());
50 }
51}
52
54 uint32_t event_group_bits = xEventGroupGetBits(this->event_group_);
55
56 if ((event_group_bits & SpeakerEventGroupBits::COMMAND_START) && (this->state_ == speaker::STATE_STOPPED)) {
58 xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::COMMAND_START);
59 }
60
61 // Handle the task's state
62 if (event_group_bits & SpeakerEventGroupBits::TASK_STARTING) {
63 ESP_LOGD(TAG, "Starting");
64 xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::TASK_STARTING);
65 }
66 if (event_group_bits & SpeakerEventGroupBits::TASK_RUNNING) {
67 ESP_LOGV(TAG, "Started");
68 xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::TASK_RUNNING);
70 }
71 if (event_group_bits & SpeakerEventGroupBits::TASK_STOPPING) {
72 ESP_LOGV(TAG, "Stopping");
73 // Lockstep-breaking error bits are latched by the task and cleared along with all other bits
74 // when TASK_STOPPED is processed; log them here, exactly once, as the task winds down.
75 if (event_group_bits & SpeakerEventGroupBits::ERR_DROPPED_EVENT) {
76 ESP_LOGE(TAG, "ISR event queue overflow, restarting speaker task to recover timestamp sync");
77 }
78 if (event_group_bits & SpeakerEventGroupBits::ERR_PARTIAL_WRITE) {
79 ESP_LOGE(TAG, "Partial DMA write broke buffer alignment, restarting speaker task");
80 }
81 if (event_group_bits & SpeakerEventGroupBits::ERR_LOCKSTEP_DESYNC) {
82 ESP_LOGE(TAG, "Event/record queues desynced, restarting speaker task");
83 }
84 xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::TASK_STOPPING);
86 }
87 if (event_group_bits & SpeakerEventGroupBits::TASK_STOPPED) {
88 ESP_LOGD(TAG, "Stopped");
89
90 vTaskDelete(this->speaker_task_handle_);
91 this->speaker_task_handle_ = nullptr;
92
93 this->stop_i2s_driver_();
94 xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::ALL_BITS);
95 this->status_clear_error();
96
97 this->on_task_stopped();
98
100 }
101
102 if (event_group_bits & SpeakerEventGroupBits::ERR_ESP_NO_MEM) {
103 ESP_LOGE(TAG, "Speaker task setup failed (allocation, preload, or channel enable)");
104 xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_NO_MEM);
105 }
106
107 // Handle the speaker's state
108 switch (this->state_) {
110 if (this->status_has_error()) {
111 break;
112 }
113
114 if (this->start_i2s_driver(this->audio_stream_info_) != ESP_OK) {
115 ESP_LOGE(TAG, "Driver failed to start; retrying in 1 second");
116 this->status_momentary_error("driver-failure", 1000);
117 break;
118 }
119
120 if (this->speaker_task_handle_ == nullptr) {
121 xTaskCreate(I2SAudioSpeakerBase::speaker_task, "speaker_task", TASK_STACK_SIZE, (void *) this, TASK_PRIORITY,
122 &this->speaker_task_handle_);
123
124 if (this->speaker_task_handle_ == nullptr) {
125 ESP_LOGE(TAG, "Task failed to start, retrying in 1 second");
126 this->status_momentary_error("task-failure", 1000);
127 this->stop_i2s_driver_(); // Stops the driver to return the lock; will be reloaded in next attempt
128 }
129 }
130 break;
131 case speaker::STATE_RUNNING: // Intentional fallthrough
132 case speaker::STATE_STOPPING: // Intentional fallthrough
134 break;
135 }
136}
137
138void I2SAudioSpeakerBase::set_volume(float volume) {
139 this->volume_ = volume;
140#ifdef USE_AUDIO_DAC
141 if (this->audio_dac_ != nullptr) {
142 if (volume > 0.0) {
143 this->audio_dac_->set_mute_off();
144 }
145 this->audio_dac_->set_volume(volume);
146 } else
147#endif // USE_AUDIO_DAC
148 {
149 // Fallback to software volume control by using a Q31 fixed point scaling factor.
150 // At maximum volume (1.0), set to INT32_MAX to bypass volume processing entirely
151 // and avoid any floating-point precision issues that could cause slight volume reduction.
152 if (volume >= 1.0f) {
153 this->q31_volume_factor_ = INT32_MAX;
154 } else if (volume <= 0.0f) {
155 this->q31_volume_factor_ = 0;
156 } else {
157 this->q31_volume_factor_ =
158 esp_audio_libs::gain::db_to_q31(remap<float, float>(volume, 0.0f, 1.0f, SOFTWARE_VOLUME_MIN_DB, 0.0f));
159 }
160 }
161}
162
163void I2SAudioSpeakerBase::set_mute_state(bool mute_state) {
164 this->mute_state_ = mute_state;
165#ifdef USE_AUDIO_DAC
166 if (this->audio_dac_) {
167 if (mute_state) {
168 this->audio_dac_->set_mute_on();
169 } else {
170 this->audio_dac_->set_mute_off();
171 }
172 } else
173#endif // USE_AUDIO_DAC
174 {
175 if (mute_state) {
176 // Fallback to software volume control and scale by 0
177 this->q31_volume_factor_ = 0;
178 } else {
179 // Revert to previous volume when unmuting
180 this->set_volume(this->volume_);
181 }
182 }
183}
184
185size_t I2SAudioSpeakerBase::play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) {
186 if (this->is_failed()) {
187 ESP_LOGE(TAG, "Setup failed; cannot play audio");
188 return 0;
189 }
190
192 this->start();
193 }
194
195 if (this->state_ != speaker::STATE_RUNNING) {
196 // Unable to write data to a running speaker, so delay the max amount of time so it can get ready
197 vTaskDelay(ticks_to_wait);
198 ticks_to_wait = 0;
199 }
200
201 size_t bytes_written = 0;
202 if (this->state_ == speaker::STATE_RUNNING) {
203 std::shared_ptr<ring_buffer::RingBuffer> temp_ring_buffer = this->audio_ring_buffer_.lock();
204 if (temp_ring_buffer != nullptr) {
205 // The weak_ptr locks successfully only while the speaker task owns the ring buffer, so it is safe to write
206 bytes_written = temp_ring_buffer->write_without_replacement((void *) data, length, ticks_to_wait);
207 }
208 }
209
210 return bytes_written;
211}
212
214 if (this->audio_ring_buffer_.use_count() > 0) {
215 std::shared_ptr<ring_buffer::RingBuffer> temp_ring_buffer = this->audio_ring_buffer_.lock();
216 return temp_ring_buffer->available() > 0;
217 }
218 return false;
219}
220
221void I2SAudioSpeakerBase::speaker_task(void *params) {
222 I2SAudioSpeakerBase *this_speaker = (I2SAudioSpeakerBase *) params;
223 this_speaker->run_speaker_task();
224}
225
227 if (!this->is_ready() || this->is_failed() || this->status_has_error())
228 return;
229 if ((this->state_ == speaker::STATE_STARTING) || (this->state_ == speaker::STATE_RUNNING))
230 return;
231
232 // Mark STARTING immediately to avoid transient STOPPED observations before loop() processes COMMAND_START.
234 xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_START);
235}
236
237void I2SAudioSpeakerBase::stop() { this->stop_(false); }
238
239void I2SAudioSpeakerBase::finish() { this->stop_(true); }
240
241void I2SAudioSpeakerBase::stop_(bool wait_on_empty) {
242 if (this->is_failed())
243 return;
244 if (this->state_ == speaker::STATE_STOPPED)
245 return;
246
247 if (wait_on_empty) {
249 } else {
250 xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP);
251 }
252}
253
254esp_err_t I2SAudioSpeakerBase::init_i2s_channel_(const i2s_chan_config_t &chan_cfg, const i2s_std_config_t &std_cfg,
255 size_t event_queue_size) {
256 esp_err_t err = i2s_new_channel(&chan_cfg, &this->tx_handle_, NULL);
257 if (err != ESP_OK) {
258 ESP_LOGE(TAG, "I2S channel allocation failed: %s", esp_err_to_name(err));
259 this->parent_->unlock();
260 return err;
261 }
262
263 err = i2s_channel_init_std_mode(this->tx_handle_, &std_cfg);
264 if (err != ESP_OK) {
265 ESP_LOGE(TAG, "Failed to initialize I2S channel");
266 i2s_del_channel(this->tx_handle_);
267 this->tx_handle_ = nullptr;
268 this->parent_->unlock();
269 return err;
270 }
271
272 if (this->i2s_event_queue_ == nullptr) {
273 this->i2s_event_queue_ = xQueueCreate(event_queue_size, sizeof(int64_t));
274 } else {
275 // Reset queue to clear any stale events from previous task
276 xQueueReset(this->i2s_event_queue_);
277 }
278
279 // Lockstep records queue. One record per in-flight DMA buffer; sized to match the I2S event queue
280 // so a fully-saturated DMA pipeline cannot overflow either side before drain.
281 if (this->write_records_queue_ == nullptr) {
282 this->write_records_queue_ = xQueueCreate(event_queue_size, sizeof(uint32_t));
283 } else {
284 xQueueReset(this->write_records_queue_);
285 }
286
287 if (this->i2s_event_queue_ == nullptr || this->write_records_queue_ == nullptr) {
288 ESP_LOGE(TAG, "Failed to allocate I2S event queue(s)");
289 i2s_del_channel(this->tx_handle_);
290 this->tx_handle_ = nullptr;
291 this->parent_->unlock();
292 return ESP_ERR_NO_MEM;
293 }
294
295 return ESP_OK;
296}
297
299 if (this->tx_handle_ != nullptr) {
300 i2s_channel_disable(this->tx_handle_);
301 i2s_del_channel(this->tx_handle_);
302 this->tx_handle_ = nullptr;
303
304 // i2s_del_channel() leaves dout wired to this port's data-out signal in the GPIO matrix: it only
305 // clears an internal reservation mask, never the esp_rom_gpio_connect_out_signal() routing that
306 // setup installed. If another speaker reuses this port (shared bus), its audio still reaches our
307 // dout. Detach the pin and drive it low so a stale output stops driving downstream hardware: a
308 // SPDIF optical transmitter would otherwise stay lit, and an analog DAC would emit noise.
309 gpio_reset_pin(this->dout_pin_);
310 gpio_set_direction(this->dout_pin_, GPIO_MODE_OUTPUT);
311 gpio_set_level(this->dout_pin_, 0);
312 }
313 this->parent_->unlock();
314}
315
316bool IRAM_ATTR I2SAudioSpeakerBase::i2s_on_sent_cb(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx) {
317 int64_t now = esp_timer_get_time();
318
319 BaseType_t need_yield1 = pdFALSE;
320 BaseType_t need_yield2 = pdFALSE;
321 BaseType_t need_yield3 = pdFALSE;
322
323 I2SAudioSpeakerBase *this_speaker = (I2SAudioSpeakerBase *) user_ctx;
324
325 if (xQueueIsQueueFullFromISR(this_speaker->i2s_event_queue_)) {
326 // Queue is full, so discard the oldest event. Once we drop a completion event, ``i2s_event_queue_``
327 // and any per-buffer record queue maintained by the task are permanently desynced, so the task
328 // must restart to recover. Set both ERR_DROPPED_EVENT (so loop() can log it) and COMMAND_STOP
329 // (so the task bails immediately, closing the race where loop() could clear the error bit
330 // before the task observes it).
331 int64_t dummy;
332 xQueueReceiveFromISR(this_speaker->i2s_event_queue_, &dummy, &need_yield1);
333 xEventGroupSetBitsFromISR(this_speaker->event_group_,
335 &need_yield2);
336 }
337
338 xQueueSendToBackFromISR(this_speaker->i2s_event_queue_, &now, &need_yield3);
339
340 return need_yield1 | need_yield2 | need_yield3;
341}
342
343void I2SAudioSpeakerBase::apply_software_volume_(uint8_t *data, size_t bytes_read) {
344 if (this->q31_volume_factor_ == INT32_MAX) {
345 return; // Max volume, no processing needed
346 }
347
348 const size_t bytes_per_sample = this->current_stream_info_.samples_to_bytes(1);
349 const uint32_t len = bytes_read / bytes_per_sample;
350
351 esp_audio_libs::gain::apply(data, data, this->q31_volume_factor_, len, bytes_per_sample);
352}
353
354void I2SAudioSpeakerBase::swap_esp32_mono_samples_(uint8_t *data, size_t bytes_read) {
355#ifdef USE_ESP32_VARIANT_ESP32
356 // For ESP32 16-bit mono mode, adjacent samples need to be swapped.
357 if (this->current_stream_info_.get_channels() == 1 && this->current_stream_info_.get_bits_per_sample() == 16) {
358 int16_t *samples = reinterpret_cast<int16_t *>(data);
359 size_t sample_count = bytes_read / sizeof(int16_t);
360 for (size_t i = 0; i + 1 < sample_count; i += 2) {
361 int16_t tmp = samples[i];
362 samples[i] = samples[i + 1];
363 samples[i + 1] = tmp;
364 }
365 }
366#endif // USE_ESP32_VARIANT_ESP32
367}
368
369} // namespace esphome::i2s_audio
370
371#endif // USE_ESP32
void mark_failed()
Mark this component as failed.
void status_momentary_error(const char *name, uint32_t length=5000)
Set error status flag and automatically clear it after a timeout.
bool is_failed() const
Definition component.h:272
void status_clear_error()
Definition component.h:295
bool is_ready() const
bool status_has_error() const
Definition component.h:280
size_t samples_to_bytes(uint32_t samples) const
Converts samples to bytes.
Definition audio.h:58
uint8_t get_channels() const
Definition audio.h:29
virtual bool set_mute_off()=0
virtual bool set_volume(float volume)=0
virtual bool set_mute_on()=0
Abstract base class for I2S audio speaker implementations.
static bool i2s_on_sent_cb(i2s_chan_handle_t handle, i2s_event_data_t *event, void *user_ctx)
Callback function used to send playback timestamps to the speaker task.
void stop_i2s_driver_()
Stops the I2S driver and unlocks the I2S port.
void apply_software_volume_(uint8_t *data, size_t bytes_read)
Apply software volume control using Q15 fixed-point scaling.
std::weak_ptr< ring_buffer::RingBuffer > audio_ring_buffer_
void swap_esp32_mono_samples_(uint8_t *data, size_t bytes_read)
Swap adjacent 16-bit mono samples for ESP32 (non-variant) hardware quirk.
virtual esp_err_t start_i2s_driver(audio::AudioStreamInfo &audio_stream_info)=0
Starts the ESP32 I2S driver.
esp_err_t init_i2s_channel_(const i2s_chan_config_t &chan_cfg, const i2s_std_config_t &std_cfg, size_t event_queue_size)
Shared I2S channel allocation, initialization, and event queue setup.
virtual void on_task_stopped()
Called in loop() when the task has stopped. Override for mode-specific cleanup.
void stop_(bool wait_on_empty)
Plays the provided audio data.
virtual size_t play(const uint8_t *data, size_t length)=0
Plays the provided audio data.
virtual void set_volume(float volume)
Definition speaker.h:70
audio_dac::AudioDac * audio_dac_
Definition speaker.h:119
virtual void set_mute_state(bool mute_state)
Definition speaker.h:80
virtual bool has_buffered_data() const =0
audio::AudioStreamInfo audio_stream_info_
Definition speaker.h:114
const void size_t len
Definition hal.h:64
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).
Definition helpers.h:765
int64_t esp_timer_get_time(void)
static void uint32_t
uint16_t length
Definition tt21100.cpp:0
spi_device_handle_t handle