-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbasetrackplayer.cpp
1058 lines (931 loc) · 37.7 KB
/
basetrackplayer.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
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 "mixer/basetrackplayer.h"
#include <QMessageBox>
#include <QMetaMethod>
#include <memory>
#include "control/controlencoder.h"
#include "control/controlobject.h"
#include "engine/channels/enginedeck.h"
#include "engine/controls/enginecontrol.h"
#include "engine/engine.h"
#include "engine/enginebuffer.h"
#include "engine/enginemixer.h"
#include "engine/sync/enginesync.h"
#include "mixer/playerinfo.h"
#include "mixer/playermanager.h"
#include "moc_basetrackplayer.cpp"
#include "track/track.h"
#include "util/sandbox.h"
#include "vinylcontrol/defs_vinylcontrol.h"
#include "waveform/renderers/waveformwidgetrenderer.h"
namespace {
constexpr double kNoTrackColor = -1;
constexpr double kShiftCuesOffsetMillis = 10;
constexpr double kShiftCuesOffsetSmallMillis = 1;
const QString kEffectGroupFormat = QStringLiteral("[EqualizerRack1_%1_Effect1]");
inline double trackColorToDouble(mixxx::RgbColor::optional_t color) {
return (color ? static_cast<double>(*color) : kNoTrackColor);
}
} // namespace
BaseTrackPlayer::BaseTrackPlayer(PlayerManager* pParent, const QString& group)
: BasePlayer(pParent, group) {
}
BaseTrackPlayerImpl::BaseTrackPlayerImpl(
PlayerManager* pParent,
UserSettingsPointer pConfig,
EngineMixer* pMixingEngine,
EffectsManager* pEffectsManager,
EngineChannel::ChannelOrientation defaultOrientation,
const ChannelHandleAndGroup& handleGroup,
bool defaultMainMix,
bool defaultHeadphones,
bool primaryDeck)
: BaseTrackPlayer(pParent, handleGroup.name()),
m_pConfig(pConfig),
m_pEngineMixer(pMixingEngine),
m_pLoadedTrack(),
m_pPrevFailedTrackId(),
m_replaygainPending(false),
m_pChannelToCloneFrom(nullptr) {
auto channel = std::make_unique<EngineDeck>(handleGroup,
pConfig,
pMixingEngine,
pEffectsManager,
defaultOrientation,
primaryDeck);
m_pChannel = channel.get();
m_pInputConfigured = make_parented<ControlProxy>(getGroup(), "input_configured", this);
#ifdef __VINYLCONTROL__
m_pVinylControlEnabled = make_parented<ControlProxy>(getGroup(), "vinylcontrol_enabled", this);
m_pVinylControlEnabled->connectValueChanged(this, &BaseTrackPlayerImpl::slotVinylControlEnabled);
m_pVinylControlStatus = make_parented<ControlProxy>(getGroup(), "vinylcontrol_status", this);
#endif
EngineBuffer* pEngineBuffer = m_pChannel->getEngineBuffer();
pMixingEngine->addChannel(std::move(channel));
// Set the routing option defaults for the main and headphone mixes.
m_pChannel->setMainMix(defaultMainMix);
m_pChannel->setPfl(defaultHeadphones);
// Connect our signals and slots with the EngineBuffer's signals and
// slots. This will let us know when the reader is done loading a track, and
// let us request that the reader load a track.
connect(pEngineBuffer, &EngineBuffer::trackLoaded, this, &BaseTrackPlayerImpl::slotTrackLoaded);
connect(pEngineBuffer,
&EngineBuffer::trackLoadFailed,
this,
&BaseTrackPlayerImpl::slotLoadFailed);
m_pEject = std::make_unique<ControlPushButton>(ConfigKey(getGroup(), "eject"));
connect(m_pEject.get(),
&ControlObject::valueChanged,
this,
&BaseTrackPlayerImpl::slotEjectTrack);
// Get loop point control objects
m_pLoopInPoint = make_parented<ControlProxy>(
getGroup(), "loop_start_position", this);
m_pLoopOutPoint = make_parented<ControlProxy>(
getGroup(), "loop_end_position", this);
// Duration of the current song, we create this one because nothing else does.
m_pDuration = std::make_unique<ControlObject>(
ConfigKey(getGroup(), "duration"));
// Track color of the current track
m_pTrackColor = std::make_unique<ControlObject>(
ConfigKey(getGroup(), "track_color"));
m_pTrackColor->set(kNoTrackColor);
m_pTrackColor->connectValueChangeRequest(
this, &BaseTrackPlayerImpl::slotTrackColorChangeRequest);
m_pTrackColorPrev = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "track_color_prev"));
connect(m_pTrackColorPrev.get(),
&ControlPushButton::valueChanged,
this,
[this](double value) {
if (value > 0) {
BaseTrackPlayerImpl::slotTrackColorSelector(-1);
}
});
m_pTrackColorNext = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "track_color_next"));
connect(m_pTrackColorNext.get(),
&ControlPushButton::valueChanged,
this,
[this](double value) {
if (value > 0) {
BaseTrackPlayerImpl::slotTrackColorSelector(1);
}
});
m_pTrackColorSelect = std::make_unique<ControlEncoder>(
ConfigKey(getGroup(), "track_color_selector"), false);
connect(m_pTrackColorSelect.get(),
&ControlEncoder::valueChanged,
this,
[this](double steps) {
int iSteps = static_cast<int>(steps);
BaseTrackPlayerImpl::slotTrackColorSelector(iSteps);
});
m_pStarsUp = std::make_unique<ControlPushButton>(ConfigKey(getGroup(), "stars_up"));
connect(m_pStarsUp.get(),
&ControlObject::valueChanged,
this,
[this](double value) {
if (value > 0) {
slotTrackRatingChangeRequestRelative(1);
}
});
m_pStarsDown = std::make_unique<ControlPushButton>(ConfigKey(getGroup(), "stars_down"));
connect(m_pStarsDown.get(),
&ControlObject::valueChanged,
this,
[this](double value) {
if (value > 0) {
slotTrackRatingChangeRequestRelative(-1);
}
});
// Deck cloning
m_pCloneFromDeck = std::make_unique<ControlObject>(
ConfigKey(getGroup(), "CloneFromDeck"),
false);
connect(m_pCloneFromDeck.get(),
&ControlObject::valueChanged,
this,
&BaseTrackPlayerImpl::slotCloneFromDeck);
// Sampler cloning
m_pCloneFromSampler = std::make_unique<ControlObject>(
ConfigKey(getGroup(), "CloneFromSampler"),
false);
connect(m_pCloneFromSampler.get(),
&ControlObject::valueChanged,
this,
&BaseTrackPlayerImpl::slotCloneFromSampler);
// Load track from other deck/sampler
m_pLoadTrackFromDeck = std::make_unique<ControlObject>(
ConfigKey(getGroup(), "LoadTrackFromDeck"),
false);
connect(m_pLoadTrackFromDeck.get(),
&ControlObject::valueChanged,
this,
&BaseTrackPlayerImpl::slotLoadTrackFromDeck);
m_pLoadTrackFromSampler = std::make_unique<ControlObject>(
ConfigKey(getGroup(), "LoadTrackFromSampler"),
false);
connect(m_pLoadTrackFromSampler.get(),
&ControlObject::valueChanged,
this,
&BaseTrackPlayerImpl::slotLoadTrackFromSampler);
// Waveform controls
// This acts somewhat like a ControlPotmeter, but the normal _up/_down methods
// do not work properly with this CO.
m_pWaveformZoom =
std::make_unique<ControlObject>(ConfigKey(getGroup(), "waveform_zoom"));
m_pWaveformZoom->connectValueChangeRequest(this,
&BaseTrackPlayerImpl::slotWaveformZoomValueChangeRequest,
Qt::DirectConnection);
m_pWaveformZoom->set(1.0);
m_pWaveformZoomUp = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "waveform_zoom_up"));
connect(m_pWaveformZoomUp.get(),
&ControlPushButton::valueChanged,
this,
&BaseTrackPlayerImpl::slotWaveformZoomUp);
m_pWaveformZoomDown = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "waveform_zoom_down"));
connect(m_pWaveformZoomDown.get(),
&ControlPushButton::valueChanged,
this,
&BaseTrackPlayerImpl::slotWaveformZoomDown);
m_pWaveformZoomSetDefault = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "waveform_zoom_set_default"));
connect(m_pWaveformZoomSetDefault.get(),
&ControlPushButton::valueChanged,
this,
&BaseTrackPlayerImpl::slotWaveformZoomSetDefault);
m_pPreGain = make_parented<ControlProxy>(getGroup(), "pregain", this);
m_pShiftCuesEarlier = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "shift_cues_earlier"));
connect(m_pShiftCuesEarlier.get(),
&ControlObject::valueChanged,
this,
[this](double value) { slotShiftCuesMillisButton(value, -kShiftCuesOffsetMillis); });
m_pShiftCuesLater = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "shift_cues_later"));
connect(m_pShiftCuesLater.get(),
&ControlObject::valueChanged,
this,
[this](double value) { slotShiftCuesMillisButton(value, kShiftCuesOffsetMillis); });
m_pShiftCuesEarlierSmall = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "shift_cues_earlier_small"));
connect(m_pShiftCuesEarlierSmall.get(),
&ControlObject::valueChanged,
this,
[this](double value) {
slotShiftCuesMillisButton(value, -kShiftCuesOffsetSmallMillis);
});
m_pShiftCuesLaterSmall = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "shift_cues_later_small"));
connect(m_pShiftCuesLaterSmall.get(),
&ControlObject::valueChanged,
this,
[this](double value) {
slotShiftCuesMillisButton(value, kShiftCuesOffsetSmallMillis);
});
m_pShiftCues = std::make_unique<ControlObject>(
ConfigKey(getGroup(), "shift_cues"));
connect(m_pShiftCues.get(),
&ControlObject::valueChanged,
this,
&BaseTrackPlayerImpl::slotShiftCuesMillis);
// BPM and key of the current song
m_pFileBPM = std::make_unique<ControlObject>(ConfigKey(getGroup(), "file_bpm"));
m_pVisualBpm = std::make_unique<ControlObject>(ConfigKey(getGroup(), "visual_bpm"));
m_pKey = make_parented<ControlProxy>(getGroup(), "file_key", this);
m_pVisualKey = std::make_unique<ControlObject>(ConfigKey(getGroup(), "visual_key"));
m_pTimeElapsed = std::make_unique<ControlObject>(ConfigKey(getGroup(), "time_elapsed"));
m_pTimeRemaining = std::make_unique<ControlObject>(ConfigKey(getGroup(), "time_remaining"));
m_pEndOfTrack = std::make_unique<ControlObject>(ConfigKey(getGroup(), "end_of_track"));
m_pReplayGain = make_parented<ControlProxy>(getGroup(), "replaygain", this);
m_pPlay = make_parented<ControlProxy>(getGroup(), "play", this);
m_pPlay->connectValueChanged(this, &BaseTrackPlayerImpl::slotPlayToggled);
m_pRateRatio = make_parented<ControlProxy>(getGroup(), "rate_ratio", this);
m_pPitchAdjust = make_parented<ControlProxy>(getGroup(), "pitch_adjust", this);
m_pUpdateReplayGainFromPregain = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "update_replaygain_from_pregain"));
m_pUpdateReplayGainFromPregain->connectValueChangeRequest(this,
&BaseTrackPlayerImpl::slotUpdateReplayGainFromPregain);
m_ejectTimer.start();
if (!primaryDeck) {
return;
}
#ifdef __STEM__
m_pStemColors.reserve(mixxx::kMaxSupportedStems);
QString group = getGroup();
for (int stemIdx = 1; stemIdx <= mixxx::kMaxSupportedStems; stemIdx++) {
QString stemGroup = QStringLiteral("%1Stem%2]")
.arg(group.left(group.size() - 1),
QString::number(stemIdx));
m_pStemColors.emplace_back(std::make_unique<ControlObject>(
ConfigKey(stemGroup, QStringLiteral("color"))));
m_pStemColors.back()->set(kNoTrackColor);
m_pStemColors.back()->setReadOnly();
}
#endif
}
BaseTrackPlayerImpl::~BaseTrackPlayerImpl() {
unloadTrack();
}
TrackPointer BaseTrackPlayerImpl::loadFakeTrack(bool bPlay, double filebpm) {
TrackPointer pTrack(Track::newTemporary());
pTrack->setAudioProperties(
mixxx::kEngineChannelOutputCount,
mixxx::audio::SampleRate(44100),
mixxx::audio::Bitrate(),
mixxx::Duration::fromSeconds(10));
if (filebpm > 0) {
pTrack->trySetBpm(filebpm);
}
TrackPointer pOldTrack = m_pLoadedTrack;
m_pLoadedTrack = pTrack;
if (m_pLoadedTrack) {
// Listen for updates to the file's BPM
connectLoadedTrack();
}
// Request a new track from EngineBuffer
EngineBuffer* pEngineBuffer = m_pChannel->getEngineBuffer();
pEngineBuffer->loadFakeTrack(pTrack, bPlay);
// await slotTrackLoaded()/slotLoadFailed()
emit loadingTrack(pTrack, pOldTrack);
return pTrack;
}
void BaseTrackPlayerImpl::loadTrack(TrackPointer pTrack) {
DEBUG_ASSERT(!m_pLoadedTrack);
m_pLoadedTrack = std::move(pTrack);
if (!m_pLoadedTrack) {
// nothing to
return;
}
// Clear loop
// It seems that the trick is to first clear the loop out point, and then
// the loop in point. If we first clear the loop in point, the loop out point
// does not get cleared.
m_pLoopOutPoint->set(kNoTrigger);
m_pLoopInPoint->set(kNoTrigger);
// The loop in and out points must be set here and not in slotTrackLoaded
// so LoopingControl::trackLoaded can access them.
if (!m_pChannelToCloneFrom) {
// Restore loop from the first loop cue with minimum hotcue number.
// For the volatile "most recent loop" the hotcue number will be -1.
// If no such loop exists, restore a saved loop cue.
CuePointer pLoopCue;
const QList<CuePointer> trackCues = m_pLoadedTrack->getCuePoints();
for (const auto& pCue : trackCues) {
if (pCue->getType() != mixxx::CueType::Loop) {
continue;
}
if (pLoopCue && pLoopCue->getHotCue() <= pCue->getHotCue()) {
continue;
}
pLoopCue = pCue;
}
if (pLoopCue) {
const auto loop = pLoopCue->getStartAndEndPosition();
if (loop.startPosition.isValid() && loop.endPosition.isValid() &&
loop.startPosition <= loop.endPosition) {
// TODO: For all loop cues, both end and start positions should
// be valid and the end position should be greater than the
// start position. We should use a VERIFY_OR_DEBUG_ASSERT to
// check this. To make this possible, we need to ensure that
// all invalid cues are discarded when saving cues to the
// database first.
m_pLoopInPoint->set(loop.startPosition.toEngineSamplePos());
m_pLoopOutPoint->set(loop.endPosition.toEngineSamplePos());
}
}
} else {
// copy loop in and out points from other deck because any new loops
// won't be saved yet
m_pLoopInPoint->set(ControlObject::get(
ConfigKey(m_pChannelToCloneFrom->getGroup(), "loop_start_position")));
m_pLoopOutPoint->set(ControlObject::get(
ConfigKey(m_pChannelToCloneFrom->getGroup(), "loop_end_position")));
#ifdef __STEM__
auto* pDeckToClone = qobject_cast<EngineDeck*>(m_pChannelToCloneFrom);
if (pDeckToClone && m_pLoadedTrack && m_pLoadedTrack->hasStem() && m_pChannel) {
m_pChannel->cloneStemState(pDeckToClone);
}
#endif
}
connectLoadedTrack();
}
void BaseTrackPlayerImpl::slotEjectTrack(double v) {
if (v <= 0) {
return;
}
// Don't allow eject while playing a track. We don't need to lock to
// call ControlObject::get() so this is fine.
if (m_pPlay->toBool()) {
return;
}
mixxx::Duration elapsed = m_ejectTimer.restart();
// Double-click always restores the last replaced track, i.e. un-eject the second
// last track: the first click ejects or unejects, and the second click reloads.
if (elapsed < mixxx::Duration::fromMillis(kUnreplaceDelay)) {
TrackPointer lastEjected = m_pPlayerManager->getSecondLastEjectedTrack();
if (lastEjected) {
slotLoadTrack(lastEjected,
#ifdef __STEM__
mixxx::StemChannelSelection(),
#endif
false);
}
return;
}
// With no loaded track a single click reloads the last ejected track.
if (!m_pLoadedTrack) {
TrackPointer lastEjected = m_pPlayerManager->getLastEjectedTrack();
if (lastEjected) {
slotLoadTrack(lastEjected,
#ifdef __STEM__
mixxx::StemChannelSelection(),
#endif
false);
}
return;
}
m_pChannel->getEngineBuffer()->ejectTrack();
}
TrackPointer BaseTrackPlayerImpl::unloadTrack() {
if (!m_pLoadedTrack) {
// nothing to do
return TrackPointer();
}
PlayerInfo::instance().setTrackInfo(getGroup(), TrackPointer());
// Save the loop that is currently to the loop cue. If no loop cue is
// currently on the track, create a new one.
// If the loop is invalid and a loop cue exists, remove it.
const auto loopStart =
mixxx::audio::FramePos::fromEngineSamplePosMaybeInvalid(
m_pLoopInPoint->get());
const auto loopEnd =
mixxx::audio::FramePos::fromEngineSamplePosMaybeInvalid(
m_pLoopOutPoint->get());
CuePointer pLoopCue;
const QList<CuePointer> cuePoints = m_pLoadedTrack->getCuePoints();
for (const auto& pCue : cuePoints) {
if (pCue->getType() == mixxx::CueType::Loop && pCue->getHotCue() == Cue::kNoHotCue) {
pLoopCue = pCue;
break;
}
}
if (loopStart.isValid() && loopEnd.isValid() && loopStart <= loopEnd) {
if (pLoopCue) {
pLoopCue->setStartAndEndPosition(loopStart, loopEnd);
} else {
pLoopCue = m_pLoadedTrack->createAndAddCue(
mixxx::CueType::Loop,
Cue::kNoHotCue,
loopStart,
loopEnd);
}
} else if (pLoopCue) {
m_pLoadedTrack->removeCue(pLoopCue);
}
disconnectLoadedTrack();
// Do not reset m_pReplayGain here, because the track might be still
// playing and the last buffer will be processed.
m_pPlay->set(0.0);
TrackPointer pUnloadedTrack(std::move(m_pLoadedTrack));
DEBUG_ASSERT(!m_pLoadedTrack);
emit trackUnloaded(pUnloadedTrack);
return pUnloadedTrack;
}
void BaseTrackPlayerImpl::connectLoadedTrack() {
connect(m_pLoadedTrack.get(),
&Track::bpmChanged,
this,
[this] {
TrackPointer pTrack = m_pLoadedTrack;
if (pTrack) {
m_pFileBPM->set(pTrack->getBpm());
}
});
connect(m_pLoadedTrack.get(),
&Track::keyChanged,
this,
[this] {
TrackPointer pTrack = m_pLoadedTrack;
if (pTrack) {
const auto key = pTrack->getKeys().getGlobalKey();
m_pKey->set(static_cast<double>(key));
}
});
// Listen for updates to the file's Replay Gain
connect(m_pLoadedTrack.get(),
&Track::replayGainUpdated,
this,
&BaseTrackPlayerImpl::slotSetReplayGain);
connect(m_pLoadedTrack.get(),
&Track::replayGainAdjusted,
this,
&BaseTrackPlayerImpl::slotAdjustReplayGain);
connect(m_pLoadedTrack.get(),
&Track::colorUpdated,
this,
&BaseTrackPlayerImpl::slotSetTrackColor);
// Forward the update signal, i.e. use BaseTrackPlayer as relay.
// Currently only used by WStarRating which is connected in
// LegacySkinParser::parseStarRating
connect(m_pLoadedTrack.get(),
&Track::ratingUpdated,
this,
&BaseTrackPlayerImpl::trackRatingChanged);
}
void BaseTrackPlayerImpl::disconnectLoadedTrack() {
// WARNING: Never. Ever. call bare disconnect() on an object. Mixxx
// relies on signals and slots to get tons of things done. Don't
// randomly disconnect things.
disconnect(m_pLoadedTrack.get(), nullptr, m_pFileBPM.get(), nullptr);
disconnect(m_pLoadedTrack.get(), nullptr, this, nullptr);
disconnect(m_pLoadedTrack.get(), nullptr, m_pKey.get(), nullptr);
}
#ifdef __STEM__
void BaseTrackPlayerImpl::slotLoadTrack(TrackPointer pNewTrack,
mixxx::StemChannelSelection stemMask,
bool bPlay) {
#else
void BaseTrackPlayerImpl::slotLoadTrack(TrackPointer pNewTrack,
bool bPlay) {
#endif
//qDebug() << "BaseTrackPlayerImpl::slotLoadTrack" << getGroup() << pNewTrack.get();
// Before loading the track, ensure we have access. This uses lazy
// evaluation to make sure track isn't NULL before we dereference it.
if (pNewTrack) {
auto fileInfo = pNewTrack->getFileInfo();
if (!Sandbox::askForAccess(&fileInfo)) {
// We don't have access.
return;
}
}
auto pOldTrack = unloadTrack();
loadTrack(pNewTrack);
// await slotTrackLoaded()/slotLoadFailed()
// emit this before pEngineBuffer->loadTrack() to avoid receiving
// unexpected slotTrackLoaded() before, in case the track is still cached #10504.
emit loadingTrack(pNewTrack, pOldTrack);
// Request a new track from EngineBuffer
EngineBuffer* pEngineBuffer = m_pChannel->getEngineBuffer();
#ifdef __STEM__
pEngineBuffer->loadTrack(pNewTrack,
stemMask,
bPlay,
m_pChannelToCloneFrom);
// Select a specific stem if requested
emit selectedStems(stemMask);
#else
pEngineBuffer->loadTrack(pNewTrack, bPlay, m_pChannelToCloneFrom);
#endif
}
void BaseTrackPlayerImpl::slotLoadFailed(TrackPointer pTrack, const QString& reason) {
// Note: This slot can be a load failure from the current track or a
// a delayed signal from a previous load.
// We have probably received a slotTrackLoaded signal, of an old track that
// was loaded before. Here we must unload the
// We must unload the track m_pLoadedTrack as well
if (pTrack == m_pLoadedTrack) {
qDebug() << "Failed to load track" << pTrack->getFileInfo() << reason;
slotTrackLoaded(TrackPointer(), pTrack);
} else if (pTrack) {
qDebug() << "Stray failed to load track" << pTrack->getFileInfo() << reason;
} else {
qDebug() << "Failed to load track (NULL track object)" << reason;
}
m_pChannelToCloneFrom = nullptr;
// Alert user.
// The QMessageBox blocks the event loop (and the GUI since it's modal dialog),
// though if a controller's Load button was pressed repeatedly we may get
// multiple identical messages for the same track.
// Avoid this and show only one message per track track at a time.
if (pTrack && m_pPrevFailedTrackId == pTrack->getId()) {
return;
} else if (pTrack) {
m_pPrevFailedTrackId = pTrack->getId();
}
QMessageBox::warning(nullptr, tr("Couldn't load track."), reason);
m_pPrevFailedTrackId = TrackId();
}
void BaseTrackPlayerImpl::slotTrackLoaded(TrackPointer pNewTrack,
TrackPointer pOldTrack) {
//qDebug() << "BaseTrackPlayerImpl::slotTrackLoaded" << pNewTrack.get() << pOldTrack.get();
if (!pNewTrack &&
pOldTrack &&
pOldTrack == m_pLoadedTrack) {
// eject Track
unloadTrack();
// Causes the track's data to be saved back to the library database and
// for all the widgets to change the track and update themselves.
emit loadingTrack(pNewTrack, pOldTrack);
m_pDuration->set(0);
m_pFileBPM->set(0);
m_pKey->set(0);
slotSetTrackColor(std::nullopt);
m_pLoopInPoint->set(kNoTrigger);
m_pLoopOutPoint->set(kNoTrigger);
m_pLoadedTrack.reset();
emit playerEmpty();
emit trackRatingChanged(0);
} else if (pNewTrack && pNewTrack == m_pLoadedTrack) {
// NOTE(uklotzde): In a previous version track metadata was reloaded
// from the source file at this point again. This is no longer necessary
// since track objects will always be created in a controlled manner
// and populated from the database and their source file as required
// before handing them out to application code.
// TODO(XXX): Don't hesitate to delete the preceding NOTE if you think
// that it is not needed anymore.
// Update the BPM and duration values that are stored in ControlObjects
m_pDuration->set(m_pLoadedTrack->getDuration());
m_pFileBPM->set(m_pLoadedTrack->getBpm());
m_pKey->set(m_pLoadedTrack->getKey());
slotSetTrackColor(m_pLoadedTrack->getColor());
if(m_pConfig->getValue(
ConfigKey("[Mixer Profile]", "EqAutoReset"), false)) {
if (m_pLowFilter) {
m_pLowFilter->set(1.0);
}
if (m_pMidFilter) {
m_pMidFilter->set(1.0);
}
if (m_pHighFilter) {
m_pHighFilter->set(1.0);
}
if (m_pLowFilterKill) {
m_pLowFilterKill->set(0.0);
}
if (m_pMidFilterKill) {
m_pMidFilterKill->set(0.0);
}
if (m_pHighFilterKill) {
m_pHighFilterKill->set(0.0);
}
}
if (m_pConfig->getValue(
ConfigKey("[Mixer Profile]", "GainAutoReset"), false)) {
m_pPreGain->set(1.0);
}
if (!m_pChannelToCloneFrom) {
BaseTrackPlayer::TrackLoadReset reset = m_pConfig->getValue(
ConfigKey("[Controls]", "SpeedAutoReset"), RESET_PITCH);
if (reset == RESET_SPEED || reset == RESET_PITCH_AND_SPEED) {
// Avoid resetting speed if sync lock is enabled and other decks with sync enabled
// are playing, as this would change the speed of already playing decks.
if (!m_pEngineMixer->getEngineSync()->otherSyncedPlaying(getGroup())) {
m_pRateRatio->set(1.0);
}
}
if (reset == RESET_PITCH || reset == RESET_PITCH_AND_SPEED) {
m_pPitchAdjust->set(0.0);
}
} else {
// perform a clone of the given channel
// Don't touch the rate_ratio if it is follower under sync control
// During sync this is applied to all synced decks
if (ControlObject::get(ConfigKey(getGroup(), "sync_mode")) !=
static_cast<double>(SyncMode::Follower)) {
m_pRateRatio->set(ControlObject::get(ConfigKey(
m_pChannelToCloneFrom->getGroup(), "rate_ratio")));
}
// copy pitch
m_pPitchAdjust->set(ControlObject::get(ConfigKey(
m_pChannelToCloneFrom->getGroup(), "pitch_adjust")));
// copy the loop state
if (ControlObject::get(ConfigKey(m_pChannelToCloneFrom->getGroup(), "loop_enabled")) == 1.0) {
ControlObject::set(ConfigKey(getGroup(), "reloop_toggle"), 1.0);
}
}
#ifdef __STEM__
if (m_pStemColors.size()) {
const auto& stemInfo = m_pLoadedTrack->getStemInfo();
DEBUG_ASSERT(stemInfo.size() <= mixxx::kMaxSupportedStems);
int stemIdx = 0;
for (const auto& stemColorCo : m_pStemColors) {
auto color = kNoTrackColor;
if (stemIdx < stemInfo.size()) {
color = trackColorToDouble(mixxx::RgbColor::fromQColor(
stemInfo.at(stemIdx).getColor()));
}
stemColorCo->forceSet(color);
stemIdx++;
}
}
#endif
emit newTrackLoaded(m_pLoadedTrack);
emit trackRatingChanged(m_pLoadedTrack->getRating());
} else {
// this is the result from an outdated load or unload signal
// A new load is already pending
// Ignore this signal and wait for the new one
qDebug() << "stray BaseTrackPlayerImpl::slotTrackLoaded()";
}
m_pChannelToCloneFrom = nullptr;
// Update the PlayerInfo class that is used in EngineBroadcast to replace
// the metadata of a stream
PlayerInfo::instance().setTrackInfo(getGroup(), m_pLoadedTrack);
}
TrackPointer BaseTrackPlayerImpl::getLoadedTrack() const {
return m_pLoadedTrack;
}
void BaseTrackPlayerImpl::slotCloneDeck() {
Syncable* syncable = m_pEngineMixer->getEngineSync()->pickNonSyncSyncTarget(m_pChannel);
if (syncable) {
slotCloneChannel(syncable->getChannel());
}
}
void BaseTrackPlayerImpl::slotCloneFromGroup(const QString& group) {
EngineChannel* pChannel = m_pEngineMixer->getChannel(group);
if (!pChannel) {
return;
}
slotCloneChannel(pChannel);
}
void BaseTrackPlayerImpl::slotCloneFromDeck(double d) {
int deck = static_cast<int>(d);
if (deck < 1) {
slotCloneDeck();
} else {
slotCloneFromGroup(PlayerManager::groupForDeck(deck - 1));
}
}
void BaseTrackPlayerImpl::slotCloneFromSampler(double d) {
int sampler = static_cast<int>(d);
if (sampler >= 1) {
slotCloneFromGroup(PlayerManager::groupForSampler(sampler - 1));
}
}
void BaseTrackPlayerImpl::slotCloneChannel(EngineChannel* pChannel) {
// don't clone from ourselves
if (!pChannel || pChannel == m_pChannel) {
return;
}
TrackPointer pTrack = pChannel->getEngineBuffer()->getLoadedTrack();
if (!pTrack) {
return;
}
m_pChannelToCloneFrom = pChannel;
bool play = ControlObject::toBool(ConfigKey(m_pChannelToCloneFrom->getGroup(), "play"));
slotLoadTrack(pTrack,
#ifdef __STEM__
mixxx::StemChannelSelection(),
#endif
play);
}
void BaseTrackPlayerImpl::slotLoadTrackFromDeck(double d) {
int deck = static_cast<int>(d);
loadTrackFromGroup(PlayerManager::groupForDeck(deck - 1));
}
void BaseTrackPlayerImpl::slotLoadTrackFromSampler(double d) {
int sampler = static_cast<int>(d);
loadTrackFromGroup(PlayerManager::groupForSampler(sampler - 1));
}
void BaseTrackPlayerImpl::loadTrackFromGroup(const QString& group) {
EngineChannel* pChannel = m_pEngineMixer->getChannel(group);
if (!pChannel) {
return;
}
TrackPointer pTrack = pChannel->getEngineBuffer()->getLoadedTrack();
if (!pTrack) {
return;
}
slotLoadTrack(pTrack,
#ifdef __STEM__
mixxx::StemChannelSelection(),
#endif
false);
}
bool BaseTrackPlayerImpl::isTrackMenuControlAvailable() {
if (m_pShowTrackMenuControl == nullptr) {
// Create the control and return true so LegacySkinParser knows it should
// connect our signal to WTrackProperty.
m_pShowTrackMenuControl = std::make_unique<ControlPushButton>(
ConfigKey(getGroup(), "show_track_menu"));
m_pShowTrackMenuControl->connectValueChangeRequest(
this,
[this](double value) {
emit trackMenuChangeRequest(value > 0);
});
return true;
} else if (isSignalConnected(
QMetaMethod::fromSignal(&BaseTrackPlayer::trackMenuChangeRequest))) {
// Control exists and we're already connected.
// This means the request was made while creating the 2nd or later WTrackProperty.
return false;
} else {
// Control already exists but signal is not connected, which is the case
// after loading a skin. Return true so LegacySkinParser makes a new connection.
return true;
}
}
void BaseTrackPlayerImpl::slotSetAndConfirmTrackMenuControl(bool visible) {
VERIFY_OR_DEBUG_ASSERT(m_pShowTrackMenuControl) {
return;
}
m_pShowTrackMenuControl->setAndConfirm(visible ? 1.0 : 0.0);
}
void BaseTrackPlayerImpl::slotSetReplayGain(mixxx::ReplayGain replayGain) {
// Do not change replay gain when track is playing because
// this may lead to an unexpected volume change.
if (m_pPlay->get() == 0.0) {
setReplayGain(replayGain.getRatio());
} else {
m_replaygainPending = true;
}
}
void BaseTrackPlayerImpl::slotAdjustReplayGain(mixxx::ReplayGain replayGain) {
const double factor = m_pReplayGain->get() / replayGain.getRatio();
const double newPregain = m_pPreGain->get() * factor;
// There is a very slight chance that there will be a buffer call in between these sets.
// Therefore, we first adjust the control that is being lowered before the control
// that is being raised. Worst case, the volume goes down briefly before rectifying.
if (factor < 1.0) {
m_pPreGain->set(newPregain);
setReplayGain(replayGain.getRatio());
} else {
setReplayGain(replayGain.getRatio());
m_pPreGain->set(newPregain);
}
}
void BaseTrackPlayerImpl::slotSetTrackColor(const mixxx::RgbColor::optional_t& color) {
m_pTrackColor->forceSet(trackColorToDouble(color));
}
void BaseTrackPlayerImpl::slotTrackColorSelector(int steps) {
if (!m_pLoadedTrack || steps == 0) {
return;
}
ColorPaletteSettings colorPaletteSettings(m_pConfig);
ColorPalette colorPalette = colorPaletteSettings.getTrackColorPalette();
mixxx::RgbColor::optional_t color = m_pLoadedTrack->getColor();
while (steps != 0) {
if (steps > 0) {
color = colorPalette.nextColor(color);
steps--;
} else {
color = colorPalette.previousColor(color);
steps++;
}
}
m_pLoadedTrack->setColor(color);
}
void BaseTrackPlayerImpl::slotTrackColorChangeRequest(double v) {
if (!m_pLoadedTrack) {
return;
}
mixxx::RgbColor::optional_t color = std::nullopt;
if (v != kNoTrackColor) {
auto colorCode = static_cast<mixxx::RgbColor::code_t>(v);
if (!mixxx::RgbColor::isValidCode(colorCode)) {
return;
}
color = mixxx::RgbColor::optional(colorCode);
}
m_pLoadedTrack->setColor(color);
}
void BaseTrackPlayerImpl::slotTrackRatingChangeRequest(int rating) {
if (!m_pLoadedTrack) {
return;
}
if (mixxx::TrackRecord::isValidRating(rating) &&
rating != m_pLoadedTrack->getRating()) {
m_pLoadedTrack->setRating(rating);
emit trackRatingChanged(rating);
}
}
void BaseTrackPlayerImpl::slotTrackRatingChangeRequestRelative(int change) {
if (!m_pLoadedTrack) {
return;
}
slotTrackRatingChangeRequest(m_pLoadedTrack->getRating() + change);
}
void BaseTrackPlayerImpl::slotPlayToggled(double value) {
if (value == 0 && m_replaygainPending) {
setReplayGain(m_pLoadedTrack->getReplayGain().getRatio());
}
}
EngineDeck* BaseTrackPlayerImpl::getEngineDeck() const {
return m_pChannel;
}
void BaseTrackPlayerImpl::setupEqControls() {
const QString group = kEffectGroupFormat.arg(getGroup());
m_pLowFilter = make_parented<ControlProxy>(group, QStringLiteral("parameter1"), this);
m_pMidFilter = make_parented<ControlProxy>(group, QStringLiteral("parameter2"), this);
m_pHighFilter = make_parented<ControlProxy>(group, QStringLiteral("parameter3"), this);
m_pLowFilterKill = make_parented<ControlProxy>(
group, QStringLiteral("button_parameter1"), this);
m_pMidFilterKill = make_parented<ControlProxy>(
group, QStringLiteral("button_parameter2"), this);
m_pHighFilterKill = make_parented<ControlProxy>(
group, QStringLiteral("button_parameter3"), this);
}
void BaseTrackPlayerImpl::slotVinylControlEnabled(double v) {
#ifdef __VINYLCONTROL__
bool configured = m_pInputConfigured->toBool();
bool vinylcontrol_enabled = v > 0.0;
// Warn the user if they try to enable vinyl control on a player with no
// configured input.
if (!configured && vinylcontrol_enabled) {
m_pVinylControlEnabled->set(0.0);
m_pVinylControlStatus->set(VINYL_STATUS_DISABLED);
emit noVinylControlInputConfigured();
}
#else
Q_UNUSED(v);
#endif
}
void BaseTrackPlayerImpl::slotWaveformZoomValueChangeRequest(double v) {
if (v <= WaveformWidgetRenderer::s_waveformMaxZoom
&& v >= WaveformWidgetRenderer::s_waveformMinZoom) {
m_pWaveformZoom->setAndConfirm(v);
}
}
void BaseTrackPlayerImpl::slotWaveformZoomUp(double pressed) {