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