Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Unified Diff: third_party/WebKit/Source/platform/audio/Biquad.cpp

Issue 1885723003: Implement Biquad lowpass and highpass filters according to spec (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/webaudio/resources/biquad-testing.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/platform/audio/Biquad.cpp
diff --git a/third_party/WebKit/Source/platform/audio/Biquad.cpp b/third_party/WebKit/Source/platform/audio/Biquad.cpp
index 913c07a4052d3accbd5485c9f58a7c8c842f070e..f29e77aa3d5091f16176b7036447c38c9bbd7920 100644
--- a/third_party/WebKit/Source/platform/audio/Biquad.cpp
+++ b/third_party/WebKit/Source/platform/audio/Biquad.cpp
@@ -255,23 +255,22 @@ void Biquad::setLowpassParams(int index, double cutoff, double resonance)
1, 0, 0);
} else if (cutoff > 0) {
// Compute biquad coefficients for lowpass filter
- resonance = std::max(0.0, resonance); // can't go negative
- double g = pow(10.0, 0.05 * resonance);
- double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);
+ resonance = pow(10, resonance / 20);
double theta = piDouble * cutoff;
- double sn = 0.5 * d * sin(theta);
- double beta = 0.5 * (1 - sn) / (1 + sn);
- double gamma = (0.5 + beta) * cos(theta);
- double alpha = 0.25 * (0.5 + beta - gamma);
-
- double b0 = 2 * alpha;
- double b1 = 2 * 2 * alpha;
- double b2 = 2 * alpha;
- double a1 = 2 * -gamma;
- double a2 = 2 * beta;
-
- setNormalizedCoefficients(index, b0, b1, b2, 1, a1, a2);
+ double alpha = sin(theta) / (2 * resonance);
+ double cosw = cos(theta);
+ double beta = (1 - cosw) / 2;
+
+ double b0 = beta;
+ double b1 = 2 * beta;
+ double b2 = beta;
+
+ double a0 = 1 + alpha;
+ double a1 = -2 * cosw;
+ double a2 = 1 - alpha;
+
+ setNormalizedCoefficients(index, b0, b1, b2, a0, a1, a2);
} else {
// When cutoff is zero, nothing gets through the filter, so set
// coefficients up correctly.
@@ -293,23 +292,22 @@ void Biquad::setHighpassParams(int index, double cutoff, double resonance)
1, 0, 0);
} else if (cutoff > 0) {
// Compute biquad coefficients for highpass filter
- resonance = std::max(0.0, resonance); // can't go negative
- double g = pow(10.0, 0.05 * resonance);
- double d = sqrt((4 - sqrt(16 - 16 / (g * g))) / 2);
+ resonance = pow(10, resonance / 20);
double theta = piDouble * cutoff;
- double sn = 0.5 * d * sin(theta);
- double beta = 0.5 * (1 - sn) / (1 + sn);
- double gamma = (0.5 + beta) * cos(theta);
- double alpha = 0.25 * (0.5 + beta + gamma);
-
- double b0 = 2 * alpha;
- double b1 = 2 * -2 * alpha;
- double b2 = 2 * alpha;
- double a1 = 2 * -gamma;
- double a2 = 2 * beta;
-
- setNormalizedCoefficients(index, b0, b1, b2, 1, a1, a2);
+ double alpha = sin(theta) / (2 * resonance);
+ double cosw = cos(theta);
+ double beta = (1 + cosw) / 2;
+
+ double b0 = beta;
+ double b1 = -2 * beta;
+ double b2 = beta;
+
+ double a0 = 1 + alpha;
+ double a1 = -2 * cosw;
+ double a2 = 1 - alpha;
+
+ setNormalizedCoefficients(index, b0, b1, b2, a0, a1, a2);
} else {
// When cutoff is zero, we need to be careful because the above
// gives a quadratic divided by the same quadratic, with poles
« no previous file with comments | « third_party/WebKit/LayoutTests/webaudio/resources/biquad-testing.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698