From 941bafca17b13547a88668b787ce4c8e064ef7ff Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 21 Feb 2023 13:16:58 +1100 Subject: Add a memory arena for the audio pipeline --- src/audio/fatfs_audio_input.cpp | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/audio/fatfs_audio_input.cpp') diff --git a/src/audio/fatfs_audio_input.cpp b/src/audio/fatfs_audio_input.cpp index fd1c1f3a..829064d8 100644 --- a/src/audio/fatfs_audio_input.cpp +++ b/src/audio/fatfs_audio_input.cpp @@ -5,6 +5,7 @@ #include #include +#include "arena.hpp" #include "esp_heap_caps.h" #include "freertos/portmacro.h" @@ -19,11 +20,12 @@ static const char* kTag = "SRC"; namespace audio { -// 32KiB to match the minimum himen region size. static const std::size_t kChunkSize = 24 * 1024; +static const std::size_t kChunkReadahead = 2; FatfsAudioInput::FatfsAudioInput(std::shared_ptr storage) : IAudioElement(), + arena_(kChunkSize, kChunkReadahead, MALLOC_CAP_SPIRAM), storage_(storage), current_file_(), is_file_open_(false) {} @@ -80,25 +82,28 @@ auto FatfsAudioInput::ProcessEndOfStream() -> void { auto FatfsAudioInput::Process() -> cpp::result { if (is_file_open_) { - auto dest_event = std::unique_ptr( - StreamEvent::CreateChunkData(input_events_, kChunkSize)); - UINT bytes_read = 0; + auto dest_block = memory::ArenaRef::Acquire(&arena_); + if (!dest_block) { + return {}; + } - FRESULT result = f_read(¤t_file_, dest_event->chunk_data.raw_bytes, - kChunkSize, &bytes_read); + FRESULT result = f_read(¤t_file_, dest_block->ptr.start, + dest_block->ptr.size, &dest_block->ptr.used_size); if (result != FR_OK) { ESP_LOGE(kTag, "file I/O error %d", result); return cpp::fail(IO_ERROR); } - dest_event->chunk_data.bytes = - dest_event->chunk_data.bytes.first(bytes_read); - SendOrBufferEvent(std::move(dest_event)); - - if (bytes_read < kChunkSize || f_eof(¤t_file_)) { + if (dest_block->ptr.used_size < dest_block->ptr.size || + f_eof(¤t_file_)) { f_close(¤t_file_); is_file_open_ = false; } + + auto dest_event = std::unique_ptr( + StreamEvent::CreateArenaChunk(input_events_, dest_block->Release())); + + SendOrBufferEvent(std::move(dest_event)); } return {}; } -- cgit v1.2.3