-
Notifications
You must be signed in to change notification settings - Fork 182
/
BiQuad.cpp
182 lines (150 loc) · 4.2 KB
/
BiQuad.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/***************************************************/
/*! \class BiQuad
\brief STK biquad (two-pole, two-zero) filter class.
This class implements a two-pole, two-zero digital filter.
Methods are provided for creating a resonance or notch in the
frequency response while maintaining a constant filter gain.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
#include "BiQuad.h"
#include <cmath>
namespace stk {
BiQuad :: BiQuad() : Filter()
{
b_.resize( 3, 0.0 );
a_.resize( 3, 0.0 );
b_[0] = 1.0;
a_[0] = 1.0;
inputs_.resize( 3, 1, 0.0 );
outputs_.resize( 3, 1, 0.0 );
K_ = 0.0;
kSqr_ = 0.0;
denom_ = 1.0;
Stk::addSampleRateAlert( this );
}
BiQuad :: ~BiQuad()
{
Stk::removeSampleRateAlert( this );
}
void BiQuad :: setCoefficients( StkFloat b0, StkFloat b1, StkFloat b2, StkFloat a1, StkFloat a2, bool clearState )
{
b_[0] = b0;
b_[1] = b1;
b_[2] = b2;
a_[1] = a1;
a_[2] = a2;
if ( clearState ) this->clear();
}
void BiQuad :: sampleRateChanged( StkFloat newRate, StkFloat oldRate )
{
if ( !ignoreSampleRateChange_ ) {
oStream_ << "BiQuad::sampleRateChanged: you may need to recompute filter coefficients!";
handleError( StkError::WARNING );
}
}
void BiQuad :: setResonance( StkFloat frequency, StkFloat radius, bool normalize )
{
#if defined(_STK_DEBUG_)
if ( frequency < 0.0 || frequency > 0.5 * Stk::sampleRate() ) {
oStream_ << "BiQuad::setResonance: frequency argument (" << frequency << ") is out of range!";
handleError( StkError::WARNING ); return;
}
if ( radius < 0.0 || radius >= 1.0 ) {
oStream_ << "BiQuad::setResonance: radius argument (" << radius << ") is out of range!";
handleError( StkError::WARNING ); return;
}
#endif
a_[2] = radius * radius;
a_[1] = -2.0 * radius * cos( TWO_PI * frequency / Stk::sampleRate() );
if ( normalize ) {
// Use zeros at +- 1 and normalize the filter peak gain.
b_[0] = 0.5 - 0.5 * a_[2];
b_[1] = 0.0;
b_[2] = -b_[0];
}
else {
b_[0] = 1.0;
b_[1] = 0.0;
b_[2] = 0.0;
}
}
void BiQuad :: setNotch( StkFloat frequency, StkFloat radius )
{
#if defined(_STK_DEBUG_)
if ( frequency < 0.0 || frequency > 0.5 * Stk::sampleRate() ) {
oStream_ << "BiQuad::setNotch: frequency argument (" << frequency << ") is out of range!";
handleError( StkError::WARNING ); return;
}
if ( radius < 0.0 ) {
oStream_ << "BiQuad::setNotch: radius argument (" << radius << ") is negative!";
handleError( StkError::WARNING ); return;
}
#endif
// This method does not attempt to normalize the filter gain.
b_[0] = 1.0;
b_[1] = (StkFloat) -2.0 * radius * cos( TWO_PI * (double) frequency / Stk::sampleRate() );
b_[2] = radius * radius;
a_[1] = 0.0;
a_[2] = 0.0;
}
void BiQuad :: setLowPass( StkFloat fc, StkFloat Q )
{
setCommonFilterValues(fc, Q);
b_[0] = kSqr_ * Q * denom_;
b_[1] = 2 * b_[0];
b_[2] = b_[0];
}
void BiQuad :: setHighPass( StkFloat fc, StkFloat Q )
{
setCommonFilterValues(fc, Q);
b_[0] = Q * denom_;
b_[1] = -2 * b_[0];
b_[2] = b_[0];
}
void BiQuad :: setBandPass( StkFloat fc, StkFloat Q )
{
setCommonFilterValues(fc, Q);
b_[0] = K_ * denom_;
b_[1] = 0.0;
b_[2] = -b_[0];
}
void BiQuad :: setBandReject( StkFloat fc, StkFloat Q )
{
setCommonFilterValues(fc, Q);
b_[0] = Q * (kSqr_ + 1) * denom_;
b_[1] = 2 * Q * (kSqr_ - 1) * denom_;
b_[2] = b_[0];
}
void BiQuad :: setAllPass( StkFloat fc, StkFloat Q )
{
setCommonFilterValues(fc, Q);
b_[0] = a_[2];
b_[1] = a_[1];
b_[2] = 1;
}
void BiQuad :: setEqualGainZeroes( void )
{
b_[0] = 1.0;
b_[1] = 0.0;
b_[2] = -1.0;
}
void BiQuad :: setCommonFilterValues( StkFloat fc, StkFloat Q)
{
#if defined(_STK_DEBUG_)
if ( fc < 0.0 ) {
oStream_ << "BiQuad::updateKValues: fc argument (" << fc << ") is negative!";
handleError( StkError::WARNING ); return;
}
if ( Q < 0.0 ) {
oStream_ << "BiQuad::updateKValues: Q argument (" << Q << ") is negative!";
handleError( StkError::WARNING ); return;
}
#endif
K_ = tan(PI * fc / Stk::sampleRate());
kSqr_ = K_ * K_;
denom_ = 1 / (kSqr_ * Q + K_ + Q);
a_[1] = 2 * Q * (kSqr_ - 1) * denom_;
a_[2] = (kSqr_ * Q - K_ + Q) * denom_;
}
} // stk namespace