-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusic_player.cpp
317 lines (282 loc) · 8.81 KB
/
music_player.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <capo/capo.hpp>
#include <ktl/async/kthread.hpp>
#include <ktl/kformat.hpp>
#include <cassert>
#include <chrono>
#include <fstream>
#include <iostream>
#include <thread>
#include <ktl/async/kmutex.hpp>
#include <atomic>
#include <future>
namespace {
using namespace std::chrono_literals;
static constexpr int fail_code = 2;
constexpr capo::utils::EnumArray<capo::State, std::string_view> g_stateNames = {
"UNKNOWN", "IDLE", "PLAYING", "PAUSED", "STOPPED",
};
constexpr bool is_last(std::size_t index, std::size_t total) { return index + 1 == total; }
using Tracklist = std::vector<std::string>;
Tracklist valid_tracks(Tracklist tracklist, ktl::not_null<capo::Instance*> instance) {
Tracklist ret;
ret.reserve(tracklist.size());
capo::Music music(instance);
for (auto& track : tracklist) {
if (!music.open(track.c_str())) {
std::cerr << "Failed to open " << track << std::endl;
continue;
}
ret.push_back(std::move(track));
}
return ret;
}
class Playlist {
public:
enum class Mode { eStream, ePreload };
bool load(Tracklist tracklist, Mode mode) {
if (tracklist.empty()) {
std::cerr << "Failed to load any valid tracks";
return false;
}
m_mode = mode;
m_tracklist = std::move(tracklist);
if (m_mode == Mode::ePreload) {
m_current = load(m_tracklist[m_idx].c_str());
load_cache();
}
return true;
}
std::size_t index() const noexcept { return m_idx; }
std::size_t size() const noexcept { return m_tracklist.size(); }
bool multi_track() const noexcept { return size() > 1; }
bool is_last_track() const noexcept { return is_last(index(), size()); }
std::string const& path() const noexcept { return m_tracklist[index()]; }
capo::PCM const& pcm() const noexcept { return m_current; }
Mode mode() const noexcept { return m_mode; }
void next() {
if (multi_track()) {
m_idx = next_idx();
if (m_mode == Mode::ePreload) {
m_current = std::move(m_cache.next)();
load_cache();
}
}
}
void prev() {
if (multi_track()) {
m_idx = prev_idx();
if (m_mode == Mode::ePreload) {
auto& cache = m_cache.prev.fence.valid() ? m_cache.prev : m_cache.next;
m_current = std::move(cache)();
load_cache();
}
}
}
private:
struct Cache {
std::future<void> fence;
capo::PCM pcm;
capo::PCM operator()() && {
fence.wait();
return std::move(pcm);
}
};
static capo::PCM load(char const* path) { return *capo::PCM::from_file(path); }
std::size_t next_idx() const noexcept { return (m_idx + 1) % m_tracklist.size(); }
std::size_t prev_idx() const noexcept { return (m_idx + m_tracklist.size() - 1) % m_tracklist.size(); }
void load_cache() {
if (multi_track()) {
if (m_cache.next.fence.wait_for(0s) == std::future_status::deferred) { m_cache.next.fence.wait(); }
m_cache.next.fence = std::async([this]() { m_cache.next.pcm = load(m_tracklist[next_idx()].c_str()); });
}
if (m_tracklist.size() > 2) {
if (m_cache.prev.fence.wait_for(0s) == std::future_status::deferred) { m_cache.prev.fence.wait(); }
m_cache.prev.fence = std::async([this]() { m_cache.prev.pcm = load(m_tracklist[prev_idx()].c_str()); });
}
}
struct {
Cache next;
Cache prev;
} m_cache;
Tracklist m_tracklist;
capo::PCM m_current;
std::size_t m_idx{};
Mode m_mode = Mode::eStream;
};
class Player {
public:
Player(ktl::not_null<capo::Instance*> instance) { ktl::klock(m_shared)->music = capo::Music(instance); }
bool run(Tracklist tracklist, Playlist::Mode mode) {
if (!init(std::move(tracklist), mode)) { return false; }
m_thread = ktl::kthread([this](ktl::kthread::stop_t stop) {
while (!stop.stop_requested()) {
if (play_next()) {
next();
play();
menu();
}
ktl::kthread::yield();
}
});
m_thread.m_join = ktl::kthread::policy::stop;
menu();
while (input()) { menu(); }
return true;
}
private:
enum class State { eStopped, ePlaying };
bool init(Tracklist tracklist, Playlist::Mode mode) {
ktl::klock lock(m_shared);
if (!lock->playlist.load(std::move(tracklist), mode)) { return false; }
load(lock->music, lock->playlist);
return true;
}
void menu() const {
ktl::klock lock(m_shared);
auto const length = capo::utils::Length(lock->music.position());
std::cout << '\n' << lock->playlist.path();
if (lock->playlist.mode() == Playlist::Mode::ePreload) { std::cout << " [preloaded]"; }
std::cout << ktl::kformat(" [{:.2f} gain] [{}]", lock->music.gain(), length) << '\n';
std::cout << "\n == " << g_stateNames[lock->music.state()] << " ==";
if (lock->playlist.multi_track()) { std::cout << " [" << lock->playlist.index() + 1 << '/' << lock->playlist.size() << ']'; }
std::cout << "\n [t/g] <value>\t: seek to seconds / set gain";
if (m_state.load() == State::ePlaying) {
std::cout << "\n [p/s]\t\t: pause / stop";
} else {
std::cout << "\n [p]\t\t: play";
}
if (lock->playlist.multi_track()) { std::cout << "\n [</>]\t\t: previous / next"; }
std::cout << "\n [q]\t\t: quit\n [?]\t\t: refresh\n: " << std::flush;
}
bool input() {
char ch;
std::cin >> ch;
switch (ch) {
case 't': {
float time;
std::cin >> time;
if (!ktl::klock(m_shared)->music.seek(capo::Time(time))) { std::cerr << "\nseek fail!\n"; }
break;
}
case 's': {
ktl::klock(m_shared)->music.stop();
m_state = State::eStopped;
break;
}
case 'p': {
ktl::klock lock(m_shared);
if (lock->music.state() == capo::State::ePlaying) {
lock->music.pause();
} else {
lock->music.play();
}
m_state = State::ePlaying;
break;
}
case 'g': {
float gain;
std::cin >> gain;
if (!ktl::klock(m_shared)->music.gain(gain)) { std::cerr << "\ngain fail!\n"; }
break;
}
case '>': next(); break;
case '<': prev(); break;
case 'q': return false;
default: break;
}
std::cin.clear();
std::cin.ignore();
return true;
}
bool play_next() {
ktl::klock lock(m_shared);
auto const stopped = lock->music.state() == capo::State::eStopped;
auto const last = lock->playlist.is_last_track();
if (last && stopped) { m_state = State::eStopped; }
return m_state.load() == State::ePlaying && stopped && !last;
}
void play() { ktl::klock(m_shared)->music.play(); }
void next() {
ktl::klock lock(m_shared);
if (lock->playlist.multi_track()) {
lock->playlist.next();
advance(lock->music, lock->playlist);
}
}
void prev() {
ktl::klock lock(m_shared);
if (lock->playlist.multi_track()) {
lock->playlist.prev();
advance(lock->music, lock->playlist);
}
}
void load(capo::Music& out_music, Playlist const& playlist) {
if (playlist.mode() == Playlist::Mode::ePreload) {
out_music.preload(playlist.pcm());
} else {
out_music.open(playlist.path().c_str());
}
auto const& meta = out_music.meta();
std::cout << ktl::kformat("\n {}\n\t{:.1f}s Length\n\t{} Channel(s)\n\t{} Sample Rate\n\t{} Size\n", playlist.path(), meta.length().count(),
meta.channel_count(meta.format), out_music.sample_rate(), out_music.size());
}
void advance(capo::Music& out_music, Playlist const& playlist) {
out_music.stop();
load(out_music, playlist);
if (m_state.load() == State::ePlaying) { out_music.play(); }
}
struct Shared {
Playlist playlist;
capo::Music music;
};
ktl::strict_tmutex<Shared> m_shared;
ktl::kthread m_thread;
std::atomic<State> m_state = State::eStopped;
};
std::vector<std::string> build_tracklist(std::string_view path) {
std::vector<std::string> ret;
if (auto file = std::ifstream(path.data())) {
for (std::string line; std::getline(file, line); line.clear()) { ret.push_back(std::move(line)); }
}
return ret;
}
std::string_view app_name(std::string_view path) {
auto it = path.find_last_of('/');
if (it == std::string_view::npos) { it = path.find_last_of('\\'); }
if (it != std::string_view::npos) { return path.substr(it + 1); }
return path;
}
} // namespace
int main(int argc, char const* const argv[]) {
constexpr std::string_view playlist_name = "capo_playlist.txt";
int argi{};
std::string_view const name = app_name(argv[argi++]);
Tracklist tracklist;
auto mode = Playlist::Mode::eStream;
if (argi < argc) {
if (std::string_view const arg(argv[argi]); arg == "--preload" || arg == "-p") {
mode = Playlist::Mode::ePreload;
++argi;
}
}
if (argi == argc) {
if (tracklist = build_tracklist(playlist_name); tracklist.empty()) {
std::cerr << "Usage: " << name << " [-p|--preload] [capo_playlist.txt] <file_path0> [file_path1 ...]" << std::endl;
return fail_code;
}
}
if (argi < argc) {
if (std::string_view const arg = argv[argi]; arg.ends_with(".txt")) {
tracklist = build_tracklist(arg);
++argi;
}
}
for (; argi < argc; ++argi) { tracklist.push_back(argv[argi]); }
auto instance = capo::Instance::make();
if (!instance->valid()) {
std::cerr << "Failed to create instance" << std::endl;
return fail_code;
}
Player player(instance.get());
if (!player.run(valid_tracks(std::move(tracklist), instance.get()), mode)) { return fail_code; }
}