From 1573a8c4cde1cd9528b422b2dcc598e37ffe94a7 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 2 May 2024 19:12:26 +1000 Subject: WIP merge cyclically dependent components into one big component --- src/tangara/audio/fatfs_audio_input.cpp | 163 ++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/tangara/audio/fatfs_audio_input.cpp (limited to 'src/tangara/audio/fatfs_audio_input.cpp') diff --git a/src/tangara/audio/fatfs_audio_input.cpp b/src/tangara/audio/fatfs_audio_input.cpp new file mode 100644 index 00000000..e5fb3b21 --- /dev/null +++ b/src/tangara/audio/fatfs_audio_input.cpp @@ -0,0 +1,163 @@ +/* + * Copyright 2023 jacqueline + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include "fatfs_audio_input.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "esp_heap_caps.h" +#include "esp_log.h" +#include "ff.h" +#include "freertos/portmacro.h" +#include "freertos/projdefs.h" +#include "readahead_source.hpp" + +#include "audio_events.hpp" +#include "audio_fsm.hpp" +#include "audio_source.hpp" +#include "codec.hpp" +#include "event_queue.hpp" +#include "fatfs_source.hpp" +#include "future_fetcher.hpp" +#include "spi.hpp" +#include "tag_parser.hpp" +#include "tasks.hpp" +#include "track.hpp" +#include "types.hpp" + +[[maybe_unused]] static const char* kTag = "SRC"; + +namespace audio { + +FatfsAudioInput::FatfsAudioInput(database::ITagParser& tag_parser, + tasks::WorkerPool& bg_worker) + : IAudioSource(), + tag_parser_(tag_parser), + bg_worker_(bg_worker), + new_stream_mutex_(), + new_stream_(), + has_new_stream_(false) {} + +FatfsAudioInput::~FatfsAudioInput() {} + +auto FatfsAudioInput::SetPath(std::optional path) -> void { + if (path) { + SetPath(*path); + } else { + SetPath(); + } +} + +auto FatfsAudioInput::SetPath(const std::string& path, uint32_t offset) + -> void { + std::lock_guard guard{new_stream_mutex_}; + if (OpenFile(path, offset)) { + has_new_stream_ = true; + has_new_stream_.notify_one(); + } +} + +auto FatfsAudioInput::SetPath() -> void { + std::lock_guard guard{new_stream_mutex_}; + new_stream_.reset(); + has_new_stream_ = true; + has_new_stream_.notify_one(); +} + +auto FatfsAudioInput::HasNewStream() -> bool { + return has_new_stream_; +} + +auto FatfsAudioInput::NextStream() -> std::shared_ptr { + while (true) { + has_new_stream_.wait(false); + + { + std::lock_guard guard{new_stream_mutex_}; + if (!has_new_stream_.exchange(false)) { + // If the new stream went away, then we need to go back to waiting. + continue; + } + + if (new_stream_ == nullptr) { + continue; + } + + auto stream = new_stream_; + new_stream_ = nullptr; + return stream; + } + } +} + +auto FatfsAudioInput::OpenFile(const std::string& path, uint32_t offset) + -> bool { + ESP_LOGI(kTag, "opening file %s", path.c_str()); + + auto tags = tag_parser_.ReadAndParseTags(path); + if (!tags) { + ESP_LOGE(kTag, "failed to read tags"); + return false; + } + if (!tags->title()) { + tags->title(path); + } + + auto stream_type = ContainerToStreamType(tags->encoding()); + if (!stream_type.has_value()) { + ESP_LOGE(kTag, "couldn't match container to stream"); + return false; + } + + std::unique_ptr file = std::make_unique(); + FRESULT res; + + { + auto lock = drivers::acquire_spi(); + res = f_open(file.get(), path.c_str(), FA_READ); + } + + if (res != FR_OK) { + ESP_LOGE(kTag, "failed to open file! res: %i", res); + return false; + } + + auto source = + std::make_unique(stream_type.value(), std::move(file)); + new_stream_.reset(new TaggedStream(tags, std::move(source), path, offset)); + return true; +} + +auto FatfsAudioInput::ContainerToStreamType(database::Container enc) + -> std::optional { + switch (enc) { + case database::Container::kMp3: + return codecs::StreamType::kMp3; + case database::Container::kWav: + return codecs::StreamType::kWav; + case database::Container::kOgg: + return codecs::StreamType::kVorbis; + case database::Container::kFlac: + return codecs::StreamType::kFlac; + case database::Container::kOpus: + return codecs::StreamType::kOpus; + case database::Container::kUnsupported: + default: + return {}; + } +} + +} // namespace audio -- cgit v1.2.3 From 7d7f7755d17e1e0a2348d75d797097f166b70471 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 2 May 2024 21:41:56 +1000 Subject: start moving include files into subdirs --- src/tangara/audio/fatfs_audio_input.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/tangara/audio/fatfs_audio_input.cpp') diff --git a/src/tangara/audio/fatfs_audio_input.cpp b/src/tangara/audio/fatfs_audio_input.cpp index e5fb3b21..0868d71a 100644 --- a/src/tangara/audio/fatfs_audio_input.cpp +++ b/src/tangara/audio/fatfs_audio_input.cpp @@ -4,7 +4,7 @@ * SPDX-License-Identifier: GPL-3.0-only */ -#include "fatfs_audio_input.hpp" +#include "audio/fatfs_audio_input.hpp" #include #include @@ -18,24 +18,24 @@ #include #include +#include "audio/readahead_source.hpp" #include "esp_heap_caps.h" #include "esp_log.h" #include "ff.h" #include "freertos/portmacro.h" #include "freertos/projdefs.h" -#include "readahead_source.hpp" -#include "audio_events.hpp" -#include "audio_fsm.hpp" -#include "audio_source.hpp" +#include "audio/audio_events.hpp" +#include "audio/audio_fsm.hpp" +#include "audio/audio_source.hpp" +#include "audio/fatfs_source.hpp" #include "codec.hpp" -#include "event_queue.hpp" -#include "fatfs_source.hpp" -#include "future_fetcher.hpp" +#include "database/future_fetcher.hpp" +#include "database/tag_parser.hpp" +#include "database/track.hpp" +#include "events/event_queue.hpp" #include "spi.hpp" -#include "tag_parser.hpp" #include "tasks.hpp" -#include "track.hpp" #include "types.hpp" [[maybe_unused]] static const char* kTag = "SRC"; @@ -61,8 +61,8 @@ auto FatfsAudioInput::SetPath(std::optional path) -> void { } } -auto FatfsAudioInput::SetPath(const std::string& path, uint32_t offset) - -> void { +auto FatfsAudioInput::SetPath(const std::string& path, + uint32_t offset) -> void { std::lock_guard guard{new_stream_mutex_}; if (OpenFile(path, offset)) { has_new_stream_ = true; @@ -103,8 +103,8 @@ auto FatfsAudioInput::NextStream() -> std::shared_ptr { } } -auto FatfsAudioInput::OpenFile(const std::string& path, uint32_t offset) - -> bool { +auto FatfsAudioInput::OpenFile(const std::string& path, + uint32_t offset) -> bool { ESP_LOGI(kTag, "opening file %s", path.c_str()); auto tags = tag_parser_.ReadAndParseTags(path); -- cgit v1.2.3 From 26eb580043ad176bdc58d996f30d470e1073ef00 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 2 May 2024 21:52:59 +1000 Subject: move driver includes into a subdir as well --- src/tangara/audio/fatfs_audio_input.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/tangara/audio/fatfs_audio_input.cpp') diff --git a/src/tangara/audio/fatfs_audio_input.cpp b/src/tangara/audio/fatfs_audio_input.cpp index 0868d71a..dc9133ed 100644 --- a/src/tangara/audio/fatfs_audio_input.cpp +++ b/src/tangara/audio/fatfs_audio_input.cpp @@ -33,8 +33,8 @@ #include "database/future_fetcher.hpp" #include "database/tag_parser.hpp" #include "database/track.hpp" +#include "drivers/spi.hpp" #include "events/event_queue.hpp" -#include "spi.hpp" #include "tasks.hpp" #include "types.hpp" -- cgit v1.2.3