-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilter.h
69 lines (60 loc) · 1.73 KB
/
Filter.h
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
#pragma once
class Filter {
private:
// config
const int* samplerate;
const double *octaveNumber; // 0-8 Middle A (a4) is in octave 4
const double *resonance;
const double *lfo;
// state
double pos = 0.0;
double vel = 0.0;
public:
Filter(const int*samplerate, const double *octaveNumber, const double *resonance, const double *lfo) {
this->samplerate = samplerate;
this->octaveNumber = octaveNumber;
this->resonance = resonance;
this->lfo = lfo;
}
double Tick(double input) {
double frequency = 27.5 * pow(2, *octaveNumber);
input /= 2.;
double inputForce = input - pos;
if (pos > 1.0 || pos < -1.0) {
inputForce = 0;
}
double restoringForce = (0 - pos) * 0.1; // low cut
double mass = 2000.0 / (frequency * frequency);
double resistance = vel * -1.0 * 0.01;
double impulse = (inputForce + restoringForce + resistance) / mass / (*samplerate);
vel += impulse;
pos += vel * *resonance;
pos += impulse * (1.0 - *resonance) * 5.0;
if (pos > 1.0 ) {
vel -= (0.01 * 48000.0 / *samplerate);
if (vel < 0.0) {
vel = 0.0;
}
}
if (pos < -1.0 ) {
vel += (0.01 * 48000.0 / *samplerate);
if (vel > 0.0) {
vel = 0.0;
}
}
// end stops
if (pos > 4.0) {
pos = 4.0;
if (vel>0.0) {
vel = 0.0;
}
}
if (pos < -4.0) {
pos = -4.0;
if (vel<0.0) {
vel = 0.0;
}
}
return pos;
}
};