*
*/
+#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
}
}
+float
+ARDOUR::DSP::log_meter (float power) {
+ // compare to gtk2_ardour/logmeter.h
+ static const float lower_db = -192.f;
+ static const float upper_db = 0.f;
+ static const float non_linearity = 8.0;
+ return (power < lower_db ? 0.0 : powf ((power - lower_db) / (upper_db - lower_db), non_linearity));
+}
+
+float
+ARDOUR::DSP::log_meter_coeff (float coeff) {
+ if (coeff <= 0) return 0;
+ return log_meter (fast_coefficient_to_dB (coeff));
+}
+
+void
+ARDOUR::DSP::peaks (float *data, float &min, float &max, uint32_t n_samples) {
+ for (uint32_t i = 0; i < n_samples; ++i) {
+ if (data[i] < min) min = data[i];
+ if (data[i] > max) max = data[i];
+ }
+}
LowPass::LowPass (double samplerate, float freq)
: _rate (samplerate)
z = data[i];
}
_z = z;
+ if (!isfinite_local (_z)) { _z = 0; }
}
void
///////////////////////////////////////////////////////////////////////////////
-BiQuad::BiQuad (double samplerate)
+Biquad::Biquad (double samplerate)
: _rate (samplerate)
, _z1 (0.0)
, _z2 (0.0)
{
}
-BiQuad::BiQuad (const BiQuad &other)
+Biquad::Biquad (const Biquad &other)
: _rate (other._rate)
, _z1 (0.0)
, _z2 (0.0)
}
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];
_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.; }
+ if (freq >= _rate) { freq = _rate; }
+
/* Compute biquad filter settings.
* Based on 'Cookbook formulae for audio EQ biquad filter coefficents'
* by Robert Bristow-Johnson
*/
- const double A = pow (10.0, (gain / 40.0));
+ const double A = pow (10.0, (gain / 40.0));
const double W0 = (2.0 * M_PI * freq) / _rate;
- const double sinW0 = sin (W0);
- const double cosW0 = cos (W0);
+ const double sinW0 = sin (W0);
+ const double cosW0 = cos (W0);
const double alpha = sinW0 / (2.0 * Q);
const double beta = sqrt (A) / Q;
_a1 /= _a0;
_a2 /= _a0;
}
+
+float
+Biquad::dB_at_freq (float freq) const
+{
+ const double W0 = (2.0 * M_PI * freq) / _rate;
+ const float c1 = cosf (W0);
+ const float s1 = sinf (W0);
+
+ const float A = _b0 + _b2;
+ const float B = _b0 - _b2;
+ const float C = 1.0 + _a2;
+ const float D = 1.0 - _a2;
+
+ const float a = A * c1 + _b1;
+ const float b = B * s1;
+ const float c = C * c1 + _a1;
+ const float d = D * s1;
+
+#define SQUARE(x) ( (x) * (x) )
+ 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));
+}