123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- /*
- * eqfilter.cpp - defination of EqFilterclass.
- *
- * Copyright (c) 2014 David French <dave/dot/french3/at/googlemail/dot/com>
- *
- * This file is part of LMMS - https://lmms.io
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with this program (see COPYING); if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 USA.
- *
- */
- #ifndef EQFILTER_H
- #define EQFILTER_H
- #include "BasicFilters.h"
- #include "lmms_math.h"
- ///
- /// \brief The EqFilter class.
- /// A wrapper for the StereoBiQuad class, giving it freq, res, and gain controls.
- /// Used on a per channel per frame basis with recalculation of coefficents
- /// upon parameter changes. The intention is to use this as a bass class, children override
- /// the calcCoefficents() function, providing the coefficents a1, a2, b0, b1, b2.
- ///
- class EqFilter
- {
- public:
- EqFilter() :
- m_sampleRate(0),
- m_freq(0),
- m_res(0),
- m_gain(0),
- m_bw(0)
- {
- }
- virtual inline void setSampleRate( int sampleRate )
- {
- if( sampleRate != m_sampleRate )
- {
- m_sampleRate = sampleRate;
- calcCoefficents();
- }
- }
- virtual inline void setFrequency( float freq ){
- if ( freq != m_freq )
- {
- m_freq = freq;
- calcCoefficents();
- }
- }
- virtual inline void setQ( float res )
- {
- if ( res != m_res )
- {
- m_res = res;
- calcCoefficents();
- }
- }
- virtual inline void setGain( float gain )
- {
- if ( gain != m_gain )
- {
- m_gain = gain;
- calcCoefficents();
- }
- }
- virtual inline void setParameters( float sampleRate, float freq, float res, float gain )
- {
- bool hasChanged = ( sampleRate != m_sampleRate ||
- freq != m_freq ||
- res != m_res ||
- gain != m_gain );
- if ( hasChanged )
- {
- m_sampleRate = sampleRate;
- m_freq = freq;
- m_res = res;
- m_gain = gain;
- calcCoefficents();
- }
- }
- ///
- /// \brief update
- /// filters using two BiQuads, then crossfades,
- /// depending on on percentage of period processes
- /// \param in
- /// \param ch
- /// \param frameProgress percentage of frame processed
- /// \return
- ///
- inline float update( float in, ch_cnt_t ch, float frameProgress)
- {
- float initailF = m_biQuadFrameInitial.update( in, ch );
- float targetF = m_biQuadFrameTarget.update( in, ch );
- if(frameProgress > 0.99999 )
- {
- m_biQuadFrameInitial= m_biQuadFrameTarget;
- }
- return (1.0f-frameProgress) * initailF + frameProgress * targetF;
- }
- protected:
- ///
- /// \brief calcCoefficents
- /// Override this in child classes to provide the coefficents, based on
- /// Freq, Res and Gain
- virtual void calcCoefficents(){
- setCoeffs( 0, 0, 0, 0, 0 );
- }
- inline void setCoeffs( float a1, float a2, float b0, float b1, float b2 )
- {
- m_biQuadFrameTarget.setCoeffs( a1, a2, b0, b1, b2 );
- }
- float m_sampleRate;
- float m_freq;
- float m_res;
- float m_gain;
- float m_bw;
- StereoBiQuad m_biQuadFrameInitial;
- StereoBiQuad m_biQuadFrameTarget;
- };
- ///
- /// \brief The EqHp12Filter class
- /// A 2 pole High Pass Filter
- /// Coefficent calculations from http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
- class EqHp12Filter : public EqFilter
- {
- public :
- virtual void calcCoefficents()
- {
- // calc intermediate
- float w0 = F_2PI * m_freq / m_sampleRate;
- float c = cosf( w0 );
- float s = sinf( w0 );
- float alpha = s / ( 2 * m_res );
- float a0, a1, a2, b0, b1, b2; // coeffs to calculate
- //calc coefficents
- b0 = ( 1 + c ) * 0.5;
- b1 = ( -( 1 + c ) );
- b2 = ( 1 + c ) * 0.5;
- a0 = 1 + alpha;
- a1 = ( -2 * c );
- a2 = 1 - alpha;
- //normalise
- b0 /= a0;
- b1 /= a0;
- b2 /= a0;
- a1 /= a0;
- a2 /= a0;
- a0 = 1;
- setCoeffs( a1, a2, b0, b1, b2 );
- }
- };
- ///
- /// \brief The EqLp12Filter class.
- /// A 2 pole low pass filter
- /// Coefficent calculations from http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
- ///
- class EqLp12Filter : public EqFilter
- {
- public :
- virtual void calcCoefficents()
- {
- // calc intermediate
- float w0 = F_2PI * m_freq / m_sampleRate;
- float c = cosf( w0 );
- float s = sinf( w0 );
- float alpha = s / ( 2 * m_res );
- float a0, a1, a2, b0, b1, b2; // coeffs to calculate
- //calc coefficents
- b0 = ( 1 - c ) * 0.5;
- b1 = 1 - c;
- b2 = ( 1 - c ) * 0.5;
- a0 = 1 + alpha;
- a1 = -2 * c;
- a2 = 1 - alpha;
- //normalise
- b0 /= a0;
- b1 /= a0;
- b2 /= a0;
- a1 /= a0;
- a2 /= a0;
- a0 = 1;
- setCoeffs( a1, a2, b0, b1, b2 );
- }
- };
- ///
- /// \brief The EqPeakFilter class
- /// A Peak Filter
- /// Coefficent calculations from http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
- ///
- class EqPeakFilter : public EqFilter
- {
- public:
- virtual void calcCoefficents()
- {
- // calc intermediate
- float w0 = F_2PI * m_freq / m_sampleRate;
- float c = cosf( w0 );
- float s = sinf( w0 );
- float A = pow( 10, m_gain * 0.025);
- float alpha = s * sinh( log( 2 ) / 2 * m_bw * w0 / sinf(w0) );
- float a0, a1, a2, b0, b1, b2; // coeffs to calculate
- //calc coefficents
- b0 = 1 + alpha*A;
- b1 = -2*c;
- b2 = 1 - alpha*A;
- a0 = 1 + alpha/A;
- a1 = -2*c;
- a2 = 1 - alpha/A;
- //normalise
- b0 /= a0;
- b1 /= a0;
- b2 /= a0;
- a1 /= a0;
- a2 /= a0;
- a0 = 1;
- setCoeffs( a1, a2, b0, b1, b2 );
- }
- virtual inline void setParameters( float sampleRate, float freq, float bw, float gain )
- {
- bool hasChanged = false;
- if( sampleRate != m_sampleRate )
- {
- m_sampleRate = sampleRate;
- hasChanged = true;
- }
- if ( freq != m_freq )
- {
- m_freq = freq;
- hasChanged = true;
- }
- if ( bw != m_bw )
- {
- m_bw = bw;
- hasChanged = true;
- }
- if ( gain != m_gain )
- {
- m_gain = gain;
- hasChanged = true;
- }
- if ( hasChanged ) { calcCoefficents(); }
- }
- };
- class EqLowShelfFilter : public EqFilter
- {
- public :
- virtual void calcCoefficents()
- {
- // calc intermediate
- float w0 = F_2PI * m_freq / m_sampleRate;
- float c = cosf( w0 );
- float s = sinf( w0 );
- float A = pow( 10, m_gain * 0.025);
- // float alpha = s / ( 2 * m_res );
- float beta = sqrt( A ) / m_res;
- float a0, a1, a2, b0, b1, b2; // coeffs to calculate
- //calc coefficents
- b0 = A * ( ( A+1 ) - ( A-1 ) * c + beta * s );
- b1 = 2 * A * ( ( A - 1 ) - ( A + 1 ) * c) ;
- b2 = A * ( ( A + 1 ) - ( A - 1 ) * c - beta * s);
- a0 = ( A + 1 ) + ( A - 1 ) * c + beta * s;
- a1 = -2 * ( ( A - 1 ) + ( A + 1 ) * c );
- a2 = ( A + 1 ) + ( A - 1) * c - beta * s;
- //normalise
- b0 /= a0;
- b1 /= a0;
- b2 /= a0;
- a1 /= a0;
- a2 /= a0;
- a0 = 1;
- setCoeffs( a1, a2, b0, b1, b2 );
- }
- };
- class EqHighShelfFilter : public EqFilter
- {
- public :
- virtual void calcCoefficents()
- {
- // calc intermediate
- float w0 = F_2PI * m_freq / m_sampleRate;
- float c = cosf( w0 );
- float s = sinf( w0 );
- float A = pow( 10, m_gain * 0.025 );
- float beta = sqrt( A ) / m_res;
- float a0, a1, a2, b0, b1, b2; // coeffs to calculate
- //calc coefficents
- b0 = A *( ( A +1 ) + ( A - 1 ) * c + beta * s);
- b1 = -2 * A * ( ( A - 1 ) + ( A + 1 ) * c );
- b2 = A * ( ( A + 1 ) + ( A - 1 ) * c - beta * s);
- a0 = ( A + 1 ) - ( A - 1 ) * c + beta * s;
- a1 = 2 * ( ( A - 1 ) - ( A + 1 ) * c );
- a2 = ( A + 1) - ( A - 1 ) * c - beta * s;
- //normalise
- b0 /= a0;
- b1 /= a0;
- b2 /= a0;
- a1 /= a0;
- a2 /= a0;
- a0 = 1;
- setCoeffs( a1, a2, b0, b1, b2 );
- }
- };
- class EqLinkwitzRiley : public StereoLinkwitzRiley
- {
- public:
- EqLinkwitzRiley() :
- StereoLinkwitzRiley( 44100),
- m_freq(0 ),
- m_sr( 1 )
- {
- }
- virtual inline void setSR( int sampleRate )
- {
- if( sampleRate != m_sr )
- {
- m_sr = sampleRate;
- setSampleRate( sampleRate );
- setLowpass(m_freq);
- }
- }
- virtual inline void setFrequency( float freq ){
- if ( freq != m_freq )
- {
- m_freq = freq;
- setLowpass(m_freq);
- }
- }
- virtual void processBuffer( sampleFrame* buf, const fpp_t frames )
- {
- for ( fpp_t f = 0 ; f < frames ; ++f)
- {
- buf[f][0] = update( buf[f][0] , 0);
- buf[f][1] = update( buf[f][1] , 1);
- }
- }
- protected:
- float m_freq;
- int m_sr;
- };
- #endif // EQFILTER_H
|