diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-11-07 10:32:07 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-11-07 10:32:07 +1100 |
| commit | 499d5a942fc2ad0149b0a16e978e090336dd8319 (patch) | |
| tree | 1f671f0e3025b1350b25511e2442dfede19677df /src/audio/include/readahead_source.hpp | |
| parent | d36fe9be6b522a3dade389213a0bb7e26a169627 (diff) | |
| download | tangara-fw-499d5a942fc2ad0149b0a16e978e090336dd8319.tar.gz | |
Add a wrapper codec source that does readahead
Diffstat (limited to 'src/audio/include/readahead_source.hpp')
| -rw-r--r-- | src/audio/include/readahead_source.hpp | 53 |
1 files changed, 53 insertions, 0 deletions
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 <me@jacqueline.id.au> + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#pragma once + +#include <cstddef> +#include <cstdint> +#include <memory> + +#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<codecs::IStream>); + ~ReadaheadSource(); + + auto Read(cpp::span<std::byte> 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<codecs::IStream> wrapped_; + + std::atomic<bool> is_refilling_; + StreamBufferHandle_t buffer_; + int64_t tell_; +}; + +} // namespace audio
\ No newline at end of file |
