summaryrefslogtreecommitdiff
path: root/src/audio/include/fatfs_audio_input.hpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-08-10 15:33:00 +1000
committerjacqueline <me@jacqueline.id.au>2023-08-10 15:33:00 +1000
commitd8fc77101dcf80a3643a00b3446dca1e390ce997 (patch)
tree9e03881f3857c7b4c6a0b6e3a062947daecc69d1 /src/audio/include/fatfs_audio_input.hpp
parent67caeb6e3cda44205ba8fe783274b20dc7ea216e (diff)
downloadtangara-fw-d8fc77101dcf80a3643a00b3446dca1e390ce997.tar.gz
Give codecs complete control of their input files
Diffstat (limited to 'src/audio/include/fatfs_audio_input.hpp')
-rw-r--r--src/audio/include/fatfs_audio_input.hpp79
1 files changed, 8 insertions, 71 deletions
diff --git a/src/audio/include/fatfs_audio_input.hpp b/src/audio/include/fatfs_audio_input.hpp
index e13e49e2..df40696a 100644
--- a/src/audio/include/fatfs_audio_input.hpp
+++ b/src/audio/include/fatfs_audio_input.hpp
@@ -12,6 +12,7 @@
#include <memory>
#include <string>
+#include "codec.hpp"
#include "ff.h"
#include "audio_source.hpp"
@@ -24,54 +25,6 @@
namespace audio {
/*
- * Handles coordination with a persistent background task to asynchronously
- * read files from disk into a StreamBuffer.
- */
-class FileStreamer {
- public:
- FileStreamer(StreamBufferHandle_t dest, SemaphoreHandle_t first_read);
- ~FileStreamer();
-
- /*
- * Continues reading data into the destination buffer until the destination
- * is full.
- */
- auto Fetch() -> void;
-
- /* Returns true if the streamer has run out of data from the current file. */
- auto HasFinished() -> bool;
-
- /*
- * Clears any remaining buffered data, and begins reading again from the
- * given file. This function respects any seeking/reading that has already
- * been done on the new source file.
- */
- auto Restart(std::unique_ptr<FIL>) -> void;
-
- FileStreamer(const FileStreamer&) = delete;
- FileStreamer& operator=(const FileStreamer&) = delete;
-
- private:
- // Note: private methods here should only be called from the streamer's task.
-
- auto Main() -> void;
- auto CloseFile() -> void;
-
- enum Command {
- kRestart,
- kRefillBuffer,
- kQuit,
- };
- QueueHandle_t control_;
- StreamBufferHandle_t destination_;
- SemaphoreHandle_t data_was_read_;
-
- std::atomic<bool> has_data_;
- std::unique_ptr<FIL> file_;
- std::unique_ptr<FIL> next_file_;
-};
-
-/*
* Audio source that fetches data from a FatFs (or exfat i guess) filesystem.
*
* All public methods are safe to call from any task.
@@ -89,43 +42,27 @@ class FatfsAudioInput : public IAudioSource {
auto SetPath(const std::string&) -> void;
auto SetPath() -> void;
- auto Read(std::function<void(Flags, InputStream&)>, TickType_t)
- -> void override;
+ auto HasNewStream() -> bool override;
+ auto NextStream() -> std::shared_ptr<codecs::IStream> override;
FatfsAudioInput(const FatfsAudioInput&) = delete;
FatfsAudioInput& operator=(const FatfsAudioInput&) = delete;
private:
- // Note: private methods assume that the appropriate locks have already been
- // acquired.
-
- auto OpenFile(const std::string& path) -> void;
- auto CloseCurrentFile() -> void;
- auto HasDataRemaining() -> bool;
+ auto OpenFile(const std::string& path) -> bool;
- auto ContainerToStreamType(database::Encoding)
+ auto ContainerToStreamType(database::Container)
-> std::optional<codecs::StreamType>;
- auto IsCurrentFormatMp3() -> bool;
std::shared_ptr<database::ITagParser> tag_parser_;
- // Semaphore used to block when this source is out of data. This should be
- // acquired before attempting to read data, and returned after each incomplete
- // read.
- SemaphoreHandle_t has_data_;
-
- StreamBufferHandle_t streamer_buffer_;
- std::unique_ptr<FileStreamer> streamer_;
-
- std::unique_ptr<RawStream> input_buffer_;
+ std::mutex new_stream_mutex_;
+ std::shared_ptr<codecs::IStream> new_stream_;
- // Mutex guarding the current file/stream associated with this source. Must be
- // held during readings, and before altering the current file.
- std::mutex source_mutex_;
+ SemaphoreHandle_t has_new_stream_;
std::unique_ptr<database::FutureFetcher<std::optional<std::string>>>
pending_path_;
- bool is_first_read_;
};
} // namespace audio