-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathtrackrecord.cpp
367 lines (345 loc) · 14.1 KB
/
trackrecord.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
#include "track/trackrecord.h"
#include "sources/metadatasource.h"
#include "track/keyfactory.h"
#include "util/logger.h"
namespace mixxx {
namespace {
const Logger kLogger("TrackRecord");
} // anonymous namespace
/*static*/ const QString TrackRecord::kTrackTotalPlaceholder = QStringLiteral("//");
TrackRecord::TrackRecord(TrackId id)
: m_id(std::move(id)),
m_mainCuePosition(mixxx::audio::kStartFramePos),
m_rating(0),
m_bpmLocked(false),
m_headerParsed(false) {
}
void TrackRecord::setKeys(const Keys& keys) {
refMetadata().refTrackInfo().setKey(KeyUtils::getGlobalKeyText(keys));
m_keys = std::move(keys);
}
bool TrackRecord::updateGlobalKey(
track::io::key::ChromaticKey key,
track::io::key::Source keySource) {
if (key == track::io::key::INVALID) {
return false;
} else {
Keys keys = KeyFactory::makeBasicKeys(key, keySource);
if (m_keys.getGlobalKey() != keys.getGlobalKey()) {
setKeys(keys);
return true;
}
}
return false;
}
UpdateResult TrackRecord::updateGlobalKeyText(
const QString& keyText,
track::io::key::Source keySource) {
Keys keys = KeyFactory::makeBasicKeysFromText(keyText, keySource);
if (keys.getGlobalKey() == track::io::key::INVALID) {
return UpdateResult::Rejected;
}
if (m_keys.getGlobalKey() == keys.getGlobalKey()) {
return UpdateResult::Unchanged;
}
setKeys(keys);
return UpdateResult::Updated;
}
namespace {
#if defined(__EXTRA_METADATA__)
bool mergeReplayGainMetadataProperty(
ReplayGain* pMergedReplayGain,
const ReplayGain& importedReplayGain) {
bool modified = false;
// Preserve the values calculated by Mixxx and only merge missing
// values from the imported replay gain.
if (!pMergedReplayGain->hasRatio() &&
importedReplayGain.hasRatio()) {
pMergedReplayGain->setRatio(importedReplayGain.getRatio());
modified = true;
}
if (!pMergedReplayGain->hasPeak() &&
importedReplayGain.hasPeak()) {
pMergedReplayGain->setPeak(importedReplayGain.getPeak());
modified = true;
}
return modified;
}
#endif // __EXTRA_METADATA__
// This conditional copy operation only works for nullable properties
// like QString or QUuid.
template<typename T>
bool copyIfNotNull(
T* pMergedProperty,
const T& importedProperty) {
if (pMergedProperty->isNull() &&
*pMergedProperty != importedProperty) {
*pMergedProperty = importedProperty;
return true;
}
return false;
}
// This conditional copy operation only works for properties where
// empty = missing.
template<typename T>
bool copyIfNotEmpty(
T* pMergedProperty,
const T& importedProperty) {
if (pMergedProperty->isEmpty() &&
*pMergedProperty != importedProperty) {
*pMergedProperty = importedProperty;
return true;
}
return false;
}
} // anonymous namespace
bool TrackRecord::updateSourceSynchronizedAt(
const QDateTime& sourceSynchronizedAt) {
VERIFY_OR_DEBUG_ASSERT(sourceSynchronizedAt.isValid()) {
// Cannot be reset after it has been set at least once.
// This is required to prevent unintended and repeated
// reimporting of metadata from file tags.
return false;
}
if (getSourceSynchronizedAt() == sourceSynchronizedAt) {
return false; // unchanged
}
if (getSourceSynchronizedAt().isValid() &&
getSourceSynchronizedAt() > sourceSynchronizedAt) {
kLogger.warning()
<< "Backdating source synchronization time from"
<< getSourceSynchronizedAt()
<< "to"
<< sourceSynchronizedAt;
}
setSourceSynchronizedAt(sourceSynchronizedAt);
m_headerParsed = sourceSynchronizedAt.isValid();
return true;
}
TrackRecord::SourceSyncStatus TrackRecord::checkSourceSyncStatus(
const FileInfo& fileInfo) const {
// This method cannot be used to update m_headerParsed after modifying
// m_sourceSynchronizedAt during a short moment of inconsistency.
// Otherwise the debug assertion triggers!
DEBUG_ASSERT(m_headerParsed ||
!getSourceSynchronizedAt().isValid());
// Legacy fallback: The property sourceSynchronizedAt has been added later.
// Files that have been added before that time and that have never been
// re-imported will only have that legacy flag set while sourceSynchronizedAt
// is still invalid.
if (!m_headerParsed) {
// Enforce initial import of metadata if it hasn't succeeded
// at least once yet.
return SourceSyncStatus::Void;
}
if (!getSourceSynchronizedAt().isValid()) {
// Existing tracks that have been imported before database version
// 37 don't have a synchronization time stamp.
return SourceSyncStatus::Unknown;
}
const QDateTime fileSourceSynchronizedAt =
MetadataSource::getFileSynchronizedAt(fileInfo.toQFile());
if (!fileSourceSynchronizedAt.isValid()) {
kLogger.warning()
<< "Failed to obtain synchronization time stamp for file"
<< mixxx::FileInfo(fileInfo)
<< ": Is this file missing or inaccessible?";
return SourceSyncStatus::Undefined;
}
if (getSourceSynchronizedAt() < fileSourceSynchronizedAt) {
return SourceSyncStatus::Outdated;
}
// This could actually happen when users replace files with
// an older version on the file system. But it should never
// happen under normal operation. Otherwise it might indicate
// a serious programming error and we should detect it early
// before the release. Debug assertions are only enabled in
// development and testing versions.
VERIFY_OR_DEBUG_ASSERT(getSourceSynchronizedAt() <= fileSourceSynchronizedAt) {
kLogger.warning()
<< "Internal source synchronization time stamp"
<< getSourceSynchronizedAt()
<< "is ahead of"
<< fileSourceSynchronizedAt
<< "for file"
<< mixxx::FileInfo(fileInfo)
<< ": Has this file been replaced with an older version?";
}
return SourceSyncStatus::Synchronized;
}
bool TrackRecord::replaceMetadataFromSource(
TrackMetadata&& importedMetadata,
const QDateTime& sourceSynchronizedAt) {
VERIFY_OR_DEBUG_ASSERT(sourceSynchronizedAt.isValid()) {
return false;
}
DEBUG_ASSERT(sourceSynchronizedAt.timeSpec() == Qt::UTC);
if (m_streamInfoFromSource) {
// Preserve precise stream info if available, i.e. discard the
// audio properties that are also stored as track metadata.
importedMetadata.updateStreamInfoFromSource(*m_streamInfoFromSource);
}
bool modified = false;
if (getMetadata() != importedMetadata) {
setMetadata(std::move(importedMetadata));
modified = true;
}
if (updateSourceSynchronizedAt(sourceSynchronizedAt)) {
modified = true;
}
return modified;
}
bool TrackRecord::mergeExtraMetadataFromSource(
const TrackMetadata& importedMetadata) {
bool modified = false;
TrackInfo* pMergedTrackInfo = m_metadata.ptrTrackInfo();
const TrackInfo& importedTrackInfo = importedMetadata.getTrackInfo();
if (pMergedTrackInfo->getTrackTotal() == kTrackTotalPlaceholder) {
pMergedTrackInfo->setTrackTotal(importedTrackInfo.getTrackTotal());
// Also set the track number if it is still empty due
// to insufficient parsing capabilities of Mixxx in
// previous versions.
if (pMergedTrackInfo->getTrackNumber().isEmpty() &&
!importedTrackInfo.getTrackNumber().isEmpty()) {
pMergedTrackInfo->setTrackNumber(importedTrackInfo.getTrackNumber());
modified = true;
}
}
#if defined(__EXTRA_METADATA__)
modified |= copyIfNotNull(
pMergedTrackInfo->ptrConductor(),
importedTrackInfo.getConductor());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrDiscNumber(),
importedTrackInfo.getDiscNumber());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrDiscTotal(),
importedTrackInfo.getDiscTotal());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrEncoder(),
importedTrackInfo.getEncoder());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrEncoderSettings(),
importedTrackInfo.getEncoderSettings());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrISRC(),
importedTrackInfo.getISRC());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrLanguage(),
importedTrackInfo.getLanguage());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrLyricist(),
importedTrackInfo.getLyricist());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrMood(),
importedTrackInfo.getMood());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrMovement(),
importedTrackInfo.getMovement());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrMusicBrainzArtistId(),
importedTrackInfo.getMusicBrainzArtistId());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrMusicBrainzRecordingId(),
importedTrackInfo.getMusicBrainzRecordingId());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrMusicBrainzReleaseId(),
importedTrackInfo.getMusicBrainzReleaseId());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrMusicBrainzWorkId(),
importedTrackInfo.getMusicBrainzWorkId());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrRemixer(),
importedTrackInfo.getRemixer());
modified |= copyIfNotEmpty(
pMergedTrackInfo->ptrSeratoTags(),
importedTrackInfo.getSeratoTags());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrSubtitle(),
importedTrackInfo.getSubtitle());
modified |= copyIfNotNull(
pMergedTrackInfo->ptrWork(),
importedTrackInfo.getWork());
AlbumInfo* pMergedAlbumInfo = refMetadata().ptrAlbumInfo();
const AlbumInfo& importedAlbumInfo = importedMetadata.getAlbumInfo();
modified |= mergeReplayGainMetadataProperty(
pMergedAlbumInfo->ptrReplayGain(),
importedAlbumInfo.getReplayGain());
modified |= copyIfNotNull(
pMergedAlbumInfo->ptrCopyright(),
importedAlbumInfo.getCopyright());
modified |= copyIfNotNull(
pMergedAlbumInfo->ptrLicense(),
importedAlbumInfo.getLicense());
modified |= copyIfNotNull(
pMergedAlbumInfo->ptrMusicBrainzArtistId(),
importedAlbumInfo.getMusicBrainzArtistId());
modified |= copyIfNotNull(
pMergedAlbumInfo->ptrMusicBrainzReleaseGroupId(),
importedAlbumInfo.getMusicBrainzReleaseGroupId());
modified |= copyIfNotNull(
pMergedAlbumInfo->ptrMusicBrainzReleaseId(),
importedAlbumInfo.getMusicBrainzReleaseId());
modified |= copyIfNotNull(
pMergedAlbumInfo->ptrRecordLabel(),
importedAlbumInfo.getRecordLabel());
#endif // __EXTRA_METADATA__
return modified;
}
bool TrackRecord::updateStreamInfoFromSource(
mixxx::audio::StreamInfo streamInfoFromSource) {
// Complete missing properties from metadata. Some properties
// are mandatory while others like the bitrate might not be
// reported by all decoders.
VERIFY_OR_DEBUG_ASSERT(streamInfoFromSource.getSignalInfo().getChannelCount().isValid()) {
streamInfoFromSource.refSignalInfo().setChannelCount(
getMetadata().getStreamInfo().getSignalInfo().getChannelCount());
}
VERIFY_OR_DEBUG_ASSERT(streamInfoFromSource.getSignalInfo().getSampleRate().isValid()) {
streamInfoFromSource.refSignalInfo().setSampleRate(
getMetadata().getStreamInfo().getSignalInfo().getSampleRate());
}
VERIFY_OR_DEBUG_ASSERT(streamInfoFromSource.getDuration() > Duration::empty()) {
streamInfoFromSource.setDuration(
getMetadata().getStreamInfo().getDuration());
}
if (!streamInfoFromSource.getBitrate().isValid()) {
// The bitrate might not be reported by the SoundSource
streamInfoFromSource.setBitrate(
getMetadata().getStreamInfo().getBitrate());
}
// Stream properties are not expected to vary during a session
VERIFY_OR_DEBUG_ASSERT(!m_streamInfoFromSource ||
*m_streamInfoFromSource == streamInfoFromSource) {
kLogger.warning()
<< "Varying stream properties:"
<< *m_streamInfoFromSource
<< "->"
<< streamInfoFromSource;
}
m_streamInfoFromSource = streamInfoFromSource;
// Stream info from source is always propagated to metadata and
// unconditionally overwrites any properties that have once been
// parsed from file tags! This is required to store the most
// accurate information about the audio stream in the database.
const bool metadataUpdated = refMetadata().updateStreamInfoFromSource(streamInfoFromSource);
DEBUG_ASSERT(getMetadata().getStreamInfo() == streamInfoFromSource);
return metadataUpdated;
}
bool operator==(const TrackRecord& lhs, const TrackRecord& rhs) {
return lhs.getMetadata() == rhs.getMetadata() &&
lhs.getCoverInfo() == rhs.getCoverInfo() &&
lhs.getId() == rhs.getId() &&
lhs.getSourceSynchronizedAt() == rhs.getSourceSynchronizedAt() &&
lhs.getDateAdded() == rhs.getDateAdded() &&
lhs.getFileType() == rhs.getFileType() &&
lhs.getUrl() == rhs.getUrl() &&
lhs.getPlayCounter() == rhs.getPlayCounter() &&
lhs.getColor() == rhs.getColor() &&
lhs.getMainCuePosition() == rhs.getMainCuePosition() &&
lhs.getBpmLocked() == rhs.getBpmLocked() &&
lhs.getKeys() == rhs.getKeys() &&
lhs.getRating() == rhs.getRating() &&
lhs.m_headerParsed == rhs.m_headerParsed;
}
} // namespace mixxx