From 499d5a942fc2ad0149b0a16e978e090336dd8319 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 7 Nov 2023 10:32:07 +1100 Subject: Add a wrapper codec source that does readahead --- src/audio/include/fatfs_audio_input.hpp | 4 ++- src/audio/include/readahead_source.hpp | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/audio/include/readahead_source.hpp (limited to 'src/audio/include') diff --git a/src/audio/include/fatfs_audio_input.hpp b/src/audio/include/fatfs_audio_input.hpp index 08527350..9b516478 100644 --- a/src/audio/include/fatfs_audio_input.hpp +++ b/src/audio/include/fatfs_audio_input.hpp @@ -19,6 +19,7 @@ #include "codec.hpp" #include "future_fetcher.hpp" #include "tag_parser.hpp" +#include "tasks.hpp" #include "types.hpp" namespace audio { @@ -30,7 +31,7 @@ namespace audio { */ class FatfsAudioInput : public IAudioSource { public: - explicit FatfsAudioInput(database::ITagParser& tag_parser); + explicit FatfsAudioInput(database::ITagParser&, tasks::Worker&); ~FatfsAudioInput(); /* @@ -54,6 +55,7 @@ class FatfsAudioInput : public IAudioSource { -> std::optional; database::ITagParser& tag_parser_; + tasks::Worker& bg_worker_; std::mutex new_stream_mutex_; std::shared_ptr new_stream_; diff --git a/src/audio/include/readahead_source.hpp b/src/audio/include/readahead_source.hpp new file mode 100644 index 00000000..dea3ff3f --- /dev/null +++ b/src/audio/include/readahead_source.hpp @@ -0,0 +1,53 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include +#include +#include + +#include "freertos/FreeRTOS.h" + +#include "ff.h" +#include "freertos/stream_buffer.h" + +#include "audio_source.hpp" +#include "codec.hpp" +#include "tasks.hpp" + +namespace audio { + +/* + * Wraps another stream, proactively buffering large chunks of it into memory + * at a time. + */ +class ReadaheadSource : public codecs::IStream { + public: + ReadaheadSource(tasks::Worker&, std::unique_ptr); + ~ReadaheadSource(); + + auto Read(cpp::span dest) -> ssize_t override; + + auto CanSeek() -> bool override; + + auto SeekTo(int64_t destination, SeekFrom from) -> void override; + + auto CurrentPosition() -> int64_t override; + + ReadaheadSource(const ReadaheadSource&) = delete; + ReadaheadSource& operator=(const ReadaheadSource&) = delete; + + private: + tasks::Worker& worker_; + std::unique_ptr wrapped_; + + std::atomic is_refilling_; + StreamBufferHandle_t buffer_; + int64_t tell_; +}; + +} // namespace audio \ No newline at end of file -- cgit v1.2.3