Add methods to compute magnitude and phase response for biquads
[WebKit-https.git] / Source / WebCore / webaudio / BiquadProcessor.cpp
index 192755a5b8ef9935fabc64290e7641d6e2588978..12243c0c99be1cc7753ec2a9f7952839f8699625 100644 (file)
@@ -90,14 +90,10 @@ PassOwnPtr<AudioDSPKernel> BiquadProcessor::createKernel()
     return adoptPtr(new BiquadDSPKernel(this));
 }
 
-void BiquadProcessor::process(AudioBus* source, AudioBus* destination, size_t framesToProcess)
+void BiquadProcessor::checkForDirtyCoefficients()
 {
-    if (!isInitialized()) {
-        destination->zero();
-        return;
-    }
-        
-    // Deal with smoothing / de-zippering.  Start out assuming filter parameters are not changing.
+    // Deal with smoothing / de-zippering. Start out assuming filter parameters are not changing.
+
     // The BiquadDSPKernel objects rely on this value to see if they need to re-compute their internal filter coefficients.
     m_filterCoefficientsDirty = false;
     
@@ -109,14 +105,24 @@ void BiquadProcessor::process(AudioBus* source, AudioBus* destination, size_t fr
         m_filterCoefficientsDirty = true;
         m_hasJustReset = false;
     } else {
-        // Smooth all of the filter parameters.  If they haven't yet converged to their target value then mark coefficients as dirty.
+        // Smooth all of the filter parameters. If they haven't yet converged to their target value then mark coefficients as dirty.
         bool isStable1 = m_parameter1->smooth();
         bool isStable2 = m_parameter2->smooth();
         bool isStable3 = m_parameter3->smooth();
         if (!(isStable1 && isStable2 && isStable3))
             m_filterCoefficientsDirty = true;
     }
+}
+
+void BiquadProcessor::process(AudioBus* source, AudioBus* destination, size_t framesToProcess)
+{
+    if (!isInitialized()) {
+        destination->zero();
+        return;
+    }
         
+    checkForDirtyCoefficients();
+            
     // For each channel of our input, process using the corresponding BiquadDSPKernel into the output channel.
     for (unsigned i = 0; i < m_kernels.size(); ++i)
         m_kernels[i]->process(source->channel(i)->data(), destination->channel(i)->data(), framesToProcess);
@@ -130,6 +136,20 @@ void BiquadProcessor::setType(FilterType type)
     }
 }
 
+void BiquadProcessor::getFrequencyResponse(int nFrequencies,
+                                           const float* frequencyHz,
+                                           float* magResponse,
+                                           float* phaseResponse)
+{
+    // Compute the frequency response on a separate temporary kernel
+    // to avoid interfering with the processing running in the audio
+    // thread on the main kernels.
+    
+    OwnPtr<BiquadDSPKernel> responseKernel = adoptPtr(new BiquadDSPKernel(this));
+
+    responseKernel->getFrequencyResponse(nFrequencies, frequencyHz, magResponse, phaseResponse);
+}
+
 } // namespace WebCore
 
 #endif // ENABLE(WEB_AUDIO)