Jamoma API  0.6.0.a19
TTOverdrive.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspEffectsLib
4  *
5  * @brief #TTOverdrive is a soft saturation distortion effect.
6  *
7  * @authors Tim Place, Trond Lossius
8  *
9  * @copyright Copyright © 2008, Tim Place @n
10  * This code is licensed under the terms of the "New BSD License" @n
11  * http://creativecommons.org/licenses/BSD/
12  */
13 
14 
15 #include "TTOverdrive.h"
16 #include "TTEnvironment.h"
17 
18 #define thisTTClass TTOverdrive
19 #define thisTTClassName "overdrive"
20 #define thisTTClassTags "dspEffectsLib, audio, processor, distortion"
21 
22 
23 TT_AUDIO_CONSTRUCTOR,
24  dcBlockerUnit(kTTSym_dcblock)
25 {
26  TTChannelCount initialMaxNumChannels = arguments;
27 
28  // Register Attributes
30  addAttributeProperty(Drive, range, TTValue(1.0, 10.0));
31  addAttributeProperty(Drive, rangeChecking, TT("clip")); // options are "clip" "cliphigh" "cliplow"
33  addAttributeWithSetter(Mode, kTypeBoolean);// IMPORTANT: if we have more modes, the datatype need to change here
35 
36  // Register Messages
37  addMessage(clear);
38  addUpdates(MaxNumChannels);
39 
40  // Set Defaults
41  setAttributeValue(kTTSym_maxNumChannels, initialMaxNumChannels);
42  setAttributeValue("mode", 1);
43  setAttributeValue("preamp", 0.0);
44  setAttributeValue("drive", 3.0);
45  setAttributeValue("dcBlocker", YES);
46 }
47 
48 
49 TTOverdrive::~TTOverdrive()
50 {
51  ;
52 }
53 
54 
56 {
57  return dcBlockerUnit.set(kTTSym_maxNumChannels, mMaxNumChannels);
58 }
59 
60 
62 {
63  TTFloat64 f;
64  int i;
65 
66  mDrive = newValue;
67  // These calculations really only apply to mode 1...
68  f = (mDrive - 0.999) * 0.111; // range is roughly [0.001 to 0.999]
69 
70  z = kTTPi * f;
71  s = 1.0 / sin(z);
72  b = 1.0 / f;
73  TTLimit(b, 0.0, 1.0);
74  nb = b * -1.0;
75  i = int(f);
76  if ((f-(TTFloat64)i) > 0.5)
77  scale = sin(z); // sin(f * kTTPi);
78  else
79  scale = 1.0;
80 
81  return kTTErrNone;
82 }
83 
84 
86 {
87  mDcBlocker = newValue;
88  TTBoolean bypass = !mDcBlocker;
89 
90  return dcBlockerUnit.set("bypass", bypass);
91 }
92 
93 
95 {
96  mMode = newValue;
97  if (mMode == 0)
99  else
101  return kTTErrNone;
102 }
103 
104 
106 {
108  return kTTErrNone;
109 }
110 
112 {
113  mPreamp = TTDecibelsToLinearGain(newValue);
114  return kTTErrNone;
115 }
116 
117 
119 {
120  return dcBlockerUnit.send("clear");
121 }
122 
123 
125 {
126  TTAudioSignal& in = inputs->getSignal(0);
127  TTAudioSignal& out = outputs->getSignal(0);
128  TTUInt16 vs;
129  TTSampleValue *inSample,
130  *outSample;
131  TTChannelCount numchannels = TTAudioSignal::getMinChannelCount(in, out);
132  TTChannelCount channel;
133  TTSampleValue temp,
134  sign;
135 
136  dcBlockerUnit.process(in, out);
137 
138  for (channel=0; channel<numchannels; channel++) {
139  inSample = in.mSampleVectors[channel];
140  outSample = out.mSampleVectors[channel];
141  vs = in.getVectorSizeAsInt();
142 
143  while (vs--) {
144  temp = *inSample++ * mPreamp;
145 
146  // the equation only works in the positive quadrant...
147  // so we strip off the sign, apply the equation, and reapply the sign
148  if (temp < 0.0) {
149  temp = -temp;
150  sign = -1.0;
151  }
152  else
153  sign = 1.0;
154 
155  if (temp > 1.0) // clip signal if it's out of range
156  *outSample++ = TTClip(temp * sign, TTSampleValue(-1.0), TTSampleValue(1.0));
157  else
158  *outSample++ = sign * (1.0 - exp(mDrive * log(1.0 - temp)));
159  }
160  }
161  return kTTErrNone;
162 }
163 
164 
166 {
167  TTAudioSignal& in = inputs->getSignal(0);
168  TTAudioSignal& out = outputs->getSignal(0);
169  short vs;
170  TTSampleValue *inSample,
171  *outSample;
172  TTChannelCount numchannels = TTAudioSignal::getMinChannelCount(in, out);
173  TTChannelCount channel;
174  TTSampleValue temp;
175 #ifdef TT_PLATFORM_WIN
176  TTSampleValue sign;
177 #endif
178 
179  dcBlockerUnit.process(in, out);
180 
181  for (channel=0; channel<numchannels; channel++) {
182  inSample = in.mSampleVectors[channel];
183  outSample = out.mSampleVectors[channel];
184  vs = in.getVectorSizeAsInt();
185 
186  while (vs--) {
187  temp = *inSample++ * mPreamp;
188  if (temp > b)
189  temp = 1.0;
190  else if (temp < nb)
191  temp = -1.0;
192  #ifdef TT_PLATFORM_WIN
193  else {
194  // http://redmine.jamoma.org/issues/show/54
195  // It is insane, but on Windows sin() returns bad values
196  // if the argument is negative, so we have to do this crazy workaround.
197  if (temp < 0.0) {
198  temp = -temp;
199  sign = -1.0;
200  }
201  else
202  sign = 1.0;
203  temp = sign * sin(z * temp) * s;
204  // instead of using this more simple solution:
205  //temp = (z * temp) * s;
206  }
207  #else
208  else
209  temp = sin(z * temp) * s;
210  #endif
211  *outSample++ = temp * scale;
212  }
213  }
214  return kTTErrNone;
215 }
216 
TTFloat64 TTDecibelsToLinearGain(TTFloat64 value)
Convert decibels into linear ampliude.
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
TTErr setDrive(const TTValue &value)
Setter for the mode attribute.
Definition: TTOverdrive.cpp:61
TTFloat64 TTLinearGainToDecibels(const TTFloat64 value)
Convert linear amplitude into deciBels.
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.
TTFloat64 mPreamp
Attribute: linear gain for preamp (attr setter sets it in dB)
Definition: TTOverdrive.h:45
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTFloat64 mDrive
Attribute: The amount of saturation to apply.
Definition: TTOverdrive.h:42
TTOverdrive is a soft saturation distortion effect.
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 updateMaxNumChannels(const TTValue &oldMaxNumChannels, TTValue &)
This method gets called when the inherited maxNumChannels attribute is changed.
Definition: TTOverdrive.cpp:55
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 set(const TTSymbol aName, T aValue)
Set an attribute value for an object.
Boolean (1/0) or (true/false) flag.
Definition: TTBase.h:281
TTErr setPreamp(const TTValue &value)
Setter for the mode attribute.
static TTChannelCount getMinChannelCount(const TTAudioSignal &signal1, const TTAudioSignal &signal2)
Use this class method to determine the least number of channels the two signals have in common...
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTAudioObject dcBlockerUnit
A TTDCBlock object.
Definition: TTOverdrive.h:51
TTUInt16 TTChannelCount
Data type used when counting the number of channels in multi-channel audio signals and processes...
Definition: TTAudioSignal.h:31
TTErr getPreamp(TTValue &value)
Getter for the mode attribute.
TTErr clear()
Reset the DC Blocker.
A simple container for an array of TTAudioSignal pointers.
TTEnvironment is a global object providing information on the environemt.
TTBoolean mMode
Attribute: What distortion function to use.
Definition: TTOverdrive.h:44
TTErr processMode0(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Process method when mode == 0.
TTBoolean mDcBlocker
Attribute: Pass processed signal through a DC blocking filter?
Definition: TTOverdrive.h:43
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
TTErr processMode1(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
Process method when mode == 1.
TTFloat64 TTSampleValue
A value representing a single audio sample.
Definition: TTBase.h:230
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
TTErr setMode(const TTValue &value)
Setter for the mode attribute.
Definition: TTOverdrive.cpp:94
TTErr setDcBlocker(const TTValue &value)
Setter for the mode attribute.
Definition: TTOverdrive.cpp:85
#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