Jamoma API  0.6.0.a19
TTLowpassButterworth2.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFilterLib
4  *
5  * @brief #TTLowpassButterworth2 is a second-order Butterworth lowpass 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 "TTLowpassButterworth2.h"
20 
21 #define thisTTClass TTLowpassButterworth2
22 #define thisTTClassName "lowpass.butterworth.2"
23 #define thisTTClassTags "dspFilterLib, audio, processor, filter, lowpass, butterworth"
24 
25 
26 TT_AUDIO_CONSTRUCTOR
27 {
28  // register attributes
30  addAttributeProperty(Frequency, range, TTValue(10.0, sr*0.475));
31  addAttributeProperty(Frequency, rangeChecking, TT("clip"));
32 
33  // register for notifications from the parent class so we can allocate memory as required
34  addUpdates(MaxNumChannels);
35  // register for notifications from the parent class so we can recalculate coefficients as required
36  addUpdates(SampleRate);
37  // make the clear method available to the outside world
38  addMessage(clear);
39 
40  // Set Defaults...
41  setAttributeValue(kTTSym_maxNumChannels, arguments); // This attribute is inherited
42  setAttributeValue(TT("frequency"), 1000.0);
43  setCalculateMethod(calculateValue);
44  setProcessMethod(processAudio);
45 }
46 
47 
48 TTLowpassButterworth2::~TTLowpassButterworth2()
49 {
50  ;
51 }
52 
53 
55 {
56  mX1.resize(mMaxNumChannels);
57  mX2.resize(mMaxNumChannels);
58  mY1.resize(mMaxNumChannels);
59  mY2.resize(mMaxNumChannels);
60  clear();
61  return kTTErrNone;
62 }
63 
64 
65 TTErr TTLowpassButterworth2::updateSampleRate(const TTValue& oldSampleRate, TTValue&)
66 {
68  return setFrequency(v);
69 }
70 
71 
73 {
74  mX1.assign(mMaxNumChannels, 0.0);
75  mX2.assign(mMaxNumChannels, 0.0);
76  mY1.assign(mMaxNumChannels, 0.0);
77  mY2.assign(mMaxNumChannels, 0.0);
78  return kTTErrNone;
79 }
80 
81 
83 {
84  mFrequency = newValue;
85 
86  mC = 1.0 / ( tan( kTTPi*(mFrequency/sr) ) );
87  mCSquared = mC * mC;
88  calculateCoefficients();
89  return kTTErrNone;
90 }
91 
92 
93 void TTLowpassButterworth2::calculateCoefficients()
94 {
95  mA0 = 1.0 / (1.0 + kTTSqrt2*mC + mCSquared);
96  mA1 = 2.0*mA0;
97  //mA2 = mA0;
98  mB1 = mA1*( 1.0 - mCSquared ); //2*mA0*( 1 - mCSquared );
99  mB2 = mA0 * (1.0 - kTTSqrt2*mC + mCSquared);
100 }
101 
102 
104 {
105  //y = TTAntiDenormal(mA0*x + mA1*mX1[channel] + mA2*mX2[channel] - mB1*mY1[channel] - mB2*mY2[channel]);
106  // since mA0 = mA2, one can optimize to:
107  y = mA0*(x + mX2[channel]) + mA1*mX1[channel] - mB1*mY1[channel] - mB2*mY2[channel];
108  TTZeroDenormal(y);
109  mX2[channel] = mX1[channel];
110  mX1[channel] = x;
111  mY2[channel] = mY1[channel];
112  mY1[channel] = y;
113  return kTTErrNone;
114 }
115 
116 
118 {
119  TT_WRAP_CALCULATE_METHOD(calculateValue);
120 }
121 
TTErr processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Standard audio processing method as used by TTBlue objects.
TTErr calculateValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt channel)
Standard single value calculate method as used by DSP objects.
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTSampleVector mX2
Input sample n-2.
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTSampleVector mY1
Output sample n-1.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
TTErr setFrequency(const TTValue &value)
Setter for the frequency attribute.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTSampleVector mY2
Output sample n-2.
#define setCalculateMethod(methodName)
A convenience macro to be used by subclasses for setting the calculate method.
TTLowpassButterworth2 is a second-order Butterworth lowpass filter.
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
TTFOUNDATION_EXPORT const TTFloat64 kTTSqrt2
Pre-calculated square-root of 2 (1.4142).
Definition: TTBase.cpp:25
TTFloat64 mFrequency
filter cutoff frequency
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
TTFloat64 mB2
filter coefficients.
TTSampleVector mX1
Input sample n-1.
TTErr updateMaxNumChannels(const TTValue &oldMaxNumChannels, TTValue &)
Receives notifications when there are changes to the inherited mMaxNumChannels attribute.
#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
TTErr clear()
This algorithm uses an IIR filter, meaning that it relies on feedback.
No Error.
Definition: TTBase.h:343
TTFOUNDATION_EXPORT const TTFloat64 kTTPi
[doxygenAppendixC_constExample]
Definition: TTBase.cpp:23
[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
TTUInt32 sr
Current sample rate being used by this object.