-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathFlangerEffect.cpp
523 lines (437 loc) · 15 KB
/
FlangerEffect.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
#include "FlangerEffect.h"
#include "Tunings.h"
#include <algorithm>
FlangerEffect::FlangerEffect(SurgeStorage *storage, FxStorage *fxdata, pdata *pd)
: Effect(storage, fxdata, pd)
{
haveProcessed = false;
}
FlangerEffect::~FlangerEffect() {}
void FlangerEffect::init()
{
for (int c = 0; c < 2; ++c)
for (int i = 0; i < COMBS_PER_CHANNEL; ++i)
{
lfophase[c][i] = 1.f * (i + 0.5 * c) / COMBS_PER_CHANNEL;
lfosandhtarget[c][i] = 0.0;
}
longphase[0] = 0;
longphase[1] = 0.5;
for (int i = 0; i < LFO_TABLE_SIZE; ++i)
{
sin_lfo_table[i] = sin(2.0 * M_PI * i / LFO_TABLE_SIZE);
saw_lfo_table[i] = 0;
// http://www.cs.cmu.edu/~music/icm-online/readings/panlaws/
double panAngle = 1.0 * i / (LFO_TABLE_SIZE - 1) * M_PI / 2.0;
auto piby2 = M_PI / 2.0;
auto lW = sqrt((piby2 - panAngle) / piby2 * cos(panAngle));
auto rW = sqrt(panAngle * sin(panAngle) / piby2);
}
haveProcessed = false;
}
void FlangerEffect::setvars(bool init) {}
void FlangerEffect::process(float *dataL, float *dataR)
{
if (!haveProcessed)
{
float v0 = *f[fl_voice_basepitch];
if (v0 > 0)
haveProcessed = true;
vzeropitch.startValue(v0);
}
// So here is a flanger with everything fixed
float rate = envelope_rate_linear(-limit_range(*f[fl_rate], -8.f, 10.f)) *
(fxdata->p[fl_rate].temposync ? storage->temposyncratio : 1.f);
for (int c = 0; c < 2; ++c)
{
longphase[c] += rate;
if (longphase[c] >= COMBS_PER_CHANNEL)
longphase[c] -= COMBS_PER_CHANNEL;
}
const float oneoverFreq0 = 1.0f / Tunings::MIDI_0_FREQ;
int mode = *pdata_ival[fl_mode];
int mwave = *pdata_ival[fl_wave];
float depth_val = limit_range(*f[fl_depth], 0.f, 2.f);
float v0 = *f[fl_voice_basepitch];
vzeropitch.newValue(v0);
vzeropitch.process();
v0 = vzeropitch.v;
float averageDelayBase = 0.0;
for (int c = 0; c < 2; ++c)
for (int i = 0; i < COMBS_PER_CHANNEL; ++i)
{
bool lforeset = false;
lfophase[c][i] += rate;
if (lfophase[c][i] > 1)
{
lforeset = true;
lfophase[c][i] -= 1;
}
float lfoout = lfoval[c][i].v;
float thisphase = lfophase[c][i];
if (mode == flm_arp_mix || mode == flm_arp_solo)
{
// arpeggio - everyone needs to use the same phase with the voice swap
thisphase = longphase[c] - (int)longphase[c];
}
switch (mwave)
{
case flw_sine:
{
float ps = thisphase * LFO_TABLE_SIZE;
int psi = (int)ps;
float psf = ps - psi;
int psn = (psi + 1) & LFO_TABLE_MASK;
lfoout = sin_lfo_table[psi] * (1.0 - psf) + psf * sin_lfo_table[psn];
lfoval[c][i].newValue(lfoout);
break;
}
case flw_tri:
lfoout = (2.f * fabs(2.f * thisphase - 1.f) - 1.f);
lfoval[c][i].newValue(lfoout);
break;
case flw_saw: // Gentler than a pure saw, more like a heavily skewed triangle
{
float cutAt = 0.98;
float usephase;
if (thisphase < cutAt)
{
usephase = thisphase / cutAt;
lfoout = usephase * 2.0f - 1.f;
}
else
{
usephase = (thisphase - cutAt) / (1.0 - cutAt);
lfoout = (1.0 - usephase) * 2.f - 1.f;
}
lfoval[c][i].newValue(lfoout);
break;
}
case flw_square:
{
auto cutOffset = 0.02f;
auto m = 2.f / cutOffset;
auto c2 = cutOffset / 2.f;
if (thisphase < 0.5f - c2)
{
lfoout = 1.f;
}
else if ((thisphase >= 0.5 + c2) && (thisphase <= 1.f - cutOffset))
{
lfoout = -1.f;
}
else if ((thisphase > 0.5 - c2) && (thisphase < 0.5 + c2))
{
lfoout = -m * thisphase + (m / 2);
}
else
{
lfoout = (m * thisphase) - (2 * m) + m + 1;
}
lfoval[c][i].newValue(lfoout);
break;
}
case flw_sng: // Sample & Hold random
case flw_snh: // Sample & Glide smoothed random
{
if (lforeset)
{
lfosandhtarget[c][i] = storage->rand_01() - 1.f;
}
if (mwave == flw_sng)
{
// FIXME - exponential creep up. We want to get there in a time related to our
// rate
auto cv = lfoval[c][i].v;
auto diff = (lfosandhtarget[c][i] - cv) * rate * 2;
lfoval[c][i].newValue(cv + diff);
}
else
{
lfoval[c][i].newValue(lfosandhtarget[c][i]);
}
}
break;
}
auto combspace = *f[fl_voice_spacing];
float pitch = v0 + combspace * i;
float nv = samplerate * oneoverFreq0 * storage->note_to_pitch_inv((float)(pitch));
// OK so biggest tap = delaybase[c][i].v * ( 1.0 + lfoval[c][i].v * depth.v ) + 1;
// Assume lfoval is [-1,1] and depth is known
float maxtap = nv * (1.0 + depth_val) + 1;
if (maxtap >= InterpDelay::DELAY_SIZE)
{
nv = nv * 0.999 * InterpDelay::DELAY_SIZE / maxtap;
}
delaybase[c][i].newValue(nv);
averageDelayBase += delaybase[c][i].new_v;
}
averageDelayBase /= (2 * COMBS_PER_CHANNEL);
vzeropitch.process();
float dApprox = rate * samplerate / BLOCK_SIZE * averageDelayBase * depth_val;
depth.newValue(depth_val);
mix.newValue(*f[fl_mix]);
voices.newValue(limit_range(*f[fl_voices], 1.f, 4.f));
float feedbackScale = 0.4 * sqrt((limit_range(dApprox, 2.f, 60.f) + 30) / 100.0);
// Feedback adjust based on mode
switch (mode)
{
case flm_classic:
{
float dv = (voices.v - 1);
feedbackScale += (3.0 - dv) * 0.45 / 3.0;
break;
}
case flm_doppler:
{
float dv = (voices.v - 1);
feedbackScale += (3.0 - dv) * 0.45 / 3.0;
break;
}
case flm_arp_solo:
{
feedbackScale += 0.2; // this is one voice doppler basically
}
case flm_arp_mix:
{
feedbackScale += 0.3; // this is one voice classic basically and the steady signal clamps
// away feedback more
}
default:
break;
}
float fbv = *f[fl_feedback];
if (fbv > 0)
ringout_value = samplerate * 32.0;
else
ringout_value = 1024;
if (mwave == flw_saw || mwave == flw_snh)
{
feedbackScale *= 0.7;
}
if (fbv < 0)
fbv = fbv;
else if (fbv > 1)
fbv = fbv;
else
fbv = sqrt(fbv);
feedback.newValue(feedbackScale * fbv);
fb_lf_damping.newValue(0.4 * *f[fl_damping]);
float combs alignas(16)[2][BLOCK_SIZE];
// Obviously when we implement stereo spread this will be different
for (int c = 0; c < 2; ++c)
{
for (int i = 0; i < COMBS_PER_CHANNEL; ++i)
vweights[c][i] = 0;
if (mode == flm_arp_mix || mode == flm_arp_solo)
{
int ilp = (int)longphase[c];
float flp = longphase[c] - ilp;
if (ilp == COMBS_PER_CHANNEL)
ilp = 0;
if (flp > 0.9)
{
float dt = (flp - 0.9) * 10; // this will be between 0,1
float nxt = sqrt(dt);
float prr = sqrt(1.f - dt);
// std::cout << _D(longphase) << _D(dt) << _D(nxt) << _D(prr) << _D(ilp) << _D(flp)
// << std::endl;
vweights[c][ilp] = prr;
if (ilp == COMBS_PER_CHANNEL - 1)
vweights[c][0] = nxt;
else
vweights[c][ilp + 1] = nxt;
}
else
{
vweights[c][ilp] = 1.f;
}
}
else
{
float voices = limit_range(*f[fl_voices], 1.f, COMBS_PER_CHANNEL * 1.f);
vweights[c][0] = 1.0;
for (int i = 0; i < voices && i < 4; ++i)
vweights[c][i] = 1.0;
int li = (int)voices;
float fi = voices - li;
if (li < 4)
vweights[c][li] = fi;
}
}
for (int b = 0; b < BLOCK_SIZE; ++b)
{
for (int c = 0; c < 2; ++c)
{
combs[c][b] = 0;
for (int i = 0; i < COMBS_PER_CHANNEL; ++i)
{
if (vweights[c][i] > 0)
{
auto tap = delaybase[c][i].v * (1.0 + lfoval[c][i].v * depth.v) + 1;
auto v = idels[c].value(tap);
combs[c][b] += vweights[c][i] * v;
}
lfoval[c][i].process();
delaybase[c][i].process();
}
}
// softclip the feedback to avoid explosive runaways
float fbl = 0.f;
float fbr = 0.f;
if (feedback.v > 0)
{
fbl = clamp1bp(feedback.v * combs[0][b]);
fbr = clamp1bp(feedback.v * combs[1][b]);
fbl = 1.5 * fbl - 0.5 * fbl * fbl * fbl;
fbr = 1.5 * fbr - 0.5 * fbr * fbr * fbr;
// and now we have clipped, apply the damping. FIXME - move to one mul form
float df = limit_range(fb_lf_damping.v, 0.01f, 0.99f);
lpaL = lpaL * (1.0 - df) + fbl * df;
fbl = fbl - lpaL;
lpaR = lpaR * (1.0 - df) + fbr * df;
fbr = fbr - lpaR;
}
auto vl = dataL[b] - fbl;
auto vr = dataR[b] - fbr;
idels[0].push(vl);
idels[1].push(vr);
auto origw = 1.f;
if (mode == flm_doppler || mode == flm_arp_solo)
{
// doppler modes
origw = 0.f;
}
float outl = origw * dataL[b] + mix.v * combs[0][b];
float outr = origw * dataR[b] + mix.v * combs[1][b];
// Some gain heueirstics
float gainadj = 0.0;
switch (mode)
{
case flm_classic:
gainadj = -1 / sqrt(7 - voices.v);
break;
case flm_doppler:
gainadj = -1 / sqrt(8 - voices.v);
break;
case flm_arp_mix:
gainadj = -1 / sqrt(6);
break;
case flm_arp_solo:
gainadj = -1 / sqrt(7);
break;
}
gainadj -= 0.07 * mix.v;
outl = clamp1bp((1.0f + gainadj) * outl);
outr = clamp1bp((1.0f + gainadj) * outr);
outl = 1.5 * outl - 0.5 * outl * outl * outl;
outr = 1.5 * outr - 0.5 * outr * outr * outr;
dataL[b] = outl;
dataR[b] = outr;
depth.process();
mix.process();
feedback.process();
fb_lf_damping.process();
voices.process();
}
width.set_target_smoothed(db_to_linear(*f[fl_width]) / 3);
float M alignas(16)[BLOCK_SIZE], S alignas(16)[BLOCK_SIZE];
encodeMS(dataL, dataR, M, S, BLOCK_SIZE_QUAD);
width.multiply_block(S, BLOCK_SIZE_QUAD);
decodeMS(M, S, dataL, dataR, BLOCK_SIZE_QUAD);
}
float FlangerEffect::InterpDelay::value(float delayBy)
{
// so if delayBy is 19.2
int itap = (int)std::min(delayBy, (float)(DELAY_SIZE - 2)); // this is 19
float fractap = delayBy - itap; // this is .2
int k0 = (k + DELAY_SIZE - itap - 1) & DELAY_SIZE_MASK; // this is 20 back
int k1 = (k + DELAY_SIZE - itap) & DELAY_SIZE_MASK; // this is 19 back
// std::cout << "KDATUM" << k << "," << delayBy << "," << itap << "," << k0 << "," << k1 << ","
// << fractap << std::endl; so the result is
float result =
line[k0] * fractap + line[k1] * (1.0 - fractap); // FIXME move to the one mul form
// std::cout << "id::value " << _D(delayBy) << _D(itap) << _D(fractap) << _D(k) << _D(k0) <<
// _D(k1) << _D(result) << _D(line[k0]) << _D(line[k1]) << std::endl;
return result;
}
void FlangerEffect::suspend() { init(); }
const char *FlangerEffect::group_label(int id)
{
switch (id)
{
case 0:
return "Modulation";
case 1:
return "Combs";
case 2:
return "Feedback";
case 3:
return "Output";
}
return 0;
}
int FlangerEffect::group_label_ypos(int id)
{
switch (id)
{
case 0:
return 1;
case 1:
return 9;
case 2:
return 17;
case 3:
return 23;
}
return 0;
}
void FlangerEffect::init_ctrltypes()
{
Effect::init_ctrltypes();
fxdata->p[fl_mode].set_name("Mode");
fxdata->p[fl_mode].set_type(ct_flangermode);
fxdata->p[fl_wave].set_name("Waveform");
fxdata->p[fl_wave].set_type(ct_fxlfowave);
fxdata->p[fl_rate].set_name("Rate");
fxdata->p[fl_rate].set_type(ct_lforate);
fxdata->p[fl_depth].set_name("Depth");
fxdata->p[fl_depth].set_type(ct_percent);
fxdata->p[fl_voices].set_name("Count");
fxdata->p[fl_voices].set_type(ct_flangervoices);
fxdata->p[fl_voice_basepitch].set_name("Base Pitch");
fxdata->p[fl_voice_basepitch].set_type(ct_flangerpitch);
fxdata->p[fl_voice_spacing].set_name("Spacing");
fxdata->p[fl_voice_spacing].set_type(ct_flangerspacing);
fxdata->p[fl_feedback].set_name("Feedback");
fxdata->p[fl_feedback].set_type(ct_percent);
fxdata->p[fl_damping].set_name("LF Damping");
fxdata->p[fl_damping].set_type(ct_percent);
fxdata->p[fl_width].set_name("Width");
fxdata->p[fl_width].set_type(ct_decibel_narrow);
fxdata->p[fl_mix].set_name("Mix");
fxdata->p[fl_mix].set_type(ct_percent_bipolar);
fxdata->p[fl_wave].posy_offset = -1;
fxdata->p[fl_rate].posy_offset = -1;
fxdata->p[fl_depth].posy_offset = -1;
fxdata->p[fl_voices].posy_offset = 1;
fxdata->p[fl_voice_basepitch].posy_offset = 1;
fxdata->p[fl_voice_spacing].posy_offset = 1;
fxdata->p[fl_feedback].posy_offset = 3;
fxdata->p[fl_damping].posy_offset = 3;
fxdata->p[fl_mode].posy_offset = 23;
fxdata->p[fl_width].posy_offset = 7;
fxdata->p[fl_mix].posy_offset = 7;
}
void FlangerEffect::init_default_values()
{
fxdata->p[fl_rate].val.f = -2.f;
fxdata->p[fl_depth].val.f = 1.f;
fxdata->p[fl_voices].val.f = 4.f;
fxdata->p[fl_voice_basepitch].val.f = 60.f;
fxdata->p[fl_voice_spacing].val.f = 0.f;
fxdata->p[fl_feedback].val.f = 0.f;
fxdata->p[fl_damping].val.f = 0.1f;
fxdata->p[fl_width].val.f = 0.f;
fxdata->p[fl_mix].val.f = 0.8f;
}