-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
autodjprocessor.cpp
894 lines (771 loc) · 33 KB
/
autodjprocessor.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
#include "library/autodj/autodjprocessor.h"
#include "library/trackcollection.h"
#include "control/controlpushbutton.h"
#include "control/controlproxy.h"
#include "util/math.h"
#include "mixer/playermanager.h"
#include "mixer/basetrackplayer.h"
#define kConfigKey "[Auto DJ]"
const char* kTransitionPreferenceName = "Transition";
const double kTransitionPreferenceDefault = 10.0;
static const bool sDebug = false;
DeckAttributes::DeckAttributes(int index,
BaseTrackPlayer* pPlayer,
EngineChannel::ChannelOrientation orientation)
: index(index),
group(pPlayer->getGroup()),
posThreshold(1.0),
fadeDuration(0.0),
m_orientation(orientation),
m_playPos(group, "playposition"),
m_play(group, "play"),
m_repeat(group, "repeat"),
m_pPlayer(pPlayer) {
connect(m_pPlayer, SIGNAL(newTrackLoaded(TrackPointer)),
this, SLOT(slotTrackLoaded(TrackPointer)));
connect(m_pPlayer, SIGNAL(loadingTrack(TrackPointer, TrackPointer)),
this, SLOT(slotLoadingTrack(TrackPointer, TrackPointer)));
connect(m_pPlayer, SIGNAL(playerEmpty()),
this, SLOT(slotPlayerEmpty()));
m_playPos.connectValueChanged(this, SLOT(slotPlayPosChanged(double)));
m_play.connectValueChanged(this, SLOT(slotPlayChanged(double)));
}
DeckAttributes::~DeckAttributes() {
}
void DeckAttributes::slotPlayChanged(double v) {
emit(playChanged(this, v > 0.0));
}
void DeckAttributes::slotPlayPosChanged(double v) {
emit(playPositionChanged(this, v));
}
void DeckAttributes::slotTrackLoaded(TrackPointer pTrack) {
emit(trackLoaded(this, pTrack));
}
void DeckAttributes::slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack) {
//qDebug() << "DeckAttributes::slotLoadingTrack";
emit(loadingTrack(this, pNewTrack, pOldTrack));
}
void DeckAttributes::slotPlayerEmpty() {
emit(playerEmpty(this));
}
TrackPointer DeckAttributes::getLoadedTrack() const {
return m_pPlayer != NULL ? m_pPlayer->getLoadedTrack() : TrackPointer();
}
AutoDJProcessor::AutoDJProcessor(QObject* pParent,
UserSettingsPointer pConfig,
PlayerManagerInterface* pPlayerManager,
int iAutoDJPlaylistId,
TrackCollection* pTrackCollection)
: QObject(pParent),
m_pConfig(pConfig),
m_pPlayerManager(pPlayerManager),
m_pAutoDJTableModel(NULL),
m_eState(ADJ_DISABLED),
m_transitionTime(kTransitionPreferenceDefault),
m_nextTransitionTime(kTransitionPreferenceDefault) {
m_pAutoDJTableModel = new PlaylistTableModel(this, pTrackCollection,
"mixxx.db.model.autodj");
m_pAutoDJTableModel->setTableModel(iAutoDJPlaylistId);
m_pShufflePlaylist = new ControlPushButton(
ConfigKey("[AutoDJ]", "shuffle_playlist"));
connect(m_pShufflePlaylist, SIGNAL(valueChanged(double)),
this, SLOT(controlShuffle(double)));
m_pSkipNext = new ControlPushButton(
ConfigKey("[AutoDJ]", "skip_next"));
connect(m_pSkipNext, SIGNAL(valueChanged(double)),
this, SLOT(controlSkipNext(double)));
m_pFadeNow = new ControlPushButton(
ConfigKey("[AutoDJ]", "fade_now"));
connect(m_pFadeNow, SIGNAL(valueChanged(double)),
this, SLOT(controlFadeNow(double)));
m_pEnabledAutoDJ = new ControlPushButton(
ConfigKey("[AutoDJ]", "enabled"));
m_pEnabledAutoDJ->setButtonMode(ControlPushButton::TOGGLE);
connect(m_pEnabledAutoDJ, SIGNAL(valueChanged(double)),
this, SLOT(controlEnable(double)));
// TODO(rryan) listen to signals from PlayerManager and add/remove as decks
// are created.
for (unsigned int i = 0; i < pPlayerManager->numberOfDecks(); ++i) {
QString group = PlayerManager::groupForDeck(i);
BaseTrackPlayer* pPlayer = pPlayerManager->getPlayer(group);
// Shouldn't be possible.
if (pPlayer == NULL) {
qWarning() << "PROGRAMMING ERROR deck does not exist" << i;
continue;
}
EngineChannel::ChannelOrientation orientation =
(i % 2 == 0) ? EngineChannel::LEFT : EngineChannel::RIGHT;
m_decks.append(new DeckAttributes(i, pPlayer, orientation));
}
// Auto-DJ needs at least two decks
DEBUG_ASSERT(m_decks.length() > 1);
m_pCOCrossfader = new ControlProxy("[Master]", "crossfader");
m_pCOCrossfaderReverse = new ControlProxy("[Mixer Profile]", "xFaderReverse");
QString str_autoDjTransition = m_pConfig->getValueString(
ConfigKey(kConfigKey, kTransitionPreferenceName));
if (!str_autoDjTransition.isEmpty()) {
m_transitionTime = str_autoDjTransition.toDouble();
m_nextTransitionTime = m_transitionTime;
}
}
AutoDJProcessor::~AutoDJProcessor() {
qDeleteAll(m_decks);
m_decks.clear();
delete m_pCOCrossfader;
delete m_pCOCrossfaderReverse;
delete m_pSkipNext;
delete m_pShufflePlaylist;
delete m_pEnabledAutoDJ;
delete m_pFadeNow;
delete m_pAutoDJTableModel;
}
double AutoDJProcessor::getCrossfader() const {
if (m_pCOCrossfaderReverse->toBool()) {
return m_pCOCrossfader->get() * -1.0;
}
return m_pCOCrossfader->get();
}
void AutoDJProcessor::setCrossfader(double value, bool right) {
if (m_pCOCrossfaderReverse->get() > 0.0) {
value *= -1.0;
right = !right;
}
double current_value = m_pCOCrossfader->get();
if (right) {
// ignore if we move slider left
if (value > current_value) {
m_pCOCrossfader->set(value);
}
} else {
// ignore if we move slider right
if (value < current_value) {
m_pCOCrossfader->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() {
// Auto-DJ needs at least two decks
DEBUG_ASSERT_AND_HANDLE(m_decks.length() > 1) {
return;
}
if (m_eState != ADJ_IDLE) {
// we cannot fade if AutoDj is disabled or already fading
return;
}
double crossfader = getCrossfader();
DeckAttributes& leftDeck = *m_decks[0];
DeckAttributes& rightDeck = *m_decks[1];
if (leftDeck.isPlaying() &&
(!rightDeck.isPlaying() || crossfader < 0.0)) {
// Make sure leftDeck.fadeDuration is up to date.
calculateTransition(&leftDeck, &rightDeck);
// override posThreshold to start fade now
leftDeck.posThreshold = leftDeck.playPosition() -
((crossfader + 1.0) / 2 * (leftDeck.fadeDuration));
// Repeat is disabled by FadeNow but disables auto Fade
leftDeck.setRepeat(false);
} else if (rightDeck.isPlaying()) {
// Make sure rightDeck.fadeDuration is up to date.
calculateTransition(&rightDeck, &leftDeck);
// override posThreshold to start fade now
rightDeck.posThreshold = rightDeck.playPosition() -
((1.0 - crossfader) / 2 * (rightDeck.fadeDuration));
// Repeat is disabled by FadeNow but disables auto Fade
rightDeck.setRepeat(false);
}
//else {
// No deck playing, do not know what to do
}
AutoDJProcessor::AutoDJError AutoDJProcessor::skipNext() {
if (m_eState == ADJ_DISABLED) {
return ADJ_IS_INACTIVE;
}
// Auto-DJ needs at least two decks
DEBUG_ASSERT_AND_HANDLE(m_decks.length() > 1) {
return ADJ_NOT_TWO_DECKS;
}
// Load the next song from the queue.
DeckAttributes& leftDeck = *m_decks[0];
DeckAttributes& rightDeck = *m_decks[1];
if (!leftDeck.isPlaying()) {
removeLoadedTrackFromTopOfQueue(leftDeck);
loadNextTrackFromQueue(leftDeck);
} else if (!rightDeck.isPlaying()) {
removeLoadedTrackFromTopOfQueue(rightDeck);
loadNextTrackFromQueue(rightDeck);
}
return ADJ_OK;
}
AutoDJProcessor::AutoDJError AutoDJProcessor::toggleAutoDJ(bool enable) {
DeckAttributes& leftDeck = *m_decks[0];
DeckAttributes& rightDeck = *m_decks[1];
bool deck1Playing = leftDeck.isPlaying();
bool deck2Playing = rightDeck.isPlaying();
if (enable) { // Enable Auto DJ
if (deck1Playing && deck2Playing) {
qDebug() << "One deck must be stopped before enabling Auto DJ mode";
// Keep the current state.
emitAutoDJStateChanged(m_eState);
return ADJ_BOTH_DECKS_PLAYING;
}
// TODO: This is a total bandaid for making Auto DJ work with decks 3
// and 4. We should design a nicer way to handle this.
for (int i = 2; i < m_decks.length(); ++i) {
if (m_decks[i] != NULL && m_decks[i]->isPlaying()) {
return ADJ_DECKS_3_4_PLAYING;
}
}
// Never load the same track if it is already playing
if (deck1Playing) {
removeLoadedTrackFromTopOfQueue(leftDeck);
}
if (deck2Playing) {
removeLoadedTrackFromTopOfQueue(rightDeck);
}
TrackPointer nextTrack = getNextTrackFromQueue();
if (!nextTrack) {
qDebug() << "Queue is empty now";
if (m_pEnabledAutoDJ->get() != 0.0) {
m_pEnabledAutoDJ->set(0.0);
}
emitAutoDJStateChanged(m_eState);
return ADJ_QUEUE_EMPTY;
}
// Track is available so GO
if (m_pEnabledAutoDJ->get() != 1.0) {
m_pEnabledAutoDJ->set(1.0);
}
qDebug() << "Auto DJ enabled";
connect(&leftDeck, SIGNAL(playPositionChanged(DeckAttributes*, double)),
this, SLOT(playerPositionChanged(DeckAttributes*, double)));
connect(&rightDeck, SIGNAL(playPositionChanged(DeckAttributes*, double)),
this, SLOT(playerPositionChanged(DeckAttributes*, double)));
connect(&leftDeck, SIGNAL(playChanged(DeckAttributes*, bool)),
this, SLOT(playerPlayChanged(DeckAttributes*, bool)));
connect(&rightDeck, SIGNAL(playChanged(DeckAttributes*, bool)),
this, SLOT(playerPlayChanged(DeckAttributes*, bool)));
connect(&leftDeck, SIGNAL(trackLoaded(DeckAttributes*, TrackPointer)),
this, SLOT(playerTrackLoaded(DeckAttributes*, TrackPointer)));
connect(&rightDeck, SIGNAL(trackLoaded(DeckAttributes*, TrackPointer)),
this, SLOT(playerTrackLoaded(DeckAttributes*, TrackPointer)));
connect(&leftDeck, SIGNAL(loadingTrack(DeckAttributes*, TrackPointer, TrackPointer)),
this, SLOT(playerLoadingTrack(DeckAttributes*, TrackPointer, TrackPointer)));
connect(&rightDeck, SIGNAL(loadingTrack(DeckAttributes*, TrackPointer, TrackPointer)),
this, SLOT(playerLoadingTrack(DeckAttributes*, TrackPointer, TrackPointer)));
connect(&leftDeck, SIGNAL(playerEmpty(DeckAttributes*)),
this, SLOT(playerEmpty(DeckAttributes*)));
connect(&rightDeck, SIGNAL(playerEmpty(DeckAttributes*)),
this, SLOT(playerEmpty(DeckAttributes*)));
if (!deck1Playing && !deck2Playing) {
// 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, false);
// 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, leftDeck.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 (deck1Playing) {
// Update fade thresholds for the left deck.
calculateTransition(&leftDeck, &rightDeck);
// Load track into the right deck.
emitLoadTrackToPlayer(nextTrack, rightDeck.group, false);
} else {
// Update fade thresholds for the right deck.
calculateTransition(&rightDeck, &leftDeck);
// Load track into the left deck.
emitLoadTrackToPlayer(nextTrack, leftDeck.group, false);
}
}
emitAutoDJStateChanged(m_eState);
} else { // Disable Auto DJ
if (m_pEnabledAutoDJ->get() != 0.0) {
m_pEnabledAutoDJ->set(0.0);
}
qDebug() << "Auto DJ disabled";
m_eState = ADJ_DISABLED;
leftDeck.disconnect(this);
rightDeck.disconnect(this);
m_pCOCrossfader->set(0);
emitAutoDJStateChanged(m_eState);
}
return ADJ_OK;
}
void AutoDJProcessor::controlEnable(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::playerPositionChanged(DeckAttributes* pAttributes,
double thisPlayPosition) {
if (sDebug) {
qDebug() << this << "playerPositionChanged" << pAttributes->group
<< thisPlayPosition;
}
// Auto-DJ needs at least two decks
DEBUG_ASSERT_AND_HANDLE(m_decks.length() > 1) {
return;
}
// 95% playback is when we crossfade and do stuff
// const double posThreshold = 0.95;
// 0.05; // 5% playback is crossfade duration
const double thisFadeDuration = pAttributes->fadeDuration;
// qDebug() << "player" << pAttributes->group << "PositionChanged(" << value << ")";
if (m_eState == ADJ_DISABLED) {
// nothing to do
return;
}
DeckAttributes& thisDeck = *pAttributes;
// prefer to fade to deck 0
int otherDeckIndex = 0;
if (thisDeck.index == 0) {
otherDeckIndex = 1;
}
DeckAttributes& otherDeck = *m_decks[otherDeckIndex];
DeckAttributes& leftDeck = *m_decks[0];
DeckAttributes& rightDeck = *m_decks[1];
bool leftDeckPlaying = leftDeck.isPlaying();
bool rightDeckPlaying = rightDeck.isPlaying();
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) {
if (leftDeckPlaying || rightDeckPlaying) {
// 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);
// Set crossfade thresholds for left deck.
calculateTransition(&leftDeck, &rightDeck);
} else {
// At least right Deck is playing
// Set crossfade thresholds for right deck.
calculateTransition(&rightDeck, &leftDeck);
}
emitAutoDJStateChanged(m_eState);
}
return;
}
// In P1FADING/P2FADING states, we are waiting for the deck we are fading
// from (P1 or P2) to stop playing. Once we are playing and the other deck
// is not playing -- we switch the crossfader fully to this deck's side,
// switch to IDLE mode and load the next track into the other deck.
if ((m_eState == ADJ_P1FADING && thisDeck.isRight()) ||
(m_eState == ADJ_P2FADING && thisDeck.isLeft())) {
// Once P1 or P2 has stopped switch out of fading mode to idle.
if (thisDeckPlaying && !otherDeckPlaying) {
// Force crossfader all the way to this side.
if (thisDeck.isLeft()) {
setCrossfader(-1.0, false);
} else {
setCrossfader(1.0, true);
}
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.posThreshold = 1.0;
thisDeck.fadeDuration = 0.0;
// Load the next track to otherDeck.
loadNextTrackFromQueue(otherDeck);
emitAutoDJStateChanged(m_eState);
}
return;
}
if (m_eState == ADJ_IDLE && 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 we've passed the fade end point and stop the deck
// - update the crossfader
// TODO(rryan): We need to investigate the chain of events that occur when
// the track we are fading to crosses its posThreshold before we are done
// fading to it.
if (thisPlayPosition >= thisDeck.posThreshold) {
if (m_eState == ADJ_IDLE && (thisDeckPlaying ||
thisDeck.posThreshold >= 1.0)) {
if (!otherDeckPlaying) {
// Setup the other deck's fade thresholds (since we use the
// fadeDuration next).
calculateTransition(&otherDeck, &thisDeck);
// For negative fade durations, jump back to insert a pause
// between the tracks.
// Note: This overrides the cue position
if (thisFadeDuration < 0.0) {
// Note: since the fade duration is relative to the track
// length, we need to use the other deck fade duration
// which is negative as well
otherDeck.setPlayPosition(otherDeck.fadeDuration);
} else {
// Guard against very short other tracks, or CUE points and
// seeks near end of track.
// The other track must not be finished before this track,
// otherwise Auto-DJ stops
double otherDeckPlaypos = otherDeck.playPosition();
// We need 2 x fadeDuration, to fade in and out
double maxPlaypos = 1.0 - (otherDeck.fadeDuration * 2);
if (maxPlaypos < otherDeckPlaypos) {
otherDeck.setPlayPosition(maxPlaypos);
}
}
otherDeck.play();
}
// Now that we have started the other deck playing, remove the track
// that was "on deck" from the top of the queue.
removeLoadedTrackFromTopOfQueue(otherDeck);
// Set the state as FADING.
m_eState = thisDeck.isLeft() ? ADJ_P1FADING : ADJ_P2FADING;
emitAutoDJStateChanged(m_eState);
}
double posFadeEnd = math_min(1.0, thisDeck.posThreshold + thisFadeDuration);
if (thisPlayPosition >= posFadeEnd) {
// If this track has passed the end of its target fade then we stop
// it. 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();
} else {
// We are past this deck's posThreshold but before its
// posThreshold+fadeDuration, we move the crossfader linearly with
// movements in this track's play position.
// If thisDeck is left, the new crossfade value is -1 plus the
// adjustment. If thisDeck is right, the new value is 1.0 minus the
// adjustment.
double crossfadeEdgeValue = -1.0;
double adjustment = 2 * (thisPlayPosition - thisDeck.posThreshold) /
(posFadeEnd - thisDeck.posThreshold);
bool isLeft = thisDeck.isLeft();
if (!isLeft) {
crossfadeEdgeValue = 1.0;
adjustment *= -1.0;
}
setCrossfader(crossfadeEdgeValue + adjustment, isLeft);
}
}
}
TrackPointer AutoDJProcessor::getNextTrackFromQueue() {
// Get the track at the top of the playlist.
bool randomQueueEnabled = (((m_pConfig->getValueString(
ConfigKey("[Auto DJ]", "EnableRandomQueue")).toInt())) == 1);
int minAutoDJCrateTracks = m_pConfig->getValueString(
ConfigKey(kConfigKey, "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 nextTrack = m_pAutoDJTableModel->getTrack(
m_pAutoDJTableModel->index(0, 0));
if (nextTrack) {
if (nextTrack->exists()) {
return nextTrack;
} else {
// Remove missing song from auto DJ playlist.
m_pAutoDJTableModel->removeTrack(
m_pAutoDJTableModel->index(0, 0));
}
} else {
// We're out of tracks. Return the null TrackPointer.
return nextTrack;
}
}
}
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) {
// Get loaded track for this group.
TrackPointer loadedTrack = deck.getLoadedTrack();
// No loaded track in this group.
if (loadedTrack.isNull()) {
return false;
}
return removeTrackFromTopOfQueue(loadedTrack);
}
bool AutoDJProcessor::removeTrackFromTopOfQueue(TrackPointer pTrack) {
// No track to test for.
if (pTrack.isNull()) {
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(kConfigKey, "Requeue")).toInt()) {
m_pAutoDJTableModel->appendTrack(nextId);
}
// Fill random tracks if configured
int minAutoDJCrateTracks = m_pConfig->getValueString(
ConfigKey(kConfigKey, "RandomQueueMinimumAllowed")).toInt();
bool randomQueueEnabled = (((m_pConfig->getValueString(
ConfigKey("[Auto DJ]", "EnableRandomQueue")).toInt())) == 1);
int tracksToAdd = minAutoDJCrateTracks - m_pAutoDJTableModel->rowCount();
if (randomQueueEnabled && (tracksToAdd > 0)) {
qDebug() << "Randomly adding tracks";
emit(randomTrackRequested(tracksToAdd));
}
return true;
}
void AutoDJProcessor::playerPlayChanged(DeckAttributes* pAttributes, bool playing) {
if (sDebug) {
qDebug() << this << "playerPlayChanged" << pAttributes->group << playing;
}
// We may want to do more than just calculate fade thresholds when playing
// state changes so keep these two as separate methods for now.
// This will calculate the Transition to the already loaded, in most cases
// already played other track.
// This is required because the user may have loaded a track or changed play
// manually
if (playing) {
calculateTransition(pAttributes, getOtherDeck(pAttributes));
}
}
void AutoDJProcessor::calculateTransition(DeckAttributes* pFromDeck,
DeckAttributes* pToDeck) {
if (pFromDeck == NULL) {
return;
}
if (sDebug) {
qDebug() << this << "calculateFadeThresholds" << pFromDeck->group;
}
//qDebug() << "player" << pAttributes->group << "PlayChanged(" << playing << ")";
// We require ADJ_IDLE to prevent changing the thresholds in the middle of a
// fade.
if (m_eState == ADJ_IDLE) {
TrackPointer fromTrack = pFromDeck->getLoadedTrack();
if (fromTrack) {
// TODO(rryan): Duration is super inaccurate! We should be using
// track_samples / track_samplerate instead.
double fromTrackDuration = fromTrack->getDuration();
qDebug() << fromTrack->getLocation()
<< "fromTrackDuration =" << fromTrackDuration;
// The track might be shorter than the transition period. Use a
// sensible cap.
m_nextTransitionTime = math_min(m_transitionTime,
fromTrackDuration / 2);
if (pToDeck) {
TrackPointer toTrack = pToDeck->getLoadedTrack();
if (toTrack) {
// TODO(rryan): Duration is super inaccurate! We should be using
// track_samples / track_samplerate instead.
double toTrackDuration = toTrack->getDuration();
qDebug() << toTrack->getLocation()
<< "toTrackDuration = " << toTrackDuration;
m_nextTransitionTime = math_min(m_nextTransitionTime,
toTrackDuration / 2);
}
}
if (fromTrackDuration > 0.0) {
pFromDeck->fadeDuration = m_nextTransitionTime / fromTrackDuration;
} else {
pFromDeck->fadeDuration = 0.0;
}
if (m_nextTransitionTime > 0.0) {
pFromDeck->posThreshold = 1.0 - pFromDeck->fadeDuration;
} else {
// in case of pause transition
pFromDeck->posThreshold = 1.0;
}
qDebug() << "m_fadeDuration" << pFromDeck->group << "="
<< pFromDeck->fadeDuration;
}
}
}
void AutoDJProcessor::playerTrackLoaded(DeckAttributes* pDeck, TrackPointer pTrack) {
if (sDebug) {
qDebug() << this << "playerTrackLoaded" << pDeck->group
<< (pTrack.isNull() ? "(null)" : pTrack->getLocation());
}
if (pTrack->getDuration() == 0) {
qWarning() << "Skip track with 0:00 Duration" << pTrack->getLocation();
// Remove Tack with duration < 1 s
removeTrackFromTopOfQueue(pTrack);
// Load the next track. If we are the first AutoDJ track
// (ADJ_ENABLE_P1LOADED state) then play the track.
loadNextTrackFromQueue(*pDeck, m_eState == ADJ_ENABLE_P1LOADED);
} else {
calculateTransition(getOtherDeck(pDeck, true), pDeck);
}
}
void AutoDJProcessor::playerLoadingTrack(DeckAttributes* pDeck,
TrackPointer pNewTrack, TrackPointer pOldTrack) {
if (sDebug) {
qDebug() << this << "playerLoadingTrack" << pDeck->group
<< "new:"<< (pNewTrack.isNull() ? "(null)" : pNewTrack->getLocation())
<< "old:"<< (pOldTrack.isNull() ? "(null)" : pOldTrack->getLocation());
}
// The Deck is loading an new track
// There are four conditions under which we load a track.
// 1) We are enabling AutoDJ and no decks are playing. Mode is
// ADJ_ENABLE_P1LOADED.
// 2) After #1, we load a track into the other deck. Mode is ADJ_IDLE.
// 3) We are enabling AutoDJ and a single deck is playing. Mode is ADJ_IDLE.
// 4) We have just completed fading from one deck to another. Mode is
// ADJ_IDLE.
if (pNewTrack.isNull()) {
// If a track is ejected because of a manual eject command or a load failure
// this track seams to be undesired. Remove the bad track from the queue.
removeTrackFromTopOfQueue(pOldTrack);
// wait until the track is fully unloaded and the playerEmpty()
// slot is called before load an alternative track.
}
}
void AutoDJProcessor::playerEmpty(DeckAttributes* pDeck) {
if (sDebug) {
qDebug() << this << "playerEmpty()" << pDeck->group;
}
// The Deck has ejected a track and no new one is loaded
// This happens if loading fails or the user manually ejected the track
// and would normally stop the AutoDJ flow, which is not desired.
// It should be safe to load a new track from the queue. The only case where
// we request a load-and-play is case #1 currently so we can easily test for
// this based on the mode.
// Load the next track. If we are the first AutoDJ track
// (ADJ_ENABLE_P1LOADED state) then play the track.
loadNextTrackFromQueue(*pDeck, m_eState == ADJ_ENABLE_P1LOADED);
}
void AutoDJProcessor::setTransitionTime(int time) {
if (sDebug) {
qDebug() << this << "setTransitionTime" << time;
}
// Auto-DJ needs at least two decks
DEBUG_ASSERT_AND_HANDLE(m_decks.length() > 1) {
return;
}
// Update the transition time first.
m_pConfig->set(ConfigKey(kConfigKey, kTransitionPreferenceName),
ConfigValue(time));
m_transitionTime = time;
// Then re-calculate fade thresholds for the decks.
if (m_eState == ADJ_IDLE) {
DeckAttributes& leftDeck = *m_decks[0];
DeckAttributes& rightDeck = *m_decks[1];
if (leftDeck.isPlaying()) {
calculateTransition(&leftDeck, &rightDeck);
}
if (rightDeck.isPlaying()) {
calculateTransition(&rightDeck, &leftDeck);
}
}
}
DeckAttributes* AutoDJProcessor::getOtherDeck(DeckAttributes* pThisDeck,
bool playing) {
DeckAttributes* pOtherDeck = NULL;
if (pThisDeck->isLeft()) {
// find first right deck
foreach(DeckAttributes* pDeck, m_decks) {
if (pDeck->isRight()) {
if (!playing || pDeck->isPlaying()) {
pOtherDeck = pDeck;
break;
}
}
}
} else if (pThisDeck->isRight()) {
// find first left deck
foreach(DeckAttributes* pDeck, m_decks) {
if (pDeck->isLeft()) {
if (!playing || pDeck->isPlaying()) {
pOtherDeck = pDeck;
break;
}
}
}
}
return pOtherDeck;
}
bool AutoDJProcessor::nextTrackLoaded() {
if (m_eState == ADJ_DISABLED) {
// AutoDJ always loads the top track (again) if enabled
return false;
}
DeckAttributes& leftDeck = *m_decks[0];
DeckAttributes& rightDeck = *m_decks[1];
bool leftDeckPlaying = leftDeck.isPlaying();
bool rightDeckPlaying = rightDeck.isPlaying();
// Calculate idle deck
TrackPointer loadedTrack;
if (leftDeckPlaying && !rightDeckPlaying) {
loadedTrack = rightDeck.getLoadedTrack();
} else if (!leftDeckPlaying && rightDeckPlaying) {
loadedTrack = leftDeck.getLoadedTrack();
} else if (getCrossfader() < 0.0) {
loadedTrack = rightDeck.getLoadedTrack();
} else {
loadedTrack = leftDeck.getLoadedTrack();
}
return loadedTrack == getNextTrackFromQueue();
}