Make duplicate script compatible with Python 3
[wdl/wdl-ol.git] / IPlugExamples / IPlugDistortion / IPlugDistortion.cpp
blob7a319e46b971dcd5624170be0348e485fc59b9c4
1 /*
3 IPlug distortion example
4 (c) Theo Niessink 2011
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
25 aliasing.
30 #include "IPlugDistortion.h"
31 #include "IPlug_include_in_plug_src.h"
32 #include "IAutoGUI.h"
34 #include <math.h>
35 #include "../../WDL/denormal.h"
38 inline double fast_tanh(double x)
40 x = exp(x + x);
41 return (x - 1) / (x + 1);
45 IPlugDistortion::IPlugDistortion(IPlugInstanceInfo instanceInfo):
46 IPLUG_CTOR(kNumParams, 0, instanceInfo),
47 mOversampling(8), mDC(0.2)
49 TRACE;
51 mAntiAlias.Calc(0.5 / (double)mOversampling);
52 mUpsample.Reset();
53 mDownsample.Reset();
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);
70 switch (paramIdx)
72 case kDrive:
74 double value = GetParam(kDrive)->Value();
75 mDrive = 1. + 15. * value;
76 mGain = pow(2., value) / mDrive;
77 break;
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]);
90 double output;
92 for (int j = 0; j < mOversampling; ++j)
94 // Upsample
95 if (j > 0) sample = 0.;
96 mUpsample.Process(sample, mAntiAlias.Coeffs());
97 sample = (double)mOversampling * mUpsample.Output();
99 // Distortion
100 if (WDL_DENORMAL_OR_ZERO_DOUBLE_AGGRESSIVE(&sample))
101 sample = 0.;
102 else
103 sample = mGain * (fast_tanh(mDC + mDrive * sample) - mDistortedDC);
105 // Downsample
106 mDownsample.Process(sample, mAntiAlias.Coeffs());
107 if (j == 0) output = mDownsample.Output();
110 outputs[0][i] = outputs[1][i] = output;