-
Notifications
You must be signed in to change notification settings - Fork 11
/
filter.cpp
executable file
·92 lines (62 loc) · 1.92 KB
/
filter.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
#include "filter.hpp"
//-------------------------------------------------------------------------------------------
filter::filter()
{
type = 0;
}
//-------------------------------------------------------------------------------------------
filter::~filter()
{
}
//-------------------------------------------------------------------------------------------
void filter::init()
{
buf0=0; buf1=0;
f=0; pc=0; q=0; //filter coefficients
bf0=0; bf1=0; bf2=0; bf3=0; bf4=0; //filter buffers (beware denormals!)
t1=0; t2=0; //temporary buffers
selectivity=90, gain1=0, gain2=0.25, ratio=7, cap=0;
}
//-------------------------------------------------------------------------------------------
double filter::process (double in,int type)
{
if (in==0) { return 0; }
double out;
if (frequency<0) {frequency = 0; }
if (frequency>0.6) {frequency = 0.6; }
gain1 = 1.0/(selectivity + 1.0);
cap= (in + cap*selectivity )*gain1;
in = ((in + cap*ratio)*gain2);
// Moog 24 dB/oct resonant lowpass VCF
// References: CSound source code, Stilson/Smith CCRMA paper.
// Modified by paul.kellett@maxim.abel.co.uk July 2000
// Set coefficients given frequency & resonance [0.0...1.0]
q = 1.0f - frequency;
pc = frequency + 0.8f * frequency * q;
f = pc + pc - 1.0f;
q = resonance * (1.0f + 0.5f * q * (1.0f - q + 5.6f * q * q));
// Filter (in [-1.0...+1.0])
in -= q * bf4; //feedback
t1 = bf1; bf1 = (in + bf0) * pc - bf1 * f;
t2 = bf2; bf2 = (bf1 + t1) * pc - bf2 * f;
t1 = bf3; bf3 = (bf2 + t2) * pc - bf3 * f;
bf4 = (bf3 + t1) * pc - bf4 * f;
bf4 = bf4 - bf4 * bf4 * bf4 * 0.166667f; //clipping
bf0 = in;
// Lowpass output: bf4
// Highpass output: in - bf4;
// Bandpass output: 3.0f * (bf3 - bf4);
if (type==0)
{
out = bf4;
}
if (type==1)
{
out = (3.0f * (bf3 - bf4));
}
if (type==2)
{
out = (in - bf4);
}
return out;
}