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 #ifndef IIRFilter_h
6 #define IIRFilter_h
7 
8 #include "nsTArray.h"
9 
10 typedef nsTArray<double> AudioDoubleArray;
11 
12 namespace blink {
13 
14 class IIRFilter final {
15  public:
16   // The maximum IIR filter order.  This also limits the number of feedforward
17   // coefficients.  The maximum number of coefficients is 20 according to the
18   // spec.
19   const static size_t kMaxOrder = 19;
20   IIRFilter(const AudioDoubleArray* feedforwardCoef,
21             const AudioDoubleArray* feedbackCoef);
22   ~IIRFilter();
23 
24   void process(const float* sourceP, float* destP, size_t framesToProcess);
25 
26   void reset();
27 
28   void getFrequencyResponse(int nFrequencies, const float* frequency,
29                             float* magResponse, float* phaseResponse);
30 
31   bool buffersAreZero();
32 
33  private:
34   // Filter memory
35   //
36   // For simplicity, we assume |m_xBuffer| and |m_yBuffer| have the same length,
37   // and the length is a power of two.  Since the number of coefficients has a
38   // fixed upper length, the size of xBuffer and yBuffer is fixed. |m_xBuffer|
39   // holds the old input values and |m_yBuffer| holds the old output values
40   // needed to compute the new output value.
41   //
42   // m_yBuffer[m_bufferIndex] holds the most recent output value, say, y[n].
43   // Then m_yBuffer[m_bufferIndex - k] is y[n - k].  Similarly for m_xBuffer.
44   //
45   // To minimize roundoff, these arrays are double's instead of floats.
46   AudioDoubleArray m_xBuffer;
47   AudioDoubleArray m_yBuffer;
48 
49   // Index into the xBuffer and yBuffer arrays where the most current x and y
50   // values should be stored.  xBuffer[bufferIndex] corresponds to x[n], the
51   // current x input value and yBuffer[bufferIndex] is where y[n], the current
52   // output value.
53   int m_bufferIndex;
54 
55   // Coefficients of the IIR filter.
56   const AudioDoubleArray* m_feedback;
57   const AudioDoubleArray* m_feedforward;
58 };
59 
60 }  // namespace blink
61 
62 #endif  // IIRFilter_h
63