diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-02-21 13:16:58 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-02-21 13:19:11 +1100 |
| commit | 941bafca17b13547a88668b787ce4c8e064ef7ff (patch) | |
| tree | b4a0d5528cbd258fedffc041dee837bcaf1f690f /src/audio/fatfs_audio_input.cpp | |
| parent | 12d2ffdab70df573610b81d8a24545da33bb67e3 (diff) | |
| download | tangara-fw-941bafca17b13547a88668b787ce4c8e064ef7ff.tar.gz | |
Add a memory arena for the audio pipeline
Diffstat (limited to 'src/audio/fatfs_audio_input.cpp')
| -rw-r--r-- | src/audio/fatfs_audio_input.cpp | 27 |
1 files changed, 16 insertions, 11 deletions
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 <memory> #include <string> +#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<drivers::SdStorage> 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<void, AudioProcessingError> { if (is_file_open_) { - auto dest_event = std::unique_ptr<StreamEvent>( - 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>( + StreamEvent::CreateArenaChunk(input_events_, dest_block->Release())); + + SendOrBufferEvent(std::move(dest_event)); } return {}; } |
