-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathloopingcontrol.cpp
1471 lines (1293 loc) · 53.2 KB
/
loopingcontrol.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 "engine/controls/loopingcontrol.h"
#include <QtDebug>
#include "control/controlobject.h"
#include "control/controlpushbutton.h"
#include "engine/controls/bpmcontrol.h"
#include "engine/controls/enginecontrol.h"
#include "engine/enginebuffer.h"
#include "moc_loopingcontrol.cpp"
#include "preferences/usersettings.h"
#include "track/track.h"
#include "util/compatibility.h"
#include "util/math.h"
#include "util/sample.h"
double LoopingControl::s_dBeatSizes[] = { 0.03125, 0.0625, 0.125, 0.25, 0.5,
1, 2, 4, 8, 16, 32, 64, 128, 256, 512 };
// Used to generate the beatloop_%SIZE, beatjump_%SIZE, and loop_move_%SIZE CO
// ConfigKeys.
ConfigKey keyForControl(const QString& group, const QString& ctrlName, double num) {
ConfigKey key;
key.group = group;
key.item = ctrlName.arg(num);
return key;
}
// static
QList<double> LoopingControl::getBeatSizes() {
QList<double> result;
for (unsigned int i = 0; i < (sizeof(s_dBeatSizes) / sizeof(s_dBeatSizes[0])); ++i) {
result.append(s_dBeatSizes[i]);
}
return result;
}
LoopingControl::LoopingControl(const QString& group,
UserSettingsPointer pConfig)
: EngineControl(group, pConfig),
m_bLoopingEnabled(false),
m_bLoopRollActive(false),
m_bAdjustingLoopIn(false),
m_bAdjustingLoopOut(false),
m_bAdjustingLoopInOld(false),
m_bAdjustingLoopOutOld(false),
m_bLoopOutPressedWhileLoopDisabled(false) {
m_oldLoopSamples = { kNoTrigger, kNoTrigger, false };
m_loopSamples.setValue(m_oldLoopSamples);
m_currentSample.setValue(0.0);
m_pActiveBeatLoop = nullptr;
m_pRateControl = nullptr;
//Create loop-in, loop-out, loop-exit, and reloop/exit ControlObjects
m_pLoopInButton = new ControlPushButton(ConfigKey(group, "loop_in"));
connect(m_pLoopInButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopIn,
Qt::DirectConnection);
m_pLoopInButton->set(0);
m_pLoopInGotoButton = new ControlPushButton(ConfigKey(group, "loop_in_goto"));
connect(m_pLoopInGotoButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopInGoto);
m_pLoopOutButton = new ControlPushButton(ConfigKey(group, "loop_out"));
connect(m_pLoopOutButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopOut,
Qt::DirectConnection);
m_pLoopOutButton->set(0);
m_pLoopOutGotoButton = new ControlPushButton(ConfigKey(group, "loop_out_goto"));
connect(m_pLoopOutGotoButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopOutGoto);
m_pLoopExitButton = new ControlPushButton(ConfigKey(group, "loop_exit"));
connect(m_pLoopExitButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopExit,
Qt::DirectConnection);
m_pLoopExitButton->set(0);
m_pReloopToggleButton = new ControlPushButton(ConfigKey(group, "reloop_toggle"));
connect(m_pReloopToggleButton, &ControlObject::valueChanged,
this, &LoopingControl::slotReloopToggle,
Qt::DirectConnection);
m_pReloopToggleButton->set(0);
// The old reloop_exit name was confusing. This CO does both entering and exiting.
ControlDoublePrivate::insertAlias(ConfigKey(group, "reloop_exit"),
ConfigKey(group, "reloop_toggle"));
m_pReloopAndStopButton = new ControlPushButton(ConfigKey(group, "reloop_andstop"));
connect(m_pReloopAndStopButton, &ControlObject::valueChanged,
this, &LoopingControl::slotReloopAndStop,
Qt::DirectConnection);
m_pCOLoopEnabled = new ControlObject(ConfigKey(group, "loop_enabled"));
m_pCOLoopEnabled->set(0.0);
m_pCOLoopStartPosition =
new ControlObject(ConfigKey(group, "loop_start_position"));
m_pCOLoopStartPosition->set(kNoTrigger);
connect(m_pCOLoopStartPosition, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopStartPos,
Qt::DirectConnection);
m_pCOLoopEndPosition =
new ControlObject(ConfigKey(group, "loop_end_position"));
m_pCOLoopEndPosition->set(kNoTrigger);
connect(m_pCOLoopEndPosition, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopEndPos,
Qt::DirectConnection);
m_pQuantizeEnabled = ControlObject::getControl(ConfigKey(group, "quantize"));
m_pNextBeat = ControlObject::getControl(ConfigKey(group, "beat_next"));
m_pPreviousBeat = ControlObject::getControl(ConfigKey(group, "beat_prev"));
m_pClosestBeat = ControlObject::getControl(ConfigKey(group, "beat_closest"));
m_pTrackSamples = ControlObject::getControl(ConfigKey(group, "track_samples"));
m_pSlipEnabled = ControlObject::getControl(ConfigKey(group, "slip_enabled"));
// DEPRECATED: Use beatloop_size and beatloop_set instead.
// Activates a beatloop of a specified number of beats.
m_pCOBeatLoop = new ControlObject(ConfigKey(group, "beatloop"), false);
connect(m_pCOBeatLoop, &ControlObject::valueChanged, this,
[=](double value){slotBeatLoop(value);}, Qt::DirectConnection);
m_pCOBeatLoopSize = new ControlObject(ConfigKey(group, "beatloop_size"),
true, false, false, 4.0);
m_pCOBeatLoopSize->connectValueChangeRequest(this,
&LoopingControl::slotBeatLoopSizeChangeRequest, Qt::DirectConnection);
m_pCOBeatLoopActivate = new ControlPushButton(ConfigKey(group, "beatloop_activate"));
connect(m_pCOBeatLoopActivate, &ControlObject::valueChanged,
this, &LoopingControl::slotBeatLoopToggle);
m_pCOBeatLoopRollActivate = new ControlPushButton(ConfigKey(group, "beatlooproll_activate"));
connect(m_pCOBeatLoopRollActivate, &ControlObject::valueChanged,
this, &LoopingControl::slotBeatLoopRollActivate);
// Here we create corresponding beatloop_(SIZE) CO's which all call the same
// BeatControl, but with a set value.
for (unsigned int i = 0; i < (sizeof(s_dBeatSizes) / sizeof(s_dBeatSizes[0])); ++i) {
BeatLoopingControl* pBeatLoop = new BeatLoopingControl(group, s_dBeatSizes[i]);
connect(pBeatLoop, &BeatLoopingControl::activateBeatLoop,
this, &LoopingControl::slotBeatLoopActivate,
Qt::DirectConnection);
connect(pBeatLoop, &BeatLoopingControl::activateBeatLoopRoll,
this, &LoopingControl::slotBeatLoopActivateRoll,
Qt::DirectConnection);
connect(pBeatLoop, &BeatLoopingControl::deactivateBeatLoop,
this, &LoopingControl::slotBeatLoopDeactivate,
Qt::DirectConnection);
connect(pBeatLoop, &BeatLoopingControl::deactivateBeatLoopRoll,
this, &LoopingControl::slotBeatLoopDeactivateRoll,
Qt::DirectConnection);
m_beatLoops.append(pBeatLoop);
}
m_pCOBeatJump = new ControlObject(ConfigKey(group, "beatjump"), false);
connect(m_pCOBeatJump, &ControlObject::valueChanged,
this, &LoopingControl::slotBeatJump, Qt::DirectConnection);
m_pCOBeatJumpSize = new ControlObject(ConfigKey(group, "beatjump_size"),
true, false, false, 4.0);
m_pCOBeatJumpForward = new ControlPushButton(ConfigKey(group, "beatjump_forward"));
connect(m_pCOBeatJumpForward, &ControlObject::valueChanged,
this, &LoopingControl::slotBeatJumpForward);
m_pCOBeatJumpBackward = new ControlPushButton(ConfigKey(group, "beatjump_backward"));
connect(m_pCOBeatJumpBackward, &ControlObject::valueChanged,
this, &LoopingControl::slotBeatJumpBackward);
// Create beatjump_(SIZE) CO's which all call beatjump, but with a set
// value.
for (unsigned int i = 0; i < (sizeof(s_dBeatSizes) / sizeof(s_dBeatSizes[0])); ++i) {
BeatJumpControl* pBeatJump = new BeatJumpControl(group, s_dBeatSizes[i]);
connect(pBeatJump, &BeatJumpControl::beatJump,
this, &LoopingControl::slotBeatJump,
Qt::DirectConnection);
m_beatJumps.append(pBeatJump);
}
m_pCOLoopMove = new ControlObject(ConfigKey(group, "loop_move"), false);
connect(m_pCOLoopMove, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopMove, Qt::DirectConnection);
// Create loop_move_(SIZE) CO's which all call loop_move, but with a set
// value.
for (unsigned int i = 0; i < (sizeof(s_dBeatSizes) / sizeof(s_dBeatSizes[0])); ++i) {
LoopMoveControl* pLoopMove = new LoopMoveControl(group, s_dBeatSizes[i]);
connect(pLoopMove, &LoopMoveControl::loopMove,
this, &LoopingControl::slotLoopMove,
Qt::DirectConnection);
m_loopMoves.append(pLoopMove);
}
m_pCOLoopScale = new ControlObject(ConfigKey(group, "loop_scale"), false);
connect(m_pCOLoopScale, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopScale);
m_pLoopHalveButton = new ControlPushButton(ConfigKey(group, "loop_halve"));
connect(m_pLoopHalveButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopHalve);
m_pLoopDoubleButton = new ControlPushButton(ConfigKey(group, "loop_double"));
connect(m_pLoopDoubleButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopDouble);
m_pPlayButton = ControlObject::getControl(ConfigKey(group, "play"));
}
LoopingControl::~LoopingControl() {
delete m_pLoopOutButton;
delete m_pLoopOutGotoButton;
delete m_pLoopInButton;
delete m_pLoopInGotoButton;
delete m_pLoopExitButton;
delete m_pReloopToggleButton;
delete m_pReloopAndStopButton;
delete m_pCOLoopEnabled;
delete m_pCOLoopStartPosition;
delete m_pCOLoopEndPosition;
delete m_pCOLoopScale;
delete m_pLoopHalveButton;
delete m_pLoopDoubleButton;
delete m_pCOBeatLoop;
while (!m_beatLoops.isEmpty()) {
BeatLoopingControl* pBeatLoop = m_beatLoops.takeLast();
delete pBeatLoop;
}
delete m_pCOBeatLoopSize;
delete m_pCOBeatLoopActivate;
delete m_pCOBeatLoopRollActivate;
delete m_pCOBeatJump;
delete m_pCOBeatJumpSize;
delete m_pCOBeatJumpForward;
delete m_pCOBeatJumpBackward;
while (!m_beatJumps.isEmpty()) {
BeatJumpControl* pBeatJump = m_beatJumps.takeLast();
delete pBeatJump;
}
delete m_pCOLoopMove;
while (!m_loopMoves.isEmpty()) {
LoopMoveControl* pLoopMove = m_loopMoves.takeLast();
delete pLoopMove;
}
}
void LoopingControl::slotLoopScale(double scaleFactor) {
LoopSamples loopSamples = m_loopSamples.getValue();
if (loopSamples.start == kNoTrigger || loopSamples.end == kNoTrigger) {
return;
}
const double loopLength = (loopSamples.end - loopSamples.start) * scaleFactor;
const int trackSamples = static_cast<int>(m_pTrackSamples->get());
// Abandon loops that are too short of extend beyond the end of the file.
if (loopLength < MINIMUM_AUDIBLE_LOOP_SIZE ||
loopSamples.start + loopLength > trackSamples) {
return;
}
loopSamples.end = loopSamples.start + loopLength;
// TODO(XXX) we could be smarter about taking the active beatloop, scaling
// it by the desired amount and trying to find another beatloop that matches
// it, but for now we just clear the active beat loop if somebody scales.
clearActiveBeatLoop();
// Don't allow 0 samples loop, so one can still manipulate it
if (loopSamples.end == loopSamples.start) {
if ((loopSamples.end + 2) >= trackSamples) {
loopSamples.start -= 2;
} else {
loopSamples.end += 2;
}
}
// Do not allow loops to go past the end of the song
else if (loopSamples.end > trackSamples) {
loopSamples.end = trackSamples;
}
// Reseek if the loop shrank out from under the playposition.
loopSamples.seek = (m_bLoopingEnabled && scaleFactor < 1.0);
m_loopSamples.setValue(loopSamples);
// Update CO for loop end marker
m_pCOLoopEndPosition->set(loopSamples.end);
}
void LoopingControl::slotLoopHalve(double pressed) {
if (pressed <= 0.0) {
return;
}
slotBeatLoop(m_pCOBeatLoopSize->get() / 2.0, true, false);
}
void LoopingControl::slotLoopDouble(double pressed) {
if (pressed <= 0.0) {
return;
}
slotBeatLoop(m_pCOBeatLoopSize->get() * 2.0, true, false);
}
void LoopingControl::process(const double dRate,
const double currentSample,
const int iBufferSize) {
Q_UNUSED(iBufferSize);
Q_UNUSED(dRate);
double oldCurrentSample = m_currentSample.getValue();
if (oldCurrentSample != currentSample) {
m_currentSample.setValue(currentSample);
} else {
// no transport, so we have to do scheduled seeks here
LoopSamples loopSamples = m_loopSamples.getValue();
if (m_bLoopingEnabled &&
!m_bAdjustingLoopIn && !m_bAdjustingLoopOut &&
loopSamples.start != kNoTrigger &&
loopSamples.end != kNoTrigger) {
if (loopSamples.start != m_oldLoopSamples.start ||
loopSamples.end != m_oldLoopSamples.end) {
// bool seek is only valid after the loop has changed
if (loopSamples.seek) {
// here the loop has changed and the play position
// should be moved with it
double target = seekInsideAdjustedLoop(currentSample,
m_oldLoopSamples.start, loopSamples.start, loopSamples.end);
if (target != kNoTrigger) {
// jump immediately
seekAbs(target);
}
}
m_oldLoopSamples = loopSamples;
}
}
}
if (m_bAdjustingLoopIn) {
setLoopInToCurrentPosition();
} else if (m_bAdjustingLoopOut) {
setLoopOutToCurrentPosition();
}
}
double LoopingControl::nextTrigger(bool reverse,
const double currentSample,
double *pTarget) {
*pTarget = kNoTrigger;
LoopSamples loopSamples = m_loopSamples.getValue();
// m_bAdjustingLoopIn is true while the LoopIn button is pressed while a loop is active (slotLoopIn)
if (m_bAdjustingLoopInOld != m_bAdjustingLoopIn) {
m_bAdjustingLoopInOld = m_bAdjustingLoopIn;
// When the LoopIn button is released in reverse mode we jump to the end of the loop to not fall out and disable the active loop
// This must not happen in quantized mode. The newly set start is always ahead (in time, but behind spacially) of the current position so we don't jump.
// Jumping to the end is then handled when the loop's start is reached later in this function.
if (reverse && !m_bAdjustingLoopIn && !m_pQuantizeEnabled->toBool()) {
m_oldLoopSamples = loopSamples;
*pTarget = loopSamples.end;
return currentSample;
}
}
// m_bAdjustingLoopOut is true while the LoopOut button is pressed while a loop is active (slotLoopOut)
if (m_bAdjustingLoopOutOld != m_bAdjustingLoopOut) {
m_bAdjustingLoopOutOld = m_bAdjustingLoopOut;
// When the LoopOut button is released in forward mode we jump to the start of the loop to not fall out and disable the active loop
// This must not happen in quantized mode. The newly set end is always ahead of the current position so we don't jump.
// Jumping to the start is then handled when the loop's end is reached later in this function.
if (!reverse && !m_bAdjustingLoopOut && !m_pQuantizeEnabled->toBool()) {
m_oldLoopSamples = loopSamples;
*pTarget = loopSamples.start;
return currentSample;
}
}
if (m_bLoopingEnabled &&
!m_bAdjustingLoopIn && !m_bAdjustingLoopOut &&
loopSamples.start != kNoTrigger &&
loopSamples.end != kNoTrigger) {
if (loopSamples.start != m_oldLoopSamples.start ||
loopSamples.end != m_oldLoopSamples.end) {
// bool seek is only valid after the loop has changed
if (loopSamples.seek) {
// here the loop has changed and the play position
// should be moved with it
*pTarget = seekInsideAdjustedLoop(currentSample,
m_oldLoopSamples.start, loopSamples.start, loopSamples.end);
} else {
bool movedOut = false;
// Check if we have moved out of the loop, before we could enable it
if (reverse) {
if (loopSamples.start > currentSample) {
movedOut = true;
}
} else {
if (loopSamples.end < currentSample) {
movedOut = true;
}
}
if (movedOut) {
*pTarget = seekInsideAdjustedLoop(currentSample,
loopSamples.start, loopSamples.start, loopSamples.end);
}
}
m_oldLoopSamples = loopSamples;
if (*pTarget != kNoTrigger) {
// jump immediately
return currentSample;
}
}
if (reverse) {
*pTarget = loopSamples.end;
return loopSamples.start;
} else {
*pTarget = loopSamples.start;
return loopSamples.end;
}
}
return kNoTrigger;
}
void LoopingControl::hintReader(HintVector* pHintList) {
LoopSamples loopSamples = m_loopSamples.getValue();
Hint loop_hint;
// If the loop is enabled, then this is high priority because we will loop
// sometime potentially very soon! The current audio itself is priority 1,
// but we will issue ourselves at priority 2.
if (m_bLoopingEnabled) {
// If we're looping, hint the loop in and loop out, in case we reverse
// into it. We could save information from process to tell which
// direction we're going in, but that this is much simpler, and hints
// aren't that bad to make anyway.
if (loopSamples.start >= 0) {
loop_hint.priority = 2;
loop_hint.frame = SampleUtil::floorPlayPosToFrame(loopSamples.start);
loop_hint.frameCount = Hint::kFrameCountForward;
pHintList->append(loop_hint);
}
if (loopSamples.end >= 0) {
loop_hint.priority = 10;
loop_hint.frame = SampleUtil::ceilPlayPosToFrame(loopSamples.end);
loop_hint.frameCount = Hint::kFrameCountBackward;
pHintList->append(loop_hint);
}
} else {
if (loopSamples.start >= 0) {
loop_hint.priority = 10;
loop_hint.frame = SampleUtil::floorPlayPosToFrame(loopSamples.start);
loop_hint.frameCount = Hint::kFrameCountForward;
pHintList->append(loop_hint);
}
}
}
double LoopingControl::getSyncPositionInsideLoop(double dRequestedPlaypos, double dSyncedPlayPos) {
// no loop, no adjustment
if (!m_bLoopingEnabled) {
return dSyncedPlayPos;
}
LoopSamples loopSamples = m_loopSamples.getValue();
// if the request itself is outside loop do nothing
// loop will be disabled later by notifySeek(...) as is was explicitly requested by the user
// if the requested position is the exact end of a loop it should also be disabled later by notifySeek(...)
if (dRequestedPlaypos < loopSamples.start || dRequestedPlaypos >= loopSamples.end) {
return dSyncedPlayPos;
}
// the requested position is inside the loop (e.g hotcue at start)
double loopSize = loopSamples.end - loopSamples.start;
// the synced position is in front of the loop
// adjust the synced position to same amount in front of the loop end
if (dSyncedPlayPos < loopSamples.start) {
double adjustment = loopSamples.start - dSyncedPlayPos;
// prevents jumping in front of the loop if loop is smaller than adjustment
adjustment = fmod(adjustment, loopSize);
// if the synced position is exactly the start of the loop we would end up at the exact end
// as this would disable the loop in notifySeek() replace it with the start of the loop
if (adjustment == 0) {
return loopSamples.start;
}
return loopSamples.end - adjustment;
}
// the synced position is behind the loop
// adjust the synced position to same amount behind the loop start
if (dSyncedPlayPos >= loopSamples.end) {
double adjustment = dSyncedPlayPos - loopSamples.end;
// prevents jumping behind the loop if loop is smaller than adjustment
adjustment = fmod(adjustment, loopSize);
return loopSamples.start + adjustment;
}
// both, requested and synced position are inside the loop -> do nothing
return dSyncedPlayPos;
}
void LoopingControl::setLoopInToCurrentPosition() {
// set loop-in position
const mixxx::BeatsPointer pBeats = m_pBeats;
LoopSamples loopSamples = m_loopSamples.getValue();
double quantizedBeat = -1;
double pos = m_currentSample.getValue();
if (m_pQuantizeEnabled->toBool() && pBeats) {
if (m_bAdjustingLoopIn) {
double closestBeat = m_pClosestBeat->get();
if (closestBeat == m_currentSample.getValue()) {
quantizedBeat = closestBeat;
} else {
quantizedBeat = m_pPreviousBeat->get();
}
} else {
quantizedBeat = m_pClosestBeat->get();
}
if (quantizedBeat != -1) {
pos = quantizedBeat;
}
}
// Reset the loop out position if it is before the loop in so that loops
// cannot be inverted.
if (loopSamples.end != kNoTrigger &&
loopSamples.end < pos) {
loopSamples.end = kNoTrigger;
m_pCOLoopEndPosition->set(kNoTrigger);
}
// If we're looping and the loop-in and out points are now so close
// that the loop would be inaudible, set the in point to the smallest
// pre-defined beatloop size instead (when possible)
if (loopSamples.end != kNoTrigger &&
(loopSamples.end - pos) < MINIMUM_AUDIBLE_LOOP_SIZE) {
if (quantizedBeat != -1 && pBeats) {
pos = pBeats->findNthBeat(quantizedBeat, -2);
if (pos == -1 || (loopSamples.end - pos) < MINIMUM_AUDIBLE_LOOP_SIZE) {
pos = loopSamples.end - MINIMUM_AUDIBLE_LOOP_SIZE;
}
} else {
pos = loopSamples.end - MINIMUM_AUDIBLE_LOOP_SIZE;
}
}
loopSamples.start = pos;
m_pCOLoopStartPosition->set(loopSamples.start);
// start looping
if (loopSamples.start != kNoTrigger &&
loopSamples.end != kNoTrigger) {
setLoopingEnabled(true);
loopSamples.seek = true;
} else {
loopSamples.seek = false;
}
if (m_pQuantizeEnabled->toBool()
&& loopSamples.start < loopSamples.end
&& pBeats) {
m_pCOBeatLoopSize->setAndConfirm(
pBeats->numBeatsInRange(loopSamples.start, loopSamples.end));
updateBeatLoopingControls();
} else {
clearActiveBeatLoop();
}
m_loopSamples.setValue(loopSamples);
//qDebug() << "set loop_in to " << loopSamples.start;
}
void LoopingControl::slotLoopIn(double pressed) {
if (!m_pTrack) {
return;
}
// If loop is enabled, suspend looping and set the loop in point
// when this button is released.
if (m_bLoopingEnabled) {
if (pressed > 0.0) {
m_bAdjustingLoopIn = true;
// Adjusting both the in and out point at the same time makes no sense
m_bAdjustingLoopOut = false;
} else {
setLoopInToCurrentPosition();
m_bAdjustingLoopIn = false;
}
} else {
if (pressed > 0.0) {
setLoopInToCurrentPosition();
}
m_bAdjustingLoopIn = false;
}
}
void LoopingControl::slotLoopInGoto(double pressed) {
if (pressed > 0.0) {
seekAbs(static_cast<double>(
m_loopSamples.getValue().start));
}
}
void LoopingControl::setLoopOutToCurrentPosition() {
mixxx::BeatsPointer pBeats = m_pBeats;
LoopSamples loopSamples = m_loopSamples.getValue();
double quantizedBeat = -1;
double pos = m_currentSample.getValue();
if (m_pQuantizeEnabled->toBool() && pBeats) {
if (m_bAdjustingLoopOut) {
double closestBeat = m_pClosestBeat->get();
if (closestBeat == m_currentSample.getValue()) {
quantizedBeat = closestBeat;
} else {
quantizedBeat = m_pNextBeat->get();
}
} else {
quantizedBeat = m_pClosestBeat->get();
}
if (quantizedBeat != -1) {
pos = quantizedBeat;
}
}
// If the user is trying to set a loop-out before the loop in or without
// having a loop-in, then ignore it.
if (loopSamples.start == kNoTrigger || pos < loopSamples.start) {
return;
}
// If the loop-in and out points are set so close that the loop would be
// inaudible (which can happen easily with quantize-to-beat enabled,)
// use the smallest pre-defined beatloop instead (when possible)
if ((pos - loopSamples.start) < MINIMUM_AUDIBLE_LOOP_SIZE) {
if (quantizedBeat != -1 && pBeats) {
pos = static_cast<int>(floor(pBeats->findNthBeat(quantizedBeat, 2)));
if (pos == -1 || (pos - loopSamples.start) < MINIMUM_AUDIBLE_LOOP_SIZE) {
pos = loopSamples.start + MINIMUM_AUDIBLE_LOOP_SIZE;
}
} else {
pos = loopSamples.start + MINIMUM_AUDIBLE_LOOP_SIZE;
}
}
// set loop out position
loopSamples.end = pos;
m_pCOLoopEndPosition->set(loopSamples.end);
// start looping
if (loopSamples.start != kNoTrigger &&
loopSamples.end != kNoTrigger) {
setLoopingEnabled(true);
loopSamples.seek = true;
} else {
loopSamples.seek = false;
}
if (m_pQuantizeEnabled->toBool() && pBeats) {
m_pCOBeatLoopSize->setAndConfirm(
pBeats->numBeatsInRange(loopSamples.start, loopSamples.end));
updateBeatLoopingControls();
} else {
clearActiveBeatLoop();
}
//qDebug() << "set loop_out to " << loopSamples.end;
m_loopSamples.setValue(loopSamples);
}
void LoopingControl::setRateControl(RateControl* rateControl) {
m_pRateControl = rateControl;
}
void LoopingControl::slotLoopOut(double pressed) {
if (m_pTrack == nullptr) {
return;
}
// If loop is enabled, suspend looping and set the loop out point
// when this button is released.
if (m_bLoopingEnabled) {
if (pressed > 0.0) {
m_bAdjustingLoopOut = true;
// Adjusting both the in and out point at the same time makes no sense
m_bAdjustingLoopIn = false;
} else {
// If this button was pressed to set the loop out point when loop
// was disabled, that will enable looping, so avoid moving the
// loop out point when the button is released.
if (!m_bLoopOutPressedWhileLoopDisabled) {
setLoopOutToCurrentPosition();
m_bAdjustingLoopOut = false;
} else {
m_bLoopOutPressedWhileLoopDisabled = false;
}
}
} else {
if (pressed > 0.0) {
setLoopOutToCurrentPosition();
m_bLoopOutPressedWhileLoopDisabled = true;
}
m_bAdjustingLoopOut = false;
}
}
void LoopingControl::slotLoopOutGoto(double pressed) {
if (pressed > 0.0) {
seekAbs(static_cast<double>(
m_loopSamples.getValue().end));
}
}
void LoopingControl::slotLoopExit(double val) {
if (!m_pTrack || val <= 0.0) {
return;
}
// If we're looping, stop looping
if (m_bLoopingEnabled) {
setLoopingEnabled(false);
}
}
void LoopingControl::slotReloopToggle(double val) {
if (!m_pTrack || val <= 0.0) {
return;
}
// If we're looping, stop looping
if (m_bLoopingEnabled) {
// If loop roll was active, also disable slip.
if (m_bLoopRollActive) {
m_pSlipEnabled->set(0);
m_bLoopRollActive = false;
m_activeLoopRolls.clear();
}
setLoopingEnabled(false);
//qDebug() << "reloop_toggle looping off";
} else {
// If we're not looping, enable the loop. If the loop is ahead of the
// current play position, do not jump to it.
LoopSamples loopSamples = m_loopSamples.getValue();
if (loopSamples.start != kNoTrigger && loopSamples.end != kNoTrigger &&
loopSamples.start <= loopSamples.end) {
setLoopingEnabled(true);
if (m_currentSample.getValue() > loopSamples.end) {
slotLoopInGoto(1);
}
}
//qDebug() << "reloop_toggle looping on";
}
}
void LoopingControl::slotReloopAndStop(double pressed) {
if (pressed > 0) {
m_pPlayButton->set(0.0);
seekAbs(static_cast<double>(
m_loopSamples.getValue().start));
setLoopingEnabled(true);
}
}
void LoopingControl::slotLoopStartPos(double pos) {
// This slot is called before trackLoaded() for a new Track
LoopSamples loopSamples = m_loopSamples.getValue();
if (loopSamples.start == pos) {
//nothing to do
return;
}
clearActiveBeatLoop();
if (pos == kNoTrigger) {
setLoopingEnabled(false);
}
loopSamples.seek = false;
loopSamples.start = pos;
m_pCOLoopStartPosition->set(pos);
if (loopSamples.end != kNoTrigger &&
loopSamples.end <= loopSamples.start) {
loopSamples.end = kNoTrigger;
m_pCOLoopEndPosition->set(kNoTrigger);
setLoopingEnabled(false);
}
m_loopSamples.setValue(loopSamples);
}
void LoopingControl::slotLoopEndPos(double pos) {
// This slot is called before trackLoaded() for a new Track
LoopSamples loopSamples = m_loopSamples.getValue();
if (loopSamples.end == pos) {
//nothing to do
return;
}
// Reject if the loop-in is not set, or if the new position is before the
// start point (but not -1).
if (loopSamples.start == kNoTrigger ||
(pos != kNoTrigger && pos <= loopSamples.start)) {
m_pCOLoopEndPosition->set(loopSamples.end);
return;
}
clearActiveBeatLoop();
if (pos == -1.0) {
setLoopingEnabled(false);
}
loopSamples.end = pos;
loopSamples.seek = false;
m_pCOLoopEndPosition->set(pos);
m_loopSamples.setValue(loopSamples);
}
// This is called from the engine thread
void LoopingControl::notifySeek(double dNewPlaypos) {
LoopSamples loopSamples = m_loopSamples.getValue();
double currentSample = m_currentSample.getValue();
if (m_bLoopingEnabled) {
// Disable loop when we jumping out, or over a catching loop,
// using hot cues or waveform overview.
// Jumping to the exact end of a loop is considered jumping out.
if (currentSample >= loopSamples.start &&
currentSample <= loopSamples.end &&
dNewPlaypos < loopSamples.start) {
// jumping out of loop in backwards
setLoopingEnabled(false);
}
if (currentSample <= loopSamples.end &&
dNewPlaypos >= loopSamples.end) {
// jumping out or to the exact end of a loop or over a catching loop forward
setLoopingEnabled(false);
}
}
EngineControl::notifySeek(dNewPlaypos);
}
void LoopingControl::setLoopingEnabled(bool enabled) {
m_bLoopingEnabled = enabled;
m_pCOLoopEnabled->set(enabled);
BeatLoopingControl* pActiveBeatLoop = atomicLoadRelaxed(m_pActiveBeatLoop);
if (pActiveBeatLoop != nullptr) {
if (enabled) {
pActiveBeatLoop->activate();
} else {
pActiveBeatLoop->deactivate();
}
}
}
bool LoopingControl::isLoopingEnabled() {
return m_bLoopingEnabled;
}
void LoopingControl::trackLoaded(TrackPointer pNewTrack) {
m_pTrack = pNewTrack;
mixxx::BeatsPointer pBeats;
if (pNewTrack) {
pBeats = pNewTrack->getBeats();
}
trackBeatsUpdated(pBeats);
}
void LoopingControl::trackBeatsUpdated(mixxx::BeatsPointer pBeats) {
clearActiveBeatLoop();
m_pBeats = pBeats;
if (m_pBeats) {
LoopSamples loopSamples = m_loopSamples.getValue();
if (loopSamples.start != kNoTrigger && loopSamples.end != kNoTrigger) {
double loaded_loop_size = findBeatloopSizeForLoop(
loopSamples.start, loopSamples.end);
if (loaded_loop_size != -1) {
m_pCOBeatLoopSize->setAndConfirm(loaded_loop_size);
}
}
}
}
void LoopingControl::slotBeatLoopActivate(BeatLoopingControl* pBeatLoopControl) {
if (!m_pTrack) {
return;
}
// Maintain the current start point if there is an active loop currently
// looping. slotBeatLoop will update m_pActiveBeatLoop if applicable. Note,
// this used to only maintain the current start point if a beatloop was
// enabled. See Bug #1159243.
slotBeatLoop(pBeatLoopControl->getSize(), m_bLoopingEnabled, true);
}
void LoopingControl::slotBeatLoopActivateRoll(BeatLoopingControl* pBeatLoopControl) {
if (!m_pTrack) {
return;
}
// Disregard existing loops (except beatlooprolls).
m_pSlipEnabled->set(1);
slotBeatLoop(pBeatLoopControl->getSize(), m_bLoopRollActive, true);
m_bLoopRollActive = true;
m_activeLoopRolls.push(pBeatLoopControl->getSize());
}
void LoopingControl::slotBeatLoopDeactivate(BeatLoopingControl* pBeatLoopControl) {
Q_UNUSED(pBeatLoopControl);
setLoopingEnabled(false);
}
void LoopingControl::slotBeatLoopDeactivateRoll(BeatLoopingControl* pBeatLoopControl) {
pBeatLoopControl->deactivate();
const double size = pBeatLoopControl->getSize();
auto* i = m_activeLoopRolls.begin();
while (i != m_activeLoopRolls.end()) {
if (size == *i) {
i = m_activeLoopRolls.erase(i);
} else {
++i;
}
}
// Make sure slip mode is not turned off if it was turned on
// by something that was not a rolling beatloop.
if (m_bLoopRollActive && m_activeLoopRolls.empty()) {
setLoopingEnabled(false);
m_pSlipEnabled->set(0);
m_bLoopRollActive = false;
}
// Return to the previous beatlooproll if necessary.
if (!m_activeLoopRolls.empty()) {
slotBeatLoop(m_activeLoopRolls.top(), m_bLoopRollActive, true);
}
}
void LoopingControl::clearActiveBeatLoop() {
BeatLoopingControl* pOldBeatLoop = m_pActiveBeatLoop.fetchAndStoreAcquire(nullptr);
if (pOldBeatLoop != nullptr) {
pOldBeatLoop->deactivate();
}
}
bool LoopingControl::currentLoopMatchesBeatloopSize() {
const mixxx::BeatsPointer pBeats = m_pBeats;
if (!pBeats) {
return false;
}
LoopSamples loopSamples = m_loopSamples.getValue();
// Calculate where the loop out point would be if it is a beatloop
double beatLoopOutPoint =
pBeats->findNBeatsFromSample(loopSamples.start, m_pCOBeatLoopSize->get());
return loopSamples.end > beatLoopOutPoint - 2 &&
loopSamples.end < beatLoopOutPoint + 2;
}
double LoopingControl::findBeatloopSizeForLoop(double start, double end) const {
const mixxx::BeatsPointer pBeats = m_pBeats;
if (!pBeats) {
return -1;
}
for (unsigned int i = 0; i < (sizeof(s_dBeatSizes) / sizeof(s_dBeatSizes[0])); ++i) {
double beatLoopOutPoint =
pBeats->findNBeatsFromSample(start, s_dBeatSizes[i]);
if (end > beatLoopOutPoint - 2 && end < beatLoopOutPoint + 2) {
return s_dBeatSizes[i];
}
}
return -1;
}
void LoopingControl::updateBeatLoopingControls() {
// O(n) search, but there are only ~10-ish beatloop controls so this is
// fine.
double dBeatloopSize = m_pCOBeatLoopSize->get();
for (BeatLoopingControl* pBeatLoopControl: qAsConst(m_beatLoops)) {
if (pBeatLoopControl->getSize() == dBeatloopSize) {
if (m_bLoopingEnabled) {
pBeatLoopControl->activate();
}
BeatLoopingControl* pOldBeatLoop =
m_pActiveBeatLoop.fetchAndStoreRelease(pBeatLoopControl);
if (pOldBeatLoop != nullptr && pOldBeatLoop != pBeatLoopControl) {
pOldBeatLoop->deactivate();