-
Notifications
You must be signed in to change notification settings - Fork 43
/
cuesheet.cpp
324 lines (311 loc) · 11 KB
/
cuesheet.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
318
319
320
321
322
323
324
#include <sstream>
#include <clocale>
#include <locale>
#include <regex>
#include "cuesheet.h"
#include "win32util.h"
#include "metadata.h"
#include "CompositeSource.h"
#include "NullSource.h"
#include "TrimmedSource.h"
#include "InputFactory.h"
static inline
unsigned msf2frames(unsigned mm, unsigned ss, unsigned ff)
{
return (mm * 60 + ss) * 75 + ff;
}
static inline
uint64_t frame2sample(double sampling_rate, uint32_t nframe)
{
return static_cast<uint64_t>(nframe / 75.0 * sampling_rate + 0.5);
}
template <typename CharT>
bool CueTokenizer<CharT>::nextline()
{
m_fields.clear();
int_type c;
std::basic_string<CharT> field;
while (traits_type::not_eof(c = m_sb->sbumpc())) {
if (c == '"') {
// eat until closing quote
while (traits_type::not_eof(c = m_sb->sbumpc())) {
if (c == '\n')
throw std::runtime_error(strutil::format(
"cuesheet: runaway string at line %u",
static_cast<uint32_t>(m_lineno) + 1));
else if (c != '"')
field.push_back(c);
else if (m_sb->sgetc() != '"') // closing quote
break;
else { // escaped quote
m_sb->snextc();
field.push_back(c);
}
}
}
else if (c == '\n') {
++m_lineno;
break;
}
else if (isWS(c)) {
if (field.size()) {
m_fields.push_back(field);
field.clear();
}
while (isWS(m_sb->sgetc()))
m_sb->snextc();
}
else
field.push_back(c);
}
if (field.size()) m_fields.push_back(field);
return m_fields.size() > 0 || c == '\n';
}
template struct CueTokenizer<char>;
template struct CueTokenizer<wchar_t>;
void CueTrack::addSegment(const CueSegment &seg)
{
if (m_segments.size()) {
CueSegment &last = m_segments.back();
if (last.m_index >= seg.m_index) {
if (last.m_index == 0x7fffffff) {
std::string msg =
strutil::format("cuesheet: conflicting use of "
"INDEX00 or PREGAP found on "
"track %u-%u", m_number, m_number + 1);
throw std::runtime_error(msg);
}
std::string msg =
strutil::format("cuesheet: INDEX must be in strictly "
"ascending order: track %u", m_number);
throw std::runtime_error(msg);
}
if (last.m_filename == seg.m_filename && last.m_end == seg.m_begin)
{
last.m_end = seg.m_end;
return;
}
}
m_segments.push_back(seg);
}
std::map<std::string, std::string> CueTrack::getTags() const
{
auto result = m_cuesheet->getTags();
std::for_each(m_meta.begin(), m_meta.end(),
[&](decltype(*m_meta.begin()) &tag) {
auto key = strutil::w2us(tag.first);
if (key == "PERFORMER")
key = "artist";
result[key] = strutil::w2us(tag.second);
});
result["track number"]
= strutil::format("%u/%u", number(), m_cuesheet->count());
return TextBasedTag::normalizeTags(result);
}
void CueSheet::parse(std::wstreambuf *src)
{
static struct handler_t {
const wchar_t *cmd;
void (CueSheet::*mf)(const std::wstring *args);
size_t nargs;
} handlers[] = {
{ L"FILE", &CueSheet::parseFile, 3 },
{ L"TRACK", &CueSheet::parseTrack, 3 },
{ L"INDEX", &CueSheet::parseIndex, 3 },
{ L"POSTGAP", &CueSheet::parsePostgap, 2 },
{ L"PREGAP", &CueSheet::parsePregap, 2 },
{ L"REM", &CueSheet::parseRem, 3 },
{ L"CATALOG", &CueSheet::parseMeta, 2 },
{ L"ISRC", &CueSheet::parseMeta, 2 },
{ L"PERFORMER", &CueSheet::parseMeta, 2 },
{ L"SONGWRITER", &CueSheet::parseMeta, 2 },
{ L"TITLE", &CueSheet::parseMeta, 2 },
{ 0, 0, 0 }
};
CueTokenizer<wchar_t> tokenizer(src);
while (tokenizer.nextline()) {
if (!tokenizer.m_fields.size())
continue;
m_lineno = tokenizer.m_lineno;
std::wstring cmd = tokenizer.m_fields[0];
for (handler_t *p = handlers; p->cmd; ++p) {
if (cmd != p->cmd)
continue;
if (tokenizer.m_fields.size() == p->nargs)
(this->*p->mf)(&tokenizer.m_fields[0]);
else if (cmd != L"REM")
die(strutil::format("wrong num args for %ls command", p->cmd));
break;
}
// if (!p->cmd) die("Unknown command");
}
validate();
}
std::vector<std::shared_ptr<ISeekableSource>>
CueSheet::loadTracks(bool is_embedded, const std::wstring &path,
const std::vector<int> &selection)
{
std::vector<std::shared_ptr<ISeekableSource>> tracks;
for (auto track = begin(); track != end(); ++track) {
if (selection.size()) {
if (std::find(selection.begin(), selection.end(),
track->number()) == selection.end())
continue;
}
std::shared_ptr<CompositeSource> track_source(new CompositeSource());
std::for_each(track->begin(), track->end(), [&](const CueSegment &seg) {
std::shared_ptr<ISeekableSource> src;
if (seg.m_filename == L"__GAP__") {
if (tracks.size())
src.reset(new NullSource(src->getSampleFormat()));
} else if (is_embedded) {
src = InputFactory::instance().open(path.c_str());
} else {
std::wstring ifilename =
win32::PathCombineX(path, seg.m_filename);
src = InputFactory::instance().open(ifilename.c_str());
}
if (src.get()) {
double rate = src->getSampleFormat().mSampleRate;
uint64_t begin = frame2sample(rate, seg.m_begin);
uint64_t duration = ~0ULL;
if (seg.m_end != ~0U)
duration = frame2sample(rate, seg.m_end) - begin;
src.reset(new TrimmedSource(src, begin, duration));
track_source->addSource(src);
}
});
auto &stags = track_source->getTags();
std::regex re("^CUE_TRACK(\\d+)_(.*)$", std::regex_constants::icase);
std::smatch match;
std::map<std::string, std::string> tags;
for (auto it = stags.begin(); it != stags.end(); ++it) {
if (std::regex_match(it->first, match, re)) {
int tn = atoi(match[1].str().c_str());
if (tn == track->number())
tags[match[2].str()] = it->second;
} else
tags[it->first] = it->second;
}
auto track_tags = track->getTags();
for (auto it = track_tags.begin(); it != track_tags.end(); ++it)
tags[it->first] = it->second;
track_source->setTags(tags);
tracks.push_back(track_source);
}
return tracks;
}
std::map<std::string, std::string> CueSheet::getTags() const
{
std::map<std::string, std::string> result;
std::for_each(m_meta.begin(), m_meta.end(),
[&](decltype(*m_meta.begin()) &tag) {
std::string key = strutil::w2us(tag.first);
std::string val = strutil::w2us(tag.second);
if (key == "PERFORMER") {
result["artist"] = val;
result["ALBUM ARTIST"] = val;
} else if (key == "TITLE")
result["album"] = val;
else
result[key] = val;
});
return TextBasedTag::normalizeTags(result);
}
void CueSheet::validate()
{
auto index1_missing =
[](const CueTrack &track) {
return !std::count_if(track.begin(), track.end(),
[](const CueSegment &seg) {
return seg.m_index == 1;
});
};
auto track = std::find_if(begin(), end(), index1_missing);
if (track != end()) {
std::string msg =
strutil::format("cuesheet: INDEX01 not found on track %u",
track->number());
throw std::runtime_error(msg);
};
}
void CueSheet::parseFile(const std::wstring *args)
{
if (!m_cur_file.empty() && m_cur_file != args[1])
this->m_has_multiple_files = true;
m_cur_file = args[1];
}
void CueSheet::parseTrack(const std::wstring *args)
{
if (args[2] == L"AUDIO") {
unsigned no;
if (std::swscanf(args[1].c_str(), L"%d", &no) != 1)
die("Invalid TRACK number");
m_tracks.push_back(CueTrack(this, no));
}
}
void CueSheet::parseIndex(const std::wstring *args)
{
if (!m_tracks.size())
die("INDEX command before TRACK");
if (m_cur_file.empty())
die("INDEX command before FILE");
unsigned no, mm, ss, ff, nframes;
if (std::swscanf(args[1].c_str(), L"%u", &no) != 1)
die("Invalid INDEX number");
if (std::swscanf(args[2].c_str(), L"%u:%u:%u", &mm, &ss, &ff) != 3)
die("Invalid INDEX time format");
if (ss > 59 || ff > 74)
die("Invalid INDEX time format");
nframes = msf2frames(mm, ss, ff);
CueSegment *lastseg = lastSegment();
if (lastseg && lastseg->m_filename == m_cur_file) {
lastseg->m_end = nframes;
if (lastseg->m_begin >= nframes)
die("INDEX time must be in ascending order");
}
CueSegment segment(m_cur_file, no);
segment.m_begin = nframes;
if (no > 0)
m_tracks.back().addSegment(segment);
else {
if (m_tracks.size() == 1) {
/* HTOA */
m_tracks.insert(m_tracks.begin(), CueTrack(this, 0));
m_tracks[0].setMeta(L"title", L"(HTOA)");
segment.m_index = 1;
} else
segment.m_index = 0x7fffffff;
m_tracks[m_tracks.size() - 2].addSegment(segment);
}
}
void CueSheet::parsePostgap(const std::wstring *args)
{
if (!m_tracks.size())
die("POSTGAP command before TRACK");
unsigned mm, ss, ff;
if (std::swscanf(args[1].c_str(), L"%u:%u:%u", &mm, &ss, &ff) != 3)
die("Invalid POSTGAP time format");
CueSegment segment(std::wstring(L"__GAP__"), 0x7ffffffe);
segment.m_end = msf2frames(mm, ss, ff);
m_tracks.back().addSegment(segment);
}
void CueSheet::parsePregap(const std::wstring *args)
{
if (!m_tracks.size())
die("PREGAP command before TRACK");
unsigned mm, ss, ff;
if (std::swscanf(args[1].c_str(), L"%u:%u:%u", &mm, &ss, &ff) != 3)
die("Invalid PREGAP time format");
CueSegment segment(std::wstring(L"__GAP__"), 0x7fffffff);
segment.m_end = msf2frames(mm, ss, ff);
if (m_tracks.size() > 1)
m_tracks[m_tracks.size() - 2].addSegment(segment);
}
void CueSheet::parseMeta(const std::wstring *args)
{
if (m_tracks.size())
m_tracks.back().setMeta(args[0], args[1]);
else
m_meta[args[0]] = args[1];
}