-
Notifications
You must be signed in to change notification settings - Fork 182
/
TwoPole.cpp
76 lines (62 loc) · 2.07 KB
/
TwoPole.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
/***************************************************/
/*! \class TwoPole
\brief STK two-pole filter class.
This class implements a two-pole digital filter. A method is
provided for creating a resonance in the frequency response while
maintaining a nearly constant filter gain.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
#include "TwoPole.h"
#include <cmath>
namespace stk {
TwoPole :: TwoPole( void )
{
b_.resize( 1 );
a_.resize( 3 );
inputs_.resize( 1, 1, 0.0 );
outputs_.resize( 3, 1, 0.0 );
b_[0] = 1.0;
a_[0] = 1.0;
Stk::addSampleRateAlert( this );
}
TwoPole :: ~TwoPole()
{
Stk::removeSampleRateAlert( this );
}
void TwoPole :: sampleRateChanged( StkFloat newRate, StkFloat oldRate )
{
if ( !ignoreSampleRateChange_ ) {
oStream_ << "TwoPole::sampleRateChanged: you may need to recompute filter coefficients!";
handleError( StkError::WARNING );
}
}
void TwoPole :: setResonance( StkFloat frequency, StkFloat radius, bool normalize )
{
#if defined(_STK_DEBUG_)
if ( frequency < 0.0 || frequency > 0.5 * Stk::sampleRate() ) {
oStream_ << "TwoPole::setResonance: frequency argument (" << frequency << ") is out of range!";
handleError( StkError::WARNING ); return;
}
if ( radius < 0.0 || radius >= 1.0 ) {
oStream_ << "TwoPole::setResonance: radius argument (" << radius << ") is out of range!";
handleError( StkError::WARNING ); return;
}
#endif
a_[2] = radius * radius;
a_[1] = (StkFloat) -2.0 * radius * cos(TWO_PI * frequency / Stk::sampleRate());
if ( normalize ) {
// Normalize the filter gain ... not terribly efficient.
StkFloat real = 1 - radius + (a_[2] - radius) * cos(TWO_PI * 2 * frequency / Stk::sampleRate());
StkFloat imag = (a_[2] - radius) * sin(TWO_PI * 2 * frequency / Stk::sampleRate());
b_[0] = sqrt( pow(real, 2) + pow(imag, 2) );
}
}
void TwoPole :: setCoefficients( StkFloat b0, StkFloat a1, StkFloat a2, bool clearState )
{
b_[0] = b0;
a_[1] = a1;
a_[2] = a2;
if ( clearState ) this->clear();
}
} // stk namespace