michael@0: /* michael@0: * Copyright (C) 2010 Google Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of michael@0: * its contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@0: * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY michael@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; michael@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND michael@0: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: // For M_PI from cmath michael@0: #ifdef _MSC_VER michael@0: # define _USE_MATH_DEFINES michael@0: #endif michael@0: michael@0: #include "Biquad.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: namespace WebCore { michael@0: michael@0: Biquad::Biquad() michael@0: { michael@0: // Initialize as pass-thru (straight-wire, no filter effect) michael@0: setNormalizedCoefficients(1, 0, 0, 1, 0, 0); michael@0: michael@0: reset(); // clear filter memory michael@0: } michael@0: michael@0: Biquad::~Biquad() michael@0: { michael@0: } michael@0: michael@0: void Biquad::process(const float* sourceP, float* destP, size_t framesToProcess) michael@0: { michael@0: // Create local copies of member variables michael@0: double x1 = m_x1; michael@0: double x2 = m_x2; michael@0: double y1 = m_y1; michael@0: double y2 = m_y2; michael@0: michael@0: double b0 = m_b0; michael@0: double b1 = m_b1; michael@0: double b2 = m_b2; michael@0: double a1 = m_a1; michael@0: double a2 = m_a2; michael@0: michael@0: for (size_t i = 0; i < framesToProcess; ++i) { michael@0: // FIXME: this can be optimized by pipelining the multiply adds... michael@0: double x = sourceP[i]; michael@0: double y = b0*x + b1*x1 + b2*x2 - a1*y1 - a2*y2; michael@0: michael@0: destP[i] = y; michael@0: michael@0: // Update state variables michael@0: x2 = x1; michael@0: x1 = x; michael@0: y2 = y1; michael@0: y1 = y; michael@0: } michael@0: michael@0: // Avoid introducing a stream of subnormals when input is silent and the michael@0: // tail approaches zero. michael@0: if (x1 == 0.0 && x2 == 0.0 && (y1 != 0.0 || y2 != 0.0) && michael@0: fabs(y1) < FLT_MIN && fabs(y2) < FLT_MIN) { michael@0: // Flush future values to zero (until there is new input). michael@0: y1 = y2 = 0.0; michael@0: // Flush calculated values. michael@0: for (int i = framesToProcess; i-- && fabsf(destP[i]) < FLT_MIN; ) { michael@0: destP[i] = 0.0f; michael@0: } michael@0: } michael@0: // Local variables back to member. michael@0: m_x1 = x1; michael@0: m_x2 = x2; michael@0: m_y1 = y1; michael@0: m_y2 = y2; michael@0: } michael@0: michael@0: void Biquad::reset() michael@0: { michael@0: m_x1 = m_x2 = m_y1 = m_y2 = 0; michael@0: } michael@0: michael@0: void Biquad::setLowpassParams(double cutoff, double resonance) michael@0: { michael@0: // Limit cutoff to 0 to 1. michael@0: cutoff = std::max(0.0, std::min(cutoff, 1.0)); michael@0: michael@0: if (cutoff == 1) { michael@0: // When cutoff is 1, the z-transform is 1. michael@0: setNormalizedCoefficients(1, 0, 0, michael@0: 1, 0, 0); michael@0: } else if (cutoff > 0) { michael@0: // Compute biquad coefficients for lowpass filter michael@0: resonance = std::max(0.0, resonance); // can't go negative michael@0: double g = pow(10.0, 0.05 * resonance); michael@0: double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2); michael@0: michael@0: double theta = M_PI * cutoff; michael@0: double sn = 0.5 * d * sin(theta); michael@0: double beta = 0.5 * (1 - sn) / (1 + sn); michael@0: double gamma = (0.5 + beta) * cos(theta); michael@0: double alpha = 0.25 * (0.5 + beta - gamma); michael@0: michael@0: double b0 = 2 * alpha; michael@0: double b1 = 2 * 2 * alpha; michael@0: double b2 = 2 * alpha; michael@0: double a1 = 2 * -gamma; michael@0: double a2 = 2 * beta; michael@0: michael@0: setNormalizedCoefficients(b0, b1, b2, 1, a1, a2); michael@0: } else { michael@0: // When cutoff is zero, nothing gets through the filter, so set michael@0: // coefficients up correctly. michael@0: setNormalizedCoefficients(0, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } michael@0: michael@0: void Biquad::setHighpassParams(double cutoff, double resonance) michael@0: { michael@0: // Limit cutoff to 0 to 1. michael@0: cutoff = std::max(0.0, std::min(cutoff, 1.0)); michael@0: michael@0: if (cutoff == 1) { michael@0: // The z-transform is 0. michael@0: setNormalizedCoefficients(0, 0, 0, michael@0: 1, 0, 0); michael@0: } else if (cutoff > 0) { michael@0: // Compute biquad coefficients for highpass filter michael@0: resonance = std::max(0.0, resonance); // can't go negative michael@0: double g = pow(10.0, 0.05 * resonance); michael@0: double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2); michael@0: michael@0: double theta = M_PI * cutoff; michael@0: double sn = 0.5 * d * sin(theta); michael@0: double beta = 0.5 * (1 - sn) / (1 + sn); michael@0: double gamma = (0.5 + beta) * cos(theta); michael@0: double alpha = 0.25 * (0.5 + beta + gamma); michael@0: michael@0: double b0 = 2 * alpha; michael@0: double b1 = 2 * -2 * alpha; michael@0: double b2 = 2 * alpha; michael@0: double a1 = 2 * -gamma; michael@0: double a2 = 2 * beta; michael@0: michael@0: setNormalizedCoefficients(b0, b1, b2, 1, a1, a2); michael@0: } else { michael@0: // When cutoff is zero, we need to be careful because the above michael@0: // gives a quadratic divided by the same quadratic, with poles michael@0: // and zeros on the unit circle in the same place. When cutoff michael@0: // is zero, the z-transform is 1. michael@0: setNormalizedCoefficients(1, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } michael@0: michael@0: void Biquad::setNormalizedCoefficients(double b0, double b1, double b2, double a0, double a1, double a2) michael@0: { michael@0: double a0Inverse = 1 / a0; michael@0: michael@0: m_b0 = b0 * a0Inverse; michael@0: m_b1 = b1 * a0Inverse; michael@0: m_b2 = b2 * a0Inverse; michael@0: m_a1 = a1 * a0Inverse; michael@0: m_a2 = a2 * a0Inverse; michael@0: } michael@0: michael@0: void Biquad::setLowShelfParams(double frequency, double dbGain) michael@0: { michael@0: // Clip frequencies to between 0 and 1, inclusive. michael@0: frequency = std::max(0.0, std::min(frequency, 1.0)); michael@0: michael@0: double A = pow(10.0, dbGain / 40); michael@0: michael@0: if (frequency == 1) { michael@0: // The z-transform is a constant gain. michael@0: setNormalizedCoefficients(A * A, 0, 0, michael@0: 1, 0, 0); michael@0: } else if (frequency > 0) { michael@0: double w0 = M_PI * frequency; michael@0: double S = 1; // filter slope (1 is max value) michael@0: double alpha = 0.5 * sin(w0) * sqrt((A + 1 / A) * (1 / S - 1) + 2); michael@0: double k = cos(w0); michael@0: double k2 = 2 * sqrt(A) * alpha; michael@0: double aPlusOne = A + 1; michael@0: double aMinusOne = A - 1; michael@0: michael@0: double b0 = A * (aPlusOne - aMinusOne * k + k2); michael@0: double b1 = 2 * A * (aMinusOne - aPlusOne * k); michael@0: double b2 = A * (aPlusOne - aMinusOne * k - k2); michael@0: double a0 = aPlusOne + aMinusOne * k + k2; michael@0: double a1 = -2 * (aMinusOne + aPlusOne * k); michael@0: double a2 = aPlusOne + aMinusOne * k - k2; michael@0: michael@0: setNormalizedCoefficients(b0, b1, b2, a0, a1, a2); michael@0: } else { michael@0: // When frequency is 0, the z-transform is 1. michael@0: setNormalizedCoefficients(1, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } michael@0: michael@0: void Biquad::setHighShelfParams(double frequency, double dbGain) michael@0: { michael@0: // Clip frequencies to between 0 and 1, inclusive. michael@0: frequency = std::max(0.0, std::min(frequency, 1.0)); michael@0: michael@0: double A = pow(10.0, dbGain / 40); michael@0: michael@0: if (frequency == 1) { michael@0: // The z-transform is 1. michael@0: setNormalizedCoefficients(1, 0, 0, michael@0: 1, 0, 0); michael@0: } else if (frequency > 0) { michael@0: double w0 = M_PI * frequency; michael@0: double S = 1; // filter slope (1 is max value) michael@0: double alpha = 0.5 * sin(w0) * sqrt((A + 1 / A) * (1 / S - 1) + 2); michael@0: double k = cos(w0); michael@0: double k2 = 2 * sqrt(A) * alpha; michael@0: double aPlusOne = A + 1; michael@0: double aMinusOne = A - 1; michael@0: michael@0: double b0 = A * (aPlusOne + aMinusOne * k + k2); michael@0: double b1 = -2 * A * (aMinusOne + aPlusOne * k); michael@0: double b2 = A * (aPlusOne + aMinusOne * k - k2); michael@0: double a0 = aPlusOne - aMinusOne * k + k2; michael@0: double a1 = 2 * (aMinusOne - aPlusOne * k); michael@0: double a2 = aPlusOne - aMinusOne * k - k2; michael@0: michael@0: setNormalizedCoefficients(b0, b1, b2, a0, a1, a2); michael@0: } else { michael@0: // When frequency = 0, the filter is just a gain, A^2. michael@0: setNormalizedCoefficients(A * A, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } michael@0: michael@0: void Biquad::setPeakingParams(double frequency, double Q, double dbGain) michael@0: { michael@0: // Clip frequencies to between 0 and 1, inclusive. michael@0: frequency = std::max(0.0, std::min(frequency, 1.0)); michael@0: michael@0: // Don't let Q go negative, which causes an unstable filter. michael@0: Q = std::max(0.0, Q); michael@0: michael@0: double A = pow(10.0, dbGain / 40); michael@0: michael@0: if (frequency > 0 && frequency < 1) { michael@0: if (Q > 0) { michael@0: double w0 = M_PI * frequency; michael@0: double alpha = sin(w0) / (2 * Q); michael@0: double k = cos(w0); michael@0: michael@0: double b0 = 1 + alpha * A; michael@0: double b1 = -2 * k; michael@0: double b2 = 1 - alpha * A; michael@0: double a0 = 1 + alpha / A; michael@0: double a1 = -2 * k; michael@0: double a2 = 1 - alpha / A; michael@0: michael@0: setNormalizedCoefficients(b0, b1, b2, a0, a1, a2); michael@0: } else { michael@0: // When Q = 0, the above formulas have problems. If we look at michael@0: // the z-transform, we can see that the limit as Q->0 is A^2, so michael@0: // set the filter that way. michael@0: setNormalizedCoefficients(A * A, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } else { michael@0: // When frequency is 0 or 1, the z-transform is 1. michael@0: setNormalizedCoefficients(1, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } michael@0: michael@0: void Biquad::setAllpassParams(double frequency, double Q) michael@0: { michael@0: // Clip frequencies to between 0 and 1, inclusive. michael@0: frequency = std::max(0.0, std::min(frequency, 1.0)); michael@0: michael@0: // Don't let Q go negative, which causes an unstable filter. michael@0: Q = std::max(0.0, Q); michael@0: michael@0: if (frequency > 0 && frequency < 1) { michael@0: if (Q > 0) { michael@0: double w0 = M_PI * frequency; michael@0: double alpha = sin(w0) / (2 * Q); michael@0: double k = cos(w0); michael@0: michael@0: double b0 = 1 - alpha; michael@0: double b1 = -2 * k; michael@0: double b2 = 1 + alpha; michael@0: double a0 = 1 + alpha; michael@0: double a1 = -2 * k; michael@0: double a2 = 1 - alpha; michael@0: michael@0: setNormalizedCoefficients(b0, b1, b2, a0, a1, a2); michael@0: } else { michael@0: // When Q = 0, the above formulas have problems. If we look at michael@0: // the z-transform, we can see that the limit as Q->0 is -1, so michael@0: // set the filter that way. michael@0: setNormalizedCoefficients(-1, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } else { michael@0: // When frequency is 0 or 1, the z-transform is 1. michael@0: setNormalizedCoefficients(1, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } michael@0: michael@0: void Biquad::setNotchParams(double frequency, double Q) michael@0: { michael@0: // Clip frequencies to between 0 and 1, inclusive. michael@0: frequency = std::max(0.0, std::min(frequency, 1.0)); michael@0: michael@0: // Don't let Q go negative, which causes an unstable filter. michael@0: Q = std::max(0.0, Q); michael@0: michael@0: if (frequency > 0 && frequency < 1) { michael@0: if (Q > 0) { michael@0: double w0 = M_PI * frequency; michael@0: double alpha = sin(w0) / (2 * Q); michael@0: double k = cos(w0); michael@0: michael@0: double b0 = 1; michael@0: double b1 = -2 * k; michael@0: double b2 = 1; michael@0: double a0 = 1 + alpha; michael@0: double a1 = -2 * k; michael@0: double a2 = 1 - alpha; michael@0: michael@0: setNormalizedCoefficients(b0, b1, b2, a0, a1, a2); michael@0: } else { michael@0: // When Q = 0, the above formulas have problems. If we look at michael@0: // the z-transform, we can see that the limit as Q->0 is 0, so michael@0: // set the filter that way. michael@0: setNormalizedCoefficients(0, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } else { michael@0: // When frequency is 0 or 1, the z-transform is 1. michael@0: setNormalizedCoefficients(1, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } michael@0: michael@0: void Biquad::setBandpassParams(double frequency, double Q) michael@0: { michael@0: // No negative frequencies allowed. michael@0: frequency = std::max(0.0, frequency); michael@0: michael@0: // Don't let Q go negative, which causes an unstable filter. michael@0: Q = std::max(0.0, Q); michael@0: michael@0: if (frequency > 0 && frequency < 1) { michael@0: double w0 = M_PI * frequency; michael@0: if (Q > 0) { michael@0: double alpha = sin(w0) / (2 * Q); michael@0: double k = cos(w0); michael@0: michael@0: double b0 = alpha; michael@0: double b1 = 0; michael@0: double b2 = -alpha; michael@0: double a0 = 1 + alpha; michael@0: double a1 = -2 * k; michael@0: double a2 = 1 - alpha; michael@0: michael@0: setNormalizedCoefficients(b0, b1, b2, a0, a1, a2); michael@0: } else { michael@0: // When Q = 0, the above formulas have problems. If we look at michael@0: // the z-transform, we can see that the limit as Q->0 is 1, so michael@0: // set the filter that way. michael@0: setNormalizedCoefficients(1, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } else { michael@0: // When the cutoff is zero, the z-transform approaches 0, if Q michael@0: // > 0. When both Q and cutoff are zero, the z-transform is michael@0: // pretty much undefined. What should we do in this case? michael@0: // For now, just make the filter 0. When the cutoff is 1, the michael@0: // z-transform also approaches 0. michael@0: setNormalizedCoefficients(0, 0, 0, michael@0: 1, 0, 0); michael@0: } michael@0: } michael@0: michael@0: void Biquad::setZeroPolePairs(const Complex &zero, const Complex &pole) michael@0: { michael@0: double b0 = 1; michael@0: double b1 = -2 * zero.real(); michael@0: michael@0: double zeroMag = abs(zero); michael@0: double b2 = zeroMag * zeroMag; michael@0: michael@0: double a1 = -2 * pole.real(); michael@0: michael@0: double poleMag = abs(pole); michael@0: double a2 = poleMag * poleMag; michael@0: setNormalizedCoefficients(b0, b1, b2, 1, a1, a2); michael@0: } michael@0: michael@0: void Biquad::setAllpassPole(const Complex &pole) michael@0: { michael@0: Complex zero = Complex(1, 0) / pole; michael@0: setZeroPolePairs(zero, pole); michael@0: } michael@0: michael@0: void Biquad::getFrequencyResponse(int nFrequencies, michael@0: const float* frequency, michael@0: float* magResponse, michael@0: float* phaseResponse) michael@0: { michael@0: // Evaluate the Z-transform of the filter at given normalized michael@0: // frequency from 0 to 1. (1 corresponds to the Nyquist michael@0: // frequency.) michael@0: // michael@0: // The z-transform of the filter is michael@0: // michael@0: // H(z) = (b0 + b1*z^(-1) + b2*z^(-2))/(1 + a1*z^(-1) + a2*z^(-2)) michael@0: // michael@0: // Evaluate as michael@0: // michael@0: // b0 + (b1 + b2*z1)*z1 michael@0: // -------------------- michael@0: // 1 + (a1 + a2*z1)*z1 michael@0: // michael@0: // with z1 = 1/z and z = exp(j*pi*frequency). Hence z1 = exp(-j*pi*frequency) michael@0: michael@0: // Make local copies of the coefficients as a micro-optimization. michael@0: double b0 = m_b0; michael@0: double b1 = m_b1; michael@0: double b2 = m_b2; michael@0: double a1 = m_a1; michael@0: double a2 = m_a2; michael@0: michael@0: for (int k = 0; k < nFrequencies; ++k) { michael@0: double omega = -M_PI * frequency[k]; michael@0: Complex z = Complex(cos(omega), sin(omega)); michael@0: Complex numerator = b0 + (b1 + b2 * z) * z; michael@0: Complex denominator = Complex(1, 0) + (a1 + a2 * z) * z; michael@0: Complex response = numerator / denominator; michael@0: magResponse[k] = static_cast(abs(response)); michael@0: phaseResponse[k] = static_cast(atan2(imag(response), real(response))); michael@0: } michael@0: } michael@0: michael@0: } // namespace WebCore michael@0: