2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "ardour/dsp_filter.h"
25 #define M_PI 3.14159265358979323846
28 using namespace ARDOUR::DSP;
31 ARDOUR::DSP::memset (float *data, const float val, const uint32_t n_samples) {
32 for (uint32_t i = 0; i < n_samples; ++i) {
38 ARDOUR::DSP::mmult (float *data, float *mult, const uint32_t n_samples) {
39 for (uint32_t i = 0; i < n_samples; ++i) {
45 LowPass::LowPass (double samplerate, float freq)
53 LowPass::set_cutoff (float freq)
55 _a = 1.f - expf (-2.f * M_PI * freq / _rate);
59 LowPass::proc (float *data, const uint32_t n_samples)
64 for (uint32_t i = 0; i < n_samples; ++i) {
65 data[i] += a * (data[i] - z);
72 LowPass::ctrl (float *data, const float val, const uint32_t n_samples)
77 for (uint32_t i = 0; i < n_samples; ++i) {
78 data[i] += a * (val - z);
84 ///////////////////////////////////////////////////////////////////////////////
86 BiQuad::BiQuad (double samplerate)
98 BiQuad::BiQuad (const BiQuad &other)
111 BiQuad::run (float *data, const uint32_t n_samples)
113 for (uint32_t i = 0; i < n_samples; ++i) {
114 const float xn = data[i];
115 const float z = _b0 * xn + _z1;
116 _z1 = _b1 * xn - _a1 * z + _z2;
117 _z2 = _b2 * xn - _a2 * z;
123 BiQuad::compute (Type type, double freq, double Q, double gain)
125 /* Compute biquad filter settings.
126 * Based on 'Cookbook formulae for audio EQ biquad filter coefficents'
127 * by Robert Bristow-Johnson
129 const double A = pow (10.0, (gain / 40.0));
130 const double W0 = (2.0 * M_PI * freq) / _rate;
131 const double sinW0 = sin (W0);
132 const double cosW0 = cos (W0);
133 const double alpha = sinW0 / (2.0 * Q);
134 const double beta = sqrt (A) / Q;
140 _b0 = (1.0 - cosW0) / 2.0;
142 _b2 = (1.0 - cosW0) / 2.0;
149 _b0 = (1.0 + cosW0) / 2.0;
150 _b1 = -(1.0 + cosW0);
151 _b2 = (1.0 + cosW0) / 2.0;
157 case BandPassSkirt: /* Constant skirt gain, peak gain = Q */
166 case BandPass0dB: /* Constant 0 dB peak gain */
194 _b0 = 1.0 + (alpha * A);
196 _b2 = 1.0 - (alpha * A);
197 _a0 = 1.0 + (alpha / A);
199 _a2 = 1.0 - (alpha / A);
203 _b0 = A * ((A + 1) - ((A - 1) * cosW0) + (beta * sinW0));
204 _b1 = (2.0 * A) * ((A - 1) - ((A + 1) * cosW0));
205 _b2 = A * ((A + 1) - ((A - 1) * cosW0) - (beta * sinW0));
206 _a0 = (A + 1) + ((A - 1) * cosW0) + (beta * sinW0);
207 _a1 = -2.0 * ((A - 1) + ((A + 1) * cosW0));
208 _a2 = (A + 1) + ((A - 1) * cosW0) - (beta * sinW0);
212 _b0 = A * ((A + 1) + ((A - 1) * cosW0) + (beta * sinW0));
213 _b1 = -(2.0 * A) * ((A - 1) + ((A + 1) * cosW0));
214 _b2 = A * ((A + 1) + ((A - 1) * cosW0) - (beta * sinW0));
215 _a0 = (A + 1) - ((A - 1) * cosW0) + (beta * sinW0);
216 _a1 = 2.0 * ((A - 1) - ((A + 1) * cosW0));
217 _a2 = (A + 1) - ((A - 1) * cosW0) - (beta * sinW0);
220 abort(); /*NOTREACHED*/