Jamoma API  0.6.0.a19
TTMirror5.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFilterLib
4  *
5  * @brief #TTMirror5 is a 5th-order lowpass/highpass filter built up from a 2-path allpass structure
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 "TTMirror5.h"
18 
19 #define thisTTClass TTMirror5
20 #define thisTTClassName "mirror.5"
21 #define thisTTClassTags "dspFilterLib, audio, processor, filter, lowpass, highpass"
22 
23 #ifdef TT_PLATFORM_WIN
24 #include <Algorithm>
25 #endif
26 
27 TT_AUDIO_CONSTRUCTOR,
28  mF0("allpass.2a"),
29  mF1("allpass.1a"),
30  mF2("allpass.2a")
31 {
32  TTChannelCount initialMaxNumChannels = arguments;
33 
36  addMessage(clear);
37  addUpdates(MaxNumChannels);
38 
39  setAttributeValue(kTTSym_maxNumChannels, initialMaxNumChannels);
40  setAttributeValue(TT("mode"), TT("lowpass"));
41  setAttributeValue(TT("frequency"), sr/4.0);
42 }
43 
44 
45 TTMirror5::~TTMirror5()
46 {
47 }
48 
49 
50 TTErr TTMirror5::updateMaxNumChannels(const TTValue& oldMaxNumChannels, TTValue&)
51 {
52  mF0.set(kTTSym_maxNumChannels, mMaxNumChannels);
53  mF1.set(kTTSym_maxNumChannels, mMaxNumChannels);
54  mF2.set(kTTSym_maxNumChannels, mMaxNumChannels);
55  clear();
56  return kTTErrNone;
57 }
58 
59 
60 TTErr TTMirror5::clear()
61 {
62  mF0.send(kTTSym_clear);
63  mF1.send(kTTSym_clear);
64  mF2.send(kTTSym_clear);
65  return kTTErrNone;
66 }
67 
68 
69 TTErr TTMirror5::setMode(const TTValue& newValue)
70 {
71  TTSymbol newMode = newValue;
72 
73  if (newMode == TT("highpass")) {
74  mMode = TT("highpass");
75  setCalculateMethod(calculateHighpass);
76  setProcessMethod(processHighpass);
77  }
78  else { // lowpass
79  mMode = TT("lowpass");
80  setCalculateMethod(calculateLowpass);
81  setProcessMethod(processLowpass);
82  }
83  return kTTErrNone;
84 }
85 
86 
87 TTErr TTMirror5::setFrequency(const TTValue& newValue)
88 {
89  mFrequency = newValue;
90 
91  const TTFloat64 alpha_0 = 0.1413486; // alpha coefficients come from the halfband filter, which we are transforming
92  const TTFloat64 alpha_1 = 0.5899948;
93  const TTFloat64 omega_b = TTClip<TTFloat64>(sr/2.0 - mFrequency, 0.0, sr/2.0);
94  const TTFloat64 omega_s = sr;
95  const TTFloat64 theta_b = (omega_b / omega_s) * kTTTwoPi;
96  const TTFloat64 b = (1 - tan(theta_b / 2.0)) / (1 + tan(theta_b / 2.0));
97  TTFloat64 c_1 = ((2.0 * b) + (2.0 * b * alpha_0)) / (1 + alpha_0 * (b * b));
98  TTFloat64 c_2 = ((b*b) + alpha_0) / (1 + alpha_0 * (b*b));
99 
100  mF0.set("c1", c_1);
101  mF0.set("c2", c_2);
102 
103  c_1 = ((2.0 * b) + (2.0 * b * alpha_1)) / (1 + alpha_1 * (b * b));
104  c_2 = ((b*b) + alpha_1) / (1 + alpha_1 * (b*b));
105  mF1.set("alpha", b);
106  mF2.set("c1", c_1);
107  mF2.set("c2", c_2);
108 
109  return kTTErrNone;
110 }
111 
112 
113 inline void TTMirror5::filterKernel(const TTFloat64& input, TTFloat64& outputPath0, TTFloat64& outputPath1, TTPtrSizedInt channel)
114 {
115  TTFloat64 temp;
116 
117  TTBASE(mF0, TTAllpass2a)->calculateValue(input, outputPath0, channel);
118 
119  TTBASE(mF1, TTAllpass1a)->calculateValue(input, temp, channel);
120  TTBASE(mF2, TTAllpass2a)->calculateValue(temp, outputPath1, channel);
121 }
122 
123 
124 TTErr TTMirror5::calculateLowpass(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt channel)
125 {
126  TTFloat64 outputFromPath0;
127  TTFloat64 outputFromPath1;
128 
129  filterKernel(x, outputFromPath0, outputFromPath1, channel);
130  y = (outputFromPath0 + outputFromPath1) * 0.5;
131  return kTTErrNone;
132 }
133 
134 
135 TTErr TTMirror5::calculateHighpass(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt channel)
136 {
137  TTFloat64 outputFromPath0;
138  TTFloat64 outputFromPath1;
139 
140  filterKernel(x, outputFromPath0, outputFromPath1, channel);
141  y = (outputFromPath0 - outputFromPath1) * 0.5;
142  return kTTErrNone;
143 }
144 
145 
146 TTErr TTMirror5::processLowpass(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
147 {
148  TT_WRAP_CALCULATE_METHOD(calculateLowpass);
149 }
150 
151 
152 TTErr TTMirror5::processHighpass(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
153 {
154  TT_WRAP_CALCULATE_METHOD(calculateHighpass);
155 }
156 
TTErr send(const TTSymbol aName)
Send a message to this object with no arguments.
Definition: TTObject.cpp:135
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 TTBASE(instance_, class_)
Macro to access the actual C++ class that is contained inside of the TTObject as a pointer...
Definition: TTObject.h:211
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
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
TTAudioObject mF2
filter2 (in the lower path, second block)
Definition: TTMirror5.h:35
#define setCalculateMethod(methodName)
A convenience macro to be used by subclasses for setting the calculate method.
64-bit floating point
Definition: TTBase.h:272
TTErr set(const TTSymbol aName, T aValue)
Set an attribute value for an object.
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
TTAudioObject mF0
filter0 (in the upper path)
Definition: TTMirror5.h:33
A first-order building-block allpass filter.
Definition: TTAllpass1a.h:25
A second-order building-block allpass filter.
Definition: TTAllpass2a.h:26
TTUInt16 TTChannelCount
Data type used when counting the number of channels in multi-channel audio signals and processes...
Definition: TTAudioSignal.h:31
TTAudioObject mF1
filter1 (in the lower path, first block)
Definition: TTMirror5.h:34
TTFloat64 mFrequency
attribute: in hertz
Definition: TTMirror5.h:36
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
TTMirror5 is a 5th-order lowpass/highpass filter built up from a 2-path allpass structure ...
TTSymbol mMode
attribute: lowpass or highpass
Definition: TTMirror5.h:37
[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.