-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
loopingcontrol.cpp
1942 lines (1732 loc) · 74 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/controls/ratecontrol.h"
#include "engine/enginebuffer.h"
#include "moc_loopingcontrol.cpp"
#include "preferences/usersettings.h"
#include "track/track.h"
#include "util/compatibility/qatomic.h"
#include "util/make_const_iterator.h"
#include "util/math.h"
namespace {
constexpr mixxx::audio::FrameDiff_t kMinimumAudibleLoopSizeFrames = 150;
// returns true if a is valid and is fairly close to target (within +/- 1 frame).
bool positionNear(mixxx::audio::FramePos a, mixxx::audio::FramePos target) {
return a.isValid() && a > target - 1 && a < target + 1;
}
} // namespace
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_bLoopWasEnabledBeforeSlipEnable(false),
m_bAdjustingLoopIn(false),
m_bAdjustingLoopOut(false),
m_bAdjustingLoopInOld(false),
m_bAdjustingLoopOutOld(false),
m_bLoopOutPressedWhileLoopDisabled(false) {
m_oldLoopInfo = {mixxx::audio::kInvalidFramePos,
mixxx::audio::kInvalidFramePos,
LoopSeekMode::MovedOut};
m_loopInfo.setValue(m_oldLoopInfo);
m_currentPosition.setValue(mixxx::audio::kStartFramePos);
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.
m_pReloopToggleButton->addAlias(ConfigKey(group, QStringLiteral("reloop_exit")));
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_pCOLoopEnabled->connectValueChangeRequest(this,
&LoopingControl::slotLoopEnabledValueChangeRequest,
Qt::DirectConnection);
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_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,
[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_pCOBeatJumpSize->connectValueChangeRequest(this,
&LoopingControl::slotBeatJumpSizeChangeRequest,
Qt::DirectConnection);
m_pCOBeatJumpSizeHalve = new ControlPushButton(ConfigKey(group, "beatjump_size_halve"));
m_pCOBeatJumpSizeHalve->setKbdRepeatable(true);
connect(m_pCOBeatJumpSizeHalve,
&ControlObject::valueChanged,
this,
&LoopingControl::slotBeatJumpSizeHalve);
m_pCOBeatJumpSizeDouble = new ControlPushButton(ConfigKey(group, "beatjump_size_double"));
m_pCOBeatJumpSizeDouble->setKbdRepeatable(true);
connect(m_pCOBeatJumpSizeDouble,
&ControlObject::valueChanged,
this,
&LoopingControl::slotBeatJumpSizeDouble);
m_pCOBeatJumpForward = new ControlPushButton(ConfigKey(group, "beatjump_forward"));
m_pCOBeatJumpForward->setKbdRepeatable(true);
connect(m_pCOBeatJumpForward, &ControlObject::valueChanged,
this, &LoopingControl::slotBeatJumpForward);
m_pCOBeatJumpBackward = new ControlPushButton(ConfigKey(group, "beatjump_backward"));
m_pCOBeatJumpBackward->setKbdRepeatable(true);
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"));
m_pLoopHalveButton->setKbdRepeatable(true);
connect(m_pLoopHalveButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopHalve);
m_pLoopDoubleButton = new ControlPushButton(ConfigKey(group, "loop_double"));
m_pLoopDoubleButton->setKbdRepeatable(true);
connect(m_pLoopDoubleButton, &ControlObject::valueChanged,
this, &LoopingControl::slotLoopDouble);
m_pLoopRemoveButton = new ControlPushButton(ConfigKey(group, "loop_remove"));
m_pLoopRemoveButton->setButtonMode(ControlPushButton::TRIGGER);
connect(m_pLoopRemoveButton,
&ControlObject::valueChanged,
this,
&LoopingControl::slotLoopRemove);
m_pPlayButton = ControlObject::getControl(ConfigKey(group, "play"));
m_pRepeatButton = ControlObject::getControl(ConfigKey(group, "repeat"));
}
LoopingControl::~LoopingControl() {
// TODO Use unique_ptr to manage lifetime
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_pLoopRemoveButton;
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_pCOBeatJumpSizeHalve;
delete m_pCOBeatJumpSizeDouble;
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) {
LoopInfo loopInfo = m_loopInfo.getValue();
if (!loopInfo.startPosition.isValid() || !loopInfo.endPosition.isValid()) {
return;
}
const mixxx::audio::FrameDiff_t loopLength =
(loopInfo.endPosition - loopInfo.startPosition) * scaleFactor;
const FrameInfo info = frameInfo();
const auto trackEndPosition = info.trackEndPosition;
if (!trackEndPosition.isValid()) {
return;
}
// Abandon loops that are too short of extend beyond the end of the file.
if (loopLength < kMinimumAudibleLoopSizeFrames ||
loopInfo.startPosition + loopLength > trackEndPosition) {
return;
}
loopInfo.endPosition = loopInfo.startPosition + 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 (loopInfo.endPosition == loopInfo.startPosition) {
if ((loopInfo.endPosition + 1) >= trackEndPosition) {
loopInfo.startPosition -= 1;
} else {
loopInfo.endPosition += 1;
}
}
// Do not allow loops to go past the end of the song
else if (loopInfo.endPosition > trackEndPosition) {
loopInfo.endPosition = trackEndPosition;
}
// Reseek if the loop shrank out from under the playposition.
loopInfo.seekMode = (m_bLoopingEnabled && scaleFactor < 1.0)
? LoopSeekMode::Changed
: LoopSeekMode::MovedOut;
m_loopInfo.setValue(loopInfo);
emit loopUpdated(loopInfo.startPosition, loopInfo.endPosition);
// Update CO for loop end marker
m_pCOLoopEndPosition->set(loopInfo.endPosition.toEngineSamplePos());
}
void LoopingControl::slotLoopHalve(double pressed) {
if (pressed <= 0.0) {
return;
}
m_pCOBeatLoopSize->set(m_pCOBeatLoopSize->get() / 2.0);
}
void LoopingControl::slotLoopDouble(double pressed) {
if (pressed <= 0.0) {
return;
}
m_pCOBeatLoopSize->set(m_pCOBeatLoopSize->get() * 2.0);
}
void LoopingControl::process(const double dRate,
mixxx::audio::FramePos currentPosition,
const int iBufferSize) {
Q_UNUSED(iBufferSize);
const auto previousPosition = m_currentPosition.getValue();
if (previousPosition != currentPosition) {
m_currentPosition.setValue(currentPosition);
} else {
// no transport, so we have to do scheduled seeks here
LoopInfo loopInfo = m_loopInfo.getValue();
if (m_bLoopingEnabled &&
!m_bAdjustingLoopIn && !m_bAdjustingLoopOut &&
loopInfo.startPosition.isValid() &&
loopInfo.endPosition.isValid()) {
if (loopInfo.startPosition != m_oldLoopInfo.startPosition ||
loopInfo.endPosition != m_oldLoopInfo.endPosition) {
// bool seek is only valid after the loop has changed
if (loopInfo.seekMode == LoopSeekMode::Changed) {
// here the loop has changed and the play position
// should be moved with it
const auto targetPosition =
adjustedPositionInsideAdjustedLoop(currentPosition,
dRate < 0, // reverse
m_oldLoopInfo.startPosition,
m_oldLoopInfo.endPosition,
loopInfo.startPosition,
loopInfo.endPosition);
if (targetPosition.isValid()) {
// jump immediately
seekAbs(targetPosition);
}
}
m_oldLoopInfo = loopInfo;
}
}
}
if (m_bAdjustingLoopIn) {
setLoopInToCurrentPosition();
} else if (m_bAdjustingLoopOut) {
setLoopOutToCurrentPosition();
}
}
mixxx::audio::FramePos LoopingControl::nextTrigger(bool reverse,
mixxx::audio::FramePos currentPosition,
mixxx::audio::FramePos* pTargetPosition) {
*pTargetPosition = mixxx::audio::kInvalidFramePos;
LoopInfo loopInfo = m_loopInfo.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_oldLoopInfo = loopInfo;
*pTargetPosition = loopInfo.endPosition;
return currentPosition;
}
}
// 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_oldLoopInfo = loopInfo;
*pTargetPosition = loopInfo.startPosition;
return currentPosition;
}
}
if (m_bLoopingEnabled &&
loopInfo.startPosition.isValid() &&
loopInfo.endPosition.isValid()) {
if (!m_bAdjustingLoopIn && !m_bAdjustingLoopOut) {
if (loopInfo.startPosition != m_oldLoopInfo.startPosition ||
loopInfo.endPosition != m_oldLoopInfo.endPosition) {
// bool seek is only valid after the loop has changed
switch (loopInfo.seekMode) {
case LoopSeekMode::Changed:
// here the loop has changed and the play position
// should be moved with it
*pTargetPosition = adjustedPositionInsideAdjustedLoop(currentPosition,
reverse,
m_oldLoopInfo.startPosition,
m_oldLoopInfo.endPosition,
loopInfo.startPosition,
loopInfo.endPosition);
break;
case LoopSeekMode::MovedOut: {
bool movedOut = false;
// Check if we have moved out of the loop, before we could enable it
if (reverse) {
if (loopInfo.startPosition > currentPosition) {
movedOut = true;
}
} else {
if (loopInfo.endPosition < currentPosition) {
movedOut = true;
}
}
if (movedOut) {
*pTargetPosition = adjustedPositionInsideAdjustedLoop(currentPosition,
reverse,
loopInfo.startPosition,
loopInfo.endPosition,
loopInfo.startPosition,
loopInfo.endPosition);
}
break;
}
case LoopSeekMode::None:
// Nothing to do here. This is used for enabling saved loops
// which we want to do without jumping to the loop start
// position.
break;
}
m_oldLoopInfo = loopInfo;
if (pTargetPosition->isValid()) {
// jump immediately
return currentPosition;
}
}
if (reverse) {
*pTargetPosition = loopInfo.endPosition;
return loopInfo.startPosition;
} else {
*pTargetPosition = loopInfo.startPosition;
return loopInfo.endPosition;
}
} else {
// LOOP in or out button is pressed for adjusting.
// Jump back to loop start, when reaching the track end this
// prevents that the track stops outside the adjusted loop.
if (!reverse) {
if (m_bAdjustingLoopIn) {
// Just in case the user does not release loop-in in time.
*pTargetPosition = m_oldLoopInfo.startPosition;
return loopInfo.endPosition;
}
const FrameInfo info = frameInfo();
*pTargetPosition = loopInfo.startPosition;
return info.trackEndPosition;
} else {
if (m_bAdjustingLoopOut) {
// Just in case the user does not release loop-out in time.
*pTargetPosition = m_oldLoopInfo.endPosition;
return loopInfo.startPosition;
}
}
}
}
// Return trigger if repeat is enabled
if (m_pRepeatButton->toBool()) {
const FrameInfo info = frameInfo();
if (reverse) {
*pTargetPosition = info.trackEndPosition;
return mixxx::audio::kStartFramePos;
} else {
*pTargetPosition = mixxx::audio::kStartFramePos;
return info.trackEndPosition;
}
}
return mixxx::audio::kInvalidFramePos;
}
double LoopingControl::getTrackSamples() const {
const FrameInfo info = frameInfo();
return info.trackEndPosition.toEngineSamplePos();
}
void LoopingControl::hintReader(gsl::not_null<HintVector*> pHintList) {
LoopInfo loopInfo = m_loopInfo.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 (loopInfo.startPosition.isValid()) {
loop_hint.type = Hint::Type::LoopStartEnabled;
loop_hint.frame = static_cast<SINT>(
loopInfo.startPosition.toLowerFrameBoundary().value());
loop_hint.frameCount = Hint::kFrameCountForward;
pHintList->append(loop_hint);
}
if (loopInfo.endPosition.isValid()) {
loop_hint.type = Hint::Type::LoopEndEnabled;
loop_hint.frame = static_cast<SINT>(
loopInfo.endPosition.toUpperFrameBoundary().value());
loop_hint.frameCount = Hint::kFrameCountBackward;
pHintList->append(loop_hint);
}
} else {
if (loopInfo.startPosition.isValid()) {
loop_hint.type = Hint::Type::LoopStart;
loop_hint.frame = static_cast<SINT>(
loopInfo.startPosition.toLowerFrameBoundary().value());
loop_hint.frameCount = Hint::kFrameCountForward;
pHintList->append(loop_hint);
}
}
}
mixxx::audio::FramePos LoopingControl::getSyncPositionInsideLoop(
mixxx::audio::FramePos requestedPlayPosition,
mixxx::audio::FramePos syncedPlayPosition) {
// no loop, no adjustment
if (!m_bLoopingEnabled) {
return syncedPlayPosition;
}
const LoopInfo loopInfo = m_loopInfo.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 (requestedPlayPosition < loopInfo.startPosition ||
requestedPlayPosition >= loopInfo.endPosition) {
return syncedPlayPosition;
}
// the requested position is inside the loop (e.g hotcue at start)
const mixxx::audio::FrameDiff_t loopSizeFrames = loopInfo.endPosition - loopInfo.startPosition;
// the synced position is in front of the loop
// adjust the synced position to same amount in front of the loop end
if (syncedPlayPosition < loopInfo.startPosition) {
mixxx::audio::FrameDiff_t adjustment = loopInfo.startPosition - syncedPlayPosition;
// prevents jumping in front of the loop if loop is smaller than adjustment
adjustment = fmod(adjustment, loopSizeFrames);
// 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 loopInfo.startPosition;
}
return loopInfo.endPosition - adjustment;
}
// the synced position is behind the loop
// adjust the synced position to same amount behind the loop start
if (syncedPlayPosition >= loopInfo.endPosition) {
mixxx::audio::FrameDiff_t adjustment = syncedPlayPosition - loopInfo.endPosition;
// prevents jumping behind the loop if loop is smaller than adjustment
adjustment = fmod(adjustment, loopSizeFrames);
return loopInfo.startPosition + adjustment;
}
// both, requested and synced position are inside the loop -> do nothing
return syncedPlayPosition;
}
void LoopingControl::setBeatLoop(mixxx::audio::FramePos startPosition, bool enabled) {
VERIFY_OR_DEBUG_ASSERT(startPosition.isValid()) {
return;
}
mixxx::BeatsPointer pBeats = m_pBeats;
if (!pBeats) {
return;
}
double beatloopSize = m_pCOBeatLoopSize->get();
// TODO(XXX): This is not realtime safe. See this Zulip discussion for details:
// https://mixxx.zulipchat.com/#narrow/stream/109171-development/topic/getting.20locks.20out.20of.20Beats
const auto endPosition = pBeats->findNBeatsFromPosition(startPosition, beatloopSize);
if (endPosition.isValid()) {
setLoop(startPosition, endPosition, enabled);
}
}
void LoopingControl::setLoop(mixxx::audio::FramePos startPosition,
mixxx::audio::FramePos endPosition,
bool enabled) {
VERIFY_OR_DEBUG_ASSERT(startPosition.isValid() && endPosition.isValid() &&
startPosition < endPosition) {
return;
}
LoopInfo loopInfo = m_loopInfo.getValue();
if (loopInfo.startPosition != startPosition || loopInfo.endPosition != endPosition) {
// Copy saved loop parameters to active loop
loopInfo.startPosition = startPosition;
loopInfo.endPosition = endPosition;
loopInfo.seekMode = LoopSeekMode::None;
clearActiveBeatLoop();
m_loopInfo.setValue(loopInfo);
m_pCOLoopStartPosition->set(loopInfo.startPosition.toEngineSamplePos());
m_pCOLoopEndPosition->set(loopInfo.endPosition.toEngineSamplePos());
}
setLoopingEnabled(enabled);
// Seek back to loop in position if we're already behind the loop end.
//
// TODO(Holzhaus): This needs to be reverted as soon as GUI controls for
// controlling saved loop behaviour are in place, because this change makes
// saved loops very risky to use and might potentially mess up your mix.
// See https://github.com/mixxxdj/mixxx/pull/2194#issuecomment-721847833
// for details.
if (enabled && m_currentPosition.getValue() > loopInfo.endPosition) {
slotLoopInGoto(1);
}
// Don't allow loop size widget setting to trigger creation of another loop.
m_pCOBeatLoopSize->blockSignals(true);
m_pCOBeatLoopSize->setAndConfirm(findBeatloopSizeForLoop(startPosition, endPosition));
m_pCOBeatLoopSize->blockSignals(false);
}
void LoopingControl::setLoopInToCurrentPosition() {
// set loop-in position
const mixxx::BeatsPointer pBeats = m_pBeats;
LoopInfo loopInfo = m_loopInfo.getValue();
mixxx::audio::FramePos quantizedBeatPosition;
const FrameInfo info = frameInfo();
// Note: currentPos can be past the end of the track, in the padded
// silence of the last buffer. This position might be not reachable in
// a future runs, depending on the buffering.
mixxx::audio::FramePos position = math_min(info.currentPosition, info.trackEndPosition);
if (m_pQuantizeEnabled->toBool() && pBeats) {
mixxx::audio::FramePos prevBeatPosition;
mixxx::audio::FramePos nextBeatPosition;
if (pBeats->findPrevNextBeats(position, &prevBeatPosition, &nextBeatPosition, false)) {
// both beat positions are valid
mixxx::audio::FramePos closestBeatPosition =
(nextBeatPosition - position > position - prevBeatPosition)
? prevBeatPosition
: nextBeatPosition;
if (m_bAdjustingLoopIn) {
if (closestBeatPosition == position) {
quantizedBeatPosition = closestBeatPosition;
} else {
quantizedBeatPosition = prevBeatPosition;
}
} else {
if (closestBeatPosition > info.trackEndPosition) {
quantizedBeatPosition = prevBeatPosition;
} else {
quantizedBeatPosition = closestBeatPosition;
}
}
position = quantizedBeatPosition;
}
}
// Reset the loop out position if it is before the loop in so that loops
// cannot be inverted.
if (loopInfo.endPosition.isValid() && loopInfo.endPosition <= position) {
loopInfo.endPosition = mixxx::audio::kInvalidFramePos;
m_pCOLoopEndPosition->set(loopInfo.endPosition.toEngineSamplePosMaybeInvalid());
if (m_bLoopingEnabled) {
setLoopingEnabled(false);
}
}
// 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 (loopInfo.endPosition.isValid() &&
(loopInfo.endPosition - position) < kMinimumAudibleLoopSizeFrames) {
if (quantizedBeatPosition.isValid() && pBeats) {
position = pBeats->findNthBeat(quantizedBeatPosition, -2);
if (!position.isValid() ||
(loopInfo.endPosition - position) <
kMinimumAudibleLoopSizeFrames) {
position = loopInfo.endPosition - kMinimumAudibleLoopSizeFrames;
}
} else {
position = loopInfo.endPosition - kMinimumAudibleLoopSizeFrames;
}
}
loopInfo.startPosition = position;
m_pCOLoopStartPosition->set(loopInfo.startPosition.toEngineSamplePosMaybeInvalid());
// start looping
if (loopInfo.startPosition.isValid() && loopInfo.endPosition.isValid()) {
setLoopingEnabled(true);
loopInfo.seekMode = LoopSeekMode::Changed;
} else {
loopInfo.seekMode = LoopSeekMode::MovedOut;
}
if (m_pQuantizeEnabled->toBool() && loopInfo.startPosition.isValid() &&
loopInfo.endPosition.isValid() &&
loopInfo.startPosition < loopInfo.endPosition && pBeats) {
m_pCOBeatLoopSize->setAndConfirm(pBeats->numBeatsInRange(
loopInfo.startPosition, loopInfo.endPosition));
updateBeatLoopingControls();
} else {
clearActiveBeatLoop();
}
m_loopInfo.setValue(loopInfo);
//qDebug() << "set loop_in to " << loopInfo.startPosition;
}
// Clear the last active loop while saved loop (cue + info) remains untouched
void LoopingControl::slotLoopRemove() {
setLoopingEnabled(false);
LoopInfo loopInfo = m_loopInfo.getValue();
loopInfo.startPosition = mixxx::audio::kInvalidFramePos;
loopInfo.endPosition = mixxx::audio::kInvalidFramePos;
loopInfo.seekMode = LoopSeekMode::None;
m_loopInfo.setValue(loopInfo);
m_pCOLoopStartPosition->set(loopInfo.startPosition.toEngineSamplePosMaybeInvalid());
m_pCOLoopEndPosition->set(loopInfo.endPosition.toEngineSamplePosMaybeInvalid());
}
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;
LoopInfo loopInfo = m_loopInfo.getValue();
if (loopInfo.startPosition < loopInfo.endPosition) {
emit loopUpdated(loopInfo.startPosition, loopInfo.endPosition);
} else {
emit loopReset();
}
}
} else {
emit loopReset();
if (pressed > 0.0) {
setLoopInToCurrentPosition();
}
m_bAdjustingLoopIn = false;
}
}
void LoopingControl::slotLoopInGoto(double pressed) {
if (pressed == 0.0) {
return;
}
const auto loopInPosition = m_loopInfo.getValue().startPosition;
if (loopInPosition.isValid()) {
seekAbs(loopInPosition);
}
}
void LoopingControl::setLoopOutToCurrentPosition() {
mixxx::BeatsPointer pBeats = m_pBeats;
LoopInfo loopInfo = m_loopInfo.getValue();
mixxx::audio::FramePos quantizedBeatPosition;
FrameInfo info = frameInfo();
// Note: currentPos can be past the end of the track, in the padded
// silence of the last buffer. This position might be not reachable in
// a future runs, depending on the buffering.
mixxx::audio::FramePos position = math_min(info.currentPosition, info.trackEndPosition);
if (m_pQuantizeEnabled->toBool() && pBeats) {
mixxx::audio::FramePos prevBeatPosition;
mixxx::audio::FramePos nextBeatPosition;
if (pBeats->findPrevNextBeats(position, &prevBeatPosition, &nextBeatPosition, false)) {
// both beat positions are valid
const mixxx::audio::FramePos closestBeatPosition =
(nextBeatPosition - position > position - prevBeatPosition)
? prevBeatPosition
: nextBeatPosition;
if (m_bAdjustingLoopOut) {
if (closestBeatPosition == position) {
quantizedBeatPosition = closestBeatPosition;
} else {
if (nextBeatPosition > info.trackEndPosition) {
quantizedBeatPosition = prevBeatPosition;
} else {
quantizedBeatPosition = nextBeatPosition;
}
}
} else {
if (closestBeatPosition > info.trackEndPosition) {
quantizedBeatPosition = prevBeatPosition;
} else {
quantizedBeatPosition = closestBeatPosition;
}
}
position = quantizedBeatPosition;
}
}
// If the user is trying to set a loop-out before the loop in or without
// having a loop-in, then ignore it.
if (!loopInfo.startPosition.isValid() || position <= loopInfo.startPosition) {
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 ((position - loopInfo.startPosition) < kMinimumAudibleLoopSizeFrames) {
if (quantizedBeatPosition.isValid() && pBeats) {
position = pBeats->findNthBeat(quantizedBeatPosition, 2);
if (!position.isValid() ||
(position - loopInfo.startPosition) <
kMinimumAudibleLoopSizeFrames) {
position = loopInfo.startPosition + kMinimumAudibleLoopSizeFrames;
}
} else {
position = loopInfo.startPosition + kMinimumAudibleLoopSizeFrames;
}
}
// set loop out position
loopInfo.endPosition = position;
m_pCOLoopEndPosition->set(loopInfo.endPosition.toEngineSamplePosMaybeInvalid());
// start looping
if (loopInfo.startPosition.isValid() && loopInfo.endPosition.isValid()) {
setLoopingEnabled(true);
loopInfo.seekMode = LoopSeekMode::Changed;
} else {
loopInfo.seekMode = LoopSeekMode::MovedOut;
}
if (m_pQuantizeEnabled->toBool() && pBeats) {
m_pCOBeatLoopSize->setAndConfirm(pBeats->numBeatsInRange(
loopInfo.startPosition, loopInfo.endPosition));
updateBeatLoopingControls();
} else {
clearActiveBeatLoop();
}
//qDebug() << "set loop_out to " << loopInfo.endPosition;
m_loopInfo.setValue(loopInfo);
}
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();
LoopInfo loopInfo = m_loopInfo.getValue();
if (loopInfo.startPosition < loopInfo.endPosition) {
emit loopUpdated(loopInfo.startPosition, loopInfo.endPosition);
} else {
emit loopReset();
}
m_bAdjustingLoopOut = false;
} else {
m_bLoopOutPressedWhileLoopDisabled = false;
}
}
} else {
emit loopReset();
if (pressed > 0.0) {
setLoopOutToCurrentPosition();
m_bLoopOutPressedWhileLoopDisabled = true;
}
m_bAdjustingLoopOut = false;
}
}
void LoopingControl::slotLoopOutGoto(double pressed) {
if (pressed == 0.0) {
return;
}
const auto loopOutPosition = m_loopInfo.getValue().endPosition;
if (loopOutPosition.isValid()) {
seekAbs(loopOutPosition);
}
}
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::slotLoopEnabledValueChangeRequest(double value) {
if (!m_pTrack) {
return;
}
if (value > 0.0) {
// Requested to set loop_enabled to 1
if (m_bLoopingEnabled) {
VERIFY_OR_DEBUG_ASSERT(m_pCOLoopEnabled->toBool()) {
m_pCOLoopEnabled->setAndConfirm(1.0);
}
} else {
// Looping is currently disabled, try to enable the loop. In
// contrast to the reloop_toggle CO, we jump in no case.
LoopInfo loopInfo = m_loopInfo.getValue();
if (loopInfo.startPosition.isValid() &&
loopInfo.endPosition.isValid() &&