From c245c554e2321308cbf2e12e4a8f1a457fd6da40 Mon Sep 17 00:00:00 2001 From: NP v/d Spek Date: Fri, 25 Oct 2024 21:06:35 +0200 Subject: [PATCH] make sure that only the component locking is in place. --- .../i2s_audio/speaker/i2s_audio_speaker.cpp | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp index f56b39b85b..0b9c628543 100644 --- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp +++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp @@ -26,6 +26,7 @@ static const char *const TAG = "i2s_audio.speaker"; enum SpeakerEventGroupBits : uint32_t { COMMAND_START = (1 << 0), // Starts the main task purpose COMMAND_STOP = (1 << 1), // stops the main task + COMMAND_STOP_GRACEFULLY = (1 << 2), // Stops the task once all data has been written MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE = (1 << 5), // Locks the ring buffer when not set STATE_STARTING = (1 << 10), STATE_RUNNING = (1 << 11), @@ -236,15 +237,17 @@ bool I2SAudioSpeaker::has_buffered_data() const { void I2SAudioSpeaker::speaker_task(void *params) { I2SAudioSpeaker *this_speaker = (I2SAudioSpeaker *) params; - uint32_t event_group_bits = xEventGroupWaitBits( - this_speaker->event_group_, - SpeakerEventGroupBits::COMMAND_START | SpeakerEventGroupBits::COMMAND_STOP, // Bit message to read - pdTRUE, // Clear the bits on exit - pdFALSE, // Don't wait for all the bits, - portMAX_DELAY); // Block indefinitely until a bit is set + uint32_t event_group_bits = + xEventGroupWaitBits(this_speaker->event_group_, + SpeakerEventGroupBits::COMMAND_START | SpeakerEventGroupBits::COMMAND_STOP | + SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY, // Bit message to read + pdTRUE, // Clear the bits on exit + pdFALSE, // Don't wait for all the bits, + portMAX_DELAY); // Block indefinitely until a bit is set - if (event_group_bits & (SpeakerEventGroupBits::COMMAND_STOP)) { - // Received a stop signal before the task was requested to start + if (event_group_bits & (SpeakerEventGroupBits::COMMAND_STOP | + SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY)) { // Received a stop signal before the task + // was requested to start this_speaker->delete_task_(0); } @@ -285,6 +288,9 @@ void I2SAudioSpeaker::speaker_task(void *params) { if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP) { break; } + if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY) { + stop_gracefully = true; + } size_t bytes_to_read = dma_buffers_size; size_t bytes_read = this_speaker->audio_ring_buffer_->read((void *) this_speaker->data_buffer_, bytes_to_read, @@ -312,6 +318,15 @@ void I2SAudioSpeaker::speaker_task(void *params) { if (bytes_written != bytes_read) { xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_SIZE); } + + } else { + // No data received + + if (stop_gracefully) { + break; + } + + i2s_zero_dma_buffer(this_speaker->parent_->get_port()); } } } else { @@ -367,7 +382,7 @@ void I2SAudioSpeaker::stop_(bool wait_on_empty) { return; if (wait_on_empty) { - this->state_ = speaker::STATE_STOPPING; + xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY); } else { xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP); }