86 const uint32_t dma_buffers_duration_ms = DMA_BUFFER_DURATION_MS * DMA_BUFFERS_COUNT;
94 const bool pair_aligned = source_alignment > bytes_per_frame;
96 const size_t ring_buffer_size =
103 const bool narrowing = input_bytes_per_sample > output_bytes_per_sample;
104#ifdef USE_ESP32_VARIANT_ESP32
105 const bool widening = input_bytes_per_sample < output_bytes_per_sample;
113 size_t dma_buffer_bytes;
114 i2s_chan_info_t chan_info;
115 if (i2s_channel_get_info(this->
tx_handle_, &chan_info) == ESP_OK && chan_info.total_dma_buf_size > 0) {
117 dma_buffer_bytes = chan_info.total_dma_buf_size / DMA_BUFFERS_COUNT;
128 bool successful_setup =
false;
130 std::unique_ptr<audio::RingBufferAudioSource> audio_source;
134 uint8_t *silence_buffer = silence_allocator.
allocate(dma_buffer_bytes);
136 if (silence_buffer !=
nullptr) {
137 memset(silence_buffer, 0, dma_buffer_bytes);
141 static_cast<uint8_t
>(source_alignment));
143 if (audio_source !=
nullptr) {
146 successful_setup =
true;
152 auto preload_silence = [&]() ->
bool {
153 for (
size_t i = 0; i < DMA_BUFFERS_COUNT; i++) {
154 size_t bytes_loaded = 0;
155 esp_err_t err = i2s_channel_preload_data(this->
tx_handle_, silence_buffer, dma_buffer_bytes, &bytes_loaded);
156 if (err != ESP_OK || bytes_loaded != dma_buffer_bytes) {
157 ESP_LOGV(TAG,
"Failed to preload silence into DMA buffer %u (err=%d, loaded=%u)", (
unsigned) i, (
int) err,
158 (
unsigned) bytes_loaded);
164 ESP_LOGV(TAG,
"Failed to push preload write record");
171 if (successful_setup) {
172 successful_setup = preload_silence();
175 if (successful_setup) {
178 const i2s_event_callbacks_t callbacks = {.on_sent = i2s_on_sent_cb};
179 i2s_channel_register_event_callback(this->
tx_handle_, &callbacks,
this);
181 if (i2s_channel_enable(this->
tx_handle_) != ESP_OK) {
182 ESP_LOGV(TAG,
"Failed to enable I2S channel");
183 successful_setup =
false;
187 if (!successful_setup) {
190 bool stop_gracefully =
false;
195 bool resync_needed =
false;
218 xEventGroupClearBits(this->
event_group_, SpeakerEventGroupBits::COMMAND_STOP);
219 ESP_LOGV(TAG,
"Exiting: COMMAND_STOP received");
223 xEventGroupClearBits(this->
event_group_, SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY);
224 stop_gracefully =
true;
229 ESP_LOGV(TAG,
"Exiting: stream info changed");
234 ESP_LOGE(TAG,
"ISR event queue overflow, resyncing DMA lockstep");
235 resync_needed =
true;
239 const bool resynced = this->resync_lockstep_(unrecorded_frames, preload_silence);
240 unrecorded_frames = 0;
241 pending_real_buffers = 0;
242 resync_needed =
false;
244 ESP_LOGE(TAG,
"DMA lockstep resync failed, restarting speaker task");
252 int64_t write_timestamp;
253 bool lockstep_broken =
false;
258 ESP_LOGE(TAG,
"Event without matching write record, resyncing DMA lockstep");
259 lockstep_broken =
true;
262 if (real_frames > 0) {
263 pending_real_buffers--;
267 const uint32_t silence_frames = frames_per_dma_buffer - real_frames;
268 const int64_t adjusted_ts =
273 if (lockstep_broken) {
274 resync_needed =
true;
282 if (stop_gracefully && audio_source->available() == 0 && !this->has_buffered_data() &&
283 pending_real_buffers == 0) {
284 ESP_LOGV(TAG,
"Exiting: graceful stop complete");
293 size_t bytes_written_total = 0;
295 bool partial_write_failure =
false;
298 while (bytes_written_total < dma_buffer_bytes) {
299 size_t bytes_read = audio_source->fill(pdMS_TO_TICKS(DMA_BUFFER_DURATION_MS) / 2);
300 if (bytes_read > 0) {
302 uint8_t *
new_data = audio_source->mutable_data() + audio_source->available() - bytes_read;
303 this->apply_software_volume_(
new_data, bytes_read);
312 if (pair_aligned && frames_available == 0 && frames_room >= 2 && !audio_source->has_buffered_data()) {
315 const size_t real_bytes = audio_source->expose_padded_partial(bytes_per_frame);
316 if (real_bytes > 0) {
317 this->apply_software_volume_(audio_source->mutable_data(), real_bytes);
322 const uint32_t frames_to_write = std::min(frames_available, frames_room);
323 if (frames_to_write == 0) {
332 uint8_t *chunk = audio_source->mutable_data();
334#ifdef USE_ESP32_VARIANT_ESP32
344 esp_audio_libs::pcm_convert::copy_frames(chunk, chunk, input_bytes_per_sample, channels,
345 output_bytes_per_sample, channels, frames_to_write);
348 i2s_channel_write(this->
tx_handle_, chunk, output_bytes, &bw, WRITE_TIMEOUT_TICKS);
350 if (bw != output_bytes) {
353 ESP_LOGE(TAG,
"Partial DMA write (%u of %u bytes), resyncing DMA lockstep", (
unsigned) bw,
354 (
unsigned) output_bytes);
355 audio_source->consume(input_bytes);
356 real_frames_total += frames_to_write - padding_frames;
357 partial_write_failure =
true;
360 audio_source->consume(input_bytes);
361 bytes_written_total += output_bytes;
362 real_frames_total += frames_to_write - padding_frames;
363 if (padding_frames > 0) {
368 if (real_frames_total > 0) {
369 last_data_received_time =
millis();
373 if (partial_write_failure) {
374 unrecorded_frames += real_frames_total;
375 resync_needed =
true;
379 const size_t silence_bytes = dma_buffer_bytes - bytes_written_total;
380 if (silence_bytes > 0) {
382 i2s_channel_write(this->
tx_handle_, silence_buffer, silence_bytes, &bw, WRITE_TIMEOUT_TICKS);
383 if (bw != silence_bytes) {
385 ESP_LOGE(TAG,
"Partial DMA write (%u of %u bytes), resyncing DMA lockstep", (
unsigned) bw,
386 (
unsigned) silence_bytes);
387 unrecorded_frames += real_frames_total;
388 resync_needed =
true;
397 ESP_LOGE(TAG,
"Write records queue full, resyncing DMA lockstep");
398 unrecorded_frames += real_frames_total;
399 resync_needed =
true;
402 if (real_frames_total > 0) {
403 pending_real_buffers++;
410 audio_source.reset();
412 if (silence_buffer !=
nullptr) {
413 silence_allocator.
deallocate(silence_buffer, dma_buffer_bytes);
414 silence_buffer =
nullptr;
421 vTaskDelay(pdMS_TO_TICKS(10));
513 i2s_std_config_t &std_cfg)
const {
518 ESP_LOGV(TAG,
"I2S DMA config: %zu buffers x %lu frames", (
size_t) DMA_BUFFERS_COUNT,
519 (
unsigned long) dma_buffer_length);
522 .id = this->
parent_->get_port(),
524 .dma_desc_num = DMA_BUFFERS_COUNT,
525 .dma_frame_num = dma_buffer_length,
531 i2s_std_clk_config_t clk_cfg = {
540 slot_mode = I2S_SLOT_MODE_MONO;
542 slot_mode = I2S_SLOT_MODE_STEREO;
543 slot_mask = I2S_STD_SLOT_BOTH;
547 i2s_data_bit_width_t data_bit_width = (i2s_data_bit_width_t) output_stream_info.
get_bits_per_sample();
548#ifdef USE_ESP32_VARIANT_ESP32
554 i2s_std_slot_config_t slot_cfg;
557 slot_cfg = I2S_STD_PCM_SLOT_DEFAULT_CONFIG(data_bit_width, slot_mode);
560 slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(data_bit_width, slot_mode);
563 slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(data_bit_width, slot_mode);
571 slot_cfg.slot_mask = slot_mask;
573 i2s_std_gpio_config_t gpio_cfg = this->
parent_->get_pin_config();
578 .slot_cfg = slot_cfg,
579 .gpio_cfg = gpio_cfg,