This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowPassFilter.pde
67 lines (58 loc) · 1.69 KB
/
lowPassFilter.pde
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
class lowPassFilter{
int sampleRate;
float filterFrequency;
float filterResonance;
float c;
float a1;
float a2;
float a3;
float b1;
float b2;
float drive;
ArrayList preFilter;
ArrayList postFilter;
lowPassFilter( int _sampleRate){
this.sampleRate = _sampleRate;
this.filterFrequency = sampleRate/2;
this.filterResonance = 1;
this.preFilter = new ArrayList();
this.postFilter = new ArrayList();
this.drive = 0;
for(int i = 0; i < 3; i++){
this.preFilter.add(0.);
this.postFilter.add(0.);
}
}
void setFrequency(float _frequency){
this.filterFrequency = _frequency;
}
float getFrequency(){
return this.filterFrequency;
}
void setResonance(float _resonance){
this.filterResonance = _resonance;
}
void setDrive(float _drive){
this.drive = _drive;
}
float getValue(float sample){
float a = sin(((this.drive+1)/101)*(PI/2)) ;
float k = 2*a/(1-a);
this.c = 1.0 / tan(PI * this.filterFrequency / this.sampleRate);
this.a1 = 1.0 / ( 1.0 + this.filterResonance * this.c + this.c * this.c);
this.a2 = 2* this.a1;
this.a3 = this.a1;
this.b1 = 2.0 * ( 1.0 - this.c*this.c) * this.a1;
this.b2 = ( 1.0 - this.filterResonance * this.c + this.c * this.c) * this.a1;
this.preFilter.add(sample);
this.preFilter.remove(0);
sample = this.a1 * (Float)preFilter.get(2) +
this.a2 * (Float)preFilter.get(1) +
this.a3 * (Float)preFilter.get(0) -
this.b1 * (Float)postFilter.get(2) -
this.b2 * (Float)postFilter.get(1);
this.postFilter.add(sample);
this.postFilter.remove(0);
return sample*0.09999;
}
}