-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtapdistortion.cpp
76 lines (67 loc) · 2.13 KB
/
tapdistortion.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
#include "tapdistortion.h"
TapDistortion::TapDistortion() {
is_active = false;
srate = 0;
meter = 0.f;
rdrive = rbdr = kpa = kpb = kna = knb = ap = an = imr = kc = srct = sq = pwrq = prev_med = prev_out = 0.f;
drive_old = blend_old = -1.f;
over = 1;
}
TapDistortion::~TapDistortion() { }
void TapDistortion::activate() {
is_active = true;
set_params(0.f, 0.f);
}
void TapDistortion::deactivate() {
is_active = false;
}
void TapDistortion::set_params(float blend, float drive) {
// set distortion coeffs
if ((drive_old != drive) || (blend_old != blend)) {
rdrive = 12.0f / drive;
rbdr = rdrive / (10.5f - blend) * 780.0f / 33.0f;
kpa = D(2.0f * (rdrive * rdrive) - 1.0f) + 1.0f;
kpb = (2.0f - kpa) / 2.0f;
ap = ((rdrive * rdrive) - kpa + 1.0f) / 2.0f;
kc = kpa / D(2.0f * D(2.0f * (rdrive * rdrive) - 1.0f) - 2.0f * rdrive * rdrive);
srct = (0.1f * srate) / (0.1f * srate + 1.0f);
sq = kc * kc + 1.0f;
knb = -1.0f * rbdr / D(sq);
kna = 2.0f * kc * rbdr / D(sq);
an = rbdr * rbdr / sq;
imr = 2.0f * knb + D(2.0f * kna + 4.0f * an - 1.0f);
pwrq = 2.0f / (imr + 1.0f);
drive_old = drive;
blend_old = blend;
}
}
void TapDistortion::set_sample_rate(uint32_t sr) {
srate = sr;
over = srate * 2 > 96000 ? 1 : 2;
// resampler.set_params(srate, over, 2);
}
#include <iostream>
float TapDistortion::process(float in) {
// double *samples = resampler.upsample((double)in);
meter = 0.f;
// for (int o = 0; o < over; o++) {
float proc = in; // samples[o];
float med;
if (proc >= 0.0f) {
med = (D(ap + proc * (kpa - proc)) + kpb) * pwrq;
} else {
med = (D(an - proc * (kna + proc)) + knb) * pwrq * -1.0f;
}
proc = srct * (med - prev_med + prev_out);
prev_med = M(med);
prev_out = M(proc);
// samples[o] = proc;
// std::cout<<med<<std::endl;
meter = std::max(meter, proc);
//}
// float out = (float)resampler.downsample(samples);
return proc; // out;
}
float TapDistortion::get_distortion_level() {
return meter;
}