Jamoma API  0.6.0.a19
TTOnePole.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFilterLib
4  *
5  * @brief #TTOnePole is a 1-pole filter.
6  *
7  * @details
8  *
9  * @authors Timothy Place, Trond Lossius
10  *
11  * @copyright Copyright © 2010, Timothy Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "TTOnePole.h"
18 
19 #define thisTTClass TTOnePole
20 #define thisTTClassName "onepole"
21 #define thisTTClassTags "dspFilterLib, audio, processor, filter, lowpass, highpass"
22 
23 
24 TT_AUDIO_CONSTRUCTOR
25 {
26  // register attributes
28 
30  addAttributeProperty(Frequency, range, TTValue(2.0, sr*0.475));
31  addAttributeProperty(Frequency, rangeChecking, TT("clip"));
32 
34 
35  // message called as a notification from the parent class so we can allocate memory as required
36  addUpdates(MaxNumChannels);
37 
38  // message called as a notification from the parent class so we can recalculate coefficients as required
39  addUpdates(SampleRate);
40 
41  // make the clear method available to the outside world
42  addMessage(clear);
43 
44  // Set Defaults...
45  setAttributeValue(TT("mode"), TT("lowpass"));
46  setAttributeValue(TT("frequency"), 1000.0);
47  setAttributeValue(kTTSym_maxNumChannels, arguments); // This attribute is inherited
48 }
49 
50 
51 TTOnePole::~TTOnePole()
52 {
53  ; // Nothing to clean up for this class
54 }
55 
56 
57 // Messages
58 
60 {
61  mFeedback.resize(mMaxNumChannels);
62  clear();
63  return kTTErrNone;
64 }
65 
66 
68 {
70  return setFrequency(v);
71 }
72 
73 
75 {
76  mFeedback.assign(mMaxNumChannels, 0.0);
77  return kTTErrNone;
78 }
79 
80 
81 // A change of the mode attribute switches the process method that is called on-the-fly
82 
84 {
85  TTSymbol mode = newValue;
86 
87  if (mode == TT("lowpass")) {
88  setProcessMethod(processLowpass);
89  setCalculateMethod(calculateLowpass);
90  }
91  else if (mode == TT("highpass")) {
92  setProcessMethod(processHighpass);
93  setCalculateMethod(calculateHighpass);
94  }
95  else
96  return kTTErrInvalidValue;
97 
98  mMode = mode;
99  return kTTErrNone;
100 }
101 
102 
103 // Setting the frequency calculates the coefficients for both lowpass and highpass modes
104 
106 {
107  TTFloat64 radians;
108 
109  mFrequency = newValue;
110  radians = hertzToRadians(mFrequency);
111 
112  mLowpassCoefficient = sin(radians);
114 
117 
118  return kTTErrNone;
119 }
120 
121 
122 TTErr TTOnePole::setCoefficient(const TTValue& newValue)
123 {
124  TTFloat64 radians;
125 
126  mLowpassCoefficient = newValue;
128 
131 
132  // FIXME: this isn't really accurate -- the frequency uses a trig function
133 
134  radians = mLowpassCoefficient * kTTPi;
135  mFrequency = radiansToHertz(radians);
136  return kTTErrNone;
137 }
138 
139 
140 TTErr TTOnePole::getCoefficient(TTValue& returnedValue)
141 {
142  returnedValue = mLowpassCoefficient;
143  return kTTErrNone;
144 }
145 
146 
147 // calculate: y = f(x)
148 
149 inline TTErr TTOnePole::calculateLowpass(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt channel)
150 {
151  y = mFeedback[channel] = TTAntiDenormal((x * mLowpassCoefficient) + (mFeedback[channel] * mOneMinusLowpassCoefficient));
152  return kTTErrNone;
153 }
154 
155 
156 inline TTErr TTOnePole::calculateHighpass(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt channel)
157 {
158  y = mFeedback[channel] = TTAntiDenormal((x * mHighpassCoefficient) + (mFeedback[channel] * mOneMinusHighpassCoefficient));
159  return kTTErrNone;
160 }
161 
162 
163 // process: frame-processing for blocks of N samples by M channels
164 
165 TTErr TTOnePole::processLowpass(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
166 {
167  TT_WRAP_CALCULATE_METHOD(calculateLowpass);
168 }
169 
170 
171 TTErr TTOnePole::processHighpass(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
172 {
173  TT_WRAP_CALCULATE_METHOD(calculateHighpass);
174 }
An inappropriate value was specified for an attribute or variable.
Definition: TTBase.h:349
TTChannelCount mMaxNumChannels
This is the maximum number of channels that can be guaranteed to work.
TTFloat64 mHighpassCoefficient
filter coefficients
Definition: TTOnePole.h:34
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTOnePole is a 1-pole filter.
TTFloat64 mFrequency
filter cutoff frequency
Definition: TTOnePole.h:30
TTFloat64 radiansToHertz(const TTFloat64 radians)
Convert radians into Hertz.
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
Symbol type.
Definition: TTBase.h:282
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.
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
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTErr updateSampleRate(const TTValue &oldSampleRate, TTValue &)
Receives notifications when there are changes to the inherited sr attribute.
Definition: TTOnePole.cpp:67
TTFloat64 hertzToRadians(const TTFloat64 hz)
Convert Hertz to radians.
TTErr updateMaxNumChannels(const TTValue &oldMaxNumChannels, TTValue &)
Receives notifications when there are changes to the inherited mMaxNumChannels attribute.
Definition: TTOnePole.cpp:59
TTErr setFrequency(const TTValue &value)
Setter for the frequency attribute.
Definition: TTOnePole.cpp:105
TTFloat64 mLowpassCoefficient
filter coefficients
Definition: TTOnePole.h:32
A simple container for an array of TTAudioSignal pointers.
TTSampleVector mFeedback
previous output sample for each channel
Definition: TTOnePole.h:36
TTErr clear()
This algorithm uses an IIR filter, meaning that it relies on feedback.
Definition: TTOnePole.cpp:74
long TTPtrSizedInt
An integer that is the same size as a pointer.
Definition: TTBase.h:240
TTFloat64 mOneMinusHighpassCoefficient
filter coefficients
Definition: TTOnePole.h:35
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTSymbol mMode
filter mode: 'lowpass' or 'highpass'
Definition: TTOnePole.h:29
TTErr setMode(const TTValue &value)
Setter for the mode attribute.
Definition: TTOnePole.cpp:83
TTFloat64 mOneMinusLowpassCoefficient
filter coefficients
Definition: TTOnePole.h:33
#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
[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
#define addAttributeWithGetterAndSetter(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter and sett...
Definition: TTAttribute.h:57