Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FilterNode.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 #pragma once
25 
26 #include "cinder/audio/Node.h"
28 
29 #include <vector>
30 
31 // TODO: add api for setting biquad with arbitrary set of coefficients, similar to pd's [biquad~]
32 
33 namespace cinder { namespace audio {
34 
35 typedef std::shared_ptr<class FilterLowPassNode> FilterLowPassNodeRef;
36 typedef std::shared_ptr<class FilterHighPassNode> FilterHighPassNodeRef;
37 typedef std::shared_ptr<class FilterBandPassNode> FilterBandPassNodeRef;
38 
40 class FilterBiquadNode : public Node {
41  public:
44 
46  FilterBiquadNode( Mode mode = Mode::LOWPASS, const Format &format = Format() );
47  virtual ~FilterBiquadNode() {}
48 
50  void setMode( Mode mode ) { mMode = mode; mCoeffsDirty = true; }
52  Mode getMode() const { return mMode; }
54  void setFreq( float freq ) { mFreq = freq; mCoeffsDirty = true; }
56  float getFreq() const { return mFreq; }
58  void setQ( float q ) { mQ = q; mCoeffsDirty = true; }
59  // Returns the q, or 'quality', parameter of the Biquad.
60  float getQ() const { return mQ; }
62  void setGain( float gain ) { mGain = gain; mCoeffsDirty = true; }
64  float getGain() const { return mGain; }
65 
66  protected:
67  void initialize() override;
68  void uninitialize() override;
69  void process( Buffer *buffer ) override;
70 
71  void updateBiquadParams();
72 
73  std::vector<dsp::Biquad> mBiquads;
74  std::atomic<bool> mCoeffsDirty;
76  size_t mNiquist;
77 
79  float mFreq, mQ, mGain;
80 };
81 
84  public:
87  virtual ~FilterLowPassNode() {}
88 
90  void setCutoffFreq( float freq ) { setFreq( freq ); }
92  float getCutoffFreq() const { return mFreq; }
94  void setResonance( float resonance ) { setQ( resonance ); }
96  float getResonance() const { return mQ; }
97 };
98 
101  public:
104  virtual ~FilterHighPassNode() {}
105 
107  void setCutoffFreq( float freq ) { setFreq( freq ); }
109  float getCutoffFreq() const { return mFreq; }
111  void setResonance( float resonance ) { setQ( resonance ); }
113  float getResonance() const { return mQ; }
114 };
115 
118  public:
121  virtual ~FilterBandPassNode() {}
122 
124  void setCenterFreq( float freq ) { setFreq( freq ); }
126  float getCenterFreq() const { return mFreq; }
128  void setWidth( float width ) { setQ( width ); }
130  float getWidth() const { return mQ; }
131 };
132 
133 } } // namespace cinder::audio
Mode mMode
Definition: FilterNode.h:78
float getWidth() const
Returns the width of the filtering.
Definition: FilterNode.h:130
GLenum mode
Definition: GLee.h:3042
size_t mNiquist
Definition: FilterNode.h:76
FilterBandPassNode(const Format &format=Format())
Constructs a FilterBandPassNode with optional format.
Definition: FilterNode.h:120
void setWidth(float width)
Sets the width of the filtering.
Definition: FilterNode.h:128
virtual ~FilterBiquadNode()
Definition: FilterNode.h:47
void setMode(Mode mode)
Sets the mode, which updates the coefficients so that the frequency response is that of a common type...
Definition: FilterNode.h:50
float getCenterFreq() const
Returns the center frequency of the filter in hertz.
Definition: FilterNode.h:126
float getQ() const
Definition: FilterNode.h:60
GLenum GLsizei width
Definition: GLee.h:969
A high-pass filtering Node. This is a subclass of FilterBiquadNode and manages its configuration appr...
Definition: FilterNode.h:100
General class for filtering nodes based on a biquad (two pole, two zero) filter.
Definition: FilterNode.h:40
FilterHighPassNode(const Format &format=Format())
Constructs a FilterHighPassNode with optional format.
Definition: FilterNode.h:103
void setCenterFreq(float freq)
Sets the center frequency of the filter in hertz.
Definition: FilterNode.h:124
A band-pass filtering Node. This is a subclass of FilterBiquadNode and manages its configuration appr...
Definition: FilterNode.h:117
std::shared_ptr< class FilterHighPassNode > FilterHighPassNodeRef
Definition: FilterNode.h:36
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: FilterNode.cpp:35
float getResonance() const
Returns the resonance of the filter in decibels.
Definition: FilterNode.h:96
float getCutoffFreq() const
Returns the cutoff frequency in hertz.
Definition: FilterNode.h:109
std::shared_ptr< class FilterBandPassNode > FilterBandPassNodeRef
Definition: FilterNode.h:37
GLdouble GLdouble GLdouble GLdouble q
Definition: GLee.h:1522
float getCutoffFreq() const
Returns the cutoff frequency in hertz.
Definition: FilterNode.h:92
float mGain
Definition: FilterNode.h:79
float getGain() const
Returns the gain of the filter in decibels.
Definition: FilterNode.h:64
GLuint buffer
Definition: GLee.h:2065
A low-pass filtering Node. This is a subclass of FilterBiquadNode and manages its configuration appro...
Definition: FilterNode.h:83
float mFreq
Definition: FilterNode.h:79
void setCutoffFreq(float freq)
Sets the cutoff frequency in hertz, above which frequencies are attenuated.
Definition: FilterNode.h:90
float getResonance() const
Returns the resonance of the filter in decibels.
Definition: FilterNode.h:113
Definition: Node.h:72
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: FilterNode.cpp:52
float getFreq() const
Returns the current frequency in hertz.
Definition: FilterNode.h:56
float mQ
Definition: FilterNode.h:79
virtual ~FilterHighPassNode()
Definition: FilterNode.h:104
Fundamental building block for creating an audio processing graph.
Definition: Node.h:59
void setFreq(float freq)
Sets the frequency in hertz. This is interpreted differently depending on what the current Mode is...
Definition: FilterNode.h:54
void setResonance(float resonance)
Sets the resonance of the filter in decibels.
Definition: FilterNode.h:111
Mode getMode() const
Returns the current mode.
Definition: FilterNode.h:52
virtual ~FilterBandPassNode()
Definition: FilterNode.h:121
void setGain(float gain)
Sets the gain of the filter in decibels. Not used in all Mode's.
Definition: FilterNode.h:62
GLenum GLsizei GLenum format
Definition: GLee.h:969
BufferT< double > mBufferd
Definition: FilterNode.h:75
std::vector< dsp::Biquad > mBiquads
Definition: FilterNode.h:73
Mode
The modes that are available as 'preset' coefficients, which set the frequency response to a common t...
Definition: FilterNode.h:43
FilterLowPassNode(const Format &format=Format())
Constructs a FilterLowPassNode with optional format.
Definition: FilterNode.h:86
std::shared_ptr< class FilterLowPassNode > FilterLowPassNodeRef
Definition: FilterNode.h:35
void setResonance(float resonance)
Sets the resonance of the filter in decibels.
Definition: FilterNode.h:94
void setQ(float q)
Sets the q, or 'quality', parameter of the Biquad, which can be thought of as the sharpness of the fi...
Definition: FilterNode.h:58
void updateBiquadParams()
Definition: FilterNode.cpp:65
void uninitialize() override
Called once the contents of initialize are no longer relevant, i.e. connections have changed...
Definition: FilterNode.cpp:47
FilterBiquadNode(Mode mode=Mode::LOWPASS, const Format &format=Format())
Constructs a FilterBiquadNode, initializing the mode to mode (default = Mode::LOWPASS). Can optionally provide format.
Definition: FilterNode.cpp:30
virtual ~FilterLowPassNode()
Definition: FilterNode.h:87
std::atomic< bool > mCoeffsDirty
Definition: FilterNode.h:74
void setCutoffFreq(float freq)
Sets the cutoff frequency in hertz, below which frequencies are attenuated.
Definition: FilterNode.h:107