1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include "fatfs_audio_input.hpp"
#include <algorithm>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <future>
#include <memory>
#include <mutex>
#include <string>
#include <variant>
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "ff.h"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h"
#include "idf_additions.h"
#include "span.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 "tag_parser.hpp"
#include "tasks.hpp"
#include "track.hpp"
#include "types.hpp"
static const char* kTag = "SRC";
namespace audio {
FatfsAudioInput::FatfsAudioInput(database::ITagParser& tag_parser)
: IAudioSource(),
tag_parser_(tag_parser),
new_stream_mutex_(),
new_stream_(),
has_new_stream_(xSemaphoreCreateBinary()),
pending_path_() {}
FatfsAudioInput::~FatfsAudioInput() {
vSemaphoreDelete(has_new_stream_);
}
auto FatfsAudioInput::SetPath(std::future<std::optional<std::pmr::string>> fut)
-> void {
std::lock_guard<std::mutex> guard{new_stream_mutex_};
pending_path_.reset(
new database::FutureFetcher<std::optional<std::pmr::string>>(
std::move(fut)));
xSemaphoreGive(has_new_stream_);
}
auto FatfsAudioInput::SetPath(const std::pmr::string& path) -> void {
std::lock_guard<std::mutex> guard{new_stream_mutex_};
if (OpenFile(path)) {
xSemaphoreGive(has_new_stream_);
}
}
auto FatfsAudioInput::SetPath() -> void {
std::lock_guard<std::mutex> guard{new_stream_mutex_};
new_stream_.reset();
xSemaphoreGive(has_new_stream_);
}
auto FatfsAudioInput::HasNewStream() -> bool {
bool res = xSemaphoreTake(has_new_stream_, 0);
if (res) {
xSemaphoreGive(has_new_stream_);
}
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.
if (pending_path_) {
auto res = pending_path_->Result();
pending_path_.reset();
if (res && *res) {
OpenFile(**res);
}
}
if (new_stream_ == nullptr) {
continue;
}
auto stream = new_stream_;
new_stream_ = nullptr;
return stream;
}
}
}
auto FatfsAudioInput::OpenFile(const std::pmr::string& path) -> 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;
}
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<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;
}
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
|