-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlooper.cpp
682 lines (605 loc) · 19 KB
/
looper.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
#include "looper.h"
#include "Utility/dsp.h"
using namespace wreath;
using namespace daisysp;
void Looper::Init(int32_t sampleRate, float *buffer, float *buffer2, int32_t maxBufferSamples)
{
sampleRate_ = sampleRate;
readHeads_[0].Init(buffer, buffer2, maxBufferSamples);
readHeads_[1].Init(buffer, buffer2, maxBufferSamples);
writeHead_.Init(buffer, buffer2, maxBufferSamples);
Reset();
movement_ = Movement::NORMAL;
direction_ = Direction::FORWARD;
readRate_ = 1.f;
readHeads_[0].SetRate(readRate_);
readHeads_[1].SetRate(readRate_);
readSpeed_ = sampleRate_ * readRate_;
sampleRateSpeed_ = static_cast<int32_t>(sampleRate_ / readRate_);
readHeads_[0].SetActive(true);
readHeads_[1].SetActive(true);
readHeads_[0].SetLooping(true);
readHeads_[1].SetLooping(true);
writeRate_ = 1.f;
writeHead_.SetRate(writeRate_);
writeSpeed_ = sampleRate_ * writeRate_;
writeHead_.SetActive(true);
writeHead_.SetLooping(true);
}
void Looper::Reset()
{
std::srand(static_cast<unsigned>(time(0)));
eRand_ = std::rand() / (float)RAND_MAX;
readHeads_[0].Reset();
readHeads_[1].Reset();
writeHead_.Reset();
bufferSamples_ = 0;
bufferSeconds_ = 0.f;
loopStart_ = 0;
loopStartSeconds_ = 0.f;
loopEnd_ = 0;
loopLength_ = 0.f;
intLoopEnd_ = 0;
intLoopLength_ = 0;
loopLengthSeconds_ = 0.f;
readPos_ = 0.f;
readPosSeconds_ = 0.f;
writePos_ = 0.f;
}
void Looper::ClearBuffer()
{
writeHead_.ClearBuffer();
}
bool Looper::Buffer(float value)
{
bool end = writeHead_.Buffer(value);
bufferSamples_ = writeHead_.GetBufferSamples();
bufferSeconds_ = bufferSamples_ / static_cast<float>(sampleRate_);
return end;
}
void Looper::StopBuffering()
{
float samples = writeHead_.StopBuffering();
readHeads_[0].InitBuffer(samples);
readHeads_[1].InitBuffer(samples);
loopStart_ = 0;
loopStartSeconds_ = 0.f;
loopEnd_ = bufferSamples_ - 1;
intLoopEnd_ = loopEnd_;
loopLength_ = bufferSamples_;
intLoopLength_ = bufferSamples_;
loopLengthSeconds_ = loopLength_ / sampleRate_;
}
void Looper::StartReading(bool now)
{
if (readingActive_)
{
return;
}
readHeads_[0].SetActive(true);
readHeads_[1].SetActive(true);
readingActive_ = true;
if (!now)
{
startReadingFade.Init(Fader::FadeType::FADE_SINGLE, kSamplesToFadeTrigger, readRate_);
}
}
void Looper::StopReading(bool now)
{
if (!readingActive_)
{
return;
}
if (now)
{
readHeads_[0].SetActive(false);
readHeads_[1].SetActive(false);
readingActive_ = false;
}
else
{
stopReadingFade.Init(Fader::FadeType::FADE_SINGLE, kSamplesToFadeTrigger, readRate_);
}
}
void Looper::StartWriting(bool now)
{
if (writingActive_)
{
return;
}
writingActive_ = true;
if (!now)
{
startWritingFade.Init(Fader::FadeType::FADE_SINGLE, kSamplesToFadeTrigger, writeRate_);
}
}
void Looper::StopWriting(bool now)
{
if (!writingActive_)
{
return;
}
if (now)
{
writingActive_ = false;
}
else
{
stopWritingFade.Init(Fader::FadeType::FADE_SINGLE, kSamplesToFadeTrigger, writeRate_);
}
}
void Looper::Trigger(bool restart)
{
// Update the loop start
readHeads_[0].SetLoopStart(loopStart_);
readHeads_[1].SetLoopStart(loopStart_);
// When a trigger is received while playing we fade out and then in the
// reading, resetting the heads position in between.
if (readingActive_)
{
StopReading(false);
triggered_ = true;
}
// Otherwise, just read from the start.
else if (restart)
{
readHeads_[0].ResetPosition();
readHeads_[1].ResetPosition();
if (loopSync_)
{
writeHead_.ResetPosition();
}
StartReading(false);
}
}
void Looper::SetSamplesToFade(float samples)
{
readHeads_[0].SetSamplesToFade(samples);
readHeads_[1].SetSamplesToFade(samples);
writeHead_.SetSamplesToFade(samples);
}
void Looper::SetLoopStart(float start)
{
// Do not change value if there's a loop fade going.
if (loopFade.IsActive() && loopLength_ > kMinSamplesForFlanger)
{
return;
}
loopLengthGrown_ = start > loopStart_;
loopChanged_ = start != loopStart_;
// Always change the inactive head first.
loopStart_ = readHeads_[!activeReadHead_].SetLoopStart(start);
// Also change the active one if the loop is short or we are not reading.
if (loopLength_ <= kMinSamplesForFlanger || !readingActive_)
{
readHeads_[activeReadHead_].SetLoopStart(loopStart_);
if (loopLength_ <= kMinSamplesForFlanger)
{
// Keep the heads inside the loop.
readHeads_[0].ResetPosition();
readHeads_[1].ResetPosition();
if (loopSync_)
{
writeHead_.ResetPosition();
}
}
}
intLoopStart_ = loopStart_;
loopStartSeconds_ = loopStart_ / static_cast<float>(sampleRate_);
loopEnd_ = readHeads_[!activeReadHead_].GetLoopEnd();
intLoopEnd_ = loopEnd_;
crossPointFound_ = false;
// In delay mode, keep the loop synched.
if (loopSync_)
{
writeHead_.SetLoopStart(loopStart_);
}
}
void Looper::SetLoopLength(float length)
{
// Do not change value if there's a loop fade going.
if (loopFade.IsActive() && loopLength_ > kMinSamplesForFlanger)
{
return;
}
loopLengthGrown_ = length > loopLength_;
loopChanged_ = length != loopLength_;
// Always change the inactive head first.
loopLength_ = readHeads_[!activeReadHead_].SetLoopLength(length);
// Also change the active one if the loop is short or we are not reading.
if (length <= kMinSamplesForFlanger || !readingActive_)
{
readHeads_[activeReadHead_].SetLoopLength(loopLength_);
}
intLoopLength_ = loopLength_;
loopLengthSeconds_ = loopLength_ / sampleRate_;
loopEnd_ = readHeads_[!activeReadHead_].GetLoopEnd();
intLoopEnd_ = loopEnd_;
crossPointFound_ = false;
// In delay mode, keep the loop synched.
if (loopSync_)
{
writeHead_.SetLoopLength(loopLength_);
}
}
void Looper::SetReadRate(float rate)
{
readHeads_[0].SetRate(rate);
readHeads_[1].SetRate(rate);
readRate_ = rate;
readSpeed_ = sampleRate_ * readRate_;
sampleRateSpeed_ = static_cast<int32_t>(sampleRate_ / readRate_);
crossPointFound_ = false;
// In delay mode, when setting the rate back to 1 we flag for a realignment
// of the heads at the next loop to keep the correct delay time.
if (loopSync_ && rate == 1.f)
{
mustSyncHeads_ = true;
}
}
void Looper::SetWriteRate(float rate)
{
writeHead_.SetRate(rate);
writeRate_ = rate;
writeSpeed_ = sampleRate_ * writeRate_;
crossPointFound_ = false;
}
void Looper::SetMovement(Movement movement)
{
readHeads_[0].SetMovement(movement);
readHeads_[1].SetMovement(movement);
movement_ = movement;
}
void Looper::SetDirection(Direction direction)
{
readHeads_[0].SetDirection(direction);
readHeads_[1].SetDirection(direction);
direction_ = direction;
crossPointFound_ = false;
}
void Looper::SetReadPos(float position)
{
readHeads_[0].SetIndex(position);
readHeads_[1].SetIndex(position);
readPos_ = position;
crossPointFound_ = false;
}
void Looper::SetWritePos(float position)
{
writeHead_.SetIndex(position);
writePos_ = position;
crossPointFound_ = false;
}
void Looper::SetLooping(bool looping)
{
readHeads_[0].SetLooping(looping);
readHeads_[1].SetLooping(looping);
looping_ = looping;
crossPointFound_ = false;
}
void Looper::SetLoopSync(bool loopSync)
{
// If loopSync = true it means we're in delay mode, so the writing head must
// loop when the reading head does.
if (loopSync_ && !loopSync)
{
writeHead_.SetLoopLength(bufferSamples_);
}
else if (!loopSync_ && loopSync)
{
writeHead_.SetLoopLength(readHeads_[activeReadHead_].GetLoopLength());
}
loopSync_ = loopSync;
readHeads_[0].SetLoopSync(loopSync_);
readHeads_[1].SetLoopSync(loopSync_);
writeHead_.SetLoopSync(loopSync_);
crossPointFound_ = false;
}
float Looper::Read()
{
float value = readHeads_[activeReadHead_].Read();
// Fade in reading.
if (startReadingFade.IsActive())
{
startReadingFade.Process(0, value);
value = startReadingFade.GetOutput();
}
// Fade out reading.
else if (stopReadingFade.IsActive())
{
if (Fader::FadeStatus::ENDED == stopReadingFade.Process(value, 0))
{
readHeads_[0].SetActive(false);
readHeads_[1].SetActive(false);
readingActive_ = false;
// If the looper had been re-triggered while playing, at the end of
// the fade out we reset the heads and then fade in reading.
if (triggered_)
{
readHeads_[0].ResetPosition();
readHeads_[1].ResetPosition();
if (loopSync_)
{
writeHead_.ResetPosition();
}
StartReading(false);
triggered_ = false;
}
}
value = stopReadingFade.GetOutput();
}
else if (!readingActive_)
{
return 0.f;
}
if (loopFade.IsActive())
{
if (Fader::FadeStatus::ENDED == loopFade.Process(readHeads_[!activeReadHead_].Read(), value))
{
readHeads_[!activeReadHead_].SetLoopStartAndLength(loopStart_, loopLength_);
readHeads_[!activeReadHead_].SetIndex(readPos_);
if (loopSync_)
{
writeHead_.SetIndex(readPos_);
}
}
value = loopFade.GetOutput();
}
if (freeze_ > 0)
{
// Crossfade with the frozen buffer.
value = Fader::EqualCrossFade(value, readHeads_[activeReadHead_].ReadFrozen(), freeze_);
}
// Handle fade on re-triggering.
if (triggerFade.IsActive())
{
triggerFade.Process(0, value);
value = triggerFade.GetOutput();
}
return value;
}
void Looper::Write(float input)
{
// Fade in writing.
if (startWritingFade.IsActive())
{
startWritingFade.Process(0, input);
input = startWritingFade.GetOutput();
}
// Fade out writing.
else if (stopWritingFade.IsActive())
{
if (Fader::FadeStatus::ENDED == stopWritingFade.Process(input, 0))
{
writingActive_ = false;
}
input = stopWritingFade.GetOutput();
}
else if (!writingActive_)
{
return;
}
if (freeze_ < 1.f && headsCrossFade.IsActive())
{
headsCrossFade.Process(input, writeHead_.Read());
input = headsCrossFade.GetOutput();
}
writeHead_.Write(input);
}
float Looper::Degrade(float input)
{
if (degradation_ > 0.f)
{
float d = 1.f - ((std::rand() / (float)RAND_MAX) * degradation_) * 0.5f;
// Use an Euclidean rhythm generator to apply degradation at fixed
// buffer points
return writeHead_.BresenhamEuclidean(eRand_ * 64, degradation_) ? input : input * d;
}
return input;
}
void Looper::FadeReadingToResetPosition()
{
if (loopFade.IsActive())
{
return;
}
if (loopLengthGrown_)
{
readHeads_[activeReadHead_].ResetPosition();
// When going backwards, if we don't have enough space for the fading of
// the inactive reading head, we must reset its position.
if (!IsGoingForward() && loopStart_ <= readHeads_[!activeReadHead_].GetSamplesToFade())
{
readHeads_[!activeReadHead_].ResetPosition();
}
loopLengthGrown_ = false;
}
else
{
readHeads_[!activeReadHead_].ResetPosition();
// When going backwards, if we don't have enough space for the fading of
// the active reading head, we must reset its position.
if (!IsGoingForward() && loopStart_ <= readHeads_[activeReadHead_].GetSamplesToFade())
{
readHeads_[activeReadHead_].ResetPosition();
}
}
// TODO: Probably the samples to fade could be calculated more precisely.
// Active: length - buffer
// inactive: length
float samples = std::min(readHeads_[activeReadHead_].GetSamplesToFade(), readHeads_[!activeReadHead_].GetSamplesToFade());
loopFade.Init(Fader::FadeType::FADE_SINGLE, samples, readRate_);
activeReadHead_ = !activeReadHead_;
}
void Looper::UpdateReadPos()
{
Head::Action action = readHeads_[activeReadHead_].UpdatePosition();
// When the loop length shrunk, the inactive reading head dictates when
// looping occurs, so we need to update its position as well.
if (loopChanged_ && !loopLengthGrown_)
{
action = readHeads_[!activeReadHead_].UpdatePosition();
}
// Otherwise, just sync it with the active reading head.
else
{
readHeads_[!activeReadHead_].SetIndex(readHeads_[activeReadHead_].GetPosition());
readHeads_[!activeReadHead_].SetOffset(readHeads_[activeReadHead_].GetOffset());
}
// Note that in delay mode we don't need to fade the loop, and we wouldn't do
// it anyway because it'd need a few samples from outside the loop and these
// samples are probably unrelated.Fading when the loop changes yields the
// same problem, but it sounds better than if we don't.
// Also note that when going backwards, when the loop changes we fade right
// away.
if ((loopChanged_ && !IsGoingForward()) || (Head::Action::LOOP == action && loopLength_ > kMinSamplesForFlanger && (loopChanged_ || (!loopSync_ && loopLength_ < bufferSamples_))))
{
FadeReadingToResetPosition();
loopChanged_ = false;
}
// Here we handle normal looping in delay mode or when the loop length is
// small.
else if (Head::Action::LOOP == action && (loopLength_ <= kMinSamplesForFlanger || loopSync_) && loopLength_ < bufferSamples_)
{
readHeads_[0].ResetPosition();
readHeads_[1].ResetPosition();
}
else if (Head::Action::STOP == action)
{
StopReading(false);
}
readPos_ = readHeads_[activeReadHead_].GetPosition();
readPosSeconds_ = readPos_ / sampleRate_;
}
void Looper::UpdateWritePos()
{
Head::Action action = writeHead_.UpdatePosition();
writePos_ = writeHead_.GetIntPosition();
if (Head::Action::LOOP == action && loopSync_)
{
// Loop the writing head.
if (loopLength_ < bufferSamples_)
{
writeHead_.ResetPosition();
}
// In delay mode, keep in sync the reading and the writing heads'
// position each time the latter reaches either the start or the end of
// the loop (depending on the reading direction).
if (mustSyncHeads_)
{
readHeads_[0].ResetPosition();
readHeads_[1].ResetPosition();
mustSyncHeads_ = false;
}
}
// When reading and writing speeds differ or we're going backwards, we
// calculate the point where the two heads will meet and set up a writing
// fade at that point.
if (freeze_ < 1.f && !headsCrossFade.IsActive() && (readSpeed_ != writeSpeed_ || !IsGoingForward()))
{
headsDistance_ = CalculateDistance(readPos_, writePos_, readSpeed_, writeSpeed_, direction_);
// Calculate the cross point when the two heads are close enough.
if (!crossPointFound_ && headsDistance_ > 0 && headsDistance_ <= writeHead_.GetSamplesToFade() * 2)
{
CalculateCrossPoint();
}
if (crossPointFound_)
{
float samples = CalculateDistance(writePos_, crossPoint_, writeSpeed_, 0, Direction::FORWARD);
// If the condition are met, set up the cross point fade.
if (samples > 0 && samples <= writeHead_.GetSamplesToFade())
{
crossPointFound_ = false;
headsCrossFade.Init(Fader::FadeType::FADE_OUT_IN, samples * 2, writeRate_);
}
}
}
}
void Looper::ToggleDirection()
{
direction_ = readHeads_[0].ToggleDirection();
readHeads_[1].ToggleDirection();
}
void Looper::SetFreeze(float amount)
{
freeze_ = amount;
readHeads_[0].SetFreeze(amount);
readHeads_[1].SetFreeze(amount);
writeHead_.SetFreeze(amount);
}
void Looper::SetDegradation(float amount)
{
degradation_ = amount;
}
float Looper::CalculateDistance(float a, float b, float aSpeed, float bSpeed, Direction direction)
{
if (a == b)
{
return 0;
}
if (loopStart_ > loopEnd_)
{
// Broken loop, case where a is in the second segment and b in the first.
if (a >= loopStart_ && b <= loopEnd_)
{
return (IsGoingForward() && aSpeed > bSpeed) ? loopLength_ - ((loopEnd_ - b) + (a - loopStart_)) : (loopEnd_ - b) + (a - loopStart_);
}
// Broken loop, case where b is in the second segment and a in the first.
if (b >= loopStart_ && a <= loopEnd_)
{
return (!IsGoingForward() || bSpeed > aSpeed) ? loopLength_ - ((loopEnd_ - a) + (b - loopStart_)) : (loopEnd_ - a) + (b - loopStart_);
}
}
if (a > b)
{
return (IsGoingForward() && aSpeed > bSpeed) ? loopLength_ - (a - b) : a - b;
}
return (!IsGoingForward() || bSpeed > aSpeed) ? loopLength_ - (b - a) : b - a;
}
void Looper::CalculateCrossPoint()
{
// Do not calculate the cross point if the write head is outside of
// the loop (this is especially true in looper mode, when it roams
// along all the buffer).
if ((loopEnd_ > loopStart_ && (writePos_ < loopStart_ || writePos_ > loopEnd_)) || (loopStart_ > loopEnd_ && writePos_ < loopStart_ && writePos_ > loopEnd_))
{
return;
}
float relSpeed{writeSpeed_ > readSpeed_ ? writeSpeed_ - readSpeed_ : readSpeed_ - writeSpeed_};
if (!IsGoingForward())
{
relSpeed = writeSpeed_ + readSpeed_;
}
float deltaTime = headsDistance_ / relSpeed;
crossPoint_ = writePos_ + writeSpeed_ * deltaTime;
// Normal loop
if (loopEnd_ > loopStart_)
{
// Wrap the crossing point if it's outside of the loop.
if (crossPoint_ > loopEnd_)
{
crossPoint_ = loopStart_ + std::fmod(crossPoint_, loopLength_);
}
else if (crossPoint_ < loopStart_)
{
crossPoint_ = loopStart_ + std::fmod(crossPoint_ + loopStart_, loopLength_);
}
}
// Inverted loop
else
{
// Wrap the crossing point if it's outside of the buffer.
if (crossPoint_ >= bufferSamples_)
{
crossPoint_ = std::fmod(crossPoint_, loopLength_);
}
// If the cross point falls just between the loop's start and end point,
// nudge it forward.
if (crossPoint_ > loopEnd_ && crossPoint_ < loopStart_)
{
crossPoint_ = loopStart_ + (crossPoint_ - loopEnd_);
}
}
crossPoint_ = std::floor(crossPoint_);
crossPointFound_ = true;
}