From a231fd1c8afedbeb14b0bc77d76bad61db986059 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 2 May 2024 17:06:25 +1000 Subject: Replace cpp::span shim with std::span --- src/drivers/i2s_dac.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/drivers/i2s_dac.cpp') diff --git a/src/drivers/i2s_dac.cpp b/src/drivers/i2s_dac.cpp index aef466c2..7fe5547d 100644 --- a/src/drivers/i2s_dac.cpp +++ b/src/drivers/i2s_dac.cpp @@ -207,7 +207,7 @@ auto I2SDac::Reconfigure(Channels ch, BitsPerSample bps, SampleRate rate) } } -auto I2SDac::WriteData(const cpp::span& data) -> void { +auto I2SDac::WriteData(const std::span& data) -> void { std::size_t bytes_written = 0; esp_err_t err = i2s_channel_write(i2s_handle_, data.data(), data.size_bytes(), &bytes_written, portMAX_DELAY); -- cgit v1.2.3 From 26eb580043ad176bdc58d996f30d470e1073ef00 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 2 May 2024 21:52:59 +1000 Subject: move driver includes into a subdir as well --- src/drivers/i2s_dac.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/drivers/i2s_dac.cpp') diff --git a/src/drivers/i2s_dac.cpp b/src/drivers/i2s_dac.cpp index 7fe5547d..a271fce0 100644 --- a/src/drivers/i2s_dac.cpp +++ b/src/drivers/i2s_dac.cpp @@ -4,7 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-only */ -#include "i2s_dac.hpp" +#include "drivers/i2s_dac.hpp" #include #include @@ -25,11 +25,11 @@ #include "hal/gpio_types.h" #include "hal/i2c_types.h" -#include "gpios.hpp" +#include "drivers/gpios.hpp" +#include "drivers/i2c.hpp" +#include "drivers/wm8523.hpp" #include "hal/i2s_types.h" -#include "i2c.hpp" #include "soc/clk_tree_defs.h" -#include "wm8523.hpp" namespace drivers { @@ -142,8 +142,9 @@ auto I2SDac::SetPaused(bool paused) -> void { static volatile bool sSwapWords = false; -auto I2SDac::Reconfigure(Channels ch, BitsPerSample bps, SampleRate rate) - -> void { +auto I2SDac::Reconfigure(Channels ch, + BitsPerSample bps, + SampleRate rate) -> void { std::lock_guard lock(configure_mutex_); if (i2s_active_) { -- cgit v1.2.3 From 265049c5192cf0ce862c7db7b4745636afb6c17b Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 8 May 2024 16:03:03 +1000 Subject: Count samples going in and out of the drain buffer This is a more accurate way of knowing which track is playing when, and also simplifies a lot of fragile logic in audio_fsm --- src/drivers/i2s_dac.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/drivers/i2s_dac.cpp') diff --git a/src/drivers/i2s_dac.cpp b/src/drivers/i2s_dac.cpp index a271fce0..e5efe198 100644 --- a/src/drivers/i2s_dac.cpp +++ b/src/drivers/i2s_dac.cpp @@ -5,6 +5,7 @@ */ #include "drivers/i2s_dac.hpp" +#include #include #include @@ -140,11 +141,10 @@ auto I2SDac::SetPaused(bool paused) -> void { } } -static volatile bool sSwapWords = false; +DRAM_ATTR static volatile bool sSwapWords = false; -auto I2SDac::Reconfigure(Channels ch, - BitsPerSample bps, - SampleRate rate) -> void { +auto I2SDac::Reconfigure(Channels ch, BitsPerSample bps, SampleRate rate) + -> void { std::lock_guard lock(configure_mutex_); if (i2s_active_) { @@ -217,6 +217,8 @@ auto I2SDac::WriteData(const std::span& data) -> void { } } +DRAM_ATTR static volatile uint32_t sSamplesRead = 0; + extern "C" IRAM_ATTR auto callback(i2s_chan_handle_t handle, i2s_event_data_t* event, void* user_ctx) -> bool { @@ -235,6 +237,14 @@ extern "C" IRAM_ATTR auto callback(i2s_chan_handle_t handle, size_t bytes_written = xStreamBufferReceiveFromISR(src, buf, event->size, &ret); + // Assume 16 bit samples. + size_t samples = bytes_written / 2; + if (UINT32_MAX - sSamplesRead < samples) { + sSamplesRead = samples - (UINT32_MAX - sSamplesRead); + } else { + sSamplesRead = sSamplesRead + samples; + } + // The ESP32's I2S peripheral has a different endianness to its processors. // ESP-IDF handles this difference for stereo channels, but not for mono // channels. We therefore sometimes need to swap each pair of words as they're @@ -276,6 +286,10 @@ auto I2SDac::SetSource(StreamBufferHandle_t buffer) -> void { } } +auto I2SDac::SamplesUsed() -> uint32_t { + return sSamplesRead; +} + auto I2SDac::set_channel(bool enabled) -> void { if (i2s_active_ == enabled) { return; -- cgit v1.2.3