forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrack.cpp
More file actions
2004 lines (1785 loc) · 66.9 KB
/
Copy pathtrack.cpp
File metadata and controls
2004 lines (1785 loc) · 66.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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "track/track.h"
#include <QDebug>
#include <atomic>
#include "library/library_prefs.h"
#include "moc_track.cpp"
#include "sources/metadatasource.h"
#include "track/keyfactory.h"
#include "util/assert.h"
#include "util/logger.h"
#include "util/time.h"
namespace {
const mixxx::Logger kLogger("Track");
constexpr bool kLogStats = false;
// Count the number of currently existing instances for detecting
// memory leaks.
std::atomic<int> s_numberOfInstances;
template<typename T>
inline bool compareAndSet(gsl::not_null<T*> pField, const T& value) {
if (*pField != value) {
// Copy the value into its final location
*pField = value;
return true;
} else {
// Ignore the value if unmodified
return false;
}
}
// Overload with a forwarding reference argument for efficiently
// passing large, movable values.
template<typename T>
inline bool compareAndSet(gsl::not_null<T*> pField, T&& value) {
if (*pField != value) {
// Forward the value into its final location
*pField = std::forward<T>(value);
return true;
} else {
// Ignore the value if unmodified
return false;
}
}
inline mixxx::Bpm getBeatsPointerBpm(
const mixxx::BeatsPointer& pBeats, double durationSecs) {
if (!pBeats) {
return mixxx::Bpm{};
}
const auto trackEndPosition = mixxx::audio::FramePos{durationSecs * pBeats->getSampleRate()};
return pBeats->getBpmInRange(mixxx::audio::kStartFramePos, trackEndPosition);
}
constexpr int kMaxBeatsUndoStack = 10;
// The minimum time that has to pass between beat changes to consider them 'separate'.
// Used to filter actions done in quick succession.
constexpr int kQuickBeatChangeDeltaMillis = 800;
} // anonymous namespace
// Don't change this string without an entry in the CHANGELOG!
// Otherwise 3rd party software that picks up the currently
// playing track from the main window and relies on this
// formatting would stop working.
//static
const QString Track::kArtistTitleSeparator = QStringLiteral(" - ");
//static
SyncTrackMetadataParams SyncTrackMetadataParams::readFromUserSettings(
const UserSettings& userSettings) {
return SyncTrackMetadataParams{
.resetMissingTagMetadataOnImport =
userSettings.getValue<bool>(
mixxx::library::prefs::kResetMissingTagMetadataOnImportConfigKey),
.syncSeratoMetadata = userSettings.getValue<bool>(
mixxx::library::prefs::kSyncSeratoMetadataConfigKey),
};
}
Track::Track(
mixxx::FileAccess fileAccess,
TrackId trackId)
: m_qMutex(QT_RECURSIVE_MUTEX_INIT),
m_fileAccess(std::move(fileAccess)),
m_record(trackId),
m_bDirty(false),
m_bMarkedForMetadataExport(false),
m_undoingBeatsChange(false) {
if (kLogStats && kLogger.debugEnabled()) {
long numberOfInstancesBefore = s_numberOfInstances.fetch_add(1);
kLogger.debug()
<< "Creating instance:"
<< this
<< numberOfInstancesBefore
<< "->"
<< numberOfInstancesBefore + 1;
}
m_beatChangeTimer.start();
}
Track::~Track() {
if (m_pBeatsImporterPending && !m_pBeatsImporterPending->isEmpty()) {
kLogger.warning()
<< "Import of beats is still pending and discarded";
}
if (m_pCueInfoImporterPending && !m_pCueInfoImporterPending->isEmpty()) {
kLogger.warning()
<< "Import of"
<< m_pCueInfoImporterPending->size()
<< "cue(s) is still pending and discarded";
}
if (kLogStats && kLogger.debugEnabled()) {
long numberOfInstancesBefore = s_numberOfInstances.fetch_sub(1);
kLogger.debug()
<< "Destroying instance:"
<< this
<< numberOfInstancesBefore
<< "->"
<< numberOfInstancesBefore - 1;
}
}
//static
TrackPointer Track::newTemporary(
mixxx::FileAccess fileAccess) {
return std::make_shared<Track>(
std::move(fileAccess));
}
//static
TrackPointer Track::newDummy(
const QString& filePath,
TrackId trackId) {
return std::make_shared<Track>(
mixxx::FileAccess(mixxx::FileInfo(filePath)),
trackId);
}
void Track::relocate(
mixxx::FileAccess fileAccess) {
const auto locked = lockMutex(&m_qMutex);
m_fileAccess = std::move(fileAccess);
// The track does not need to be marked as dirty,
// because this function will always be called with
// the updated location from the database.
}
void Track::replaceMetadataFromSource(
mixxx::TrackMetadata importedMetadata,
const QDateTime& sourceSynchronizedAt) {
// Information stored in Serato tags is imported separately after
// importing the metadata (see below). The Serato tags BLOB itself
// is updated together with the metadata.
auto pSeratoBeatsImporter = importedMetadata.getTrackInfo().getSeratoTags().importBeats();
const bool seratoBpmLocked = importedMetadata.getTrackInfo().getSeratoTags().isBpmLocked();
std::unique_ptr<mixxx::CueInfoImporter> pSeratoCuesImporter =
importedMetadata.getTrackInfo()
.getSeratoTags()
.createCueInfoImporter();
{
// Save some new values for later
const auto importedBpm = importedMetadata.getTrackInfo().getBpm();
const QString importedKeyText = importedMetadata.getTrackInfo().getKeyText();
// enter locking scope
auto locked = lockMutex(&m_qMutex);
// Preserve current bpm and key temporarily to avoid
// overwriting with an inconsistent value. The bpm must always be
// set together with the beat grid and the key text must be parsed
// and validated.
importedMetadata.refTrackInfo().setBpm(getBpmWhileLocked());
importedMetadata.refTrackInfo().setKeyText(
m_record.getMetadata().getTrackInfo().getKeyText());
const auto oldReplayGain =
m_record.getMetadata().getTrackInfo().getReplayGain();
bool modified = m_record.replaceMetadataFromSource(
std::move(importedMetadata),
sourceSynchronizedAt);
const auto newReplayGain =
m_record.getMetadata().getTrackInfo().getReplayGain();
// Need to set BPM after sample rate since beat grid creation depends on
// knowing the sample rate #6559.
auto beatsAndBpmModified = false;
if (importedBpm.isValid() &&
(!m_pBeats ||
!getBeatsPointerBpm(m_pBeats, getDuration())
.isValid())) {
// Only use the imported BPM if the current beat grid is either
// missing or not valid! The BPM value in the metadata might be
// imprecise (normalized or rounded), e.g. ID3v2 only supports
// integer values.
beatsAndBpmModified = trySetBpmWhileLocked(importedBpm);
}
modified |= beatsAndBpmModified;
auto keysModified = false;
const Keys newKeys = KeyFactory::makeBasicKeysKeepText(
importedKeyText, mixxx::track::io::key::FILE_METADATA);
if (newKeys.getGlobalKey() != mixxx::track::io::key::INVALID &&
m_record.getMetadata().getTrackInfo().getKeyText() != importedKeyText) {
// Only update the current key with a valid value. Otherwise preserve
// the existing value.
setKeys(newKeys);
keysModified = true;
}
modified |= keysModified;
// Import track color from Serato tags if available
const std::optional<mixxx::RgbColor::optional_t> newColor =
m_record.getMetadata()
.getTrackInfo()
.getSeratoTags()
.getTrackColor();
const bool colorModified = newColor && compareAndSet(m_record.ptrColor(), *newColor);
modified |= colorModified;
DEBUG_ASSERT(!colorModified || m_record.getColor() == *newColor);
if (!modified) {
// Unmodified, nothing todo
return;
}
// Explicitly unlock before emitting signals
markDirtyAndUnlock(&locked);
if (beatsAndBpmModified) {
emitBeatsAndBpmUpdated();
}
if (keysModified) {
emit keyChanged();
}
if (oldReplayGain != newReplayGain) {
emit replayGainUpdated(newReplayGain);
}
if (colorModified) {
DEBUG_ASSERT(newColor);
emit colorUpdated(*newColor);
}
emitChangedSignalsForAllMetadata();
}
// TODO: Import Serato metadata within the locking scope and not
// as a post-processing step.
if (pSeratoBeatsImporter) {
kLogger.debug() << "Importing Serato beats";
tryImportBeats(std::move(pSeratoBeatsImporter), seratoBpmLocked);
}
if (pSeratoCuesImporter) {
kLogger.debug() << "Importing Serato cues";
importCueInfos(std::move(pSeratoCuesImporter));
}
}
bool Track::mergeExtraMetadataFromSource(
const mixxx::TrackMetadata& importedMetadata) {
auto locked = lockMutex(&m_qMutex);
if (!m_record.mergeExtraMetadataFromSource(importedMetadata)) {
// Not modified
return false;
}
markDirtyAndUnlock(&locked);
// Modified
emitChangedSignalsForAllMetadata();
return true;
}
mixxx::TrackMetadata Track::getMetadata(
mixxx::TrackRecord::SourceSyncStatus* pSourceSyncStatus) const {
const auto locked = lockMutex(&m_qMutex);
if (pSourceSyncStatus) {
*pSourceSyncStatus =
m_record.checkSourceSyncStatus(m_fileAccess.info());
}
return m_record.getMetadata();
}
mixxx::TrackRecord Track::getRecord(
bool* pDirty) const {
const auto locked = lockMutex(&m_qMutex);
if (pDirty) {
*pDirty = m_bDirty;
}
return m_record;
}
bool Track::replaceRecord(
mixxx::TrackRecord newRecord,
mixxx::BeatsPointer pOptionalBeats) {
const auto newReplayGain = newRecord.getMetadata().getTrackInfo().getReplayGain();
const auto newColor = newRecord.getColor();
const auto newRating = newRecord.getRating();
auto locked = lockMutex(&m_qMutex);
const bool recordUnchanged = m_record == newRecord;
if (recordUnchanged && !pOptionalBeats) {
return false;
}
const auto oldReplayGain = m_record.getMetadata().getTrackInfo().getReplayGain();
const auto oldColor = m_record.getColor();
const auto oldRating = m_record.getRating();
bool bpmUpdatedFlag;
if (pOptionalBeats) {
bpmUpdatedFlag = trySetBeatsWhileLocked(pOptionalBeats);
if (recordUnchanged && !bpmUpdatedFlag) {
return false;
}
} else {
// Setting the bpm manually may in turn update the beat grid
bpmUpdatedFlag = trySetBpmWhileLocked(
newRecord.getMetadata().getTrackInfo().getBpm());
}
// The bpm in m_record has already been updated. Read it and copy it into
// the new record to ensure it will be consistent with the new beat grid.
const auto newBpm = m_record.getMetadata().getTrackInfo().getBpm();
newRecord.refMetadata().refTrackInfo().setBpm(newBpm);
// Finally replace the current with the new record
m_record = std::move(newRecord);
// Unlock before emitting signals
markDirtyAndUnlock(&locked);
if (bpmUpdatedFlag) {
emit beatsUpdated();
}
if (oldReplayGain != newReplayGain) {
emit replayGainUpdated(newReplayGain);
}
if (oldColor != newColor) {
emit colorUpdated(newColor);
}
if (oldRating != newRating) {
emit ratingUpdated(newRating);
}
emitChangedSignalsForAllMetadata();
return true;
}
mixxx::ReplayGain Track::getReplayGain() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getReplayGain();
}
void Track::setReplayGain(const mixxx::ReplayGain& replayGain) {
auto locked = lockMutex(&m_qMutex);
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrReplayGain(), replayGain)) {
markDirtyAndUnlock(&locked);
emit replayGainUpdated(replayGain);
}
}
void Track::adjustReplayGainFromPregain(double gain, const QString& requestingPlayerGroup) {
auto locked = lockMutex(&m_qMutex);
mixxx::ReplayGain replayGain = m_record.getMetadata().getTrackInfo().getReplayGain();
replayGain.setRatio(gain * replayGain.getRatio());
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrReplayGain(), replayGain)) {
markDirtyAndUnlock(&locked);
emit replayGainAdjusted(replayGain, requestingPlayerGroup);
}
}
mixxx::Bpm Track::getBpmWhileLocked() const {
// BPM values must be synchronized at all times!
DEBUG_ASSERT(m_record.getMetadata().getTrackInfo().getBpm() ==
getBeatsPointerBpm(m_pBeats, getDuration()));
return m_record.getMetadata().getTrackInfo().getBpm();
}
bool Track::trySetBpmWhileLocked(mixxx::Bpm bpm) {
if (!bpm.isValid()) {
// If the user sets the BPM to an invalid value, we assume
// they want to clear the beatgrid.
return trySetBeatsWhileLocked(nullptr);
} else if (!m_pBeats) {
// No beat grid available -> create and initialize
mixxx::audio::FramePos cuePosition = m_record.getMainCuePosition();
if (!cuePosition.isValid()) {
cuePosition = mixxx::audio::kStartFramePos;
}
auto pBeats = mixxx::Beats::fromConstTempo(getSampleRate(),
cuePosition,
bpm);
return trySetBeatsWhileLocked(pBeats);
} else if (getBeatsPointerBpm(m_pBeats, getDuration()) != bpm) {
// Continue with the regular cases
const auto newBeats = m_pBeats->trySetBpm(bpm);
if (newBeats) {
if (kLogger.debugEnabled()) {
kLogger.debug() << "Updating BPM:" << getLocation();
}
return trySetBeatsWhileLocked(*newBeats);
}
}
return false;
}
double Track::getBpm() const {
const auto locked = lockMutex(&m_qMutex);
const mixxx::Bpm bpm = getBpmWhileLocked();
return bpm.isValid() ? bpm.value() : mixxx::Bpm::kValueUndefined;
}
bool Track::trySetBpm(mixxx::Bpm bpm) {
auto locked = lockMutex(&m_qMutex);
if (!trySetBpmWhileLocked(bpm)) {
return false;
}
afterBeatsAndBpmUpdated(&locked);
return true;
}
bool Track::trySetBeats(mixxx::BeatsPointer pBeats) {
auto locked = lockMutex(&m_qMutex);
return trySetBeatsMarkDirtyAndUnlock(&locked, pBeats, false);
}
bool Track::trySetAndLockBeats(mixxx::BeatsPointer pBeats) {
auto locked = lockMutex(&m_qMutex);
return trySetBeatsMarkDirtyAndUnlock(&locked, pBeats, true);
}
bool Track::setBeatsWhileLocked(mixxx::BeatsPointer pBeats) {
if (m_pBeats == pBeats) {
return false;
}
// Don't add null beats to the undo stack. Happens when beats are deserialized,
// e.g. when opening the track menu.
// Don't add beats to stack which we're about to undo.
if (!m_undoingBeatsChange && m_pBeats != nullptr) {
if (m_pBeatsUndoStack.size() >= kMaxBeatsUndoStack) {
m_pBeatsUndoStack.removeFirst();
}
// If changes done in quick succession, e.g. quick beats_translate_later,
// we only store the beats from before the first quick action.
mixxx::Duration elapsed = m_beatChangeTimer.restart();
if (elapsed > mixxx::Duration::fromMillis(kQuickBeatChangeDeltaMillis)) {
m_pBeatsUndoStack.push(m_pBeats);
}
}
m_pBeats = std::move(pBeats);
m_record.refMetadata().refTrackInfo().setBpm(getBeatsPointerBpm(m_pBeats, getDuration()));
return true;
}
bool Track::trySetBeatsWhileLocked(
mixxx::BeatsPointer pBeats,
bool lockBpmAfterSet) {
if (m_pBeats && m_record.getBpmLocked()) {
// Track has already a valid and locked beats object, abort.
qDebug() << "Track beats is already set and BPM-locked. Discard the new beats";
return false;
}
bool dirty = false;
if (setBeatsWhileLocked(pBeats)) {
dirty = true;
}
if (compareAndSet(m_record.ptrBpmLocked(), lockBpmAfterSet)) {
dirty = true;
}
return dirty;
}
bool Track::trySetBeatsMarkDirtyAndUnlock(
QT_RECURSIVE_MUTEX_LOCKER* pLock,
mixxx::BeatsPointer pBeats,
bool lockBpmAfterSet) {
DEBUG_ASSERT(pLock);
if (!trySetBeatsWhileLocked(pBeats, lockBpmAfterSet)) {
return false;
}
afterBeatsAndBpmUpdated(pLock);
return true;
}
mixxx::BeatsPointer Track::getBeats() const {
const auto locked = lockMutex(&m_qMutex);
return m_pBeats;
}
void Track::undoBeatsChange() {
if (!canUndoBeatsChange()) {
return;
}
auto locked = lockMutex(&m_qMutex);
m_undoingBeatsChange = true;
const auto pPrevBeats = m_pBeatsUndoStack.pop();
trySetBeats(pPrevBeats);
m_undoingBeatsChange = false;
}
void Track::afterBeatsAndBpmUpdated(
QT_RECURSIVE_MUTEX_LOCKER* pLock) {
DEBUG_ASSERT(pLock);
markDirtyAndUnlock(pLock);
emitBeatsAndBpmUpdated();
}
void Track::emitBeatsAndBpmUpdated() {
emit bpmChanged();
emit beatsUpdated();
}
void Track::emitChangedSignalsForAllMetadata() {
emit artistChanged(getArtist());
emit titleChanged(getTitle());
emit albumChanged(getAlbum());
emit albumArtistChanged(getAlbumArtist());
emit genreChanged(getGenre());
emit composerChanged(getComposer());
emit groupingChanged(getGrouping());
emit yearChanged(getYear());
emit trackNumberChanged(getTrackNumber());
emit trackTotalChanged(getTrackTotal());
emit commentChanged(getComment());
emit bpmChanged();
emit timesPlayedChanged();
emit durationChanged();
emit infoChanged();
emit keyChanged();
}
bool Track::checkSourceSynchronized() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.checkSourceSyncStatus(m_fileAccess.info()) ==
mixxx::TrackRecord::SourceSyncStatus::Synchronized;
}
void Track::setSourceSynchronizedAt(const QDateTime& sourceSynchronizedAt) {
DEBUG_ASSERT(!sourceSynchronizedAt.isValid() ||
sourceSynchronizedAt.timeSpec() == Qt::UTC);
auto locked = lockMutex(&m_qMutex);
if (compareAndSet(m_record.ptrSourceSynchronizedAt(), sourceSynchronizedAt)) {
markDirtyAndUnlock(&locked);
}
}
QDateTime Track::getSourceSynchronizedAt() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getSourceSynchronizedAt();
}
QString Track::getInfo() const {
const auto locked = lockMutex(&m_qMutex);
if (m_record.getMetadata().getTrackInfo().getArtist().trimmed().isEmpty()) {
if (m_record.getMetadata().getTrackInfo().getTitle().trimmed().isEmpty()) {
return m_fileAccess.info().fileName();
} else {
return m_record.getMetadata().getTrackInfo().getTitle();
}
} else {
return m_record.getMetadata().getTrackInfo().getArtist() +
kArtistTitleSeparator +
m_record.getMetadata().getTrackInfo().getTitle();
}
}
QString Track::getTitleInfo() const {
const auto locked = lockMutex(&m_qMutex);
if (m_record.getMetadata().getTrackInfo().getArtist().trimmed().isEmpty() &&
m_record.getMetadata().getTrackInfo().getTitle().trimmed().isEmpty()) {
return m_fileAccess.info().fileName();
} else {
return m_record.getMetadata().getTrackInfo().getTitle();
}
}
QDateTime Track::getDateAdded() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getDateAdded();
}
void Track::setDateAdded(const QDateTime& dateAdded) {
auto locked = lockMutex(&m_qMutex);
m_record.setDateAdded(dateAdded);
}
void Track::setDuration(mixxx::Duration duration) {
auto locked = lockMutex(&m_qMutex);
// TODO: Move checks into TrackRecord
VERIFY_OR_DEBUG_ASSERT(!m_record.getStreamInfoFromSource() ||
m_record.getStreamInfoFromSource()->getDuration() <= mixxx::Duration::empty() ||
m_record.getStreamInfoFromSource()->getDuration() == duration) {
kLogger.warning()
<< "Cannot override stream duration:"
<< m_record.getStreamInfoFromSource()->getDuration()
<< "->"
<< duration;
return;
}
if (compareAndSet(
m_record.refMetadata().refStreamInfo().ptrDuration(),
duration)) {
markDirtyAndUnlock(&locked);
emit durationChanged();
}
}
void Track::setDuration(double duration) {
setDuration(mixxx::Duration::fromSeconds(duration));
}
double Track::getDuration() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getStreamInfo().getDuration().toDoubleSeconds();
}
int Track::getDurationSecondsInt() const {
const auto locked = lockMutex(&m_qMutex);
return static_cast<int>(m_record.getMetadata().getDurationSecondsRounded());
}
QString Track::getDurationText(
mixxx::Duration::Precision precision) const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getDurationText(precision);
}
QString Track::getTitle() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getTitle();
}
void Track::setTitle(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrTitle(), value)) {
markDirtyAndUnlock(&locked);
emit titleChanged(value);
emit infoChanged();
}
}
QString Track::getArtist() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getArtist();
}
void Track::setArtist(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrArtist(), value)) {
markDirtyAndUnlock(&locked);
emit artistChanged(value);
emit infoChanged();
}
}
QString Track::getAlbum() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getAlbumInfo().getTitle();
}
void Track::setAlbum(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refAlbumInfo().ptrTitle(), value)) {
markDirtyAndUnlock(&locked);
emit albumChanged(value);
}
}
QString Track::getAlbumArtist() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getAlbumInfo().getArtist();
}
void Track::setAlbumArtist(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refAlbumInfo().ptrArtist(), value)) {
markDirtyAndUnlock(&locked);
emit albumArtistChanged(value);
}
}
QString Track::getYear() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getYear();
}
void Track::setYear(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrYear(), value)) {
markDirtyAndUnlock(&locked);
emit yearChanged(value);
}
}
QString Track::getComposer() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getComposer();
}
void Track::setComposer(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrComposer(), value)) {
markDirtyAndUnlock(&locked);
emit composerChanged(value);
}
}
QString Track::getGrouping() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getGrouping();
}
void Track::setGrouping(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrGrouping(), value)) {
markDirtyAndUnlock(&locked);
emit groupingChanged(value);
}
}
QString Track::getTrackNumber() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getTrackNumber();
}
QString Track::getTrackTotal() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getTrackTotal();
}
void Track::setTrackNumber(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrTrackNumber(), value)) {
markDirtyAndUnlock(&locked);
emit trackNumberChanged(value);
}
}
void Track::setTrackTotal(const QString& s) {
auto locked = lockMutex(&m_qMutex);
const QString value = s.trimmed();
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrTrackTotal(), value)) {
markDirtyAndUnlock(&locked);
emit trackTotalChanged(value);
}
}
PlayCounter Track::getPlayCounter() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getPlayCounter();
}
void Track::setPlayCounter(const PlayCounter& playCounter) {
auto locked = lockMutex(&m_qMutex);
if (compareAndSet(m_record.ptrPlayCounter(), playCounter)) {
markDirtyAndUnlock(&locked);
emit timesPlayedChanged();
}
}
void Track::updatePlayCounter(bool bPlayed) {
auto locked = lockMutex(&m_qMutex);
PlayCounter playCounter(m_record.getPlayCounter());
playCounter.updateLastPlayedNowAndTimesPlayed(bPlayed);
if (compareAndSet(m_record.ptrPlayCounter(), playCounter)) {
markDirtyAndUnlock(&locked);
emit timesPlayedChanged();
}
}
void Track::updatePlayedStatusKeepPlayCount(bool bPlayed) {
auto locked = lockMutex(&m_qMutex);
PlayCounter playCounter(m_record.getPlayCounter());
playCounter.setPlayedFlag(bPlayed);
if (compareAndSet(m_record.ptrPlayCounter(), playCounter)) {
markDirtyAndUnlock(&locked);
emit timesPlayedChanged();
}
}
mixxx::RgbColor::optional_t Track::getColor() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getColor();
}
void Track::setColor(const mixxx::RgbColor::optional_t& color) {
auto locked = lockMutex(&m_qMutex);
if (compareAndSet(m_record.ptrColor(), color)) {
markDirtyAndUnlock(&locked);
emit colorUpdated(color);
}
}
QString Track::getComment() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getTrackInfo().getComment();
}
void Track::setComment(const QString& s) {
auto locked = lockMutex(&m_qMutex);
if (compareAndSet(m_record.refMetadata().refTrackInfo().ptrComment(), s)) {
markDirtyAndUnlock(&locked);
emit commentChanged(s);
}
}
QString Track::getType() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getFileType();
}
QString Track::setType(const QString& newType) {
auto locked = lockMutex(&m_qMutex);
const QString oldType = m_record.getFileType();
if (compareAndSet(m_record.ptrFileType(), newType)) {
markDirtyAndUnlock(&locked);
}
return oldType;
}
mixxx::audio::SampleRate Track::getSampleRate() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getStreamInfo().getSignalInfo().getSampleRate();
}
mixxx::audio::ChannelCount Track::getChannels() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getStreamInfo().getSignalInfo().getChannelCount();
}
int Track::getBitrate() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getStreamInfo().getBitrate();
}
QString Track::getBitrateText() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getMetadata().getBitrateText();
}
void Track::setBitrate(int iBitrate) {
auto locked = lockMutex(&m_qMutex);
const mixxx::audio::Bitrate bitrate(iBitrate);
// TODO: Move checks into TrackRecord
VERIFY_OR_DEBUG_ASSERT(!m_record.getStreamInfoFromSource() ||
!m_record.getStreamInfoFromSource()->getBitrate().isValid() ||
m_record.getStreamInfoFromSource()->getBitrate() == bitrate) {
kLogger.warning()
<< "Cannot override stream bitrate:"
<< m_record.getStreamInfoFromSource()->getBitrate()
<< "->"
<< bitrate;
return;
}
if (compareAndSet(
m_record.refMetadata().refStreamInfo().ptrBitrate(),
bitrate)) {
markDirtyAndUnlock(&locked);
}
}
TrackId Track::getId() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getId();
}
void Track::initId(TrackId id) {
const auto locked = lockMutex(&m_qMutex);
DEBUG_ASSERT(id.isValid());
if (m_record.getId() == id) {
return;
}
// The track's id must be set only once and immediately after
// the object has been created.
VERIFY_OR_DEBUG_ASSERT(!m_record.getId().isValid()) {
kLogger.warning() << "Cannot change id from"
<< m_record.getId() << "to" << id;
return; // abort
}
m_record.setId(id);
// Changing the Id does not make the track dirty because the Id is always
// generated by the database itself.
}
void Track::resetId() {
const auto locked = lockMutex(&m_qMutex);
m_record.setId(TrackId());
}
void Track::setURL(const QString& url) {
auto locked = lockMutex(&m_qMutex);
if (compareAndSet(m_record.ptrUrl(), url)) {
markDirtyAndUnlock(&locked);
}
}
QString Track::getURL() const {
const auto locked = lockMutex(&m_qMutex);
return m_record.getUrl();
}
const ConstWaveformPointer& Track::getWaveform() const {
return m_waveform;
}
void Track::setWaveform(ConstWaveformPointer pWaveform) {
m_waveform = pWaveform;
emit waveformUpdated();
}
ConstWaveformPointer Track::getWaveformSummary() const {
return m_waveformSummary;
}
void Track::setWaveformSummary(ConstWaveformPointer pWaveform) {
m_waveformSummary = pWaveform;
emit waveformSummaryUpdated();
}
void Track::setMainCuePosition(mixxx::audio::FramePos position) {
auto locked = lockMutex(&m_qMutex);
if (!compareAndSet(m_record.ptrMainCuePosition(), position)) {
// Nothing changed.
return;
}
// Store the cue point as main cue
CuePointer pLoadCue = findCueByType(mixxx::CueType::MainCue);
if (position.isValid()) {
if (pLoadCue) {
pLoadCue->setStartPosition(position);
} else {
pLoadCue = CuePointer(new Cue(
mixxx::CueType::MainCue,
Cue::kNoHotCue,
position,
mixxx::audio::kInvalidFramePos,
mixxx::PredefinedColorPalettes::kDefaultCueColor));
// While this method could be called from any thread,
// associated Cue objects should always live on the
// same thread as their host, namely this->thread().
pLoadCue->moveToThread(thread());
connect(pLoadCue.get(),
&Cue::updated,
this,
&Track::slotCueUpdated);
m_cuePoints.push_back(pLoadCue);
}
} else if (pLoadCue) {
disconnect(pLoadCue.get(), nullptr, this, nullptr);
m_cuePoints.removeOne(pLoadCue);
}
markDirtyAndUnlock(&locked);
emit cuesUpdated();
}
void Track::shiftCuePositionsMillis(double milliseconds) {
auto locked = lockMutex(&m_qMutex);
VERIFY_OR_DEBUG_ASSERT(m_record.getStreamInfoFromSource()) {
return;
}
double frames = m_record.getStreamInfoFromSource()->getSignalInfo().millis2frames(milliseconds);
for (const CuePointer& pCue : std::as_const(m_cuePoints)) {
pCue->shiftPositionFrames(frames);
}
markDirtyAndUnlock(&locked);
}
void Track::setHotcueIndicesSortedByPosition(HotcueSortMode sortMode) {
auto locked = lockMutex(&m_qMutex);
// Populate lists of positions and indices
QList<int> indices;
QList<mixxx::audio::FramePos> positions;
indices.reserve(m_cuePoints.size());
positions.reserve(m_cuePoints.size());
for (const CuePointer& pCue : std::as_const(m_cuePoints)) {