summaryrefslogtreecommitdiff
path: root/src/audio/include
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-11-07 10:32:07 +1100
committerjacqueline <me@jacqueline.id.au>2023-11-07 10:32:07 +1100
commit499d5a942fc2ad0149b0a16e978e090336dd8319 (patch)
tree1f671f0e3025b1350b25511e2442dfede19677df /src/audio/include
parentd36fe9be6b522a3dade389213a0bb7e26a169627 (diff)
downloadtangara-fw-499d5a942fc2ad0149b0a16e978e090336dd8319.tar.gz
Add a wrapper codec source that does readahead
Diffstat (limited to 'src/audio/include')
-rw-r--r--src/audio/include/fatfs_audio_input.hpp4
-rw-r--r--src/audio/include/readahead_source.hpp53
2 files changed, 56 insertions, 1 deletions
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<codecs::StreamType>;
database::ITagParser& tag_parser_;
+ tasks::Worker& bg_worker_;
std::mutex new_stream_mutex_;
std::shared_ptr<codecs::IStream> 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 <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