-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmultitrack_strategy.cpp
More file actions
133 lines (121 loc) · 4.48 KB
/
multitrack_strategy.cpp
File metadata and controls
133 lines (121 loc) · 4.48 KB
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
// *******************************************************************
// DCue (github.com/xavery/dcue)
// Copyright (c) 2022 Daniel Kamil Kozar
// *******************************************************************
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// *******************************************************************
#include "multitrack_strategy.h"
#include "naming.h"
#include <spdlog/fmt/fmt.h>
namespace {
struct multitrack_strategy_single : public multitrack_strategy {
public:
std::vector<Track> handle_index(const nlohmann::json& idx_track,
const Album& album) const override {
return convert(idx_track, album);
}
std::vector<Track> handle_medley(
const std::vector<nlohmann::json::const_iterator>& medley_tracks,
const Album& album) const override {
return convert(*(medley_tracks[0]), album);
}
static std::vector<Track> convert(const nlohmann::json& json_trk,
const Album& album) {
Track t;
t.title = json_trk.at("title");
t.length = Track::Duration{json_trk.value("duration", std::string())};
auto artists = json_trk.find("artists");
if (artists != json_trk.end()) {
t.artist = NamingFacets::concatenate_artists(json_trk["artists"]);
} else {
t.artist = album.album_artist;
}
return {std::move(t)};
}
};
struct multitrack_strategy_merge : public multitrack_strategy {
std::vector<Track> handle_index(const nlohmann::json& idx_track,
const Album& album) const override {
auto& subtracks = idx_track["sub_tracks"];
if (subtracks.empty()) {
return {};
}
Track rv;
rv.length = Track::Duration{idx_track.value("duration", std::string())};
rv.title = idx_track.value("title", std::string());
rv.title += " : ";
for (auto& sub : subtracks) {
if (sub.value("type_", std::string()) == "track") {
rv.title += sub.value("title", std::string());
if (&sub != &subtracks.back()) {
rv.title += " / ";
}
}
}
rv.artist = album.album_artist;
return {rv};
}
std::vector<Track> handle_medley(
const std::vector<nlohmann::json::const_iterator>& medley_tracks,
const Album& album) const override {
Track rv;
rv.length = Track::Duration{
medley_tracks.front()->value("duration", std::string())};
for (auto& t : medley_tracks) {
rv.title += t->value("title", std::string());
auto artists = t->find("artists");
if (artists != t->end()) {
rv.artist += NamingFacets::concatenate_artists(*artists);
}
if (&t != &medley_tracks.back()) {
rv.title += " / ";
rv.artist += " / ";
}
}
if (rv.artist.empty()) {
rv.artist = album.album_artist;
}
return {rv};
}
};
struct multitrack_strategy_separate : public multitrack_strategy {
std::vector<Track> handle_index(const nlohmann::json& idx_track,
const Album& album) const override {
auto rv = std::vector<Track>{};
for (auto& subtrack : idx_track["sub_tracks"]) {
Track t;
t.artist = album.album_artist;
t.title = fmt::format("{} : {}", idx_track.value("title", std::string()),
subtrack.value("title", std::string()));
t.length = Track::Duration{subtrack.value("duration", std::string())};
rv.emplace_back(std::move(t));
}
return rv;
}
std::vector<Track>
handle_medley(const std::vector<nlohmann::json::const_iterator>& tracks,
const Album&) const override {
auto rv = std::vector<Track>{};
for (auto& t : tracks) {
Track parsed_track;
parsed_track.artist = NamingFacets::concatenate_artists((*t)["artists"]);
parsed_track.title = t->value("title", std::string());
parsed_track.length =
Track::Duration{t->value("duration", std::string())};
rv.emplace_back(std::move(parsed_track));
}
return rv;
}
};
}
std::unique_ptr<multitrack_strategy> multitrack_strategy::single() {
return std::make_unique<multitrack_strategy_single>();
}
std::unique_ptr<multitrack_strategy> multitrack_strategy::merge() {
return std::make_unique<multitrack_strategy_merge>();
}
std::unique_ptr<multitrack_strategy> multitrack_strategy::separate() {
return std::make_unique<multitrack_strategy_separate>();
}