OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/webaudio/IIRProcessor.h" |
| 6 |
| 7 #include "modules/webaudio/IIRDSPKernel.h" |
| 8 |
| 9 namespace blink { |
| 10 |
| 11 IIRProcessor::IIRProcessor(float sampleRate, size_t numberOfChannels, const Vect
or<double>& feedforwardCoef, const Vector<double>& feedbackCoef) |
| 12 : AudioDSPKernelProcessor(sampleRate, numberOfChannels) |
| 13 { |
| 14 unsigned feedbackLength = feedbackCoef.size(); |
| 15 unsigned feedforwardLength = feedforwardCoef.size(); |
| 16 ASSERT(feedbackLength > 0); |
| 17 ASSERT(feedforwardLength > 0); |
| 18 |
| 19 m_feedforward.allocate(feedforwardLength); |
| 20 m_feedback.allocate(feedbackLength); |
| 21 m_feedforward.copyToRange(feedforwardCoef.data(), 0, feedforwardLength); |
| 22 m_feedback.copyToRange(feedbackCoef.data(), 0, feedbackLength); |
| 23 |
| 24 // Need to scale the feedback and feedforward coefficients appropriately. (I
t's up to the caller |
| 25 // to ensure feedbackCoef[0] is not 0!) |
| 26 ASSERT(feedbackCoef[0] != 0); |
| 27 |
| 28 if (feedbackCoef[0] != 1) { |
| 29 // The provided filter is: |
| 30 // |
| 31 // a[0]*y(n) + a[1]*y(n-1) + ... = b[0]*x(n) + b[1]*x(n-1) + ... |
| 32 // |
| 33 // We want the leading coefficient of y(n) to be 1: |
| 34 // |
| 35 // y(n) + a[1]/a[0]*y(n-1) + ... = b[0]/a[0]*x(n) + b[1]/a[0]*x(n-1) +
... |
| 36 // |
| 37 // Thus, the feedback and feedforward coefficients need to be scaled by
1/a[0]. |
| 38 float scale = feedbackCoef[0]; |
| 39 for (unsigned k = 1; k < feedbackLength; ++k) |
| 40 m_feedback[k] /= scale; |
| 41 |
| 42 for (unsigned k = 0; k < feedforwardLength; ++k) |
| 43 m_feedforward[k] /= scale; |
| 44 |
| 45 // The IIRFilter checks to make sure this coefficient is 1, so make it s
o. |
| 46 m_feedback[0] = 1; |
| 47 } |
| 48 |
| 49 m_responseKernel = adoptPtr(new IIRDSPKernel(this)); |
| 50 } |
| 51 |
| 52 IIRProcessor::~IIRProcessor() |
| 53 { |
| 54 if (isInitialized()) |
| 55 uninitialize(); |
| 56 } |
| 57 |
| 58 PassOwnPtr<AudioDSPKernel> IIRProcessor::createKernel() |
| 59 { |
| 60 return adoptPtr(new IIRDSPKernel(this)); |
| 61 } |
| 62 |
| 63 void IIRProcessor::process(const AudioBus* source, AudioBus* destination, size_t
framesToProcess) |
| 64 { |
| 65 if (!isInitialized()) { |
| 66 destination->zero(); |
| 67 return; |
| 68 } |
| 69 |
| 70 // For each channel of our input, process using the corresponding IIRDSPKern
el into the output |
| 71 // channel. |
| 72 for (unsigned i = 0; i < m_kernels.size(); ++i) |
| 73 m_kernels[i]->process(source->channel(i)->data(), destination->channel(i
)->mutableData(), framesToProcess); |
| 74 } |
| 75 |
| 76 void IIRProcessor::getFrequencyResponse(int nFrequencies, const float* frequency
Hz, float* magResponse, float* phaseResponse) |
| 77 { |
| 78 m_responseKernel->getFrequencyResponse(nFrequencies, frequencyHz, magRespons
e, phaseResponse); |
| 79 } |
| 80 |
| 81 } // namespace blink |
OLD | NEW |