1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/Biquad.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +/* 1.5 + * Copyright (C) 2010 Google Inc. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 1.17 + * its contributors may be used to endorse or promote products derived 1.18 + * from this software without specific prior written permission. 1.19 + * 1.20 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 1.21 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.22 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.23 + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 1.24 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.25 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 1.26 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 1.27 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.29 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#ifndef Biquad_h 1.33 +#define Biquad_h 1.34 + 1.35 +#include <complex> 1.36 + 1.37 +namespace WebCore { 1.38 + 1.39 +typedef std::complex<double> Complex; 1.40 + 1.41 +// A basic biquad (two-zero / two-pole digital filter) 1.42 +// 1.43 +// It can be configured to a number of common and very useful filters: 1.44 +// lowpass, highpass, shelving, parameteric, notch, allpass, ... 1.45 + 1.46 +class Biquad { 1.47 +public: 1.48 + Biquad(); 1.49 + ~Biquad(); 1.50 + 1.51 + void process(const float* sourceP, float* destP, size_t framesToProcess); 1.52 + 1.53 + // frequency is 0 - 1 normalized, resonance and dbGain are in decibels. 1.54 + // Q is a unitless quality factor. 1.55 + void setLowpassParams(double frequency, double resonance); 1.56 + void setHighpassParams(double frequency, double resonance); 1.57 + void setBandpassParams(double frequency, double Q); 1.58 + void setLowShelfParams(double frequency, double dbGain); 1.59 + void setHighShelfParams(double frequency, double dbGain); 1.60 + void setPeakingParams(double frequency, double Q, double dbGain); 1.61 + void setAllpassParams(double frequency, double Q); 1.62 + void setNotchParams(double frequency, double Q); 1.63 + 1.64 + // Set the biquad coefficients given a single zero (other zero will be conjugate) 1.65 + // and a single pole (other pole will be conjugate) 1.66 + void setZeroPolePairs(const Complex& zero, const Complex& pole); 1.67 + 1.68 + // Set the biquad coefficients given a single pole (other pole will be conjugate) 1.69 + // (The zeroes will be the inverse of the poles) 1.70 + void setAllpassPole(const Complex& pole); 1.71 + 1.72 + // Return true iff the next output block will contain sound even with 1.73 + // silent input. 1.74 + bool hasTail() const { return m_y1 || m_y2 || m_x1 || m_x2; } 1.75 + 1.76 + // Resets filter state 1.77 + void reset(); 1.78 + 1.79 + // Filter response at a set of n frequencies. The magnitude and 1.80 + // phase response are returned in magResponse and phaseResponse. 1.81 + // The phase response is in radians. 1.82 + void getFrequencyResponse(int nFrequencies, 1.83 + const float* frequency, 1.84 + float* magResponse, 1.85 + float* phaseResponse); 1.86 +private: 1.87 + void setNormalizedCoefficients(double b0, double b1, double b2, double a0, double a1, double a2); 1.88 + 1.89 + // Filter coefficients. The filter is defined as 1.90 + // 1.91 + // y[n] + m_a1*y[n-1] + m_a2*y[n-2] = m_b0*x[n] + m_b1*x[n-1] + m_b2*x[n-2]. 1.92 + double m_b0; 1.93 + double m_b1; 1.94 + double m_b2; 1.95 + double m_a1; 1.96 + double m_a2; 1.97 + 1.98 + // Filter memory 1.99 + // 1.100 + // Double precision for the output values is valuable because errors can 1.101 + // accumulate. Input values are also stored as double so they need not be 1.102 + // converted again for computation. 1.103 + double m_x1; // input delayed by 1 sample 1.104 + double m_x2; // input delayed by 2 samples 1.105 + double m_y1; // output delayed by 1 sample 1.106 + double m_y2; // output delayed by 2 samples 1.107 +}; 1.108 + 1.109 +} // namespace WebCore 1.110 + 1.111 +#endif // Biquad_h