3 IPlug distortion example
5 <http://www.taletn.com/>
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software in a
17 product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
24 Simple IPlug audio effect that shows how to implement oversampling to reduce
30 #include "IPlugDistortion.h"
31 #include "IPlug_include_in_plug_src.h"
35 #include "../../WDL/denormal.h"
38 inline double fast_tanh(double x
)
41 return (x
- 1) / (x
+ 1);
45 IPlugDistortion::IPlugDistortion(IPlugInstanceInfo instanceInfo
):
46 IPLUG_CTOR(kNumParams
, 0, instanceInfo
),
47 mOversampling(8), mDC(0.2)
51 mAntiAlias
.Calc(0.5 / (double)mOversampling
);
55 mDistortedDC
= fast_tanh(mDC
);
57 GetParam(kDrive
)->InitDouble("Drive", 0.5, 0., 1., 0.001);
59 IGraphics
* pGraphics
= MakeGraphics(this, GUI_WIDTH
, GUI_HEIGHT
);
60 IText
textProps(12, &COLOR_BLACK
, "Verdana", IText::kStyleNormal
, IText::kAlignNear
, 0, IText::kQualityNonAntiAliased
);
61 GenerateKnobGUI(pGraphics
, this, &textProps
, &COLOR_WHITE
, &COLOR_BLACK
, 60, 70);
62 AttachGraphics(pGraphics
);
66 void IPlugDistortion::OnParamChange(int paramIdx
)
68 IMutexLock
lock(this);
74 double value
= GetParam(kDrive
)->Value();
75 mDrive
= 1. + 15. * value
;
76 mGain
= pow(2., value
) / mDrive
;
83 void IPlugDistortion::ProcessDoubleReplacing(double** inputs
, double** outputs
, int nFrames
)
85 bool isMono
= !IsInChannelConnected(1);
87 for (int i
= 0; i
< nFrames
; ++i
)
89 double sample
= isMono
? inputs
[0][i
] : 0.5 * (inputs
[0][i
] + inputs
[1][i
]);
92 for (int j
= 0; j
< mOversampling
; ++j
)
95 if (j
> 0) sample
= 0.;
96 mUpsample
.Process(sample
, mAntiAlias
.Coeffs());
97 sample
= (double)mOversampling
* mUpsample
.Output();
100 if (WDL_DENORMAL_OR_ZERO_DOUBLE_AGGRESSIVE(&sample
))
103 sample
= mGain
* (fast_tanh(mDC
+ mDrive
* sample
) - mDistortedDC
);
106 mDownsample
.Process(sample
, mAntiAlias
.Coeffs());
107 if (j
== 0) output
= mDownsample
.Output();
110 outputs
[0][i
] = outputs
[1][i
] = output
;