Fork of Tangara with customizations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tangara-fw/src/audio/fatfs_audio_input.cpp

167 lines
4.1 KiB

/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
2 years ago
#include "fatfs_audio_input.hpp"
2 years ago
#include <stdint.h>
2 years ago
#include <algorithm>
2 years ago
#include <climits>
#include <cstddef>
#include <cstdint>
2 years ago
#include <functional>
#include <future>
#include <memory>
2 years ago
#include <mutex>
2 years ago
#include <string>
#include <variant>
#include "codec.hpp"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "fatfs_source.hpp"
#include "ff.h"
2 years ago
#include "audio_events.hpp"
#include "audio_fsm.hpp"
#include "audio_source.hpp"
#include "event_queue.hpp"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h"
#include "future_fetcher.hpp"
#include "idf_additions.h"
2 years ago
#include "span.hpp"
#include "stream_info.hpp"
#include "tag_parser.hpp"
2 years ago
#include "tasks.hpp"
#include "types.hpp"
static const char* kTag = "SRC";
namespace audio {
2 years ago
FatfsAudioInput::FatfsAudioInput(
std::shared_ptr<database::ITagParser> tag_parser)
: IAudioSource(),
tag_parser_(tag_parser),
new_stream_mutex_(),
new_stream_(),
has_new_stream_(xSemaphoreCreateBinary()),
pending_path_() {}
2 years ago
FatfsAudioInput::~FatfsAudioInput() {
vSemaphoreDelete(has_new_stream_);
2 years ago
}
2 years ago
auto FatfsAudioInput::SetPath(std::future<std::optional<std::string>> fut)
-> void {
std::lock_guard<std::mutex> guard{new_stream_mutex_};
2 years ago
pending_path_.reset(
new database::FutureFetcher<std::optional<std::string>>(std::move(fut)));
xSemaphoreGive(has_new_stream_);
}
2 years ago
auto FatfsAudioInput::SetPath(const std::string& path) -> void {
std::lock_guard<std::mutex> guard{new_stream_mutex_};
if (OpenFile(path)) {
xSemaphoreGive(has_new_stream_);
}
2 years ago
}
auto FatfsAudioInput::SetPath() -> void {
std::lock_guard<std::mutex> guard{new_stream_mutex_};
new_stream_.reset();
xSemaphoreGive(has_new_stream_);
2 years ago
}
auto FatfsAudioInput::HasNewStream() -> bool {
bool res = xSemaphoreTake(has_new_stream_, 0);
if (res) {
xSemaphoreGive(has_new_stream_);
2 years ago
}
return res;
}
auto FatfsAudioInput::NextStream() -> std::shared_ptr<codecs::IStream> {
while (true) {
xSemaphoreTake(has_new_stream_, portMAX_DELAY);
{
std::lock_guard<std::mutex> guard{new_stream_mutex_};
// If the path is a future, then wait for it to complete.
// TODO(jacqueline): We should really make some kind of
// FreeRTOS-integrated way to block a task whilst awaiting a future.
if (pending_path_) {
while (!pending_path_->Finished()) {
vTaskDelay(pdMS_TO_TICKS(100));
}
auto res = pending_path_->Result();
pending_path_.reset();
if (res && *res) {
OpenFile(**res);
}
}
2 years ago
if (new_stream_ == nullptr) {
continue;
}
2 years ago
auto stream = new_stream_;
new_stream_ = nullptr;
return stream;
}
2 years ago
}
}
auto FatfsAudioInput::OpenFile(const std::string& path) -> bool {
ESP_LOGI(kTag, "opening file %s", path.c_str());
2 years ago
database::TrackTags tags;
2 years ago
if (!tag_parser_->ReadAndParseTags(path, &tags)) {
ESP_LOGE(kTag, "failed to read tags");
return false;
}
auto stream_type = ContainerToStreamType(tags.encoding());
if (!stream_type.has_value()) {
ESP_LOGE(kTag, "couldn't match container to stream");
return false;
}
2 years ago
std::unique_ptr<FIL> file = std::make_unique<FIL>();
FRESULT 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;
}
2 years ago
new_stream_.reset(new FatfsSource(stream_type.value(), std::move(file)));
return true;
}
auto FatfsAudioInput::ContainerToStreamType(database::Container enc)
-> std::optional<codecs::StreamType> {
switch (enc) {
case database::Container::kMp3:
return codecs::StreamType::kMp3;
case database::Container::kWav:
return codecs::StreamType::kPcm;
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