-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrosic_BiquadFilter.h
116 lines (82 loc) · 2.73 KB
/
rosic_BiquadFilter.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
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
#ifndef rosic_BiquadFilter_h
#define rosic_BiquadFilter_h
/**
This is an implementation of a simple one-pole filter unit.
*/
class BiquadFilter
{
public:
/** Enumeration of the available filter modes. */
enum modes
{
BYPASS = 0,
LOWPASS6,
LOWPASS12,
HIGHPASS6,
HIGHPASS12,
BANDPASS,
BANDREJECT,
PEAK,
LOW_SHELF,
//HIGH_SHELF,
//ALLPASS,
NUM_FILTER_MODES
};
//---------------------------------------------------------------------------------------------
// construction/destruction:
/** Constructor. */
BiquadFilter();
//---------------------------------------------------------------------------------------------
// parameter settings:
/** Sets the sample-rate (in Hz) at which the filter runs. */
void setSampleRate(float newSampleRate);
/** Sets the filter mode as one of the values in enum modes. */
void setMode(int newMode);
/** Sets the center frequency in Hz. */
void setFrequency(float newFrequency);
/** Sets the boost/cut gain in dB. */
void setGain(float newGain);
/** Sets the bandwidth in octaves. */
void setBandwidth(float newBandwidth);
//---------------------------------------------------------------------------------------------
// inquiry
/** Sets the filter mode as one of the values in enum modes. */
int getMode() const { return mode; }
/** Returns the center frequency in Hz. */
float getFrequency() const { return frequency; }
/** Returns the boost/cut gain in dB. */
float getGain() const { return gain; }
/** Returns the bandwidth in octaves. */
float getBandwidth() const { return bandwidth; }
//---------------------------------------------------------------------------------------------
// audio processing:
/** Calculates a single filtered output-sample. */
inline float getSample(float in);
//---------------------------------------------------------------------------------------------
// others:
/** Resets the internal buffers (for the \f$ x[n-1], y[n-1] \f$-samples) to zero. */
void reset();
//=============================================================================================
protected:
// internal functions:
void calcCoeffs(); // calculates filter coefficients from filter parameters
float b0, b1, b2, a1, a2;
float x1, x2, y1, y2;
float frequency, gain, bandwidth;
float sampleRate;
int mode;
};
//-----------------------------------------------------------------------------------------------
// inlined functions:
inline float BiquadFilter::getSample(float in)
{
// calculate the output sample:
float y = b0*in + b1*x1 + b2*x2 + a1*y1 + a2*y2 + TINY;
// update the buffer variables:
x2 = x1;
x1 = in;
y2 = y1;
y1 = y;
return y;
}
#endif // rosic_BiquadFilter_h