-
Notifications
You must be signed in to change notification settings - Fork 1
/
exttag.cpp
617 lines (520 loc) · 17.3 KB
/
exttag.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#include "exttag.h"
#include <boost/preprocessor.hpp>
//#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/compare.hpp>
#include <windows.h>
#include <propkey.h>
#include <asftag.h>
#include <apetag.h>
#include <id3v1tag.h>
#include <id3v2tag.h>
#include <textidentificationframe.h>
#include <unknownframe.h>
#include <xiphcomment.h>
#include <mpegfile.h>
using namespace TagLib;
#define RANGE(x) (x).begin(), (x).end()
// === How this file works. ===
// - Functions like uchar rating(TagLib::FileRef &) are exposed.
// - These take the fileref's tag, and attempt to cast it to all of the TAG_CLASSES
// - If an cast succeeds, it calls the worker function for the type (ie. readrating(APE::TAG..))
// - If the tag is none of the tag types, it's probably a tag union:
// - They will then attempt to cast the fileref's file to an MPEG file.
// - A splitter is defined that will take all the possible tag types from this file, and upcast+return them.
//
// Basically, all of the useful functionality is rooted in the ie. readrating() functions, the rest is support.
// The types of tags to attempt to extract from a tag() type.
#define TAG_CLASSES (const Ogg::XiphComment)(const ASF::Tag)(const APE::Tag)(const ID3v2::Tag)
// Attempt to upcast var to type. If it succeeds, /return/ func(upcasted);
#define RETURN_IF_UPCAST(func, type, var) if (type *ty = dynamic_cast<type *>(var)) return func(ty);
// Generate a function to split an MPEG::File into it's respective tag types,
// and attempt to call the upcasted versions.
// ID3v1 is not handled as this interface won't be reached for anything that it supports.
#define FILE_SPLITTER_MPEG(ret, func, def) \
ret func(MPEG::File *file) \
{ \
RETURN_IF_UPCAST(func, const ID3v2::Tag, file->ID3v2Tag()); \
RETURN_IF_UPCAST(func, const APE::Tag, file->APETag() ); \
def; \
}
// Inner body of the tag foreach loop; return_if_upcast all of the classes in order.
#define UPCAST_CALL_N_RETURN_TAG(r, data, elem) RETURN_IF_UPCAST(data, elem, tag)
// Generate the exposed functions and the stuff they require.
#define READER_FUNC(ret, name, def) \
FILE_SPLITTER_MPEG(ret, read##name, def); \
ret name(const TagLib::FileRef &fileref) \
{ \
const TagLib::Tag *tag = fileref.tag(); \
BOOST_PP_SEQ_FOR_EACH(UPCAST_CALL_N_RETURN_TAG, read##name, TAG_CLASSES); \
\
TagLib::File *file = fileref.file(); \
RETURN_IF_UPCAST(read##name, MPEG::File, file); \
\
def; \
}
const String emptyString;
bool is_iequal(const std::wstring& l, const std::wstring& r)
{
if (l == r)
return true;
if (l.size() != r.size())
return false;
std::wstring::const_iterator rit = r.begin();
for (std::wstring::const_iterator it = l.begin(); it != l.end(); ++it)
if (tolower(*it) != tolower(*rit++))
return false;
return true;
}
typedef std::vector<TagLib::String> tlstrvec_t;
const tlstrvec_t readTXXXStrings(const ID3v2::Tag *tag, const String &name)
{
const ID3v2::FrameList &fl = tag->frameListMap()["TXXX"];
for (ID3v2::FrameList::ConstIterator it = fl.begin(); it != fl.end(); ++it)
{
const ID3v2::TextIdentificationFrame *tif = dynamic_cast<const ID3v2::TextIdentificationFrame *>(*it);
const StringList sl = tif->fieldList();
if (sl.size() >= 2)
if (is_iequal(sl[0].toWString(), name.toWString()))
return tlstrvec_t(++sl.begin(), sl.end());
}
return tlstrvec_t();
}
const String readTXXXString(const ID3v2::Tag *tag, const String &name)
{
const tlstrvec_t sl = readTXXXStrings(tag, name);
if (!sl.size())
return emptyString;
return sl[0];
}
std::wstring readString(const APE::Tag *tag, const std::wstring &name)
{
const StringList& lst = tag->itemListMap()[name].values();
if (lst.size())
return lst[0].toWString();
throw std::domain_error("no ape");
}
std::wstring readString(const ASF::Tag *tag, const std::string &name)
{
// Ew ew ew.
const ASF::AttributeListMap &alm = const_cast<ASF::Tag *>(tag)->attributeListMap();
if (alm.contains(name))
return alm[name][0].toString().toWString();
throw std::domain_error("no asf");
}
std::wstring readString(const Ogg::XiphComment *tag, const std::string &name)
{
const StringList &sl = tag->fieldListMap()[name];
for (StringList::ConstIterator it = sl.begin(); it != sl.end(); ++it)
return it->toWString();
throw std::domain_error("no xiph");
}
unsigned char normaliseRating(int rat)
{
if (rat > 0)
switch (rat)
{
case 1: return RATING_ONE_STAR_SET;
case 2: return RATING_TWO_STARS_SET;
case 3: return RATING_THREE_STARS_SET;
case 4: return RATING_FOUR_STARS_SET;
case 5: return RATING_FIVE_STARS_SET;
default:
{
if (rat < 100)
return rat;
if (rat < 255)
return static_cast<unsigned char>(rat*100/255.f);
}
}
return RATING_UNRATED_SET;
}
unsigned char readrating(const APE::Tag *tag)
{
const StringList& lst = tag->itemListMap()[L"rating"].values();
if (lst.size())
return normaliseRating(lst[0].toInt());
return RATING_UNRATED_SET;
}
unsigned char readrating(const ASF::Tag *tag)
{
return normaliseRating(tag->rating().toInt());
}
#define FOR_EACH_ID3_FRAME_UNKNOWN(name) { \
const ID3v2::FrameList &fl = tag->frameListMap()[name]; \
for (ID3v2::FrameList::ConstIterator it = fl.begin(); it != fl.end(); ++it) \
if (ID3v2::UnknownFrame *fr = dynamic_cast<ID3v2::UnknownFrame *>(*it)) \
{ \
const ByteVector &s = fr->data();
#define FOR_EACH_ID3_FRAME_TIF(name) { \
const ID3v2::FrameList &fl = tag->frameListMap()[name]; \
for (ID3v2::FrameList::ConstIterator it = fl.begin(); it != fl.end(); ++it) \
if (ID3v2::TextIdentificationFrame *fr = dynamic_cast<ID3v2::TextIdentificationFrame *>(*it))
unsigned char readrating(const ID3v2::Tag *tag)
{
// First attempt to read from 4.17 Popularimeter. (http://www.id3.org/id3v2.4.0-frames)
// <Header for 'Popularimeter', ID: "POPM">
// Email to user <text string> $00
// Rating $xx
// Counter $xx xx xx xx (xx ...)
// The rating is 1-255 where 1 is worst and 255 is best. 0 is unknown.
FOR_EACH_ID3_FRAME_UNKNOWN("POPM")
// Locate the null byte.
ByteVector::ConstIterator sp = std::find(s.begin(), s.end(), 0);
// If we found it, and the string continues, at least another character,
// the continued character is the rating byte.
if (sp != s.end() && ++sp != s.end())
return static_cast<unsigned char>(
static_cast<unsigned char>(*sp)*100/255.f);
}
}
return normaliseRating(readTXXXString(tag, "rating").toInt());
}
unsigned char readrating(const Ogg::XiphComment *tag)
{
const StringList &sl = tag->fieldListMap()["RATING"];
for (StringList::ConstIterator it = sl.begin(); it != sl.end(); ++it)
if (int i = it->toInt())
return normaliseRating(i);
return RATING_UNRATED_SET;
}
std::wstring readalbumArtist(const APE::Tag *tag)
{
return readString(tag, L"Album Artist");
}
std::wstring readalbumArtist(const ASF::Tag *tag)
{
return readString(tag, "WM/AlbumArtist");
}
std::wstring readalbumArtist(const ID3v2::Tag *tag)
{
FOR_EACH_ID3_FRAME_TIF("TPE2") // Person 2
return fr->toString().toWString();
}
throw std::domain_error("no id3v2");
}
std::wstring readalbumArtist(const Ogg::XiphComment *tag)
{
return readString(tag, "ALBUMARTIST");
}
wstrvec_t toVector(StringList::ConstIterator beg, StringList::ConstIterator end)
{
StringList::ConstIterator it = beg;
wstrvec_t ret;
while (it != end)
ret.push_back(it++->toWString());
return ret;
}
wstrvec_t toVector(const tlstrvec_t &vec)
{
wstrvec_t ret;
ret.reserve(vec.size());
for (std::vector<TagLib::String>::const_iterator it = vec.begin(); it != vec.end(); ++it)
ret.push_back(it->toWString());
return ret;
}
wstrvec_t toVector(const StringList &lst)
{
return toVector(RANGE(lst));
}
wstrvec_t readkeywords(const APE::Tag *tag)
{
return toVector(tag->itemListMap()[L"Keywords"].values());
}
wstrvec_t readkeywords(const ASF::Tag *tag)
{
const char *name = "WM/Category";
// Ew ew ew.
const ASF::AttributeListMap &alm = const_cast<ASF::Tag *>(tag)->attributeListMap();
if (alm.contains(name))
{
ASF::AttributeList lst = alm[name];
wstrvec_t ret; ret.reserve(lst.size());
for (ASF::AttributeList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
ret.push_back(it->toString().toWString());
return ret;
}
throw std::domain_error("no asf");
}
wstrvec_t readkeywords(const ID3v2::Tag *tag)
{
const ID3v2::FrameList &fl = tag->frameListMap()["TXXX"];
for (ID3v2::FrameList::ConstIterator it = fl.begin(); it != fl.end(); ++it)
{
const ID3v2::TextIdentificationFrame *tif = dynamic_cast<const ID3v2::TextIdentificationFrame *>(*it);
const StringList sl = tif->fieldList();
if (sl.size() >= 2)
if (is_iequal(sl[0].toWString(), L"keywords"))
{
StringList::ConstIterator oo = sl.begin();
++oo;
return toVector(oo, sl.end());
}
}
return wstrvec_t();
}
wstrvec_t readkeywords(const Ogg::XiphComment *tag)
{
return toVector(tag->fieldListMap()["KEYWORDS"]);
}
SYSTEMTIME parseDate(const wstrvec_t &vec)
{
if (!vec.size())
return SYSTEMTIME();
for (wstrvec_t::const_iterator it = vec.begin(); it != vec.end(); ++it)
// more than just the year, fill it in full.
if (it->size() >= string("xxxx-xx-xx").size())
{
try
{
SYSTEMTIME full = {};
full.wYear = boost::lexical_cast<int>(it->substr(0,4));
full.wMonth = boost::lexical_cast<int>(it->substr(5,2));
full.wDay = boost::lexical_cast<int>(it->substr(8,2));
return full;
}
catch (boost::bad_lexical_cast &)
{
OutputDebugString((L"TagLibHandler: '" + *it + L"' failed to full parse as a date").c_str());
}
}
// Abandon, and just go with the year.
SYSTEMTIME ret = {};
ret.wYear = boost::lexical_cast<int>(vec.at(0));
return ret;
}
SYSTEMTIME readreleasedate(const APE::Tag *tag)
{
return parseDate(toVector(tag->itemListMap()[L"Year"].values()));
}
SYSTEMTIME readreleasedate(const ASF::Tag *tag)
{
const char *name = "WM/Year";
// Ew ew ew.
const ASF::AttributeListMap &alm = const_cast<ASF::Tag *>(tag)->attributeListMap();
if (alm.contains(name))
{
ASF::AttributeList lst = alm[name];
wstrvec_t ret; ret.reserve(lst.size());
for (ASF::AttributeList::ConstIterator it = lst.begin(); it != lst.end(); ++it)
ret.push_back(it->toString().toWString());
return parseDate(ret);
}
return SYSTEMTIME();
}
// I wrote the code below to attempt to recover from v2.3 tags (D:), and taglib just destroys all the data. \o/
// Never mind, leave it in, why not? The code is brittle anyway.
SYSTEMTIME readreleasedate(const ID3v2::Tag *tag)
{
// Attempt to read the (sane) id3v2.4 tag:
wstrvec_t drcs;
FOR_EACH_ID3_FRAME_TIF("TDRC") // Date of ReCording
drcs.push_back(fr->toString().toWString());
}
SYSTEMTIME point4 = parseDate(drcs);
if (point4.wYear)
return point4;
// Abandon all hope and try to deal with 2.3's failure:
tlstrvec_t years, days;
FOR_EACH_ID3_FRAME_TIF("TYER") // Year
years.push_back(fr->toString());
}
// No data, abort:
if (!years.size())
return SYSTEMTIME();
// DDMM, guaranteed 4 chars long.
FOR_EACH_ID3_FRAME_TIF("TDAT") // Date
days.push_back(fr->toString());
}
// If there are mismathcing year and day numbers, we can't do anything clever, return the first year.
if (years.size() != days.size())
{
SYSTEMTIME ret = {};
ret.wYear = years[0].toInt();
return ret;
}
wstrvec_t composed;
composed.reserve(years.size());
tlstrvec_t::const_iterator dit = days.begin();
for (tlstrvec_t::const_iterator yit = years.begin(); yit != years.end(); ++yit, ++dit)
{
const std::wstring day = dit->toWString();
// This could throw if the TDAT tag is misformed.
composed.push_back(yit->toWString() + L"-" + day.substr(2,2) + L"-" + day.substr(0,2));
}
return parseDate(composed);
}
SYSTEMTIME readreleasedate(const Ogg::XiphComment *tag)
{
return parseDate(toVector(tag->fieldListMap()["DATE"]));
}
std::wstring readcomposer(const APE::Tag *tag)
{
return readString(tag, L"Composer");
}
std::wstring readcomposer(const ASF::Tag *tag)
{
return readString(tag, "WM/Composer");
}
std::wstring readcomposer(const ID3v2::Tag *tag)
{
FOR_EACH_ID3_FRAME_TIF("TCOM") // Composer
return fr->toString().toWString();
}
throw std::domain_error("no id3v2");
}
std::wstring readcomposer(const Ogg::XiphComment *tag)
{
return readString(tag, "COMPOSER");
}
std::wstring readconductor(const APE::Tag *tag)
{
return readString(tag, L"Conductor");
}
std::wstring readconductor(const ASF::Tag *tag)
{
return readString(tag, "WM/Conductor");
}
std::wstring readconductor(const ID3v2::Tag *tag)
{
FOR_EACH_ID3_FRAME_TIF("TPE3") // Person 3
return fr->toString().toWString();
}
throw std::domain_error("no id3v2");
}
std::wstring readconductor(const Ogg::XiphComment *tag)
{
return readString(tag, "CONDUCTOR");
}
std::wstring readlabel(const APE::Tag *tag)
{
return readString(tag, L"Label");
}
std::wstring readlabel(const ASF::Tag *tag)
{
return readString(tag, "WM/Publisher");
}
std::wstring readlabel(const ID3v2::Tag *tag)
{
FOR_EACH_ID3_FRAME_TIF("TPUB") // Publisher
return fr->toString().toWString();
}
throw std::domain_error("no id3v2");
}
std::wstring readlabel(const Ogg::XiphComment *tag)
{
return readString(tag, "LABEL");
}
std::wstring readsubtitle(const APE::Tag *tag)
{
return readString(tag, L"Subtitle");
}
std::wstring readsubtitle(const ASF::Tag *tag)
{
return readString(tag, "WM/SubTitle");
}
std::wstring readsubtitle(const ID3v2::Tag *tag)
{
FOR_EACH_ID3_FRAME_TIF("TIT3") // Title 3
return fr->toString().toWString();
}
throw std::domain_error("no id3v2");
}
std::wstring readsubtitle(const Ogg::XiphComment *tag)
{
return readString(tag, "SUBTITLE");
}
std::wstring readproducer(const APE::Tag *tag)
{
return readString(tag, L"Producer");
}
std::wstring readproducer(const ASF::Tag *tag)
{
return readString(tag, "WM/Producer");
}
// TIPL is a set of pairs (a map), even (from/including zero) -> key, odd -> value.
std::wstring readproducer(const ID3v2::Tag *tag)
{
const ID3v2::FrameList &fl = tag->frameListMap()["TIPL"]; // Involved People
for (TagLib::uint i = 0; i < fl.size(); i+=2)
if (ID3v2::TextIdentificationFrame *fr = dynamic_cast<ID3v2::TextIdentificationFrame *>(fl[i]))
if (fr->toString() == L"producer")
return dynamic_cast<ID3v2::TextIdentificationFrame *>(fl[i+1])
->toString().toWString();
throw std::domain_error("no id3v2");
}
std::wstring readproducer(const Ogg::XiphComment *tag)
{
return readString(tag, "PRODUCER");
}
std::wstring readmood(const APE::Tag *tag)
{
return readString(tag, L"Mood");
}
std::wstring readmood(const ASF::Tag *tag)
{
return readString(tag, "WM/Mood");
}
std::wstring readmood(const ID3v2::Tag *tag)
{
FOR_EACH_ID3_FRAME_TIF("TMOO") // Mood
return fr->toString().toWString();
}
throw std::domain_error("no id3v2");
}
std::wstring readmood(const Ogg::XiphComment *tag)
{
return readString(tag, "MOOD");
}
std::wstring readcopyright(const APE::Tag *tag)
{
return readString(tag, L"Copyright");
}
std::wstring readcopyright(const ASF::Tag *tag)
{
return readString(tag, "WM/Copyright");
}
std::wstring readcopyright(const ID3v2::Tag *tag)
{
FOR_EACH_ID3_FRAME_TIF("TCOP") // Copyright
return fr->toString().toWString();
}
throw std::domain_error("no id3v2");
}
std::wstring readcopyright(const Ogg::XiphComment *tag)
{
return readString(tag, "COPYRIGHT");
}
std::wstring readpartofset(const APE::Tag *tag)
{
// This may need some format mangling in some cases?
return readString(tag, L"Disc");
}
std::wstring readpartofset(const ASF::Tag *tag)
{
return readString(tag, "WM/PartOfSet");
}
std::wstring readpartofset(const ID3v2::Tag *tag)
{
FOR_EACH_ID3_FRAME_TIF("TPOS") // Part Of Set
return fr->toString().toWString();
}
throw std::domain_error("no id3v2");
}
std::wstring readpartofset(const Ogg::XiphComment *tag)
{
// This is only the first part of the tuple, Picard does not write Total Discs to xiph comments.
return readString(tag, "DISCNUMBER");
}
READER_FUNC(unsigned char, rating, return RATING_UNRATED_SET)
READER_FUNC(std::wstring, albumArtist, throw std::domain_error("not good"))
READER_FUNC(wstrvec_t, keywords, return wstrvec_t())
READER_FUNC(SYSTEMTIME, releasedate, return SYSTEMTIME())
READER_FUNC(std::wstring, composer, throw std::domain_error("not good"))
READER_FUNC(std::wstring, conductor, throw std::domain_error("not good"))
READER_FUNC(std::wstring, label, throw std::domain_error("not good"))
READER_FUNC(std::wstring, subtitle, throw std::domain_error("not good"))
READER_FUNC(std::wstring, producer, throw std::domain_error("not good"))
READER_FUNC(std::wstring, mood, throw std::domain_error("not good"))
READER_FUNC(std::wstring, copyright, throw std::domain_error("not good"))
READER_FUNC(std::wstring, partofset, throw std::domain_error("not good"))