-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcueparser.cpp
More file actions
422 lines (379 loc) · 12.9 KB
/
cueparser.cpp
File metadata and controls
422 lines (379 loc) · 12.9 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* Copyright (C) 2023 Jo2003 (olenka.joerg@gmail.com)
* This file is part of cd2netmd_gui (NetMD Wizard)
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NetMD Wizard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
*/
#include "cueparser.h"
#include <QFile>
#include <QFileInfo>
#include <QRegExp>
#include <QDir>
#include <QMimeDatabase>
#include <QMimeType>
///
/// \brief The CueThrow struct
///
struct CueThrow
{
int mErr;
QString mSErr;
};
/// \brief helper marco to throw an error
#define mCueThrow(x_, ...) { QString se_; QTextStream tse_(&se_); tse_ << __VA_ARGS__; throw CueThrow{x_, se_};}
//--------------------------------------------------------------------------
//! @brief Constructs a new instance.
//!
//! @param[in] cueFileName The cue file name
//--------------------------------------------------------------------------
CueParser::CueParser(const QString &cueFileName)
{
if (!cueFileName.isEmpty())
{
parse(cueFileName);
}
}
//--------------------------------------------------------------------------
//! @brief find the audio file, try some simple searches
//!
//! @param[in] path where to look for the audio file
//! @param[in,out] file name as searched / found
//!
//! @return true file found, false not found
//--------------------------------------------------------------------------
bool CueParser::findAudioFile(const QString& path, QString& fileName) const
{
if (QFile::exists(path + "/" + fileName))
{
qInfo() << "Audio file" << fileName << "found!";
return true;
}
else
{
QMimeDatabase mimeDb;
QFileInfo fi(fileName);
QDir audioDir(path);
audioDir.setNameFilters({fi.completeBaseName() + ".*"});
for (const auto& f : audioDir.entryInfoList())
{
QMimeType mt = mimeDb.mimeTypeForFile(f);
if (mt.isValid())
{
QString mtName = mt.name();
if (mtName.toLower().contains("audio"))
{
fileName = f.fileName();
qInfo() << "Found alternate audio file" << fileName << mtName;
return true;
}
}
}
}
return false;
}
//--------------------------------------------------------------------------
//! @brief parse the cue file
//!
//! @param[in] cueFileName The cue file name
//!
//! @return 0 on success; < 0 on error
//--------------------------------------------------------------------------
int CueParser::parse(const QString &cueFileName)
{
int ret = 0;
// You thought this would be nice? You're wrong!
// disc year
QRegExp rxpYear("^REM\\s+DATE\\s+(.*)$");
// get title string
QRegExp rxpTitle("^TITLE\\s+(.*)$");
// get performer
QRegExp rxpPerf("^PERFORMER\\s+(.*)$");
// get track
QRegExp rxpTrack("^TRACK\\s+([0-9]+)\\s+([A-Za-z]+)$");
// get INDEX 01
QRegExp rxpIndex("^INDEX\\s+01\\s+([0-9:]+)$");
// get audio file
QRegExp rxpFile("^FILE\\s+(.*)\\s+(.+)$");
// pre-filter
QRegExp rxpUse("^(REM\\s+DATE|TITLE|PERFORMER|TRACK|INDEX\\s+01|FILE).*$");
QFile cueFile(cueFileName);
QFileInfo cfi(cueFileName);
bool inTrack = false;
Track track;
QString file, lastFile;
TrackType ttype = TrackType::DATA;
int audLengthMs = 0;
uint32_t audConv = 0;
mDiscData = {"", "", 0, -1, {}};
audio::STag tag;
try
{
if (!cueFile.open(QIODevice::ReadOnly))
{
mCueThrow(-1, "Can't open cue file " << cueFileName);
}
QTextStream in(&cueFile);
while (!in.atEnd())
{
QString line = in.readLine().trimmed();
// check for needed information
if (rxpUse.indexIn(line) > -1) // pre-filter!
{
if (rxpFile.indexIn(line) > -1) // Audio file
{
lastFile = file;
file = rxpFile.cap(1).replace(QChar('"'), "");
// remove any directory part
QFileInfo afi(file);
file = afi.fileName();
if (!findAudioFile(cfi.canonicalPath(), file))
{
mCueThrow(-5, "Can't find audio file " << file);
}
if (lastFile != file)
{
// get additional information from audio file
if (audio::checkAudioFile(cfi.canonicalPath() + "/" + file, audConv, audLengthMs, &tag) != 0)
{
mCueThrow(-4, "Can't recognize audio file " << file);
}
}
}
else if (rxpYear.indexIn(line) > -1) // year
{
mDiscData.mYear = rxpYear.cap(1).toInt();
qInfo() << "Found year" << mDiscData.mYear << "in cue sheet file!";
}
else if (rxpTitle.indexIn(line) > -1) // title (disc or track)
{
if (inTrack)
{
track.mTitle = rxpTitle.cap(1).replace(QChar('"'), "");
}
else
{
mDiscData.mTitle = rxpTitle.cap(1).replace(QChar('"'), "");
}
}
else if (rxpPerf.indexIn(line) > -1) // performer / artist (disc or track)
{
if (inTrack)
{
track.mPerformer = rxpPerf.cap(1).replace(QChar('"'), "");
}
else
{
mDiscData.mPerformer = rxpPerf.cap(1).replace(QChar('"'), "");
}
}
else if (rxpTrack.indexIn(line) > -1) // track
{
if (inTrack)
{
// store track
mDiscData.mTracks.append(track);
// cleanup track structure
track = {-1, TrackType::DATA, "", "", "", 0, 0, 0, 0, 0};
}
inTrack = true;
track.mNo = rxpTrack.cap(1).toUInt();
ttype = (rxpTrack.cap(2).toUpper() == "AUDIO") ? TrackType::AUDIO : TrackType::DATA;
}
else if (rxpIndex.indexIn(line) > -1) // index 01
{
// file mentioned before INDEX 01 is the track file
track.mAudioFile = file;
track.mConversion = audConv;
track.mEndMs = static_cast<uint32_t>(audLengthMs);
track.mType = ttype;
// add missing information
if (track.mTitle.isEmpty())
{
track.mTitle = !tag.mTitle.isEmpty() ? tag.mTitle : titleFromFileName(file);
}
if (track.mPerformer.isEmpty())
{
track.mPerformer = !tag.mArtist.isEmpty() ? tag.mArtist : mDiscData.mPerformer;
}
QString idx = rxpIndex.cap(1);
auto idxs = idx.split(QChar(':'));
if (idxs.size() == 3)
{
uint32_t msec = idxs.at(0).toUInt() * 60 * 1000; // minutes
msec += idxs.at(1).toUInt() * 1000; // seconds
msec += idxs.at(2).toUInt() * 10; // 100ths of seconds
track.mStartMs = msec;
// we must mark the end of the former track
if (!mDiscData.mTracks.isEmpty() && msec)
{
mDiscData.mTracks[mDiscData.mTracks.size() - 1].mEndMs = msec;
}
}
else
{
mCueThrow(-3, "Wrong index format: " << idx);
}
}
else
{
mCueThrow(-2, "Can't parse line: " << line);
}
}
else
{
qDebug() << "Ignore line" << line;
}
}
cueFile.close();
// mind the last track
if (inTrack)
{
// complete current track info
if (track.mPerformer.isEmpty())
{
track.mPerformer = mDiscData.mPerformer;
}
// store track
mDiscData.mTracks.append(track);
}
// compute disc length and LBA stuff
uint32_t lba = 0;
lastFile = "";
for (auto& t : mDiscData.mTracks)
{
if (lastFile != t.mAudioFile)
{
// new file means we start from 0
lba = 0;
}
lastFile = t.mAudioFile;
t.mStartLba = lba;
t.mLbaCount = qRound((static_cast<double>(t.mEndMs - t.mStartMs) / 1000.0) * 75.0);
lba += t.mLbaCount;
mDiscData.mLenInMs += t.mEndMs - t.mStartMs;
}
qDebug().noquote() << Qt::endl << static_cast<QString>(mDiscData);
}
catch (const CueThrow& e)
{
qWarning() << e.mSErr;
ret = e.mErr;
}
catch (...)
{
qWarning() << "Unknown error while parsing cue file" << cueFileName;
ret = -1;
}
// sanity check
if (ret == 0)
{
mValid = true;
if (!mDiscData.mTracks.isEmpty() && (mDiscData.mLenInMs > 0))
{
for (const auto& t : mDiscData.mTracks)
{
if ((t.mEndMs == 0)
|| t.mAudioFile.isEmpty()
|| ((t.mNo < 1) || (t.mNo > mDiscData.mTracks.size())))
{
mValid = false;
ret = -1;
break;
}
}
}
else
{
mValid = false;
ret = -1;
}
}
else
{
mValid = false;
}
return ret;
}
//--------------------------------------------------------------------------
//! @brief get track count
//!
//! @return number of tracks
//--------------------------------------------------------------------------
int CueParser::trackCount() const
{
return mDiscData.mTracks.size();
}
//--------------------------------------------------------------------------
//! @brief git disc title
//!
//! @return title as string
//--------------------------------------------------------------------------
const QString &CueParser::discTitle() const
{
return mDiscData.mTitle;
}
//--------------------------------------------------------------------------
//! @brief get disc year
//!
//! @return year
//--------------------------------------------------------------------------
int CueParser::discYear() const
{
return mDiscData.mYear;
}
//--------------------------------------------------------------------------
//! @brief get disc artist / performer
//!
//! @return artist as string
//--------------------------------------------------------------------------
const QString &CueParser::discPerfromer() const
{
return mDiscData.mPerformer;
}
//--------------------------------------------------------------------------
//! @brief get disc audio length in ms
//!
//! @return length in ms
//--------------------------------------------------------------------------
uint32_t CueParser::discLength() const
{
return mDiscData.mLenInMs;
}
//--------------------------------------------------------------------------
//! @brief get complete track information
//!
//! @param[in] no track number, starts with 1
//!
//! @return track information or empty struct
//--------------------------------------------------------------------------
const CueParser::Track &CueParser::track(int no) const
{
if ((no <= mDiscData.mTracks.size()) && (no > -1))
{
// not index based
return mDiscData.mTracks.at(no - 1);
}
else
{
return mEmptyTrack;
}
}
//--------------------------------------------------------------------------
//! @brief sanity check in Disc structure
//!
//! @return true if valid; false if not
//--------------------------------------------------------------------------
bool CueParser::isValid() const
{
return mValid;
}