diff --git a/include/BasicFilters.h b/include/BasicFilters.h index 98c69474a..e09d40c42 100644 --- a/include/BasicFilters.h +++ b/include/BasicFilters.h @@ -75,34 +75,34 @@ public: inline void setCoeffs( float freq ) { // wc - const float wc = F_2PI * freq; + const float wc = F_2PI * freq / m_sampleRate; const float wc2 = wc * wc; const float wc3 = wc2 * wc; m_wc4 = wc2 * wc2; // k - const float k = wc / tanf( F_PI * freq / m_sampleRate ); + const float k = wc / tan( wc * 0.5 ); const float k2 = k * k; const float k3 = k2 * k; m_k4 = k2 * k2; // a - static const float sqrt2 = sqrtf( 2.0f ); + static const double sqrt2 = sqrt( 2.0 ); const float sq_tmp1 = sqrt2 * wc3 * k; const float sq_tmp2 = sqrt2 * wc * k3; - m_a = 4.0f * wc2 * k2 + 2.0f * sq_tmp1 + m_k4 + 2.0f * sq_tmp2 + m_wc4; + m_a = 1.0f / ( 4.0f * wc2 * k2 + 2.0f * sq_tmp1 + m_k4 + 2.0f * sq_tmp2 + m_wc4 ); // b - m_b1 = ( 4.0f * ( m_wc4 + sq_tmp1 - m_k4 - sq_tmp2 ) ) / m_a; - m_b2 = ( 6.0f * m_wc4 - 8.0f * wc2 * k2 + 6.0f * m_k4 ) / m_a; - m_b3 = ( 4.0f * ( m_wc4 - sq_tmp1 + sq_tmp2 - m_k4 ) ) / m_a; - m_b4 = ( m_k4 - 2.0f * sq_tmp1 + m_wc4 - 2.0f * sq_tmp2 + 4.0f * wc2 * k2 ) / m_a; + m_b1 = ( 4.0f * ( m_wc4 + sq_tmp1 - m_k4 - sq_tmp2 ) ) * m_a; + m_b2 = ( 6.0f * m_wc4 - 8.0f * wc2 * k2 + 6.0f * m_k4 ) * m_a; + m_b3 = ( 4.0f * ( m_wc4 - sq_tmp1 + sq_tmp2 - m_k4 ) ) * m_a; + m_b4 = ( m_k4 - 2.0f * sq_tmp1 + m_wc4 - 2.0f * sq_tmp2 + 4.0f * wc2 * k2 ) * m_a; } inline void setLowpass( float freq ) { setCoeffs( freq ); - m_a0 = m_wc4 / m_a; + m_a0 = m_wc4 * m_a; m_a1 = 4.0f * m_a0; m_a2 = 6.0f * m_a0; } @@ -110,7 +110,7 @@ public: inline void setHighpass( float freq ) { setCoeffs( freq ); - m_a0 = m_k4 / m_a; + m_a0 = m_k4 * m_a; m_a1 = 4.0f * m_a0; m_a2 = 6.0f * m_a0; } diff --git a/include/lmms_constants.h b/include/lmms_constants.h index 629db886c..6f50c6f8e 100644 --- a/include/lmms_constants.h +++ b/include/lmms_constants.h @@ -25,15 +25,28 @@ #ifndef LMMS_CONSTANTS_H #define LMMS_CONSTANTS_H -const double D_PI = 3.14159265358979323846; -const double D_2PI = D_PI * 2.0; -const double D_PI_2 = D_PI * 0.5; -const double D_E = 2.71828182845904523536; +const long double LD_PI = 3.14159265358979323846264338327950288419716939937510; +const long double LD_2PI = LD_PI * 2.0; +const long double LD_PI_2 = LD_PI * 0.5; +const long double LD_PI_R = 1.0 / LD_PI; +const long double LD_PI_SQR = LD_PI * LD_PI; +const long double LD_E = 2.71828182845904523536028747135266249775724709369995; +const long double LD_E_R = 1.0 / LD_E; -const float F_PI = (float) D_PI; -const float F_2PI = (float) D_2PI; -const float F_PI_2 = (float) D_PI_2; -const float F_E = (float) D_E; +const double D_PI = (double) LD_PI; +const double D_2PI = (double) LD_2PI; +const double D_PI_2 = (double) LD_PI_2; +const double D_PI_R = (double) LD_PI_R; +const double D_PI_SQR = (double) LD_PI_SQR; +const double D_E = (double) LD_E; +const double D_E_R = (double) LD_E_R; +const float F_PI = (float) LD_PI; +const float F_2PI = (float) LD_2PI; +const float F_PI_2 = (float) LD_PI_2; +const float F_PI_R = (float) LD_PI_R; +const float F_PI_SQR = (float) LD_PI_SQR; +const float F_E = (float) LD_E; +const float F_E_R = (float) LD_E_R; #endif diff --git a/include/lmms_math.h b/include/lmms_math.h index 016e1a49f..7cde29153 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -289,4 +289,18 @@ static inline float fastSqrt( float n ) return u.f; } +//! returns value furthest from zero +template +static inline T absMax( T a, T b ) +{ + return qAbs(a) > qAbs(b) ? a : b; +} + +//! returns value nearest to zero +template +static inline T absMin( T a, T b ) +{ + return qAbs(a) < qAbs(b) ? a : b; +} + #endif diff --git a/plugins/Bitcrush/Bitcrush.cpp b/plugins/Bitcrush/Bitcrush.cpp index e9ab9639a..84f5b45ba 100644 --- a/plugins/Bitcrush/Bitcrush.cpp +++ b/plugins/Bitcrush/Bitcrush.cpp @@ -30,6 +30,8 @@ const int OS_RATE = 5; const float OS_RATIO = 1.0f / OS_RATE; const float CUTOFF_RATIO = 0.353553391f; const int SILENCEFRAMES = 10; +const float OS_RESAMPLE [5] = { 0.0001490062883964112, 0.1645978376763992, 0.6705063120704088, + 0.1645978376763992, 0.0001490062883964112 }; extern "C" { @@ -78,7 +80,7 @@ void BitcrushEffect::sampleRateChanged() { m_sampleRate = Engine::mixer()->processingSampleRate(); m_filter.setSampleRate( m_sampleRate ); - m_filter.setLowpass( m_sampleRate * CUTOFF_RATIO ); + m_filter.setLowpass( m_sampleRate * ( CUTOFF_RATIO * OS_RATIO ) ); m_needsUpdate = true; } @@ -95,6 +97,11 @@ inline float BitcrushEffect::noise( float amt ) bool BitcrushEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames ) { + if( !isEnabled() || !isRunning () ) + { + return( false ); + } + // update values if( m_needsUpdate || m_controls.m_rateEnabled.isValueChanged() ) { @@ -219,8 +226,8 @@ bool BitcrushEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames ) float rsum = 0.0f; for( int o = 0; o < OS_RATE; ++o ) { - lsum += m_buffer[f * OS_RATE + o][0] * OS_RATIO; - rsum += m_buffer[f * OS_RATE + o][1] * OS_RATIO; + lsum += m_buffer[f * OS_RATE + o][0] * OS_RESAMPLE[o]; + rsum += m_buffer[f * OS_RATE + o][1] * OS_RESAMPLE[o]; } buf[f][0] = d * buf[f][0] + w * qBound( -m_outClip, lsum, m_outClip ) * m_outGain; buf[f][1] = d * buf[f][1] + w * qBound( -m_outClip, rsum, m_outClip ) * m_outGain; diff --git a/src/core/FxMixer.cpp b/src/core/FxMixer.cpp index 797463d0d..358e31171 100644 --- a/src/core/FxMixer.cpp +++ b/src/core/FxMixer.cpp @@ -100,7 +100,7 @@ inline void FxChannel::processed() void FxChannel::incrementDeps() { m_dependenciesMet.ref(); - if( m_dependenciesMet >= m_receives.size() ) + if( m_dependenciesMet >= m_receives.size() && ! m_queued ) { m_queued = true; MixerWorkerThread::addJob( this );