Jamoma API  0.6.0.a19
TTBandrejectButterworth2.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFilterLib
4  *
5  * @brief #TTBandrejectButterworth2 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 
20 
21 #define thisTTClass TTBandrejectButterworth2
22 #define thisTTClassName "bandreject.butterworth.2"
23 #define thisTTClassTags "dspFilterLib, audio, processor, filter, notch, 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 TTBandrejectButterworth2::~TTBandrejectButterworth2()
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 TTBandrejectButterworth2::updateSampleRate(const TTValue& oldSampleRate, TTValue&)
70 {
72  return setFrequency(v);
73 }
74 
75 
76 TTErr TTBandrejectButterworth2::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 TTBandrejectButterworth2::setQ(const TTValue& newValue)
95 {
96  mQ = newValue;
97  calculateCoefficients();
98  return kTTErrNone;
99 }
100 
101 
102 void TTBandrejectButterworth2::calculateCoefficients()
103 {
104  // Avoid dividing by zero
105  if (mQ > 0.1)
106  mBw = mFrequency/mQ;
107  else
108  mBw = mFrequency/0.1;
109  mC = tan( kTTPi*(mBw/sr) );
110  mD = cos( kTTTwoPi*(mFrequency/sr) );
111  mA0 = 1.0 / (1.0 + mC);
112  mA1 = -2.0 * mA0 * mD;
113  //mA2 = mA0;
114  //mB1 = mA1;
115  mB2 = mA0 * (1.0 - mC);
116 }
117 
119 {
120  //y = TTAntiDenormal(mA0*x + mA1*mX1[channel] + mA2*mX2[channel] - mB1*mY1[channel] - mB2*mY2[channel]);
121  // since mA0 = mA2 nd mB1 = mA1, one can optimize to:
122  y = mA0*(x + mX2[channel]) + mA1*(mX1[channel] - mY1[channel]) - mB2*mY2[channel];
123  TTZeroDenormal(y);
124  mX2[channel] = mX1[channel];
125  mX1[channel] = x;
126  mY2[channel] = mY1[channel];
127  mY1[channel] = y;
128  return kTTErrNone;
129 }
130 
131 
133 {
134  TT_WRAP_CALCULATE_METHOD(calculateValue);
135 }
TTBandrejectButterworth2 is a second-order Butterworth bandpass filter.
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTErr updateMaxNumChannels(const TTValue &oldMaxNumChannels, TTValue &)
Receives notifications when there are changes to the inherited mMaxNumChannels attribute.
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.
TTSampleVector mY2
Output sample n-2.
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
TTErr calculateValue(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt channel)
Standard single value calculate method as used by DSP objects.
#define setCalculateMethod(methodName)
A convenience macro to be used by subclasses for setting the calculate method.
TTErr processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Standard audio processing method as used by TTBlue objects.
TTFloat64 mFrequency
filter cutoff frequency
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
TTFloat64 mB2
filter coefficients.
TTFloat64 mQ
filter resonance
TTSampleVector mX1
Input sample n-1.
A simple container for an array of TTAudioSignal pointers.
TTSampleVector mX2
Input sample n-2.
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
TTErr setFrequency(const TTValue &value)
Setter for the frequency attribute.
TTFOUNDATION_EXPORT const TTFloat64 kTTPi
[doxygenAppendixC_constExample]
Definition: TTBase.cpp:23
TTSampleVector mY1
Output 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
TTUInt32 sr
Current sample rate being used by this object.