-
Notifications
You must be signed in to change notification settings - Fork 345
/
musicmetadata.h
553 lines (431 loc) · 17.1 KB
/
musicmetadata.h
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
#ifndef MUSICMETADATA_H_
#define MUSICMETADATA_H_
// C/C++
#include <array>
#include <cstdint>
#include <utility>
// qt
#include <QCoreApplication>
#include <QDateTime>
#include <QImage>
#include <QMap>
#include <QMetaType>
#include <QStringList>
// MythTV
#include "libmythbase/mthread.h"
#include "libmythbase/mythcorecontext.h"
#include "libmythbase/mythtypes.h"
#include "libmythmetadata/mythmetaexp.h"
class AllMusic;
class AlbumArtImages;
class LyricsData;
class MetaIO;
enum ImageType : std::uint8_t
{
IT_UNKNOWN = 0,
IT_FRONTCOVER,
IT_BACKCOVER,
IT_CD,
IT_INLAY,
IT_ARTIST,
IT_LAST
};
class META_PUBLIC AlbumArtImage
{
public:
AlbumArtImage(void) :
m_filename(""), m_hostname(""), m_description("") {}
explicit AlbumArtImage(const AlbumArtImage * const image) :
m_id(image->m_id), m_filename(image->m_filename),
m_hostname(image->m_hostname), m_imageType(image->m_imageType),
m_description(image->m_description), m_embedded(image->m_embedded) {}
int m_id {0};
QString m_filename;
QString m_hostname;
ImageType m_imageType {IT_UNKNOWN};
QString m_description;
bool m_embedded {false};
};
using AlbumArtList = QList<AlbumArtImage*>;
enum RepoType : std::uint8_t
{
RT_Database = 0,
RT_CD = 1,
RT_Radio = 2
};
static constexpr uint8_t METADATA_BITS_FOR_REPO { 8 };
static constexpr uint8_t METADATA_REPO_SHIFT { 24 };
static constexpr uint32_t METADATA_REPO_MASK { 0xff000000 };
static constexpr uint32_t METADATA_ID_MASK { 0x00ffffff };
static constexpr uint32_t ID_TO_ID(uint32_t x) { return x & METADATA_ID_MASK; };
static constexpr uint32_t ID_TO_REPO(uint32_t x) { return x >> METADATA_REPO_SHIFT; };
static constexpr const char* METADATA_INVALID_FILENAME { "**NOT FOUND**" };
static constexpr const char* STREAMUPDATEURL { "https://services.mythtv.org/music/data/?data=streams" };
static constexpr size_t STREAMURLCOUNT { 5 };
using UrlList = std::array<QString,STREAMURLCOUNT>;
class META_PUBLIC MusicMetadata
{
Q_DECLARE_TR_FUNCTIONS(MusicMetadata);
public:
using IdType = uint32_t;
explicit MusicMetadata(QString lfilename = "", QString lartist = "", QString lcompilation_artist = "",
QString lalbum = "", QString ltitle = "", QString lgenre = "",
int lyear = 0, int ltracknum = 0, std::chrono::milliseconds llength = 0ms, int lid = 0,
int lrating = 0, int lplaycount = 0, QDateTime llastplay = QDateTime(),
QDateTime ldateadded = QDateTime(), bool lcompilation = false, QString lformat = "")
: m_artist(std::move(lartist)),
m_compilationArtist(std::move(lcompilation_artist)),
m_album(std::move(lalbum)),
m_title(std::move(ltitle)),
m_genre(std::move(lgenre)),
m_format(std::move(lformat)),
m_year(lyear),
m_trackNum(ltracknum),
m_length(llength),
m_rating(lrating),
m_lastPlay(std::move(llastplay)),
m_dateAdded(std::move(ldateadded)),
m_playCount(lplaycount),
m_compilation(lcompilation),
m_id(lid),
m_filename(std::move(lfilename))
{
checkEmptyFields();
}
MusicMetadata(int lid, QString lbroadcaster, QString lchannel, QString ldescription, const UrlList &lurls, QString llogourl,
QString lgenre, QString lmetaformat, QString lcountry, QString llanguage, QString lformat);
~MusicMetadata();
MusicMetadata(const MusicMetadata &other)
{
*this = other;
}
MusicMetadata& operator=(const MusicMetadata &rhs);
QString Artist() const { return m_artist; }
QString ArtistSort() const { return m_artistSort; }
void setArtist(const QString &lartist,
const QString &lartist_sort = nullptr)
{
m_artist = lartist;
m_artistId = -1;
m_artistSort = lartist_sort;
m_formattedArtist.clear(); m_formattedTitle.clear();
ensureSortFields();
}
QString CompilationArtist() const { return m_compilationArtist; }
QString CompilationArtistSort() const { return m_compilationArtistSort; }
void setCompilationArtist(const QString &lcompilation_artist,
const QString &lcompilation_artist_sort = nullptr)
{
m_compilationArtist = lcompilation_artist;
m_compartistId = -1;
m_compilationArtistSort = lcompilation_artist_sort;
m_formattedArtist.clear(); m_formattedTitle.clear();
ensureSortFields();
}
QString Album() const { return m_album; }
QString AlbumSort() const { return m_albumSort; }
void setAlbum(const QString &lalbum,
const QString &lalbum_sort = nullptr)
{
m_album = lalbum;
m_albumId = -1;
m_albumSort = lalbum_sort;
m_formattedArtist.clear(); m_formattedTitle.clear();
ensureSortFields();
}
QString Title() const { return m_title; }
QString TitleSort() const { return m_titleSort; }
void setTitle(const QString <itle,
const QString <itle_sort = nullptr)
{
m_title = ltitle;
m_titleSort = ltitle_sort;
ensureSortFields();
}
QString FormatArtist();
QString FormatTitle();
QString Genre() const { return m_genre; }
void setGenre(const QString &lgenre) {
m_genre = lgenre;
m_genreId = -1;
}
void setDirectoryId(int ldirectoryid) { m_directoryId = ldirectoryid; }
int getDirectoryId();
void setArtistId(int lartistid) { m_artistId = lartistid; }
int getArtistId();
void setCompilationArtistId(int lartistid) { m_compartistId = lartistid; }
int getCompilationArtistId();
void setAlbumId(int lalbumid) { m_albumId = lalbumid; }
int getAlbumId();
void setGenreId(int lgenreid) { m_genreId = lgenreid; }
int getGenreId();
int Year() const { return m_year; }
void setYear(int lyear) { m_year = lyear; }
int Track() const { return m_trackNum; }
void setTrack(int ltrack) { m_trackNum = ltrack; }
int GetTrackCount() const { return m_trackCount; }
void setTrackCount(int ltrackcount) { m_trackCount = ltrackcount; }
std::chrono::milliseconds Length() const { return m_length; }
template <typename T, std::enable_if_t<std::chrono::__is_duration<T>::value, bool> = true>
void setLength(T llength) { m_length = llength; }
int DiscNumber() const {return m_discNum;}
void setDiscNumber(int discnum) { m_discNum = discnum; }
int DiscCount() const {return m_discCount;}
void setDiscCount(int disccount) { m_discCount = disccount; }
int Playcount() const { return m_playCount; }
void setPlaycount(int lplaycount) { m_playCount = lplaycount; }
IdType ID() const { return m_id; }
void setID(IdType lid) { m_id = lid; }
void setRepo(RepoType repo) { m_id = (m_id & METADATA_ID_MASK) | (repo << METADATA_REPO_SHIFT); }
bool isCDTrack(void) const { return ID_TO_REPO(m_id) == RT_CD; }
bool isDBTrack(void) const { return ID_TO_REPO(m_id) == RT_Database; }
bool isRadio(void) const { return ID_TO_REPO(m_id) == RT_Radio; }
QString Filename(bool find = true);
void setFilename(const QString &lfilename);
QString getLocalFilename(void);
QString Hostname(void) { return m_hostname; }
void setHostname(const QString &host) { m_hostname = host; }
uint64_t FileSize() const { return m_fileSize; }
void setFileSize(uint64_t lfilesize) { m_fileSize = lfilesize; }
QString Format() const { return m_format; }
void setFormat(const QString &lformat) { m_format = lformat; }
int Rating() const { return m_rating; }
void decRating();
void incRating();
void setRating(int lrating) { m_rating = lrating; }
QDateTime LastPlay() const { return m_lastPlay; }
void setLastPlay();
void setLastPlay(const QDateTime& lastPlay);
int PlayCount() const { return m_playCount; }
void incPlayCount();
// track is part of a compilation album
bool Compilation() const { return m_compilation; }
void setCompilation(bool state)
{
m_compilation = state;
m_formattedArtist.clear();
m_formattedTitle.clear();
}
bool determineIfCompilation(bool cd = false);
// for radio streams
void setBroadcaster(const QString &broadcaster) { m_broadcaster = broadcaster; }
QString Broadcaster(void) { return m_broadcaster; }
void setChannel(const QString &channel) { m_channel = channel; }
QString Channel(void) { return m_channel; }
void setDescription(const QString &description) { m_description = description; }
QString Description(void) { return m_description; }
void setUrl(const QString &url, size_t index = 0);
QString Url(size_t index = 0);
void setLogoUrl(const QString &logourl) { m_logoUrl = logourl; }
QString LogoUrl(void) { return m_logoUrl; }
void setMetadataFormat(const QString &metaformat) { m_metaFormat = metaformat; }
QString MetadataFormat(void) { return m_metaFormat; }
void setCountry(const QString &country) { m_country = country; }
QString Country(void) { return m_country; }
void setLanguage(const QString &language) { m_language = language; }
QString Language(void) { return m_language; }
void setEmbeddedAlbumArt(AlbumArtList &albumart);
void reloadMetadata(void);
void dumpToDatabase(void);
void setField(const QString &field, const QString &data);
void getField(const QString& field, QString *data);
void toMap(InfoMap &metadataMap, const QString &prefix = "");
void persist(void);
bool hasChanged(void) const { return m_changed; }
bool compare(MusicMetadata *mdata) const;
// static functions
static MusicMetadata *createFromFilename(const QString &filename);
static MusicMetadata *createFromID(int trackid);
static void setArtistAndTrackFormats();
static QStringList fillFieldList(const QString& field);
static bool updateStreamList(void);
// this looks for any image available - preferring a front cover if available
QString getAlbumArtFile(void);
// this looks only for the given image type
QString getAlbumArtFile(ImageType type);
AlbumArtImages *getAlbumArtImages(void);
void reloadAlbumArtImages(void);
LyricsData *getLyricsData(void);
MetaIO *getTagger(void);
private:
void setCompilationFormatting(bool cd = false);
QString formatReplaceSymbols(const QString &format);
void checkEmptyFields(void);
void ensureSortFields(void);
void saveHostname(void);
QString m_artist;
QString m_artistSort;
QString m_compilationArtist;
QString m_compilationArtistSort;
QString m_album;
QString m_albumSort;
QString m_title;
QString m_titleSort;
QString m_formattedArtist;
QString m_formattedTitle;
QString m_genre;
QString m_format;
int m_year {0};
int m_trackNum {0};
int m_trackCount {0};
int m_discNum {0};
int m_discCount {0};
std::chrono::milliseconds m_length {0ms};
int m_rating {0};
int m_directoryId {-1};
int m_artistId {-1};
int m_compartistId {-1};
int m_albumId {-1};
int m_genreId {-1};
QDateTime m_lastPlay;
QDateTime m_tempLastPlay;
QDateTime m_dateAdded;
int m_playCount {0};
int m_tempPlayCount {0};
bool m_compilation {false};
AlbumArtImages *m_albumArt {nullptr};
LyricsData *m_lyricsData {nullptr};
IdType m_id {0};
QString m_filename; // file name as stored in the DB
QString m_hostname; // host where file is located as stored in the DB
QString m_actualFilename; // actual URL of the file if found
uint64_t m_fileSize {0};
bool m_changed {false};
// radio stream stuff
QString m_broadcaster;
QString m_channel;
QString m_description;
UrlList m_urls;
QString m_logoUrl;
QString m_metaFormat;
QString m_country;
QString m_language;
// Various formatting strings
static QString s_formatNormalFileArtist;
static QString s_formatNormalFileTrack;
static QString s_formatNormalCdArtist;
static QString s_formatNormalCdTrack;
static QString s_formatCompilationFileArtist;
static QString s_formatCompilationFileTrack;
static QString s_formatCompilationCdArtist;
static QString s_formatCompilationCdTrack;
};
bool operator==(MusicMetadata& a, MusicMetadata& b);
bool operator!=(MusicMetadata& a, MusicMetadata& b);
Q_DECLARE_METATYPE(MusicMetadata *)
Q_DECLARE_METATYPE(MusicMetadata)
using MetadataPtrList = QList<MusicMetadata*>;
Q_DECLARE_METATYPE(MetadataPtrList *)
Q_DECLARE_METATYPE(ImageType);
//---------------------------------------------------------------------------
class META_PUBLIC MetadataLoadingThread : public MThread
{
public:
explicit MetadataLoadingThread(AllMusic *parent_ptr)
: MThread("MetadataLoading"), m_parent(parent_ptr) {}
void run() override; // MThread
private:
AllMusic *m_parent {nullptr};
};
//---------------------------------------------------------------------------
class META_PUBLIC AllMusic
{
Q_DECLARE_TR_FUNCTIONS(AllMusic);
public:
AllMusic(void);
~AllMusic();
MusicMetadata* getMetadata(int an_id);
bool updateMetadata(int an_id, MusicMetadata *the_track);
int count() const { return m_numPcs; }
int countLoaded() const { return m_numLoaded; }
void save();
bool startLoading(void);
void resync(); // After a CD rip, for example
// cd stuff
void clearCDData(void);
void addCDTrack(const MusicMetadata &the_track);
bool checkCDTrack(MusicMetadata *the_track);
MusicMetadata* getCDMetadata(int m_the_track);
QString getCDTitle(void) const { return m_cdTitle; }
void setCDTitle(const QString &a_title) { m_cdTitle = a_title; }
int getCDTrackCount(void) const { return m_cdData.count(); }
bool doneLoading() const { return m_doneLoading; }
bool cleanOutThreads();
MetadataPtrList *getAllMetadata(void) { return &m_allMusic; }
MetadataPtrList *getAllCDMetadata(void) { return &m_cdData; }
bool isValidID(int an_id);
private:
MetadataPtrList m_allMusic;
int m_numPcs {0};
int m_numLoaded {0};
using MusicMap = QMap<int, MusicMetadata*>;
MusicMap m_musicMap;
// cd stuff
MetadataPtrList m_cdData; // More than one cd player?
QString m_cdTitle;
MetadataLoadingThread *m_metadataLoader {nullptr};
bool m_doneLoading {false};
int m_playCountMin {0};
int m_playCountMax {0};
qint64 m_lastPlayMin {0};
qint64 m_lastPlayMax {0};
};
using StreamList = QList<MusicMetadata*>;
class META_PUBLIC AllStream
{
public:
AllStream(void);
~AllStream();
void loadStreams(void);
bool isValidID(MusicMetadata::IdType an_id);
MusicMetadata* getMetadata(MusicMetadata::IdType an_id);
StreamList *getStreams(void) { return &m_streamList; }
void addStream(MusicMetadata *mdata);
void removeStream(MusicMetadata *mdata);
void updateStream(MusicMetadata *mdata);
private:
StreamList m_streamList;
};
//----------------------------------------------------------------------------
class AlbumArtScannerThread: public MThread
{
public:
explicit AlbumArtScannerThread(QStringList strList) :
MThread("AlbumArtScanner"), m_strList(std::move(strList)) {}
void run() override // MThread
{
RunProlog();
gCoreContext->SendReceiveStringList(m_strList);
RunEpilog();
}
QStringList getResult(void) { return m_strList; }
private:
QStringList m_strList;
};
class META_PUBLIC AlbumArtImages
{
Q_DECLARE_TR_FUNCTIONS(AlbumArtImages);
public:
explicit AlbumArtImages(MusicMetadata *metadata, bool loadFromDB = true);
explicit AlbumArtImages(MusicMetadata *metadata, const AlbumArtImages &other);
~AlbumArtImages();
void scanForImages(void);
void addImage(const AlbumArtImage * newImage);
uint getImageCount() { return m_imageList.size(); }
AlbumArtImage *getImage(ImageType type);
AlbumArtImage *getImageByID(int imageID);
QStringList getImageFilenames(void) const;
AlbumArtList *getImageList(void) { return &m_imageList; }
AlbumArtImage *getImageAt(uint index);
void dumpToDatabase(void);
static ImageType guessImageType(const QString &filename);
static QString getTypeName(ImageType type);
static QString getTypeFilename(ImageType type);
static ImageType getImageTypeFromName(const QString &name);
private:
void findImages(void);
MusicMetadata *m_parent {nullptr};
AlbumArtList m_imageList;
};
Q_DECLARE_METATYPE(AlbumArtImage*);
#endif