add support for different filter types in biquad

This commit is contained in:
Navin K
2022-03-21 18:32:51 -04:00
parent 893ff3d954
commit 92d81d67c2
2 changed files with 123 additions and 0 deletions

View File

@@ -24,6 +24,10 @@ BiQuad :: BiQuad() : Filter()
inputs_.resize( 3, 1, 0.0 );
outputs_.resize( 3, 1, 0.0 );
K_ = 0.0;
kSqr_ = 0.0;
denom_ = 1.0;
Stk::addSampleRateAlert( this );
}
@@ -73,6 +77,11 @@ void BiQuad :: setResonance( StkFloat frequency, StkFloat radius, bool normalize
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 )
@@ -93,6 +102,51 @@ void BiQuad :: setNotch( StkFloat frequency, StkFloat radius )
b_[1] = (StkFloat) -2.0 * radius * cos( TWO_PI * (double) frequency / Stk::sampleRate() );
}
void BiQuad :: setLowPass( StkFloat frequency, StkFloat Q )
{
setCommonFilterValues(frequency, Q);
b_[0] = kSqr_ * Q * denom_;
b_[1] = 2 * b_[0];
b_[2] = b_[0];
}
void BiQuad :: setHighPass( StkFloat frequency, StkFloat Q )
{
setCommonFilterValues(frequency, Q);
b_[0] = Q * denom_;
b_[1] = -2 * b_[0];
b_[2] = b_[0];
}
void BiQuad :: setBandPass( StkFloat frequency, StkFloat Q )
{
setCommonFilterValues(frequency, Q);
b_[0] = K_ * denom_;
b_[1] = 0.0;
b_[2] = -b_[0];
}
void BiQuad :: setBandReject( StkFloat frequency, StkFloat Q )
{
setCommonFilterValues(frequency, Q);
b_[0] = Q * (kSqr_ + 1) * denom_;
b_[1] = 2 * Q * (kSqr_ - 1) * denom_;
b_[2] = b_[0];
}
void BiQuad :: setAllPass( StkFloat frequency, StkFloat Q )
{
setCommonFilterValues(frequency, Q);
b_[0] = a_[2];
b_[1] = a_[1];
b_[2] = 1;
}
void BiQuad :: setEqualGainZeroes( void )
{
b_[0] = 1.0;
@@ -100,4 +154,25 @@ void BiQuad :: setEqualGainZeroes( void )
b_[2] = -1.0;
}
void BiQuad :: setCommonFilterValues( StkFloat frequency, StkFloat Q)
{
#if defined(_STK_DEBUG_)
if ( frequency < 0.0 ) {
oStream_ << "BiQuad::updateKValues: frequency argument (" << frequency << ") 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 * frequency / 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