Jamoma API  0.6.0.a19
TTBandpassButterworth2.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFilterLib
4  *
5  * @brief #TTBandpassButterworth2 is a second-order Butterworth bandpass filter.
6  *
7  * @details Butterworth filters have maximum flat frequency response in the pass band.
8  * Based on an algorithm from Dodge & Jerse (1997): Computer Music -
9  * Synthesis, Composition, and Performance. 2nd edition. Schirmer.
10  *
11  * @authors Trond Lossius, Timothy Place,
12  *
13  * @copyright Copyright © 2008, Trond Lossius @n
14  * This code is licensed under the terms of the "New BSD License" @n
15  * http://creativecommons.org/licenses/BSD/
16  */
17 
18 
19 #include "TTBandpassButterworth2.h"
20 
21 #define thisTTClass TTBandpassButterworth2
22 #define thisTTClassName "bandpass.butterworth.2"
23 #define thisTTClassTags "dspFilterLib, audio, processor, filter, bandpass, butterworth"
24 
25 
26 TT_AUDIO_CONSTRUCTOR
27 {
28  // register attributes
30  addAttributeProperty(Frequency, range, TTValue(10.0, sr*0.45));
31  addAttributeProperty(Frequency, rangeChecking, TT("clip"));
32 
34 
35  // register for notifications from the parent class so we can allocate memory as required
36  addUpdates(MaxNumChannels);
37  // register for notifications from the parent class so we can recalculate coefficients as required
38  addUpdates(SampleRate);
39  // make the clear method available to the outside world
40  addMessage(clear);
41 
42  // Set Defaults...
43  setAttributeValue(kTTSym_maxNumChannels, arguments); // This attribute is inherited
44  setAttributeValue(TT("frequency"), 1000.0);
45  setAttributeValue(TT("q"), 50.0);
46  setProcessMethod(processAudio);
47  setCalculateMethod(calculateValue);
48 
49 }
50 
51 
52 TTBandpassButterworth2::~TTBandpassButterworth2()
53 {
54  ;
55 }
56 
57 
59 {
60  mX1.resize(mMaxNumChannels);
61  mX2.resize(mMaxNumChannels);
62  mY1.resize(mMaxNumChannels);
63  mY2.resize(mMaxNumChannels);
64  clear();
65  return kTTErrNone;
66 }
67 
68 
69 TTErr TTBandpassButterworth2::updateSampleRate(const TTValue& oldSampleRate, TTValue&)
70 {
72  return setFrequency(v);
73 }
74 
75 
76 TTErr TTBandpassButterworth2::clear()
77 {
78  mX1.assign(mMaxNumChannels, 0.0);
79  mX2.assign(mMaxNumChannels, 0.0);
80  mY1.assign(mMaxNumChannels, 0.0);
81  mY2.assign(mMaxNumChannels, 0.0);
82  return kTTErrNone;
83 }
84 
85 
87 {
88  mFrequency = newValue;
89  calculateCoefficients();
90  return kTTErrNone;
91 }
92 
93 
94 TTErr TTBandpassButterworth2::setQ(const TTValue& newValue)
95 {
96  mQ = newValue;
97  calculateCoefficients();
98  return kTTErrNone;
99 }
100 
101 
102 void TTBandpassButterworth2::calculateCoefficients()
103 {
104 
105  // Avoid dividing by 0
106  if (mQ > 0.1)
107  mBw = mFrequency/mQ;
108  else
109  mBw = mFrequency;
110 
111  mC = 1. / tan( kTTPi*(mBw/sr) );
112  mD = 2. * cos( kTTTwoPi*(mFrequency/sr) );
113  mA0 = 1. / (1. + mC);
114  // a1 = 0.
115  mA2 = -mA0;
116  mB1 = mA2 * mC * mD;
117  mB2 = mA0 * (mC - 1.);
118 }
119 
120 
122 {
123  y = mA0*x + mA2*mX2[channel] - mB1*mY1[channel] - mB2*mY2[channel];
124  TTZeroDenormal(y);
125  mX2[channel] = mX1[channel];
126  mX1[channel] = x;
127  mY2[channel] = mY1[channel];
128  mY1[channel] = y;
129  return kTTErrNone;
130 }
131 
132 
134 {
135  TT_WRAP_CALCULATE_METHOD(calculateValue);
136 }
137 
TTErr processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Standard audio processing method as used by TTBlue objects.
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTFOUNDATION_EXPORT const TTFloat64 kTTTwoPi
Pre-calculated value of pi * 2.
Definition: TTBase.cpp:24
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTFloat64 mB2
filter coefficients. a1=0 and hence ignored
TTErr setFrequency(const TTValue &value)
Setter for the frequency attribute.
TTBandpassButterworth2 is a second-order Butterworth bandpass filter.
TTErr calculateValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt channel)
Standard single value calculate method as used by DSP objects.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
#define setCalculateMethod(methodName)
A convenience macro to be used by subclasses for setting the calculate method.
TTFloat64 mQ
filter resonance
64-bit floating point
Definition: TTBase.h:272
#define addAttributeProperty(attributeName, propertyName, initialValue)
A convenience macro to be used for registering properties of attributes.
Definition: TTAttribute.h:68
TTErr updateMaxNumChannels(const TTValue &oldMaxNumChannels, TTValue &)
Receives notifications when there are changes to the inherited mMaxNumChannels attribute.
TTFloat64 mFrequency
filter cutoff frequency
TTSampleVector mY2
Output sample n-2.
A simple container for an array of TTAudioSignal pointers.
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
#define addAttributeWithSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom setter...
Definition: TTAttribute.h:47
#define addMessage(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:19
No Error.
Definition: TTBase.h:343
TTFOUNDATION_EXPORT const TTFloat64 kTTPi
[doxygenAppendixC_constExample]
Definition: TTBase.cpp:23
TTSampleVector mX2
Input sample n-2.
TTSampleVector mX1
Input sample n-1.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
#define addUpdates(updateName)
An 'update' is a message sent to a subclass instance from its parent class.
Definition: TTMessage.h:44
TTSampleVector mY1
Output sample n-1.
TTUInt32 sr
Current sample rate being used by this object.