Add methods to compute magnitude and phase response for biquads
[WebKit-https.git] / Source / WebCore / webaudio / BiquadDSPKernel.cpp
1 /*
2  * Copyright (C) 2010, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #if ENABLE(WEB_AUDIO)
28
29 #include "BiquadDSPKernel.h"
30
31 #include "BiquadProcessor.h"
32 #include <wtf/Vector.h>
33
34 namespace WebCore {
35
36 void BiquadDSPKernel::updateCoefficientsIfNecessary(bool useSmoothing, bool forceUpdate)
37 {
38     if (forceUpdate || biquadProcessor()->filterCoefficientsDirty()) {
39         double value1;
40         double value2;
41         double gain;
42         
43         if (useSmoothing) {
44             value1 = biquadProcessor()->parameter1()->smoothedValue();
45             value2 = biquadProcessor()->parameter2()->smoothedValue();
46             gain = biquadProcessor()->parameter3()->smoothedValue();
47         } else {
48             value1 = biquadProcessor()->parameter1()->value();
49             value2 = biquadProcessor()->parameter2()->value();
50             gain = biquadProcessor()->parameter3()->value();
51         }
52
53         // Convert from Hertz to normalized frequency 0 -> 1.
54         double nyquist = this->nyquist();
55         double normalizedFrequency = value1 / nyquist;
56
57         // Configure the biquad with the new filter parameters for the appropriate type of filter.
58         switch (biquadProcessor()->type()) {
59         case BiquadProcessor::LowPass:
60             m_biquad.setLowpassParams(normalizedFrequency, value2);
61             break;
62
63         case BiquadProcessor::HighPass:
64             m_biquad.setHighpassParams(normalizedFrequency, value2);
65             break;
66
67         case BiquadProcessor::BandPass:
68             m_biquad.setBandpassParams(normalizedFrequency, value2);
69             break;
70
71         case BiquadProcessor::LowShelf:
72             m_biquad.setLowShelfParams(normalizedFrequency, gain);
73             break;
74
75         case BiquadProcessor::HighShelf:
76             m_biquad.setHighShelfParams(normalizedFrequency, gain);
77             break;
78
79         case BiquadProcessor::Peaking:
80             m_biquad.setPeakingParams(normalizedFrequency, value2, gain);
81             break;
82
83         case BiquadProcessor::Notch:
84             m_biquad.setNotchParams(normalizedFrequency, value2);
85             break;
86
87         case BiquadProcessor::Allpass:
88             m_biquad.setAllpassParams(normalizedFrequency, value2);
89             break;
90         }
91     }
92 }
93
94 void BiquadDSPKernel::process(const float* source, float* destination, size_t framesToProcess)
95 {
96     ASSERT(source && destination && biquadProcessor());
97     
98     // Recompute filter coefficients if any of the parameters have changed.
99     // FIXME: as an optimization, implement a way that a Biquad object can simply copy its internal filter coefficients from another Biquad object.
100     // Then re-factor this code to only run for the first BiquadDSPKernel of each BiquadProcessor.
101
102     updateCoefficientsIfNecessary(true, false);
103
104     m_biquad.process(source, destination, framesToProcess);
105 }
106
107 void BiquadDSPKernel::getFrequencyResponse(int nFrequencies,
108                                            const float* frequencyHz,
109                                            float* magResponse,
110                                            float* phaseResponse)
111 {
112     bool isGood = nFrequencies > 0 && frequencyHz && magResponse && phaseResponse;
113     ASSERT(isGood);
114     if (!isGood)
115         return;
116
117     Vector<float> frequency(nFrequencies);
118
119     double nyquist = this->nyquist();
120
121     // Convert from frequency in Hz to normalized frequency (0 -> 1),
122     // with 1 equal to the Nyquist frequency.
123     for (int k = 0; k < nFrequencies; ++k)
124         frequency[k] = frequencyHz[k] / nyquist;
125
126     // We want to get the final values of the coefficients and compute
127     // the response from that instead of some intermediate smoothed
128     // set. Forcefully update the coefficients even if they are not
129     // dirty.
130
131     updateCoefficientsIfNecessary(false, true);
132
133     m_biquad.getFrequencyResponse(nFrequencies, frequency.data(), magResponse, phaseResponse);
134 }
135
136 } // namespace WebCore
137
138 #endif // ENABLE(WEB_AUDIO)