Bug 865246 - Part 2: Add the Biquad processing implementation to the build system; r=padenot

This commit is contained in:
Ehsan Akhgari 2013-04-26 14:48:45 -04:00
parent f462bfb957
commit 9979321a5a
3 changed files with 19 additions and 156 deletions

View File

@ -26,20 +26,11 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(WEB_AUDIO)
#include "DenormalDisabler.h"
#include "Biquad.h"
#include "DenormalDisabler.h"
#include <algorithm>
#include <stdio.h>
#include <wtf/MathExtras.h>
#if OS(DARWIN)
#include <Accelerate/Accelerate.h>
#endif
namespace WebCore {
@ -47,18 +38,6 @@ const int kBufferSize = 1024;
Biquad::Biquad()
{
#if OS(DARWIN)
// Allocate two samples more for filter history
m_inputBuffer.allocate(kBufferSize + 2);
m_outputBuffer.allocate(kBufferSize + 2);
#endif
#if USE(WEBAUDIO_IPP)
int bufferSize;
ippsIIRGetStateSize64f_BiQuad_32f(1, &bufferSize);
m_ippInternalBuffer = ippsMalloc_8u(bufferSize);
#endif // USE(WEBAUDIO_IPP)
// Initialize as pass-thru (straight-wire, no filter effect)
setNormalizedCoefficients(1, 0, 0, 1, 0, 0);
@ -67,21 +46,10 @@ Biquad::Biquad()
Biquad::~Biquad()
{
#if USE(WEBAUDIO_IPP)
ippsFree(m_ippInternalBuffer);
#endif // USE(WEBAUDIO_IPP)
}
void Biquad::process(const float* sourceP, float* destP, size_t framesToProcess)
{
#if OS(DARWIN)
// Use vecLib if available
processFast(sourceP, destP, framesToProcess);
#elif USE(WEBAUDIO_IPP)
ippsIIR64f_32f(sourceP, destP, static_cast<int>(framesToProcess), m_biquadState);
#else // USE(WEBAUDIO_IPP)
int n = framesToProcess;
// Create local copies of member variables
@ -122,93 +90,18 @@ void Biquad::process(const float* sourceP, float* destP, size_t framesToProcess)
m_b2 = b2;
m_a1 = a1;
m_a2 = a2;
#endif
}
#if OS(DARWIN)
// Here we have optimized version using Accelerate.framework
void Biquad::processFast(const float* sourceP, float* destP, size_t framesToProcess)
{
double filterCoefficients[5];
filterCoefficients[0] = m_b0;
filterCoefficients[1] = m_b1;
filterCoefficients[2] = m_b2;
filterCoefficients[3] = m_a1;
filterCoefficients[4] = m_a2;
double* inputP = m_inputBuffer.data();
double* outputP = m_outputBuffer.data();
double* input2P = inputP + 2;
double* output2P = outputP + 2;
// Break up processing into smaller slices (kBufferSize) if necessary.
int n = framesToProcess;
while (n > 0) {
int framesThisTime = n < kBufferSize ? n : kBufferSize;
// Copy input to input buffer
for (int i = 0; i < framesThisTime; ++i)
input2P[i] = *sourceP++;
processSliceFast(inputP, outputP, filterCoefficients, framesThisTime);
// Copy output buffer to output (converts float -> double).
for (int i = 0; i < framesThisTime; ++i)
*destP++ = static_cast<float>(output2P[i]);
n -= framesThisTime;
}
}
void Biquad::processSliceFast(double* sourceP, double* destP, double* coefficientsP, size_t framesToProcess)
{
// Use double-precision for filter stability
vDSP_deq22D(sourceP, 1, coefficientsP, destP, 1, framesToProcess);
// Save history. Note that sourceP and destP reference m_inputBuffer and m_outputBuffer respectively.
// These buffers are allocated (in the constructor) with space for two extra samples so it's OK to access
// array values two beyond framesToProcess.
sourceP[0] = sourceP[framesToProcess - 2 + 2];
sourceP[1] = sourceP[framesToProcess - 1 + 2];
destP[0] = destP[framesToProcess - 2 + 2];
destP[1] = destP[framesToProcess - 1 + 2];
}
#endif // OS(DARWIN)
void Biquad::reset()
{
#if OS(DARWIN)
// Two extra samples for filter history
double* inputP = m_inputBuffer.data();
inputP[0] = 0;
inputP[1] = 0;
double* outputP = m_outputBuffer.data();
outputP[0] = 0;
outputP[1] = 0;
#elif USE(WEBAUDIO_IPP)
int bufferSize;
ippsIIRGetStateSize64f_BiQuad_32f(1, &bufferSize);
ippsZero_8u(m_ippInternalBuffer, bufferSize);
#else
m_x1 = m_x2 = m_y1 = m_y2 = 0;
#endif
}
void Biquad::setLowpassParams(double cutoff, double resonance)
{
// Limit cutoff to 0 to 1.
cutoff = std::max(0.0, std::min(cutoff, 1.0));
if (cutoff == 1) {
// When cutoff is 1, the z-transform is 1.
setNormalizedCoefficients(1, 0, 0,
@ -219,7 +112,7 @@ void Biquad::setLowpassParams(double cutoff, double resonance)
double g = pow(10.0, 0.05 * resonance);
double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);
double theta = piDouble * cutoff;
double theta = M_PI * cutoff;
double sn = 0.5 * d * sin(theta);
double beta = 0.5 * (1 - sn) / (1 + sn);
double gamma = (0.5 + beta) * cos(theta);
@ -255,7 +148,7 @@ void Biquad::setHighpassParams(double cutoff, double resonance)
double g = pow(10.0, 0.05 * resonance);
double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);
double theta = piDouble * cutoff;
double theta = M_PI * cutoff;
double sn = 0.5 * d * sin(theta);
double beta = 0.5 * (1 - sn) / (1 + sn);
double gamma = (0.5 + beta) * cos(theta);
@ -287,26 +180,13 @@ void Biquad::setNormalizedCoefficients(double b0, double b1, double b2, double a
m_b2 = b2 * a0Inverse;
m_a1 = a1 * a0Inverse;
m_a2 = a2 * a0Inverse;
#if USE(WEBAUDIO_IPP)
Ipp64f taps[6];
taps[0] = m_b0;
taps[1] = m_b1;
taps[2] = m_b2;
taps[3] = 1;
taps[4] = m_a1;
taps[5] = m_a2;
m_biquadState = 0;
ippsIIRInit64f_BiQuad_32f(&m_biquadState, taps, 1, 0, m_ippInternalBuffer);
#endif // USE(WEBAUDIO_IPP)
}
void Biquad::setLowShelfParams(double frequency, double dbGain)
{
// Clip frequencies to between 0 and 1, inclusive.
frequency = std::max(0.0, std::min(frequency, 1.0));
double A = pow(10.0, dbGain / 40);
if (frequency == 1) {
@ -314,7 +194,7 @@ void Biquad::setLowShelfParams(double frequency, double dbGain)
setNormalizedCoefficients(A * A, 0, 0,
1, 0, 0);
} else if (frequency > 0) {
double w0 = piDouble * frequency;
double w0 = M_PI * frequency;
double S = 1; // filter slope (1 is max value)
double alpha = 0.5 * sin(w0) * sqrt((A + 1 / A) * (1 / S - 1) + 2);
double k = cos(w0);
@ -349,7 +229,7 @@ void Biquad::setHighShelfParams(double frequency, double dbGain)
setNormalizedCoefficients(1, 0, 0,
1, 0, 0);
} else if (frequency > 0) {
double w0 = piDouble * frequency;
double w0 = M_PI * frequency;
double S = 1; // filter slope (1 is max value)
double alpha = 0.5 * sin(w0) * sqrt((A + 1 / A) * (1 / S - 1) + 2);
double k = cos(w0);
@ -384,7 +264,7 @@ void Biquad::setPeakingParams(double frequency, double Q, double dbGain)
if (frequency > 0 && frequency < 1) {
if (Q > 0) {
double w0 = piDouble * frequency;
double w0 = M_PI * frequency;
double alpha = sin(w0) / (2 * Q);
double k = cos(w0);
@ -420,7 +300,7 @@ void Biquad::setAllpassParams(double frequency, double Q)
if (frequency > 0 && frequency < 1) {
if (Q > 0) {
double w0 = piDouble * frequency;
double w0 = M_PI * frequency;
double alpha = sin(w0) / (2 * Q);
double k = cos(w0);
@ -456,7 +336,7 @@ void Biquad::setNotchParams(double frequency, double Q)
if (frequency > 0 && frequency < 1) {
if (Q > 0) {
double w0 = piDouble * frequency;
double w0 = M_PI * frequency;
double alpha = sin(w0) / (2 * Q);
double k = cos(w0);
@ -491,7 +371,7 @@ void Biquad::setBandpassParams(double frequency, double Q)
Q = std::max(0.0, Q);
if (frequency > 0 && frequency < 1) {
double w0 = piDouble * frequency;
double w0 = M_PI * frequency;
if (Q > 0) {
double alpha = sin(w0) / (2 * Q);
double k = cos(w0);
@ -572,7 +452,7 @@ void Biquad::getFrequencyResponse(int nFrequencies,
double a2 = m_a2;
for (int k = 0; k < nFrequencies; ++k) {
double omega = -piDouble * frequency[k];
double omega = -M_PI * frequency[k];
Complex z = Complex(cos(omega), sin(omega));
Complex numerator = b0 + (b1 + b2 * z) * z;
Complex denominator = Complex(1, 0) + (a1 + a2 * z) * z;
@ -584,4 +464,3 @@ void Biquad::getFrequencyResponse(int nFrequencies,
} // namespace WebCore
#endif // ENABLE(WEB_AUDIO)

View File

@ -29,25 +29,21 @@
#ifndef Biquad_h
#define Biquad_h
#include "AudioArray.h"
#include <sys/types.h>
#include <wtf/Complex.h>
#if USE(WEBAUDIO_IPP)
#include <ipps.h>
#endif // USE(WEBAUDIO_IPP)
#include <complex>
namespace WebCore {
typedef std::complex<double> Complex;
// A basic biquad (two-zero / two-pole digital filter)
//
// It can be configured to a number of common and very useful filters:
// lowpass, highpass, shelving, parameteric, notch, allpass, ...
class Biquad {
public:
public:
Biquad();
virtual ~Biquad();
~Biquad();
void process(const float* sourceP, float* destP, size_t framesToProcess);
@ -82,7 +78,7 @@ public:
float* phaseResponse);
private:
void setNormalizedCoefficients(double b0, double b1, double b2, double a0, double a1, double a2);
// Filter coefficients. The filter is defined as
//
// 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].
@ -92,24 +88,11 @@ private:
double m_a1;
double m_a2;
#if OS(DARWIN)
void processFast(const float* sourceP, float* destP, size_t framesToProcess);
void processSliceFast(double* sourceP, double* destP, double* coefficientsP, size_t framesToProcess);
AudioDoubleArray m_inputBuffer;
AudioDoubleArray m_outputBuffer;
#elif USE(WEBAUDIO_IPP)
IppsIIRState64f_32f* m_biquadState;
Ipp8u* m_ippInternalBuffer;
#else
// Filter memory
double m_x1; // input delayed by 1 sample
double m_x2; // input delayed by 2 samples
double m_y1; // output delayed by 1 sample
double m_y2; // output delayed by 2 samples
#endif
};
} // namespace WebCore

View File

@ -13,6 +13,7 @@ LIBRARY_NAME := gkconwebaudio_blink_s
LIBXUL_LIBRARY := 1
CPPSRCS := \
Biquad.cpp \
DynamicsCompressor.cpp \
DynamicsCompressorKernel.cpp \
ZeroPole.cpp \