Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Biquad.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014, The Cinder Project
3 
4  This code is intended to be used with the Cinder C++ library, http://libcinder.org
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
7  the following conditions are met:
8 
9  * Redistributions of source code must retain the above copyright notice, this list of conditions and
10  the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12  the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
15  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
18  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  POSSIBILITY OF SUCH DAMAGE.
22 
23 
24  This file is derived from the Biquad class included in WebKit / WebAudio, its copyright notice is as
25  follows:
26 
27  * Copyright (C) 2010 Google Inc. All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
33  * 1. Redistributions of source code must retain the above copyright
34  * notice, this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright
36  * notice, this list of conditions and the following disclaimer in the
37  * documentation and/or other materials provided with the distribution.
38  * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
39  * its contributors may be used to endorse or promote products derived
40  * from this software without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
43  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
44  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
46  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52  */
53 
54 #pragma once
55 
56 #include "cinder/audio/dsp/Dsp.h"
57 #include "cinder/audio/Buffer.h"
58 
59 namespace cinder { namespace audio { namespace dsp {
60 
67 class Biquad {
68  public:
69  Biquad();
70  virtual ~Biquad();
71 
72  void setLowpassParams( double cutoffFreq, double resonance );
73  void setHighpassParams( double frequency, double resonance );
74  void setBandpassParams( double frequency, double Q );
75  void setLowShelfParams( double frequency, double dbGain );
76  void setHighShelfParams( double frequency, double dbGain );
77  void setPeakingParams( double frequency, double Q, double dbGain );
78  void setAllpassParams( double frequency, double Q );
79  void setNotchParams( double frequency, double Q );
80 
82  void process( const float *source, float *dest, size_t framesToProcess );
84  void getFrequencyResponse( int nFrequencies, const float *frequency, float *magResponse, float *phaseResponse );
86  void reset();
87 
88  private:
89  void setNormalizedCoefficients( double b0, double b1, double b2, double a0, double a1, double a2 );
90 
91  // Filter coefficients. The filter is defined as
92  //
93  // y[n] + mA1 * y[n-1] + mA2 * y[n-2] = mB0 * x[n] + mB1 * x[n-1] + mB2 * x[n-2].
94  double mB0;
95  double mB1;
96  double mB2;
97  double mA1;
98  double mA2;
99 
100  // Filter memory
101  double mX1; // input delayed by 1 sample
102  double mX2; // input delayed by 2 samples
103  double mY1; // output delayed by 1 sample
104  double mY2; // output delayed by 2 samples
105 
106 #if defined( CINDER_AUDIO_VDSP )
107  void processVDsp( const float *source, float *dest, size_t framesToProcess );
108  void processSliceVDsp( double *source, double *dest, double *coefficientsP, size_t framesToProcess );
109 
110  // used with vDSP only
111 // AlignedArrayPtrd mInputBuffer, mOutputBuffer;
112  std::vector<double> mInputBuffer, mOutputBuffer;
113 #endif
114 };
115 
116 } } } // namespace cinder::audio::dsp
GLsizei GLsizei GLchar * source
Definition: GLee.h:2361
void setNotchParams(double frequency, double Q)
Definition: Biquad.cpp:419
void getFrequencyResponse(int nFrequencies, const float *frequency, float *magResponse, float *phaseResponse)
Filter response at a set of n frequencies. The magnitude and phase response are returned in magRespon...
Definition: Biquad.cpp:139
General filtering class (two-pole, two-zero).
Definition: Biquad.h:67
void setPeakingParams(double frequency, double Q, double dbGain)
Definition: Biquad.cpp:349
void setLowShelfParams(double frequency, double dbGain)
Definition: Biquad.cpp:283
Biquad()
Definition: Biquad.cpp:74
void setLowpassParams(double cutoffFreq, double resonance)
Definition: Biquad.cpp:175
virtual ~Biquad()
Definition: Biquad.cpp:89
void setHighpassParams(double frequency, double resonance)
Definition: Biquad.cpp:209
void setAllpassParams(double frequency, double Q)
Definition: Biquad.cpp:385
void process(const float *source, float *dest, size_t framesToProcess)
Processes the audio array of length framesToProcess provided in source, leaving the result in dest...
Definition: Biquad.cpp:93
void reset()
Resets filter state.
Definition: Biquad.cpp:453
void setBandpassParams(double frequency, double Q)
Definition: Biquad.cpp:245
void setHighShelfParams(double frequency, double dbGain)
Definition: Biquad.cpp:316