change name of a Session method to makes its intended function clear
[ardour.git] / libs / ardour / dsp_filter.cc
index f82b5e46cfd7e5e208b1d708c9deebbe2f55a76a..9d6e2cf7109233e309ecbd846d26cf69bd0b388f 100644 (file)
  *
  */
 
+#include <algorithm>
 #include <stdlib.h>
-#include <math.h>
+#include <cmath>
 #include "ardour/dB.h"
 #include "ardour/dsp_filter.h"
 
+#ifdef COMPILER_MSVC
+#include <float.h>
+#define isfinite_local(val) (bool)_finite((double)val)
+#else
+#define isfinite_local std::isfinite
+#endif
+
 #ifndef M_PI
 #define M_PI 3.14159265358979323846
 #endif
@@ -89,6 +97,7 @@ LowPass::proc (float *data, const uint32_t n_samples)
                z = data[i];
        }
        _z = z;
+       if (!isfinite_local (_z)) { _z = 0; }
 }
 
 void
@@ -106,7 +115,7 @@ LowPass::ctrl (float *data, const float val, const uint32_t n_samples)
 
 ///////////////////////////////////////////////////////////////////////////////
 
-BiQuad::BiQuad (double samplerate)
+Biquad::Biquad (double samplerate)
        : _rate (samplerate)
        , _z1 (0.0)
        , _z2 (0.0)
@@ -118,7 +127,7 @@ BiQuad::BiQuad (double samplerate)
 {
 }
 
-BiQuad::BiQuad (const BiQuad &other)
+Biquad::Biquad (const Biquad &other)
        : _rate (other._rate)
        , _z1 (0.0)
        , _z2 (0.0)
@@ -131,7 +140,7 @@ BiQuad::BiQuad (const BiQuad &other)
 }
 
 void
-BiQuad::run (float *data, const uint32_t n_samples)
+Biquad::run (float *data, const uint32_t n_samples)
 {
        for (uint32_t i = 0; i < n_samples; ++i) {
                const float xn = data[i];
@@ -140,10 +149,13 @@ BiQuad::run (float *data, const uint32_t n_samples)
                _z2           = _b2 * xn - _a2 * z;
                data[i] = z;
        }
+
+       if (!isfinite_local (_z1)) { _z1 = 0; }
+       if (!isfinite_local (_z2)) { _z2 = 0; }
 }
 
 void
-BiQuad::compute (Type type, double freq, double Q, double gain)
+Biquad::compute (Type type, double freq, double Q, double gain)
 {
        if (Q <= .001)     { Q = 0.001; }
        if (freq <= 1.)    { freq = 1.; }
@@ -256,7 +268,7 @@ BiQuad::compute (Type type, double freq, double Q, double gain)
 }
 
 float
-BiQuad::dB_at_freq (float freq) const
+Biquad::dB_at_freq (float freq) const
 {
        const double W0 = (2.0 * M_PI * freq) / _rate;
        const float c1 = cosf (W0);
@@ -273,5 +285,7 @@ BiQuad::dB_at_freq (float freq) const
        const float d = D * s1;
 
 #define SQUARE(x) ( (x) * (x) )
-       return 20.f * log10f (sqrtf ((SQUARE(a) + SQUARE(b)) * (SQUARE(c) + SQUARE(d))) / (SQUARE(c) + SQUARE(d)));
+       float rv = 20.f * log10f (sqrtf ((SQUARE(a) + SQUARE(b)) * (SQUARE(c) + SQUARE(d))) / (SQUARE(c) + SQUARE(d)));
+       if (!isfinite_local (rv)) { rv = 0; }
+       return std::min (120.f, std::max(-120.f, rv));
 }