diff --git a/libs/ardour/dsp_filter.cc b/libs/ardour/dsp_filter.cc index a997f11508..084541f098 100644 --- a/libs/ardour/dsp_filter.cc +++ b/libs/ardour/dsp_filter.cc @@ -18,18 +18,18 @@ */ #include -#include -#include #include +#include +#include -#include "ardour/dB.h" #include "ardour/buffer.h" +#include "ardour/dB.h" #include "ardour/dsp_filter.h" #include "ardour/runtime_functions.h" #ifdef COMPILER_MSVC #include -#define isfinite_local(val) (bool)_finite((double)val) +#define isfinite_local(val) (bool)_finite ((double)val) #else #define isfinite_local std::isfinite #endif @@ -41,36 +41,43 @@ using namespace ARDOUR::DSP; void -ARDOUR::DSP::memset (float *data, const float val, const uint32_t n_samples) { +ARDOUR::DSP::memset (float* data, const float val, const uint32_t n_samples) +{ for (uint32_t i = 0; i < n_samples; ++i) { data[i] = val; } } void -ARDOUR::DSP::mmult (float *data, float *mult, const uint32_t n_samples) { +ARDOUR::DSP::mmult (float* data, float* mult, const uint32_t n_samples) +{ for (uint32_t i = 0; i < n_samples; ++i) { data[i] *= mult[i]; } } float -ARDOUR::DSP::log_meter (float power) { +ARDOUR::DSP::log_meter (float power) +{ // compare to libs/ardour/log_meter.h - static const float lower_db = -192.f; - static const float upper_db = 0.f; + 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; +ARDOUR::DSP::log_meter_coeff (float coeff) +{ + if (coeff <= 0) { + return 0; + } return log_meter (fast_coefficient_to_dB (coeff)); } void -ARDOUR::DSP::peaks (const float *data, float &min, float &max, uint32_t n_samples) { +ARDOUR::DSP::peaks (const float* data, float& min, float& max, uint32_t n_samples) +{ ARDOUR::find_peaks (data, n_samples, &min, &max); } @@ -83,9 +90,9 @@ ARDOUR::DSP::process_map (BufferSet* bufs, const ChanCount& n_out, const ChanMap * This just fills output buffers, forwarding inputs as needed: * Input -> plugin-sink == plugin-src -> Output */ - for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { + for (DataType::iterator t = DataType::begin (); t != DataType::end (); ++t) { for (uint32_t out = 0; out < n_out.get (*t); ++out) { - bool valid; + bool valid; uint32_t out_idx = out_map.get (*t, out, &valid); if (!valid) { continue; @@ -116,26 +123,29 @@ LowPass::set_cutoff (float freq) } void -LowPass::proc (float *data, const uint32_t n_samples) +LowPass::proc (float* data, const uint32_t n_samples) { // localize variables const float a = _a; - float z = _z; + float z = _z; for (uint32_t i = 0; i < n_samples; ++i) { data[i] += a * (data[i] - z); z = data[i]; } _z = z; - if (!isfinite_local (_z)) { _z = 0; } - else if (!boost::math::isnormal (_z)) { _z = 0; } + if (!isfinite_local (_z)) { + _z = 0; + } else if (!boost::math::isnormal (_z)) { + _z = 0; + } } void -LowPass::ctrl (float *data, const float val, const uint32_t n_samples) +LowPass::ctrl (float* data, const float val, const uint32_t n_samples) { // localize variables const float a = _a; - float z = _z; + float z = _z; for (uint32_t i = 0; i < n_samples; ++i) { data[i] += a * (val - z); z = data[i]; @@ -157,7 +167,7 @@ Biquad::Biquad (double samplerate) { } -Biquad::Biquad (const Biquad &other) +Biquad::Biquad (const Biquad& other) : _rate (other._rate) , _z1 (0.0) , _z2 (0.0) @@ -170,20 +180,26 @@ 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]; - const float z = _b0 * xn + _z1; - _z1 = _b1 * xn - _a1 * z + _z2; - _z2 = _b2 * xn - _a2 * z; - data[i] = z; + const float z = _b0 * xn + _z1; + _z1 = _b1 * xn - _a1 * z + _z2; + _z2 = _b2 * xn - _a2 * z; + data[i] = z; } - if (!isfinite_local (_z1)) { _z1 = 0; } - else if (!boost::math::isnormal (_z1)) { _z1 = 0; } - if (!isfinite_local (_z2)) { _z2 = 0; } - else if (!boost::math::isnormal (_z2)) { _z2 = 0; } + if (!isfinite_local (_z1)) { + _z1 = 0; + } else if (!boost::math::isnormal (_z1)) { + _z1 = 0; + } + if (!isfinite_local (_z2)) { + _z2 = 0; + } else if (!boost::math::isnormal (_z2)) { + _z2 = 0; + } } void @@ -216,8 +232,8 @@ Biquad::set_vicanek_poles (const double W0, const double Q, const double A) _a2 = exp (-.5 * W0 / (A * Q)); _a1 = p <= 1. - ? -2 * _a2 * cos (W0 * sqrt (1 - p)) - : -2 * _a2 * cosh (W0 * sqrt (p - 1)); + ? -2 * _a2 * cos (W0 * sqrt (1 - p)) + : -2 * _a2 * cosh (W0 * sqrt (p - 1)); _a2 = _a2 * _a2; } @@ -225,11 +241,11 @@ void Biquad::calc_vicanek (const double W0, double& A0, double& A1, double& A2, double& phi0, double& phi1, double& phi2) { #define SQR(x) ((x) * (x)) - A0 = SQR(1. + _a1 + _a2); - A1 = SQR(1. - _a1 + _a2); + A0 = SQR (1. + _a1 + _a2); + A1 = SQR (1. - _a1 + _a2); A2 = -4 * _a2; - phi1 = SQR(sin (.5 * W0)); + phi1 = SQR (sin (.5 * W0)); phi0 = 1.0 - phi1; phi2 = 4 * phi0 * phi1; #undef SQR @@ -238,15 +254,21 @@ Biquad::calc_vicanek (const double W0, double& A0, double& A1, double& A2, doubl void Biquad::compute (Type type, double freq, double Q, double gain) { - if (Q <= .001) { Q = 0.001; } - if (freq <= 1.) { freq = 1.; } - if (freq >= 0.4998 * _rate) { freq = 0.4998 * _rate; } + if (Q <= .001) { + Q = 0.001; + } + if (freq <= 1.) { + freq = 1.; + } + if (freq >= 0.4998 * _rate) { + freq = 0.4998 * _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); @@ -359,21 +381,21 @@ Biquad::compute (Type type, double freq, double Q, double gain) const double B1 = ((A0 * phi0 + A1 * phi1 + A2 * phi2) * Q * Q - A0 * phi0) / phi1; _b0 = .5 * (B0_2 + sqrt (B1)); - _b1 = B0_2 -_b0; + _b1 = B0_2 - _b0; _b2 = 0; } break; case MatchedBandPass0dB: /* Constant 0 dB peak gain */ - _a0 = 1.0; + _a0 = 1.0; set_vicanek_poles (W0, Q); { - float fq = 2 * freq / _rate; + float fq = 2 * freq / _rate; float fq2 = fq * fq; _b1 = -.5 * (1 - _a1 + _a2) * fq / Q / sqrt ((1 - fq2) * (1 - fq2) + fq2 / (Q * Q)); - _b0 = .5 * ((1 + _a1 + _a2) / (W0 * Q) - _b1); - _b2 = -_b0 - _b1; + _b0 = .5 * ((1 + _a1 + _a2) / (W0 * Q) - _b1); + _b2 = -_b0 - _b1; } break; @@ -382,8 +404,8 @@ Biquad::compute (Type type, double freq, double Q, double gain) set_vicanek_poles (W0, Q, A); calc_vicanek (W0, A0, A1, A2, phi0, phi1, phi2); { - const double AA = A * A; - const double AAAA = AA * AA; + const double AA = A * A; + const double AAAA = AA * AA; const double R1 = (phi0 * A0 + phi1 * A1 + phi2 * A2) * AAAA; const double R2 = (A1 - A0 + 4 * (phi0 - phi1) * A2) * AAAA; @@ -404,7 +426,7 @@ Biquad::compute (Type type, double freq, double Q, double gain) break; default: - abort(); /*NOTREACHED*/ + abort (); /*NOTREACHED*/ break; } @@ -419,8 +441,8 @@ 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 c1 = cosf (W0); + const float s1 = sinf (W0); const float A = _b0 + _b2; const float B = _b0 - _b2; @@ -432,13 +454,14 @@ Biquad::dB_at_freq (float freq) const 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)); +#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)); } - Glib::Threads::Mutex FFTSpectrum::fft_planner_lock; FFTSpectrum::FFTSpectrum (uint32_t window_size, double rate) @@ -465,23 +488,23 @@ FFTSpectrum::init (uint32_t window_size, double rate) assert (window_size > 0); Glib::Threads::Mutex::Lock lk (fft_planner_lock); - _fft_window_size = window_size; - _fft_data_size = window_size / 2; + _fft_window_size = window_size; + _fft_data_size = window_size / 2; _fft_freq_per_bin = rate / _fft_data_size / 2.f; - _fft_data_in = (float *) fftwf_malloc (sizeof(float) * _fft_window_size); - _fft_data_out = (float *) fftwf_malloc (sizeof(float) * _fft_window_size); - _fft_power = (float *) malloc (sizeof(float) * _fft_data_size); + _fft_data_in = (float*)fftwf_malloc (sizeof (float) * _fft_window_size); + _fft_data_out = (float*)fftwf_malloc (sizeof (float) * _fft_window_size); + _fft_power = (float*)malloc (sizeof (float) * _fft_data_size); reset (); _fftplan = fftwf_plan_r2r_1d (_fft_window_size, _fft_data_in, _fft_data_out, FFTW_R2HC, FFTW_MEASURE); - hann_window = (float *) malloc(sizeof(float) * window_size); - double sum = 0.0; + hann_window = (float*)malloc (sizeof (float) * window_size); + double sum = 0.0; for (uint32_t i = 0; i < window_size; ++i) { - hann_window[i] = 0.5f - (0.5f * (float) cos (2.0f * M_PI * (float)i / (float)(window_size))); + hann_window[i] = 0.5f - (0.5f * (float)cos (2.0f * M_PI * (float)i / (float)(window_size))); sum += hann_window[i]; } const double isum = 2.0 / sum; @@ -502,9 +525,9 @@ FFTSpectrum::reset () } void -FFTSpectrum::set_data_hann (float const * const data, uint32_t n_samples, uint32_t offset) +FFTSpectrum::set_data_hann (float const* const data, uint32_t n_samples, uint32_t offset) { - assert(n_samples + offset <= _fft_window_size); + assert (n_samples + offset <= _fft_window_size); for (uint32_t i = 0; i < n_samples; ++i) { _fft_data_in[i + offset] = data[i] * hann_window[i + offset]; } @@ -528,7 +551,8 @@ FFTSpectrum::execute () } float -FFTSpectrum::power_at_bin (const uint32_t b, const float norm) const { +FFTSpectrum::power_at_bin (const uint32_t b, const float norm) const +{ assert (b < _fft_data_size); const float a = _fft_power[b] * norm; return a > 1e-12 ? 10.0 * fast_log10 (a) : -INFINITY; @@ -542,7 +566,8 @@ Generator::Generator () } void -Generator::set_type (Generator::Type t) { +Generator::set_type (Generator::Type t) +{ _type = t; _b0 = _b1 = _b2 = _b3 = _b4 = _b5 = _b6 = 0; _pass = false; @@ -550,22 +575,22 @@ Generator::set_type (Generator::Type t) { } void -Generator::run (float *data, const uint32_t n_samples) +Generator::run (float* data, const uint32_t n_samples) { switch (_type) { default: case UniformWhiteNoise: for (uint32_t i = 0; i < n_samples; ++i) { - data[i] = randf(); + data[i] = randf (); } break; case GaussianWhiteNoise: - for (uint32_t i = 0 ; i < n_samples; ++i) { - data[i] = 0.7079f * grandf(); + for (uint32_t i = 0; i < n_samples; ++i) { + data[i] = 0.7079f * grandf (); } break; case PinkNoise: - for (uint32_t i = 0 ; i < n_samples; ++i) { + for (uint32_t i = 0; i < n_samples; ++i) { const float white = .39572f * randf (); _b0 = .99886f * _b0 + white * .0555179f; _b1 = .99332f * _b1 + white * .0750759f; @@ -606,12 +631,12 @@ Generator::grandf () do { x1 = randf (); x2 = randf (); - r = x1 * x1 + x2 * x2; + r = x1 * x1 + x2 * x2; } while ((r >= 1.0f) || (r < 1e-22f)); r = sqrtf (-2.f * logf (r) / r); _pass = true; - _rn = r * x2; + _rn = r * x2; return r * x1; }