mirror of
https://github.com/thestk/stk
synced 2026-01-11 20:11:52 +00:00
Merge pull request #121 from navin-neu/biquad-filter-types
add support for different filter types in biquad
This commit is contained in:
@@ -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 )
|
||||
@@ -89,8 +98,57 @@ void BiQuad :: setNotch( StkFloat frequency, StkFloat radius )
|
||||
#endif
|
||||
|
||||
// This method does not attempt to normalize the filter gain.
|
||||
b_[2] = radius * radius;
|
||||
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 )
|
||||
@@ -100,4 +158,25 @@ void BiQuad :: setEqualGainZeroes( void )
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user