| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/webaudio/IIRProcessor.h" | 5 #include "modules/webaudio/IIRProcessor.h" |
| 6 | 6 |
| 7 #include "modules/webaudio/IIRDSPKernel.h" | 7 #include "modules/webaudio/IIRDSPKernel.h" |
| 8 #include "wtf/PtrUtil.h" | 8 #include "wtf/PtrUtil.h" |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 namespace blink { | 11 namespace blink { |
| 12 | 12 |
| 13 IIRProcessor::IIRProcessor(float sampleRate, | 13 IIRProcessor::IIRProcessor(float sampleRate, |
| 14 size_t numberOfChannels, | 14 size_t numberOfChannels, |
| 15 const Vector<double>& feedforwardCoef, | 15 const Vector<double>& feedforwardCoef, |
| 16 const Vector<double>& feedbackCoef) | 16 const Vector<double>& feedbackCoef) |
| 17 : AudioDSPKernelProcessor(sampleRate, numberOfChannels) { | 17 : AudioDSPKernelProcessor(sampleRate, numberOfChannels) { |
| 18 unsigned feedbackLength = feedbackCoef.size(); | 18 unsigned feedbackLength = feedbackCoef.size(); |
| 19 unsigned feedforwardLength = feedforwardCoef.size(); | 19 unsigned feedforwardLength = feedforwardCoef.size(); |
| 20 DCHECK_GT(feedbackLength, 0u); | 20 DCHECK_GT(feedbackLength, 0u); |
| 21 DCHECK_GT(feedforwardLength, 0u); | 21 DCHECK_GT(feedforwardLength, 0u); |
| 22 | 22 |
| 23 m_feedforward.allocate(feedforwardLength); | 23 m_feedforward.allocate(feedforwardLength); |
| 24 m_feedback.allocate(feedbackLength); | 24 m_feedback.allocate(feedbackLength); |
| 25 m_feedforward.copyToRange(feedforwardCoef.data(), 0, feedforwardLength); | 25 m_feedforward.copyToRange(feedforwardCoef.data(), 0, feedforwardLength); |
| 26 m_feedback.copyToRange(feedbackCoef.data(), 0, feedbackLength); | 26 m_feedback.copyToRange(feedbackCoef.data(), 0, feedbackLength); |
| 27 | 27 |
| 28 // Need to scale the feedback and feedforward coefficients appropriately. (It'
s up to the caller | 28 // Need to scale the feedback and feedforward coefficients appropriately. |
| 29 // to ensure feedbackCoef[0] is not 0!) | 29 // (It's up to the caller to ensure feedbackCoef[0] is not 0.) |
| 30 DCHECK_NE(feedbackCoef[0], 0); | 30 DCHECK_NE(feedbackCoef[0], 0); |
| 31 | 31 |
| 32 if (feedbackCoef[0] != 1) { | 32 if (feedbackCoef[0] != 1) { |
| 33 // The provided filter is: | 33 // The provided filter is: |
| 34 // | 34 // |
| 35 // a[0]*y(n) + a[1]*y(n-1) + ... = b[0]*x(n) + b[1]*x(n-1) + ... | 35 // a[0]*y(n) + a[1]*y(n-1) + ... = b[0]*x(n) + b[1]*x(n-1) + ... |
| 36 // | 36 // |
| 37 // We want the leading coefficient of y(n) to be 1: | 37 // We want the leading coefficient of y(n) to be 1: |
| 38 // | 38 // |
| 39 // y(n) + a[1]/a[0]*y(n-1) + ... = b[0]/a[0]*x(n) + b[1]/a[0]*x(n-1) + ... | 39 // y(n) + a[1]/a[0]*y(n-1) + ... = b[0]/a[0]*x(n) + b[1]/a[0]*x(n-1) + ... |
| 40 // | 40 // |
| 41 // Thus, the feedback and feedforward coefficients need to be scaled by 1/a[
0]. | 41 // Thus, the feedback and feedforward coefficients need to be scaled by |
| 42 // 1/a[0]. |
| 42 float scale = feedbackCoef[0]; | 43 float scale = feedbackCoef[0]; |
| 43 for (unsigned k = 1; k < feedbackLength; ++k) | 44 for (unsigned k = 1; k < feedbackLength; ++k) |
| 44 m_feedback[k] /= scale; | 45 m_feedback[k] /= scale; |
| 45 | 46 |
| 46 for (unsigned k = 0; k < feedforwardLength; ++k) | 47 for (unsigned k = 0; k < feedforwardLength; ++k) |
| 47 m_feedforward[k] /= scale; | 48 m_feedforward[k] /= scale; |
| 48 | 49 |
| 49 // The IIRFilter checks to make sure this coefficient is 1, so make it so. | 50 // The IIRFilter checks to make sure this coefficient is 1, so make it so. |
| 50 m_feedback[0] = 1; | 51 m_feedback[0] = 1; |
| 51 } | 52 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 63 } | 64 } |
| 64 | 65 |
| 65 void IIRProcessor::process(const AudioBus* source, | 66 void IIRProcessor::process(const AudioBus* source, |
| 66 AudioBus* destination, | 67 AudioBus* destination, |
| 67 size_t framesToProcess) { | 68 size_t framesToProcess) { |
| 68 if (!isInitialized()) { | 69 if (!isInitialized()) { |
| 69 destination->zero(); | 70 destination->zero(); |
| 70 return; | 71 return; |
| 71 } | 72 } |
| 72 | 73 |
| 73 // For each channel of our input, process using the corresponding IIRDSPKernel
into the output | 74 // For each channel of our input, process using the corresponding IIRDSPKernel |
| 74 // channel. | 75 // into the output channel. |
| 75 for (unsigned i = 0; i < m_kernels.size(); ++i) | 76 for (unsigned i = 0; i < m_kernels.size(); ++i) |
| 76 m_kernels[i]->process(source->channel(i)->data(), | 77 m_kernels[i]->process(source->channel(i)->data(), |
| 77 destination->channel(i)->mutableData(), | 78 destination->channel(i)->mutableData(), |
| 78 framesToProcess); | 79 framesToProcess); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void IIRProcessor::getFrequencyResponse(int nFrequencies, | 82 void IIRProcessor::getFrequencyResponse(int nFrequencies, |
| 82 const float* frequencyHz, | 83 const float* frequencyHz, |
| 83 float* magResponse, | 84 float* magResponse, |
| 84 float* phaseResponse) { | 85 float* phaseResponse) { |
| 85 m_responseKernel->getFrequencyResponse(nFrequencies, frequencyHz, magResponse, | 86 m_responseKernel->getFrequencyResponse(nFrequencies, frequencyHz, magResponse, |
| 86 phaseResponse); | 87 phaseResponse); |
| 87 } | 88 } |
| 88 | 89 |
| 89 } // namespace blink | 90 } // namespace blink |
| OLD | NEW |