Jamoma API  0.6.0.a19
TTWaveshaper.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspEffectsLib
4  *
5  * @brief #TTWaveshaper distorts the signal by applying a waveshaper distortion
6  *
7  * @authors Nils Peters, Trond Lossius
8  *
9  * @copyright Copyright © 2011, Nils Peters @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 "TTWaveshaper.h"
16 
17 #define thisTTClass TTWaveshaper
18 #define thisTTClassName "waveshaper"
19 #define thisTTClassTags "dspEffectsLib, audio, processor, distortion"
20 
21 
22 TT_AUDIO_CONSTRUCTOR,
23 mStrength(1),
24 mSigmoidGainCorrection(1),
25 mPoly1GainCorrection(1)
26 {
28  addAttributeProperty(Strength, range, TTValue(kTTEpsilon, 100000.0));
29  addAttributeProperty(Strength, rangeChecking, TT("clip"));
31  setProcessMethod(processAudioSin);
32  setAttributeValue(TT("strength"), kTTEpsilon);
33  setAttributeValue(TT("shape"), TT("sin"));
34 }
35 
36 
37 TTWaveshaper::~TTWaveshaper()
38 {
39  ;
40 }
41 
43 {
44  mStrength = newValue;
45  mInvStrength = 1.0/mStrength;
46  mSigmoidGainCorrection = 1.0 / (2.0/(1.0+exp(-5. * mStrength))-1.0);
47  mPoly1GainCorrection = 1.0 /(mStrength + mStrength*mStrength*mStrength);
48  return kTTErrNone;
49 }
50 
51 
53 {
54  mShape = newValue;
55  if (mShape == TT("atan"))
56  setProcessMethod(processAudioAtan);
57  else if (mShape == TT("sin"))
59  else if (mShape == TT("sigmoid"))
60  setProcessMethod(processAudioSigmoid);
61  else
62  setProcessMethod(processAudioPoly1);
63 
64  //TODO: we could use the entire function lib to drive the waveshaping based on a lookup table.
65  return kTTErrNone;
66 }
67 
69 {
70  TT_WRAP_CALCULATE_METHOD(calculateValueSin);
71  return kTTErrNone;
72 }
73 
75 {
76  y = sin(x * mStrength) * mInvStrength;
77  return kTTErrNone;
78 }
79 
80 
81 TTErr TTWaveshaper::processAudioSigmoid(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
82 {
83  TT_WRAP_CALCULATE_METHOD(calculateValueSigmoid);
84  return kTTErrNone;
85 }
86 
87 inline TTErr TTWaveshaper::calculateValueSigmoid(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt channel)
88 {
89  y = (2.0/(1.0+exp(-5. * mStrength * x))-1.0) * mSigmoidGainCorrection;
90  return kTTErrNone;
91 }
92 
93 
94 TTErr TTWaveshaper::processAudioAtan(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
95 {
96  TT_WRAP_CALCULATE_METHOD(calculateValueAtan);
97  return kTTErrNone;
98 }
99 
100 inline TTErr TTWaveshaper::calculateValueAtan(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt channel)
101 {
102  y = atan(x * mStrength) * mInvStrength;
103  return kTTErrNone;
104 }
105 
106 TTErr TTWaveshaper::processAudioPoly1(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
107 {
108  TT_WRAP_CALCULATE_METHOD(calculateValuePoly1);
109  return kTTErrNone;
110 }
111 
112 inline TTErr TTWaveshaper::calculateValuePoly1(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt channel)
113 {
114  //wawveshaping example by http://www.music.mcgill.ca/~gary/307/week12/node2.html
115  y = x * mStrength;
116  y = (y + y*y*y) * mPoly1GainCorrection;
117  return kTTErrNone;
118 }
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
TTErr setStrength(const TTValue &newValue)
Setter for the strength attribute.
TTErr setShape(const TTValue &newValue)
Setter for the shape attribute.
TTWaveshaper distorts the signal by applying a waveshaper distortion
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
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
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
No Error.
Definition: TTBase.h:343
TTFOUNDATION_EXPORT const TTFloat64 kTTEpsilon
A very very small value, used for float equality comaprisments.
Definition: TTBase.cpp:31
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
TTErr processAudioSin(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
A standard audio processing method as used by TTBlue objects.
TTErr calculateValueSin(const TTFloat64 &x, TTFloat64 &y, TTPtrSizedInt channel)
Standard single value calculate method as used by DSP objects.