Tune buffer sizes and locations for I2S

custom
jacqueline 2 years ago
parent 4d99d22e10
commit 8608f9367f
  1. 15
      src/audio/audio_converter.cpp
  2. 6
      src/audio/audio_decoder.cpp
  3. 13
      src/audio/audio_fsm.cpp
  4. 7
      src/audio/bt_audio_output.cpp
  5. 5
      src/audio/i2s_audio_output.cpp
  6. 5
      src/audio/include/audio_sink.hpp
  7. 2
      src/audio/include/bt_audio_output.hpp
  8. 3
      src/audio/include/i2s_audio_output.hpp
  9. 9
      src/drivers/i2s_dac.cpp
  10. 8
      src/drivers/include/i2s_dac.hpp
  11. 2
      src/memory/allocator.cpp
  12. 4
      src/tasks/tasks.cpp

@ -15,6 +15,7 @@
#include "esp_log.h" #include "esp_log.h"
#include "freertos/portmacro.h" #include "freertos/portmacro.h"
#include "freertos/projdefs.h" #include "freertos/projdefs.h"
#include "i2s_dac.hpp"
#include "idf_additions.h" #include "idf_additions.h"
#include "resample.hpp" #include "resample.hpp"
@ -23,9 +24,9 @@
static constexpr char kTag[] = "mixer"; static constexpr char kTag[] = "mixer";
static constexpr std::size_t kSampleBufferLength = 240 * 2 * 64; static constexpr std::size_t kSampleBufferLength =
static constexpr std::size_t kSourceBufferLength = drivers::kI2SBufferLengthFrames * sizeof(sample::Sample) * 2;
kSampleBufferLength * 2 * sizeof(sample::Sample); static constexpr std::size_t kSourceBufferLength = kSampleBufferLength * 2;
namespace audio { namespace audio {
@ -33,18 +34,18 @@ SampleConverter::SampleConverter()
: commands_(xQueueCreate(1, sizeof(Args))), : commands_(xQueueCreate(1, sizeof(Args))),
resampler_(nullptr), resampler_(nullptr),
source_(xStreamBufferCreateWithCaps(kSourceBufferLength, source_(xStreamBufferCreateWithCaps(kSourceBufferLength,
1, sizeof(sample::Sample) * 2,
MALLOC_CAP_SPIRAM)) { MALLOC_CAP_DMA)) {
input_buffer_ = { input_buffer_ = {
reinterpret_cast<sample::Sample*>(heap_caps_calloc( reinterpret_cast<sample::Sample*>(heap_caps_calloc(
kSampleBufferLength, sizeof(sample::Sample), MALLOC_CAP_SPIRAM)), kSampleBufferLength, sizeof(sample::Sample), MALLOC_CAP_DMA)),
kSampleBufferLength}; kSampleBufferLength};
input_buffer_as_bytes_ = {reinterpret_cast<std::byte*>(input_buffer_.data()), input_buffer_as_bytes_ = {reinterpret_cast<std::byte*>(input_buffer_.data()),
input_buffer_.size_bytes()}; input_buffer_.size_bytes()};
resampled_buffer_ = { resampled_buffer_ = {
reinterpret_cast<sample::Sample*>(heap_caps_calloc( reinterpret_cast<sample::Sample*>(heap_caps_calloc(
kSampleBufferLength, sizeof(sample::Sample), MALLOC_CAP_SPIRAM)), kSampleBufferLength, sizeof(sample::Sample), MALLOC_CAP_DMA)),
kSampleBufferLength}; kSampleBufferLength};
tasks::StartPersistent<tasks::Type::kAudioConverter>([&]() { Main(); }); tasks::StartPersistent<tasks::Type::kAudioConverter>([&]() { Main(); });

@ -26,6 +26,7 @@
#include "freertos/projdefs.h" #include "freertos/projdefs.h"
#include "freertos/queue.h" #include "freertos/queue.h"
#include "freertos/ringbuf.h" #include "freertos/ringbuf.h"
#include "i2s_dac.hpp"
#include "span.hpp" #include "span.hpp"
#include "audio_converter.hpp" #include "audio_converter.hpp"
@ -46,7 +47,8 @@ namespace audio {
static const char* kTag = "audio_dec"; static const char* kTag = "audio_dec";
static constexpr std::size_t kCodecBufferLength = 240 * 4 * 64; static constexpr std::size_t kCodecBufferLength =
drivers::kI2SBufferLengthFrames * sizeof(sample::Sample) * 2;
Timer::Timer(const codecs::ICodec::OutputFormat& format) Timer::Timer(const codecs::ICodec::OutputFormat& format)
: current_seconds_(0), : current_seconds_(0),
@ -93,7 +95,7 @@ Decoder::Decoder(std::shared_ptr<IAudioSource> source,
ESP_LOGI(kTag, "allocating codec buffer, %u KiB", kCodecBufferLength / 1024); ESP_LOGI(kTag, "allocating codec buffer, %u KiB", kCodecBufferLength / 1024);
codec_buffer_ = { codec_buffer_ = {
reinterpret_cast<sample::Sample*>(heap_caps_calloc( reinterpret_cast<sample::Sample*>(heap_caps_calloc(
kCodecBufferLength, sizeof(sample::Sample), MALLOC_CAP_SPIRAM)), kCodecBufferLength, sizeof(sample::Sample), MALLOC_CAP_DMA)),
kCodecBufferLength}; kCodecBufferLength};
} }

@ -26,7 +26,9 @@
#include "future_fetcher.hpp" #include "future_fetcher.hpp"
#include "i2s_audio_output.hpp" #include "i2s_audio_output.hpp"
#include "i2s_dac.hpp" #include "i2s_dac.hpp"
#include "idf_additions.h"
#include "nvs.hpp" #include "nvs.hpp"
#include "sample.hpp"
#include "service_locator.hpp" #include "service_locator.hpp"
#include "system_events.hpp" #include "system_events.hpp"
#include "track.hpp" #include "track.hpp"
@ -103,10 +105,17 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) {
return; return;
} }
constexpr size_t kDrainBufferSize =
drivers::kI2SBufferLengthFrames * sizeof(sample::Sample) * 2 * 8;
ESP_LOGI(kTag, "allocating drain buffer, size %u KiB",
kDrainBufferSize / 1024);
StreamBufferHandle_t stream = xStreamBufferCreateWithCaps(
kDrainBufferSize, sizeof(sample::Sample) * 2, MALLOC_CAP_DMA);
sFileSource.reset(new FatfsAudioInput(sServices->tag_parser())); sFileSource.reset(new FatfsAudioInput(sServices->tag_parser()));
sI2SOutput.reset(new I2SAudioOutput(sServices->gpios(), sI2SOutput.reset(new I2SAudioOutput(stream, sServices->gpios(),
std::unique_ptr<drivers::I2SDac>{*dac})); std::unique_ptr<drivers::I2SDac>{*dac}));
sBtOutput.reset(new BluetoothAudioOutput(sServices->bluetooth())); sBtOutput.reset(new BluetoothAudioOutput(stream, sServices->bluetooth()));
auto& nvs = sServices->nvs(); auto& nvs = sServices->nvs();
sI2SOutput->SetMaxVolume(nvs.AmpMaxVolume().get()); sI2SOutput->SetMaxVolume(nvs.AmpMaxVolume().get());

@ -27,10 +27,9 @@ static const char* kTag = "BTOUT";
namespace audio { namespace audio {
static constexpr size_t kDrainBufferSize = 48 * 1024; BluetoothAudioOutput::BluetoothAudioOutput(StreamBufferHandle_t s,
drivers::Bluetooth& bt)
BluetoothAudioOutput::BluetoothAudioOutput(drivers::Bluetooth& bt) : IAudioOutput(s), bluetooth_(bt) {}
: IAudioOutput(kDrainBufferSize, MALLOC_CAP_SPIRAM), bluetooth_(bt) {}
BluetoothAudioOutput::~BluetoothAudioOutput() {} BluetoothAudioOutput::~BluetoothAudioOutput() {}

@ -42,9 +42,10 @@ static constexpr uint16_t kDefaultVolume = 0x100;
static constexpr size_t kDrainBufferSize = 8 * 1024; static constexpr size_t kDrainBufferSize = 8 * 1024;
I2SAudioOutput::I2SAudioOutput(drivers::IGpios& expander, I2SAudioOutput::I2SAudioOutput(StreamBufferHandle_t s,
drivers::IGpios& expander,
std::unique_ptr<drivers::I2SDac> dac) std::unique_ptr<drivers::I2SDac> dac)
: IAudioOutput(kDrainBufferSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), : IAudioOutput(s),
expander_(expander), expander_(expander),
dac_(std::move(dac)), dac_(std::move(dac)),
current_config_(), current_config_(),

@ -27,10 +27,9 @@ class IAudioOutput {
StreamBufferHandle_t stream_; StreamBufferHandle_t stream_;
public: public:
IAudioOutput(size_t buffer_size, uint32_t caps) IAudioOutput(StreamBufferHandle_t stream) : stream_(stream) {}
: stream_(xStreamBufferCreateWithCaps(buffer_size, 1, caps)) {}
virtual ~IAudioOutput() { vStreamBufferDeleteWithCaps(stream_); } virtual ~IAudioOutput() {}
/* /*
* Indicates whether this output is currently being sent samples. If this is * Indicates whether this output is currently being sent samples. If this is

@ -21,7 +21,7 @@ namespace audio {
class BluetoothAudioOutput : public IAudioOutput { class BluetoothAudioOutput : public IAudioOutput {
public: public:
BluetoothAudioOutput(drivers::Bluetooth& bt); BluetoothAudioOutput(StreamBufferHandle_t, drivers::Bluetooth& bt);
~BluetoothAudioOutput(); ~BluetoothAudioOutput();
auto SetInUse(bool) -> void override; auto SetInUse(bool) -> void override;

@ -20,7 +20,8 @@ namespace audio {
class I2SAudioOutput : public IAudioOutput { class I2SAudioOutput : public IAudioOutput {
public: public:
I2SAudioOutput(drivers::IGpios& expander, I2SAudioOutput(StreamBufferHandle_t,
drivers::IGpios& expander,
std::unique_ptr<drivers::I2SDac> dac); std::unique_ptr<drivers::I2SDac> dac);
~I2SAudioOutput(); ~I2SAudioOutput();

@ -41,8 +41,13 @@ static const i2s_port_t kI2SPort = I2S_NUM_0;
auto I2SDac::create(IGpios& expander) -> std::optional<I2SDac*> { auto I2SDac::create(IGpios& expander) -> std::optional<I2SDac*> {
i2s_chan_handle_t i2s_handle; i2s_chan_handle_t i2s_handle;
i2s_chan_config_t channel_config = i2s_chan_config_t channel_config{
I2S_CHANNEL_DEFAULT_CONFIG(kI2SPort, I2S_ROLE_MASTER); .id = kI2SPort,
.role = I2S_ROLE_MASTER,
.dma_desc_num = 2,
.dma_frame_num = kI2SBufferLengthFrames,
.auto_clear = false,
};
ESP_ERROR_CHECK(i2s_new_channel(&channel_config, &i2s_handle, NULL)); ESP_ERROR_CHECK(i2s_new_channel(&channel_config, &i2s_handle, NULL));
// //

@ -18,7 +18,6 @@
#include "esp_err.h" #include "esp_err.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h" #include "freertos/portmacro.h"
#include "freertos/ringbuf.h"
#include "freertos/stream_buffer.h" #include "freertos/stream_buffer.h"
#include "result.hpp" #include "result.hpp"
#include "span.hpp" #include "span.hpp"
@ -28,6 +27,13 @@
namespace drivers { namespace drivers {
// DMA max buffer size for I2S is 4092. We normalise to 2-channel, 16 bit
// audio, which gives us a max of 4092 / 2 / 2 (16 bits) frames. This in turn
// means that at 48kHz, we have about 21ms of budget to fill each buffer.
// We base this off of the maximum DMA size in order to minimise the amount of
// work the CPU has to do to service the DMA callbacks.
constexpr size_t kI2SBufferLengthFrames = 1023;
/** /**
* Interface for a DAC that receives PCM samples over I2S. * Interface for a DAC that receives PCM samples over I2S.
*/ */

@ -13,7 +13,7 @@ void* operator new[](std::size_t sz) {
++sz; // avoid std::malloc(0) which may return nullptr on success ++sz; // avoid std::malloc(0) which may return nullptr on success
} }
if (sz > 256) { if (sz > 512) {
return heap_caps_malloc(sz, MALLOC_CAP_SPIRAM); return heap_caps_malloc(sz, MALLOC_CAP_SPIRAM);
} }

@ -107,11 +107,11 @@ auto Priority() -> UBaseType_t;
// highest priority. // highest priority.
template <> template <>
auto Priority<Type::kAudioDecoder>() -> UBaseType_t { auto Priority<Type::kAudioDecoder>() -> UBaseType_t {
return 18; return configMAX_PRIORITIES - 1;
} }
template <> template <>
auto Priority<Type::kAudioConverter>() -> UBaseType_t { auto Priority<Type::kAudioConverter>() -> UBaseType_t {
return 18; return configMAX_PRIORITIES - 1;
} }
// After audio issues, UI jank is the most noticeable kind of scheduling-induced // After audio issues, UI jank is the most noticeable kind of scheduling-induced
// slowness that the user is likely to notice or care about. Therefore we place // slowness that the user is likely to notice or care about. Therefore we place

Loading…
Cancel
Save