2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <wtf/MathExtras.h>
40 #include <Accelerate/Accelerate.h>
45 const int kBufferSize = 1024;
50 // Allocate two samples more for filter history
51 m_inputBuffer.allocate(kBufferSize + 2);
52 m_outputBuffer.allocate(kBufferSize + 2);
55 // Initialize as pass-thru (straight-wire, no filter effect)
62 reset(); // clear filter memory
65 void Biquad::process(const float* sourceP, float* destP, size_t framesToProcess)
68 // Use vecLib if available
69 processFast(sourceP, destP, framesToProcess);
71 int n = framesToProcess;
73 // Create local copies of member variables
86 // FIXME: this can be optimized by pipelining the multiply adds...
88 float y = b0*x + b1*x1 + b2*x2 - a1*y1 - a2*y2;
92 // Update state variables
99 // Local variables back to member
115 // Here we have optimized version using Accelerate.framework
117 void Biquad::processFast(const float* sourceP, float* destP, size_t framesToProcess)
119 double filterCoefficients[5];
120 filterCoefficients[0] = m_b0;
121 filterCoefficients[1] = m_b1;
122 filterCoefficients[2] = m_b2;
123 filterCoefficients[3] = m_a1;
124 filterCoefficients[4] = m_a2;
126 double* inputP = m_inputBuffer.data();
127 double* outputP = m_outputBuffer.data();
129 double* input2P = inputP + 2;
130 double* output2P = outputP + 2;
132 // Break up processing into smaller slices (kBufferSize) if necessary.
134 int n = framesToProcess;
137 int framesThisTime = n < kBufferSize ? n : kBufferSize;
139 // Copy input to input buffer
140 for (int i = 0; i < framesThisTime; ++i)
141 input2P[i] = *sourceP++;
143 processSliceFast(inputP, outputP, filterCoefficients, framesThisTime);
145 // Copy output buffer to output (converts float -> double).
146 for (int i = 0; i < framesThisTime; ++i)
147 *destP++ = static_cast<float>(output2P[i]);
153 void Biquad::processSliceFast(double* sourceP, double* destP, double* coefficientsP, size_t framesToProcess)
155 // Use double-precision for filter stability
156 vDSP_deq22D(sourceP, 1, coefficientsP, destP, 1, framesToProcess);
158 // Save history. Note that sourceP and destP reference m_inputBuffer and m_outputBuffer respectively.
159 // These buffers are allocated (in the constructor) with space for two extra samples so it's OK to access
160 // array values two beyond framesToProcess.
161 sourceP[0] = sourceP[framesToProcess - 2 + 2];
162 sourceP[1] = sourceP[framesToProcess - 1 + 2];
163 destP[0] = destP[framesToProcess - 2 + 2];
164 destP[1] = destP[framesToProcess - 1 + 2];
172 m_x1 = m_x2 = m_y1 = m_y2 = 0;
175 // Two extra samples for filter history
176 double* inputP = m_inputBuffer.data();
180 double* outputP = m_outputBuffer.data();
186 void Biquad::setLowpassParams(double cutoff, double resonance)
188 resonance = std::max(0.0, resonance); // can't go negative
190 double g = pow(10.0, 0.05 * resonance);
191 double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);
193 // Compute biquad coefficients for lopass filter
194 double theta = piDouble * cutoff;
195 double sn = 0.5 * d * sin(theta);
196 double beta = 0.5 * (1 - sn) / (1 + sn);
197 double gamma = (0.5 + beta) * cos(theta);
198 double alpha = 0.25 * (0.5 + beta - gamma);
201 m_b1 = 2 * 2 * alpha;
207 void Biquad::setHighpassParams(double cutoff, double resonance)
209 resonance = std::max(0.0, resonance); // can't go negative
211 double g = pow(10.0, 0.05 * resonance);
212 double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);
214 // Compute biquad coefficients for highpass filter
215 double theta = piDouble * cutoff;
216 double sn = 0.5 * d * sin(theta);
217 double beta = 0.5 * (1 - sn) / (1 + sn);
218 double gamma = (0.5 + beta) * cos(theta);
219 double alpha = 0.25 * (0.5 + beta + gamma);
222 m_b1 = 2 * -2 * alpha;
228 void Biquad::setNormalizedCoefficients(double b0, double b1, double b2, double a0, double a1, double a2)
230 double a0Inverse = 1 / a0;
232 m_b0 = b0 * a0Inverse;
233 m_b1 = b1 * a0Inverse;
234 m_b2 = b2 * a0Inverse;
235 m_a1 = a1 * a0Inverse;
236 m_a2 = a2 * a0Inverse;
239 void Biquad::setLowShelfParams(double frequency, double dbGain)
241 double w0 = piDouble * frequency;
243 double A = pow(10.0, dbGain / 40);
244 double S = 1; // filter slope (1 is max value)
245 double alpha = 0.5 * sin(w0) * sqrt((A + 1 / A) * (1 / S - 1) + 2);
248 double k2 = 2 * sqrt(A) * alpha;
250 double aPlusOne = A + 1;
251 double aMinusOne = A - 1;
253 double b0 = A * (aPlusOne - aMinusOne * k + k2);
254 double b1 = 2 * A * (aMinusOne - aPlusOne * k);
255 double b2 = A * (aPlusOne - aMinusOne * k - k2);
256 double a0 = aPlusOne + aMinusOne * k + k2;
257 double a1 = -2 * (aMinusOne + aPlusOne * k);
258 double a2 = aPlusOne + aMinusOne * k - k2;
260 setNormalizedCoefficients(b0, b1, b2, a0, a1, a2);
263 void Biquad::setHighShelfParams(double frequency, double dbGain)
265 double w0 = piDouble * frequency;
267 double A = pow(10.0, dbGain / 40);
268 double S = 1; // filter slope (1 is max value)
269 double alpha = 0.5 * sin(w0) * sqrt((A + 1 / A) * (1 / S - 1) + 2);
272 double k2 = 2 * sqrt(A) * alpha;
274 double aPlusOne = A + 1;
275 double aMinusOne = A - 1;
277 double b0 = A * (aPlusOne + aMinusOne * k + k2);
278 double b1 = -2 * A * (aMinusOne + aPlusOne * k);
279 double b2 = A * (aPlusOne + aMinusOne * k - k2);
280 double a0 = aPlusOne - aMinusOne * k + k2;
281 double a1 = 2 * (aMinusOne - aPlusOne * k);
282 double a2 = aPlusOne - aMinusOne * k - k2;
284 setNormalizedCoefficients(b0, b1, b2, a0, a1, a2);
287 void Biquad::setPeakingParams(double frequency, double Q, double dbGain)
289 double w0 = piDouble * frequency;
290 double alpha = sin(w0) / (2 * Q);
291 double A = pow(10.0, dbGain / 40);
295 double b0 = 1 + alpha * A;
297 double b2 = 1 - alpha * A;
298 double a0 = 1 + alpha / A;
300 double a2 = 1 - alpha / A;
302 setNormalizedCoefficients(b0, b1, b2, a0, a1, a2);
305 void Biquad::setAllpassParams(double frequency, double Q)
307 double w0 = piDouble * frequency;
308 double alpha = sin(w0) / (2 * Q);
312 double b0 = 1 - alpha;
314 double b2 = 1 + alpha;
315 double a0 = 1 + alpha;
317 double a2 = 1 - alpha;
319 setNormalizedCoefficients(b0, b1, b2, a0, a1, a2);
322 void Biquad::setNotchParams(double frequency, double Q)
324 double w0 = piDouble * frequency;
325 double alpha = sin(w0) / (2 * Q);
332 double a0 = 1 + alpha;
334 double a2 = 1 - alpha;
336 setNormalizedCoefficients(b0, b1, b2, a0, a1, a2);
339 void Biquad::setBandpassParams(double frequency, double Q)
341 double w0 = piDouble * frequency;
342 double alpha = sin(w0) / (2 * Q);
349 double a0 = 1 + alpha;
351 double a2 = 1 - alpha;
353 setNormalizedCoefficients(b0, b1, b2, a0, a1, a2);
356 void Biquad::setZeroPolePairs(const Complex &zero, const Complex &pole)
359 m_b1 = -2 * zero.real();
361 double zeroMag = abs(zero);
362 m_b2 = zeroMag * zeroMag;
364 m_a1 = -2 * pole.real();
366 double poleMag = abs(pole);
367 m_a2 = poleMag * poleMag;
370 void Biquad::setAllpassPole(const Complex &pole)
372 Complex zero = Complex(1, 0) / pole;
373 setZeroPolePairs(zero, pole);
376 } // namespace WebCore
378 #endif // ENABLE(WEB_AUDIO)