-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathautodjprocessor.cpp
More file actions
1783 lines (1596 loc) · 69.1 KB
/
Copy pathautodjprocessor.cpp
File metadata and controls
1783 lines (1596 loc) · 69.1 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 "library/autodj/autodjprocessor.h"
#include "engine/channels/enginedeck.h"
#include "mixer/basetrackplayer.h"
#include "mixer/playermanager.h"
#include "moc_autodjprocessor.cpp"
#include "track/track.h"
#include "util/math.h"
namespace {
const QString kPreferenceGroup = QStringLiteral("[Auto DJ]");
const QString kControlGroup = QStringLiteral("[AutoDJ]");
const char* kTransitionPreferenceName = "Transition";
const char* kTransitionModePreferenceName = "TransitionMode";
constexpr double kTransitionPreferenceDefault = 10.0;
constexpr double kKeepPosition = TrackOrDeckAttributes::kKeepPosition;
// A track needs to be longer than two callbacks to not stop AutoDJ
constexpr double kMinimumTrackDurationSec = 0.2;
constexpr bool sDebug = false;
} // anonymous namespace
AutoDJProcessor::AutoDJProcessor(
QObject* pParent,
UserSettingsPointer pConfig,
PlayerManagerInterface* pPlayerManager,
TrackCollectionManager* pTrackCollectionManager,
int iAutoDJPlaylistId)
: QObject(pParent),
m_pConfig(pConfig),
m_pAutoDJTableModel(nullptr),
m_eState(ADJ_DISABLED),
m_transitionProgress(0.0),
m_transitionTime(kTransitionPreferenceDefault),
m_pPlayerManager(pPlayerManager),
m_coCrossfader(QStringLiteral("[Master]"), QStringLiteral("crossfader")),
m_coCrossfaderReverse(QStringLiteral("[Mixer Profile]"), QStringLiteral("xFaderReverse")),
m_shufflePlaylist(ConfigKey(kControlGroup, QStringLiteral("shuffle_playlist"))),
m_skipNext(ConfigKey(kControlGroup, QStringLiteral("skip_next"))),
m_addRandomTrack(ConfigKey(kControlGroup, QStringLiteral("add_random_track"))),
m_fadeNow(ConfigKey(kControlGroup, QStringLiteral("fade_now"))),
m_enabledAutoDJ(ConfigKey(kControlGroup, QStringLiteral("enabled"))) {
m_pAutoDJTableModel = make_parented<PlaylistTableModel>(
this, pTrackCollectionManager, "mixxx.db.model.autodj");
m_pAutoDJTableModel->selectPlaylist(iAutoDJPlaylistId);
m_pAutoDJTableModel->select();
connect(&m_shufflePlaylist,
&ControlPushButton::valueChanged,
this,
&AutoDJProcessor::controlShuffle);
connect(&m_skipNext, &ControlObject::valueChanged, this, &AutoDJProcessor::controlSkipNext);
connect(&m_addRandomTrack,
&ControlObject::valueChanged,
this,
&AutoDJProcessor::controlAddRandomTrack);
connect(&m_fadeNow, &ControlObject::valueChanged, this, &AutoDJProcessor::controlFadeNow);
m_enabledAutoDJ.setButtonMode(mixxx::control::ButtonMode::Toggle);
m_enabledAutoDJ.connectValueChangeRequest(this,
&AutoDJProcessor::controlEnableChangeRequest);
connect(pPlayerManager,
&PlayerManagerInterface::numberOfDecksChanged,
this,
&AutoDJProcessor::slotNumberOfDecksChanged);
slotNumberOfDecksChanged(pPlayerManager->numberOfDecks());
QString str_autoDjTransition = m_pConfig->getValueString(
ConfigKey(kPreferenceGroup, kTransitionPreferenceName));
if (!str_autoDjTransition.isEmpty()) {
m_transitionTime = str_autoDjTransition.toDouble();
}
m_transitionMode = m_pConfig->getValue(
ConfigKey(kPreferenceGroup, kTransitionModePreferenceName),
TransitionMode::FullIntroOutro);
}
void AutoDJProcessor::slotNumberOfDecksChanged(int decks) {
m_decks.reserve(decks);
// Add more decks if we have not all yet.
// Mixxx does not support reducing the number of deck
for (int i = static_cast<int>(m_decks.size()); i < decks; ++i) {
BaseTrackPlayer* pPlayer = m_pPlayerManager->getDeckBase(i);
// Shouldn't be possible.
VERIFY_OR_DEBUG_ASSERT(pPlayer) {
return;
}
m_decks.emplace_back(std::make_unique<DeckAttributes>(i, pPlayer));
}
}
double AutoDJProcessor::getCrossfader() const {
if (m_coCrossfaderReverse.toBool()) {
return m_coCrossfader.get() * -1.0;
}
return m_coCrossfader.get();
}
void AutoDJProcessor::setCrossfader(double value) {
if (m_coCrossfaderReverse.toBool()) {
value *= -1.0;
}
m_coCrossfader.set(value);
}
AutoDJProcessor::AutoDJError AutoDJProcessor::shufflePlaylist(
const QModelIndexList& selectedIndices) {
QModelIndex exclude;
if (m_eState != ADJ_DISABLED) {
exclude = m_pAutoDJTableModel->index(0, 0);
}
m_pAutoDJTableModel->shuffleTracks(selectedIndices, exclude);
return ADJ_OK;
}
void AutoDJProcessor::fadeNow() {
if (m_eState != ADJ_IDLE) {
// we cannot fade if AutoDj is disabled or already fading
return;
}
double crossfader = getCrossfader();
DeckAttributes* pLeftDeck = getLeftDeck();
DeckAttributes* pRightDeck = getRightDeck();
if (!pLeftDeck || !pRightDeck) {
// User has changed the orientation, disable Auto DJ
toggleAutoDJ(false);
emit autoDJError(ADJ_NOT_TWO_DECKS);
return;
}
DeckAttributes* pFromDeck;
DeckAttributes* pToDeck;
if (pLeftDeck->isPlaying() &&
(!pRightDeck->isPlaying() || crossfader < 0.0)) {
pFromDeck = pLeftDeck;
pToDeck = pRightDeck;
} else if (pRightDeck->isPlaying()) {
pFromDeck = pRightDeck;
pToDeck = pLeftDeck;
} else {
// Neither deck is playing. Fading now makes no sense.
return;
}
pFromDeck->setRepeat(false);
pFromDeck->isFromDeck = true;
pToDeck->isFromDeck = false;
const double fromDeckEndSecond = getEndSecond(pFromDeck);
const double toDeckEndSecond = getEndSecond(pToDeck);
// Since the end position is measured in seconds from 0:00 it is also
// the track duration. Use this alias for better readability.
const double fromDeckDuration = fromDeckEndSecond;
const double toDeckDuration = toDeckEndSecond;
if (toDeckDuration < kMinimumTrackDurationSec) {
// Deck is empty or track too short, disable AutoDJ
// This happens only if the user has changed deck orientation to such deck.
toggleAutoDJ(false);
emit autoDJError(ADJ_NOT_TWO_DECKS);
return;
}
// playPosition() is in the range of 0..1
const double fromDeckCurrentSecond = fromDeckDuration * pFromDeck->playPosition();
const double toDeckCurrentSecond = toDeckDuration * pToDeck->playPosition();
if (toDeckDuration - toDeckCurrentSecond < kMinimumTrackDurationSec) {
// Remaining Track time is too short, user has has seeked near the end
// Re-cue the track
pToDeck->setPlayPosition(pToDeck->startPos);
}
pFromDeck->fadeBeginPos = fromDeckCurrentSecond;
// Do not seek to a calculated start point; start the to deck from wherever
// it is if the user has seeked since loading the track.
pToDeck->startPos = toDeckCurrentSecond;
// If the user presses "Fade now", assume they want to fade *now*, not later.
// So if the spinbox time is negative, do not insert silence.
double spinboxTime = fabs(m_transitionTime);
double fadeTime;
if (m_transitionMode == TransitionMode::FullIntroOutro ||
m_transitionMode == TransitionMode::FadeAtOutroStart) {
// Use the intro length as the transition time. If the user has seeked
// away from the intro start since the track was loaded, start from
// there and do not seek back to the intro start. If they have seeked
// past the introEnd or the introEnd is not marked, fall back to the
// spinbox time.
double outroEnd = getOutroEndSecond(pFromDeck);
double introEnd = getIntroEndSecond(pToDeck);
double introStart = getIntroStartSecond(pToDeck);
double timeUntilOutroEnd = outroEnd - fromDeckCurrentSecond;
// IntroStart ends up being equal to introEnd when pToDeck is
// paused and its introEnd marker is not set. getIntroEndSecond returns
// introStart thus the two end up having equal values
if (toDeckCurrentSecond >= introStart &&
toDeckCurrentSecond <= introEnd &&
introStart != introEnd) {
double timeUntilIntroEnd = introEnd - toDeckCurrentSecond;
// The fade must end by the outro end at the latest.
fadeTime = math_min(timeUntilIntroEnd, timeUntilOutroEnd);
} else {
// If this is true, the fade should have already been started
// so the user should not have been able to press the Fade button.
VERIFY_OR_DEBUG_ASSERT(timeUntilOutroEnd > 0) {
timeUntilOutroEnd = 0;
}
fadeTime = math_min(spinboxTime, timeUntilOutroEnd);
}
} else {
fadeTime = spinboxTime;
}
fadeTime = math_min(fadeTime, fromDeckEndSecond - fromDeckCurrentSecond);
fadeTime = math_min(fadeTime,
(toDeckEndSecond - toDeckCurrentSecond) / 2); // for fade in and out
pFromDeck->fadeEndPos = fromDeckCurrentSecond + fadeTime;
// These are expected to be a fraction of the track length.
pFromDeck->fadeBeginPos /= fromDeckDuration;
pFromDeck->fadeEndPos /= fromDeckDuration;
pToDeck->startPos /= toDeckDuration;
VERIFY_OR_DEBUG_ASSERT(pFromDeck->fadeBeginPos <= 1) {
pFromDeck->fadeBeginPos = 1;
}
}
AutoDJProcessor::AutoDJError AutoDJProcessor::skipNext() {
if (m_eState == ADJ_DISABLED) {
emit autoDJError(ADJ_IS_INACTIVE);
return ADJ_IS_INACTIVE;
}
// Load the next song from the queue.
DeckAttributes* pLeftDeck = getLeftDeck();
DeckAttributes* pRightDeck = getRightDeck();
if (!pLeftDeck || !pRightDeck) {
// User has changed the orientation, disable Auto DJ
toggleAutoDJ(false);
emit autoDJError(ADJ_NOT_TWO_DECKS);
return ADJ_NOT_TWO_DECKS;
}
if (!pLeftDeck->isPlaying()) {
removeLoadedTrackFromTopOfQueue(*pLeftDeck);
loadNextTrackFromQueue(*pLeftDeck);
} else if (!pRightDeck->isPlaying()) {
removeLoadedTrackFromTopOfQueue(*pRightDeck);
loadNextTrackFromQueue(*pRightDeck);
} else {
// If both decks are playing remove next track in playlist
TrackId nextId = m_pAutoDJTableModel->getTrackId(m_pAutoDJTableModel->index(0, 0));
TrackId leftId = pLeftDeck->getLoadedTrack()->getId();
TrackId rightId = pRightDeck->getLoadedTrack()->getId();
if (nextId == leftId || nextId == rightId) {
// One of the playing tracks is still on top of playlist, remove second item
m_pAutoDJTableModel->removeTrack(m_pAutoDJTableModel->index(1, 0));
} else {
m_pAutoDJTableModel->removeTrack(m_pAutoDJTableModel->index(0, 0));
}
maybeFillRandomTracks();
}
return ADJ_OK;
}
AutoDJProcessor::AutoDJError AutoDJProcessor::toggleAutoDJ(bool enable) {
if (enable) { // Enable Auto DJ
DeckAttributes* pLeftDeck = getLeftDeck();
DeckAttributes* pRightDeck = getRightDeck();
if (!pLeftDeck || !pRightDeck) {
// Keep the current state.
emitAutoDJStateChanged(m_eState);
emit autoDJError(ADJ_NOT_TWO_DECKS);
return ADJ_NOT_TWO_DECKS;
}
bool leftDeckPlaying = pLeftDeck->isPlaying();
bool rightDeckPlaying = pRightDeck->isPlaying();
if (leftDeckPlaying && rightDeckPlaying) {
qDebug() << "One deck must be stopped before enabling Auto DJ mode";
// Keep the current state.
emitAutoDJStateChanged(m_eState);
emit autoDJError(ADJ_BOTH_DECKS_PLAYING);
return ADJ_BOTH_DECKS_PLAYING;
}
// Auto-DJ needs at least two decks
DEBUG_ASSERT(m_decks.size() > 1);
// TODO: This is a total band aid for making Auto DJ work with four decks.
// We should design a nicer way to handle this.
for (const auto& pDeck : m_decks) {
VERIFY_OR_DEBUG_ASSERT(pDeck) {
continue;
}
if (pDeck.get() == pLeftDeck) {
continue;
}
if (pDeck.get() == pRightDeck) {
continue;
}
if (pDeck->isPlaying()) {
// Keep the current state.
emitAutoDJStateChanged(m_eState);
emit autoDJError(ADJ_UNUSED_DECK_PLAYING);
return ADJ_UNUSED_DECK_PLAYING;
}
}
if (pLeftDeck->index > 1 || pRightDeck->index > 1) {
// Left and/or right deck is deck 3/4 which may not be visible.
// Make sure it is, if the current skin is a 4-deck skin.
ControlObject::set(
ConfigKey(QStringLiteral("[Skin]"), QStringLiteral("show_4decks")), 1);
}
// Never load the same track if it is already playing
if (leftDeckPlaying) {
removeLoadedTrackFromTopOfQueue(*pLeftDeck);
} else if (rightDeckPlaying) {
removeLoadedTrackFromTopOfQueue(*pRightDeck);
} else {
// If the first track is already cued at a position in the first
// 2/3 in on of the Auto DJ decks, start it.
// If the track is paused at a later position, it is probably too
// close to the end. In this case it is loaded again at the stored
// cue point.
if (pLeftDeck->playPosition() < 0.66 &&
removeLoadedTrackFromTopOfQueue(*pLeftDeck)) {
pLeftDeck->play();
leftDeckPlaying = true;
} else if (pRightDeck->playPosition() < 0.66 &&
removeLoadedTrackFromTopOfQueue(*pRightDeck)) {
pRightDeck->play();
rightDeckPlaying = true;
}
}
TrackPointer nextTrack = getNextTrackFromQueue();
if (!nextTrack) {
qDebug() << "Queue is empty now, disable Auto DJ";
m_enabledAutoDJ.setAndConfirm(0.0);
emitAutoDJStateChanged(m_eState);
emit autoDJError(ADJ_QUEUE_EMPTY);
return ADJ_QUEUE_EMPTY;
}
// Track is available so GO
m_enabledAutoDJ.setAndConfirm(1.0);
qDebug() << "Auto DJ enabled";
m_coCrossfader.connectValueChanged(this, &AutoDJProcessor::crossfaderChanged);
connect(pLeftDeck,
&DeckAttributes::playPositionChanged,
this,
&AutoDJProcessor::playerPositionChanged);
connect(pRightDeck,
&DeckAttributes::playPositionChanged,
this,
&AutoDJProcessor::playerPositionChanged);
connect(pLeftDeck,
&DeckAttributes::playChanged,
this,
&AutoDJProcessor::playerPlayChanged);
connect(pRightDeck,
&DeckAttributes::playChanged,
this,
&AutoDJProcessor::playerPlayChanged);
connect(pLeftDeck,
&DeckAttributes::introStartPositionChanged,
this,
&AutoDJProcessor::playerIntroStartChanged);
connect(pRightDeck,
&DeckAttributes::introStartPositionChanged,
this,
&AutoDJProcessor::playerIntroStartChanged);
connect(pLeftDeck,
&DeckAttributes::introEndPositionChanged,
this,
&AutoDJProcessor::playerIntroEndChanged);
connect(pRightDeck,
&DeckAttributes::introEndPositionChanged,
this,
&AutoDJProcessor::playerIntroEndChanged);
connect(pLeftDeck,
&DeckAttributes::outroStartPositionChanged,
this,
&AutoDJProcessor::playerOutroStartChanged);
connect(pRightDeck,
&DeckAttributes::outroStartPositionChanged,
this,
&AutoDJProcessor::playerOutroStartChanged);
connect(pLeftDeck,
&DeckAttributes::outroEndPositionChanged,
this,
&AutoDJProcessor::playerOutroEndChanged);
connect(pRightDeck,
&DeckAttributes::outroEndPositionChanged,
this,
&AutoDJProcessor::playerOutroEndChanged);
connect(pLeftDeck,
&DeckAttributes::trackLoaded,
this,
&AutoDJProcessor::playerTrackLoaded);
connect(pRightDeck,
&DeckAttributes::trackLoaded,
this,
&AutoDJProcessor::playerTrackLoaded);
connect(pLeftDeck,
&DeckAttributes::loadingTrack,
this,
&AutoDJProcessor::playerLoadingTrack);
connect(pRightDeck,
&DeckAttributes::loadingTrack,
this,
&AutoDJProcessor::playerLoadingTrack);
connect(pLeftDeck,
&DeckAttributes::playerEmpty,
this,
&AutoDJProcessor::playerEmpty);
connect(pRightDeck,
&DeckAttributes::playerEmpty,
this,
&AutoDJProcessor::playerEmpty);
connect(pLeftDeck,
&DeckAttributes::rateChanged,
this,
&AutoDJProcessor::playerRateChanged);
connect(pRightDeck,
&DeckAttributes::rateChanged,
this,
&AutoDJProcessor::playerRateChanged);
connect(pLeftDeck,
&DeckAttributes::orientationChanged,
this,
&AutoDJProcessor::playerOrientationChanged);
connect(pRightDeck,
&DeckAttributes::orientationChanged,
this,
&AutoDJProcessor::playerOrientationChanged);
connect(m_pAutoDJTableModel,
&PlaylistTableModel::firstTrackChanged,
this,
&AutoDJProcessor::playlistFirstTrackChanged);
if (!leftDeckPlaying && !rightDeckPlaying) {
// Both decks are stopped. Load a track into deck 1 and start it
// playing. Instruct playerPositionChanged to wait for a
// playposition update from deck 1. playerPositionChanged for
// ADJ_ENABLE_P1LOADED will set the crossfader left and remove the
// loaded track from the queue and wait for the next call to
// playerPositionChanged for deck1 after the track is loaded.
m_eState = ADJ_ENABLE_P1LOADED;
// Move crossfader to the left.
setCrossfader(-1.0);
// Load track into the left deck and play. Once it starts playing,
// we will receive a playerPositionChanged update for deck 1 which
// will load a track into the right deck and switch to IDLE mode.
emitLoadTrackToPlayer(nextTrack, pLeftDeck->group, true);
} else {
// One of the two decks is playing. Switch into IDLE mode and wait
// until the playing deck crosses posThreshold to start fading.
m_eState = ADJ_IDLE;
if (leftDeckPlaying) {
// Load track into the right deck.
emitLoadTrackToPlayer(nextTrack, pRightDeck->group, false);
// Move crossfader to the left.
setCrossfader(-1.0);
} else {
// Load track into the left deck.
emitLoadTrackToPlayer(nextTrack, pLeftDeck->group, false);
// Move crossfader to the right.
setCrossfader(1.0);
}
}
emitAutoDJStateChanged(m_eState);
} else { // Disable Auto DJ
m_enabledAutoDJ.setAndConfirm(0.0);
qDebug() << "Auto DJ disabled";
m_eState = ADJ_DISABLED;
disconnect(&m_coCrossfader,
&ControlProxy::valueChanged,
this,
&AutoDJProcessor::crossfaderChanged);
for (const auto& pDeck : m_decks) {
pDeck->disconnect(this);
}
if (m_pConfig->getValue<bool>(ConfigKey(kPreferenceGroup,
QStringLiteral("center_xfader_when_disabling")))) {
m_coCrossfader.set(0);
}
emitAutoDJStateChanged(m_eState);
}
return ADJ_OK;
}
void AutoDJProcessor::controlEnableChangeRequest(double value) {
toggleAutoDJ(value > 0.0);
}
void AutoDJProcessor::controlFadeNow(double value) {
if (value > 0.0) {
fadeNow();
}
}
void AutoDJProcessor::controlShuffle(double value) {
if (value > 0.0) {
shufflePlaylist(QModelIndexList());
}
}
void AutoDJProcessor::controlSkipNext(double value) {
if (value > 0.0) {
skipNext();
}
}
void AutoDJProcessor::controlAddRandomTrack(double value) {
if (value > 0.0) {
emit randomTrackRequested(1);
}
}
void AutoDJProcessor::crossfaderChanged(double value) {
if (m_eState == ADJ_IDLE) {
// The user is changing the crossfader manually. If the user has
// moved it all the way to the other side, make the deck faded away
// from the new "to deck" by loading the next track into it.
DeckAttributes* pFromDeck = getFromDeck();
VERIFY_OR_DEBUG_ASSERT(pFromDeck) {
// we have always a from deck in case of state IDLE
return;
}
DeckAttributes* pToDeck = getOtherDeck(pFromDeck);
if (!pToDeck) {
// we have always a from deck in case of state IDLE
// if the user has not changed the deck orientation
return;
}
double crossfaderPosition = value * (m_coCrossfaderReverse.toBool() ? -1 : 1);
if ((crossfaderPosition == 1.0 && pFromDeck->isLeft()) || // crossfader right
(crossfaderPosition == -1.0 && pFromDeck->isRight())) { // crossfader left
if (!pToDeck->isPlaying()) {
if (getEndSecond(pToDeck) >= kMinimumTrackDurationSec) {
// Re-cue the track if the user has seeked it to the very end
if (pToDeck->playPosition() >= pToDeck->fadeBeginPos) {
pToDeck->setPlayPosition(pToDeck->startPos);
}
pToDeck->play();
} else {
// Track in toDeck was ejected manually, stop.
toggleAutoDJ(false);
return;
}
}
pFromDeck->stop();
// Now that we have started the other deck playing, remove the track
// that was "on deck" from the top of the queue.
removeLoadedTrackFromTopOfQueue(*pToDeck);
loadNextTrackFromQueue(*pFromDeck);
}
}
}
void AutoDJProcessor::playerPositionChanged(DeckAttributes* pAttributes,
double thisPlayPosition) {
// qDebug() << "player" << pAttributes->group << "PositionChanged(" << value << ")";
if (m_eState == ADJ_DISABLED) {
// nothing to do
return;
}
DeckAttributes* thisDeck = pAttributes;
DeckAttributes* otherDeck = getOtherDeck(thisDeck);
if (!otherDeck) {
// This happens if this deck has no orientation or
// there is no deck with the opposite orientation
return;
}
// Note: this can be a delayed call of playerPositionChanged() where
// the track was playing, but is now stopped.
bool thisDeckPlaying = thisDeck->isPlaying();
bool otherDeckPlaying = otherDeck->isPlaying();
// To switch out of ADJ_ENABLE_P1LOADED we wait for a playposition update
// for either deck.
if (m_eState == ADJ_ENABLE_P1LOADED) {
DeckAttributes* leftDeck;
DeckAttributes* rightDeck;
if (thisDeck->isLeft()) {
leftDeck = thisDeck;
DEBUG_ASSERT(otherDeck->isRight());
rightDeck = otherDeck;
} else {
DEBUG_ASSERT(thisDeck->isRight());
rightDeck = thisDeck;
DEBUG_ASSERT(otherDeck->isLeft());
leftDeck = otherDeck;
}
// Note: If a playing deck has reached the end the play state is already reset
bool leftDeckPlaying = leftDeck->isPlaying();
bool rightDeckPlaying = rightDeck->isPlaying();
bool leftDeckReachesEnd = thisDeck->isLeft() && thisPlayPosition >= 1.0;
if (leftDeckPlaying || rightDeckPlaying || leftDeckReachesEnd) {
// One of left and right is playing. Switch to IDLE mode and make
// sure our thresholds are configured (by calling calculateFadeThresholds
// for the playing deck).
m_eState = ADJ_IDLE;
if (!rightDeckPlaying) {
// Only left deck playing!
// In ADJ_ENABLE_P1LOADED mode we wait until the left deck
// successfully starts playing. We don't know in toggleAutoDJ
// whether the track will load successfully so we have to
// wait. If the track fails to load then playerTrackLoadFailed
// will remove it from the top of the queue and request another
// track. Remove the left deck's current track from the queue
// since it is the track we requested in toggleAutoDJ.
removeLoadedTrackFromTopOfQueue(*leftDeck);
// Load the next track into the right player since it is not
// playing.
loadNextTrackFromQueue(*rightDeck);
// Note: calculateTransition() is called in playerTrackLoaded()
} else {
// At least right deck is playing
// Set crossfade thresholds for right deck.
if constexpr (sDebug) {
qDebug() << this << "playerPositionChanged"
<< "right deck playing";
}
calculateTransition(rightDeck, leftDeck, false);
}
emitAutoDJStateChanged(m_eState);
}
return;
}
// In FADING states, we expect that both tracks are playing.
// Normally the the fading fromDeck stops after the transition is over and
// we need to replace it with a new track from the queue.
if (m_eState == ADJ_LEFT_FADING || m_eState == ADJ_RIGHT_FADING) {
// Once P1 or P2 has stopped switch out of fading mode to idle.
// If the user stops the toDeck during a fade, let the fade continue
// and do not load the next track.
if (!otherDeckPlaying && otherDeck->isFromDeck) {
// Force crossfader all the way to the (non fading) toDeck.
if (m_eState == ADJ_RIGHT_FADING) {
setCrossfader(-1.0);
} else {
setCrossfader(1.0);
}
m_eState = ADJ_IDLE;
// Invalidate threshold calculated for the old otherDeck
// This avoids starting a fade back before the new track is
// loaded into the otherDeck
thisDeck->fadeBeginPos = 1.0;
thisDeck->fadeEndPos = 1.0;
otherDeck->isFromDeck = false;
// Load the next track to otherDeck.
loadNextTrackFromQueue(*otherDeck);
emitAutoDJStateChanged(m_eState);
return;
}
}
if (m_eState == ADJ_IDLE) {
if (!thisDeckPlaying && thisPlayPosition < 1) {
// this is a cueing seek, recalculate the transition, from the
// new position.
// This can be our own seek to startPos or a random seek by a user.
// we need to call calculateTransition() because we are not sure.
// If using the full track mode with a transition time of 0,
// thisDeckPlaying will be false but the transition should not be
// recalculated here.
// Don't adjust transition when reaching the end. In this case it is
// always stopped.
if constexpr (sDebug) {
qDebug() << this << "playerPositionChanged"
<< "cueing seek";
}
calculateTransition(otherDeck, thisDeck, false);
} else if (thisDeck->isRepeat()) {
// repeat pauses auto DJ
return;
}
}
// If we are past this deck's posThreshold then:
// - transition into fading mode, play the other deck and fade to it.
// - check if fading is done and stop the deck
// - update the crossfader
if (thisPlayPosition >= thisDeck->fadeBeginPos && thisDeck->isFromDeck && !otherDeck->loading) {
if (m_eState == ADJ_IDLE) {
if (thisDeckPlaying || thisPlayPosition >= 1.0) {
// Set the state as FADING.
m_eState = thisDeck->isLeft() ? ADJ_LEFT_FADING : ADJ_RIGHT_FADING;
m_transitionProgress = 0.0;
emitAutoDJStateChanged(m_eState);
const double toDeckFadeDistance =
(thisDeck->fadeEndPos - thisDeck->fadeBeginPos) *
getEndSecond(thisDeck) / getEndSecond(otherDeck);
// Re-cue the track if the user has seeked forward and will miss the fadeBeginPos
if (otherDeck->playPosition() >= otherDeck->fadeBeginPos - toDeckFadeDistance) {
otherDeck->setPlayPosition(otherDeck->startPos);
}
if (m_crossfaderStartCenter) {
setCrossfader(0.0);
} else if (thisDeck->fadeBeginPos >= thisDeck->fadeEndPos) {
setCrossfader(thisDeck->isLeft() ? 1.0 : -1.0);
}
if (!otherDeckPlaying) {
otherDeck->play();
}
// Now that we have started the other deck playing, remove the track
// that was "on deck" from the top of the queue.
// Note: This is a DB call and takes long.
removeLoadedTrackFromTopOfQueue(*otherDeck);
} else {
if constexpr (sDebug) {
qDebug() << this << "playerPositionChanged()"
<< pAttributes->group << thisPlayPosition
<< "but not playing";
}
}
}
double crossfaderTarget;
if (m_eState == ADJ_LEFT_FADING) {
crossfaderTarget = 1.0;
} else if (m_eState == ADJ_RIGHT_FADING) {
crossfaderTarget = -1.0;
} else {
// this happens if the not playing track is cued into the outro region,
// calculated for the swapped roles.
return;
}
double currentCrossfader = getCrossfader();
if (currentCrossfader == crossfaderTarget) {
// We are done, the fading (from) track is silenced.
// We don't handle mode switches here since that's handled by
// the next playerPositionChanged call otherDeck (see the
// P1/P2FADING case above).
thisDeck->stop();
m_transitionProgress = 1.0;
// Note: If the user has stopped the toDeck during the transition.
// this deck just stops as well. In this case a stopped AutoDJ is accepted
// because the use did it intentionally
} else {
// We are in Fading state.
// Calculate the current transitionProgress, the place between begin
// and end position and the step we have taken since the last call
double transitionProgress = (thisPlayPosition - thisDeck->fadeBeginPos) /
(thisDeck->fadeEndPos - thisDeck->fadeBeginPos);
double transitionStep = transitionProgress - m_transitionProgress;
if (transitionStep > 0.0) {
// We have made progress.
// Backward seeks pause the transitions; forward seeks speed up
// the transitions. If there has been a seek beyond endPos, end
// the transition immediately.
double remainingCrossfader = crossfaderTarget - currentCrossfader;
double adjustment = remainingCrossfader /
(1.0 - m_transitionProgress) * transitionStep;
// we move the crossfader linearly with
// movements in this track's play position.
setCrossfader(currentCrossfader + adjustment);
}
m_transitionProgress = transitionProgress;
// if we are at 1.0 here, we need an additional callback until the last
// step is processed and we can stop the deck.
}
}
}
TrackPointer AutoDJProcessor::getNextTrackFromQueue() {
// Get the track at the top of the playlist.
bool randomQueueEnabled = m_pConfig->getValue<bool>(
ConfigKey(kPreferenceGroup, QStringLiteral("EnableRandomQueue")));
int minAutoDJCrateTracks =
m_pConfig->getValueString(ConfigKey(kPreferenceGroup,
QStringLiteral("RandomQueueMinimumAllowed")))
.toInt();
int tracksToAdd = minAutoDJCrateTracks - m_pAutoDJTableModel->rowCount();
// In case we start off with < minimum tracks
if (randomQueueEnabled && (tracksToAdd > 0)) {
emit randomTrackRequested(tracksToAdd);
}
while (true) {
TrackPointer pNextTrack = m_pAutoDJTableModel->getTrack(
m_pAutoDJTableModel->index(0, 0));
if (pNextTrack) {
if (pNextTrack->getFileInfo().checkFileExists()) {
return pNextTrack;
} else {
// Remove missing track from auto DJ playlist.
qWarning() << "Auto DJ: Skip missing track" << pNextTrack->getLocation();
m_pAutoDJTableModel->removeTrack(
m_pAutoDJTableModel->index(0, 0));
// Don't "Requeue" missing tracks to avoid andless loops
maybeFillRandomTracks();
}
} else {
// We're out of tracks. Return the null TrackPointer.
return pNextTrack;
}
}
}
bool AutoDJProcessor::loadNextTrackFromQueue(const DeckAttributes& deck, bool play) {
TrackPointer nextTrack = getNextTrackFromQueue();
// We ran out of tracks in the queue.
if (!nextTrack) {
// Disable AutoDJ.
toggleAutoDJ(false);
// And eject track (nextTrack is null) as "End of auto DJ warning"
emitLoadTrackToPlayer(nextTrack, deck.group, false);
return false;
}
emitLoadTrackToPlayer(nextTrack, deck.group, play);
return true;
}
bool AutoDJProcessor::removeLoadedTrackFromTopOfQueue(const DeckAttributes& deck) {
return removeTrackFromTopOfQueue(deck.getLoadedTrack());
}
bool AutoDJProcessor::removeTrackFromTopOfQueue(TrackPointer pTrack) {
// No track to test for.
if (!pTrack) {
return false;
}
TrackId trackId(pTrack->getId());
// Loaded track is not a library track.
if (!trackId.isValid()) {
return false;
}
// Get the track id at the top of the playlist.
TrackId nextId(m_pAutoDJTableModel->getTrackId(
m_pAutoDJTableModel->index(0, 0)));
// No track at the top of the queue.
if (!nextId.isValid()) {
return false;
}
// If the loaded track is not the next track in the queue then do nothing.
if (trackId != nextId) {
return false;
}
// Remove the top track.
m_pAutoDJTableModel->removeTrack(m_pAutoDJTableModel->index(0, 0));
// Re-queue if configured.
if (m_pConfig->getValueString(ConfigKey(kPreferenceGroup, QStringLiteral("Requeue"))).toInt()) {
m_pAutoDJTableModel->appendTrack(nextId);
}
maybeFillRandomTracks();
return true;
}
void AutoDJProcessor::maybeFillRandomTracks() {
int minAutoDJCrateTracks =
m_pConfig->getValueString(ConfigKey(kPreferenceGroup,
QStringLiteral("RandomQueueMinimumAllowed")))
.toInt();
bool randomQueueEnabled =
m_pConfig->getValueString(
ConfigKey(kPreferenceGroup,
QStringLiteral("EnableRandomQueue")))
.toInt() == 1;
int tracksToAdd = minAutoDJCrateTracks - m_pAutoDJTableModel->rowCount();
if (randomQueueEnabled && (tracksToAdd > 0)) {
qDebug() << "Randomly adding tracks";
emit randomTrackRequested(tracksToAdd);
}
}
void AutoDJProcessor::playerPlayChanged(DeckAttributes* thisDeck, bool playing) {
if constexpr (sDebug) {
qDebug() << this << "playerPlayChanged" << thisDeck->group << playing;
}
if (m_eState != ADJ_IDLE) {
// We don't want to recalculate a running transition
return;
}
if (thisDeck->loading) {
// Note: When loading a new deck this signal arrives before the
// playerTrackLoaded();
return;
}
DeckAttributes* otherDeck = getOtherDeck(thisDeck);
if (!otherDeck) {
// This happens if all decks have center orientation
return;
}
if (playing) {
if (!otherDeck->isPlaying()) {
// In case both decks were stopped and now this one just started, make
// this deck the "from deck".
calculateTransition(thisDeck, getOtherDeck(thisDeck), false);
}
} else {
// Deck paused
// This may happen if the user has previously pressed play on the "to deck"
// before fading, for example to adjust the intro/outro cues, and lets the
// deck play until the end, seek back to the start point instead of keeping
if (thisDeck->playPosition() >= 1.0 && !thisDeck->isFromDeck) {
// toDeck has stopped at the end. Recalculate the transition, because
// it has been done from a now irrelevant previous position.
// This forces the other deck to be the fromDeck.
thisDeck->startPos = kKeepPosition;
calculateTransition(otherDeck, thisDeck, true);
if (thisDeck->startPos != kKeepPosition) {
// Note: this seek will trigger the playerPositionChanged slot
// which may calls the calculateTransition() again without seek = true;
thisDeck->setPlayPosition(thisDeck->startPos);
}
}
}
}
void AutoDJProcessor::playerIntroStartChanged(DeckAttributes* pAttributes, double position) {
if constexpr (sDebug) {
qDebug() << this << "playerIntroStartChanged" << pAttributes->group << position;
}
// nothing to do, because we want not to re-cue the toDeck and the from
// Deck has already passed the intro
}
void AutoDJProcessor::playerIntroEndChanged(DeckAttributes* pAttributes, double position) {
if constexpr (sDebug) {
qDebug() << this << "playerIntroEndChanged" << pAttributes->group << position;
}
if (m_eState != ADJ_IDLE) {
// We don't want to recalculate a running transition
return;
}
if (pAttributes->isFromDeck) {
// We have already passed the intro
return;
}
DeckAttributes* fromDeck = getFromDeck();
if (!fromDeck) {
return;
}
calculateTransition(fromDeck, getOtherDeck(fromDeck), false);