Skip to content

Commit 10f3dd3

Browse files
committed
Added cliping feature
1 parent 7584a77 commit 10f3dd3

File tree

6 files changed

+59
-24
lines changed

6 files changed

+59
-24
lines changed

AudioModules.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
#include "AudioModules.h"
22

33

4-
Shaper::Shaper(iplug::IParam* pre, iplug::IParam* post, iplug::IParam* mix, iplug::IParam* wavetype) :
5-
pregainParam(pre), postgainParam(post), mixParam(mix), waveParam(wavetype) {}
4+
Shaper::Shaper(iplug::IParam* pre, iplug::IParam* post, iplug::IParam* mix, iplug::IParam* wavetype, iplug::IParam* clip)
5+
:
6+
pregainParam(pre), postgainParam(post), mixParam(mix), waveParam(wavetype), clipParam(clip) {}
7+
68
double Shaper::process(double x){
7-
return algo(x * pregain) * postgain*mix + x * (1. - mix);
9+
double procesed = algo(x * pregain) * postgain;
10+
if (clip < 5 && std::abs(procesed) > clip)
11+
{
12+
procesed = procesed/abs(procesed) * clip;
13+
}
14+
15+
return procesed * mix + x * (1. - mix);
816
}
917
void Shaper::updateParams() {
1018
pregain = pregainParam->Value() / 100.;
1119
postgain = postgainParam->Value() / 100.;
1220
mix = mixParam->Value() / 100.;
1321
wave = static_cast<Waveform>(waveParam->Int());
22+
clip = clipParam->Value() / 100.;
1423
algo = myAlgos.getAlgo(wave);
1524
}
1625

AudioModules.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct Algos
6666
class Shaper
6767
{
6868
public:
69-
Shaper(iplug::IParam* pre, iplug::IParam* post, iplug::IParam* mix, iplug::IParam* wavetype);
69+
Shaper(iplug::IParam* pre, iplug::IParam* post, iplug::IParam* mix, iplug::IParam* wavetype, iplug::IParam* clip);
7070
double process(double x);
7171
void updateParams();
7272

@@ -77,10 +77,12 @@ class Shaper
7777
iplug::IParam* postgainParam = nullptr;
7878
iplug::IParam* mixParam = nullptr;
7979
iplug::IParam* waveParam = nullptr;
80+
iplug::IParam* clipParam = nullptr;
8081
Algos myAlgos;
8182
double pregain = 1;
8283
double postgain = 1;
8384
double mix = 0;
85+
double clip = 3.0;
8486
Waveform wave = ARCTAN;
8587
};
8688

UiControlModules.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void dynamicPlot::OnMsgFromDelegate(int msgTag, int dataSize, const void* pData)
3131
if (newGain != gainOut)
3232
{
3333
gainOut = newGain;
34-
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix);
34+
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix, clip);
3535
SetDirty();
3636
}
3737
}
@@ -41,7 +41,7 @@ void dynamicPlot::OnMsgFromDelegate(int msgTag, int dataSize, const void* pData)
4141
if (newGain != gainIn)
4242
{
4343
gainIn = newGain;
44-
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix);
44+
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix, clip);
4545
SetDirty();
4646
}
4747
}
@@ -52,7 +52,7 @@ void dynamicPlot::OnMsgFromDelegate(int msgTag, int dataSize, const void* pData)
5252
if (newWaveType != mWaveType)
5353
{
5454
mWaveType = newWaveType;
55-
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix);
55+
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix, clip);
5656
SetDirty();
5757
}
5858
}
@@ -62,7 +62,17 @@ void dynamicPlot::OnMsgFromDelegate(int msgTag, int dataSize, const void* pData)
6262
if (newMix != mix)
6363
{
6464
mix = newMix;
65-
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix);
65+
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix, clip);
66+
SetDirty();
67+
}
68+
}
69+
else if (msgTag == kPlotClip)
70+
{
71+
const double newClip = *static_cast<const double*>(pData) / 100.;
72+
if (newClip != clip)
73+
{
74+
clip = newClip;
75+
this->mPlots[0].func = getPlotFunc(mWaveType, gainIn, gainOut, mix, clip);
6676
SetDirty();
6777
}
6878
}
@@ -71,9 +81,16 @@ void dynamicPlot::OnMsgFromDelegate(int msgTag, int dataSize, const void* pData)
7181
assert(false);
7282
}
7383
}
74-
std::function<double(double)> dynamicPlot::getPlotFunc(Waveform waveType, double gainIn, double gainOut, double mix)
75-
{
84+
std::function<double(double)> dynamicPlot::getPlotFunc(Waveform waveType, double gainIn, double gainOut, double mix, double clip)
85+
{
7686
const auto algo = mAlgo.getAlgo(waveType);
77-
return [algo, gainIn, gainOut, mix](double x) -> double { return algo((x * 2. - 1.) * gainIn) * gainOut * mix + (x * 2. - 1) * (1. - mix); };
87+
return [algo, gainIn, gainOut, mix, clip](double x) -> double {
88+
double procesed = algo((x * 2. - 1.) * gainIn) * gainOut;
89+
if (clip < 5 && std::abs(procesed) > clip)
90+
{
91+
procesed = procesed / std::abs(procesed) * clip;
92+
}
93+
return procesed * mix + (x * 2. - 1) * (1. - mix);
94+
};
7895
}
7996

UiControlModules.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ enum MPlotValType
1010
kPlotIn = 0,
1111
kPlotOut,
1212
kPlotWaveType,
13-
kPlotMix
13+
kPlotMix,
14+
kPlotClip,
1415
};
1516

1617

@@ -32,12 +33,13 @@ class dynamicPlot : public IVPlotControl
3233
public:
3334
dynamicPlot(const IRECT& bounds, std::function<double(double)> func, const char* label = "");
3435
void OnMsgFromDelegate(int msgTag, int dataSize, const void* pData) override;
35-
std::function<double(double)> getPlotFunc(Waveform waveType, double gainIn, double gainOut, double mix);
36+
std::function<double(double)> getPlotFunc(Waveform waveType, double gainIn, double gainOut, double mix, double clip);
3637

3738
private:
3839
double gainIn = 1.;
3940
double gainOut = 1.;
4041
double mix = 0.5;
42+
double clip = 3.0;
4143
Waveform mWaveType = TANH;
4244
Algos mAlgo;
4345
};

waveProcessor.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#include "IPlug_include_in_plug_src.h"
33
#include "IControls.h"
44

5-
#define GETPARAMS(i) GetParam(kPreGain##i)->InitDouble("Pre Gain", 100., 0., 1000.0, 0.01, "%"); \
5+
#define GETPARAMS(i) \
6+
GetParam(kPreGain##i)->InitDouble("Pre Gain", 100., 0., 1000.0, 0.01, "00 %"); \
67
GetParam(kPostGain##i)->InitDouble("Post Gain", 100., 0., 400.0, 0.01, "%");\
7-
GetParam(kWaveType##i)->InitEnum("Wave Type", TANH, 12); \
8+
GetParam(kClip##i)->InitDouble("Clip", 300., 0., 500.0, 0.01, "%", 0, "", IParam::ShapePowCurve(3.)); \
9+
GetParam(kWaveType##i)->InitEnum("Wave Type", TANH, 12); \
810
GetParam(kWaveType##i)->SetDisplayText(0, "Arctan"); \
911
GetParam(kWaveType##i)->SetDisplayText(1, "TANH"); \
1012
GetParam(kWaveType##i)->SetDisplayText(2, "TANH^3"); \
@@ -20,12 +22,13 @@ GetParam(kWaveType##i)->SetDisplayText(11, "SIN(EXP)"); \
2022
GetParam(kMix##i)->InitDouble("Mix", 50., 0., 100.0, 0.01, "%");
2123

2224
#define CONNECTGRAPHICS(x, y) \
23-
pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100) .GetHShifted(-210).GetVShifted(y), kPreGain##x, "", myStyle));\
24-
pGraphics->AttachControl(new IVMenuButtonControl(b.GetCentredInside(100).GetHShifted(-110).GetVShifted(y), kWaveType##x, "Wave Type", myStyle)); \
25-
pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100) .GetHShifted(-10).GetVShifted(y), kPostGain##x, "", myStyle));\
26-
pGraphics->AttachControl(new dynamicPlot(b.GetCentredInside(100) .GetHShifted(90) .GetVShifted(y), [](double i) -> double { return tanh(2.*i-1.); }), kCtrlTagPlot##x); \
27-
pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100) .GetHShifted(190).GetVShifted(y), kMix##x, "", myStyle));\
28-
pGraphics->AttachControl(new VuMeterControl(b.GetCentredInside(100) .GetHShifted(245) .GetVShifted(y).GetVPadded(-5).GetHPadded(-45), COLOR_BLACK, kCtrlTagVUMeter##x), kCtrlTagVUMeter##x); \
25+
pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100) .GetHShifted(-220).GetVShifted(y), kPreGain##x, "", myStyle));\
26+
pGraphics->AttachControl(new IVMenuButtonControl(b.GetCentredInside(100).GetHShifted(-120).GetVShifted(y), kWaveType##x, "Wave Type", myStyle)); \
27+
pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100) .GetHShifted(-20).GetVShifted(y), kPostGain##x, "", myStyle)); \
28+
pGraphics->AttachControl(new IVSliderControl(b.GetCentredInside(100).GetHPadded(-30).GetHShifted(40).GetVShifted(y), kClip##x, "Clip", myStyle));\
29+
pGraphics->AttachControl(new dynamicPlot(b.GetCentredInside(100).GetHShifted(110).GetVShifted(y).GetPadded(-5), [](double i) -> double { return tanh(2. * i - 1.); }), kCtrlTagPlot##x); \
30+
pGraphics->AttachControl(new IVKnobControl(b.GetCentredInside(100) .GetHShifted(210).GetVShifted(y), kMix##x, "", myStyle));\
31+
pGraphics->AttachControl(new VuMeterControl(b.GetCentredInside(100) .GetHShifted(260) .GetVShifted(y).GetVPadded(-5).GetHPadded(-45), COLOR_BLACK, kCtrlTagVUMeter##x), kCtrlTagVUMeter##x); \
2932

3033
waveProcessor::waveProcessor(const InstanceInfo& info)
3134
: iplug::Plugin(info, MakeConfig(kNumParams, kNumPresets))
@@ -65,7 +68,9 @@ paramDVal = GetParam(kPreGain##i)->Value();\
6568
paramWVal = (Waveform)GetParam(kWaveType##i)->Value(); \
6669
SendControlMsgFromDelegate(kCtrlTagPlot##i, kPlotWaveType, sizeof(Waveform), &paramWVal); \
6770
paramDVal = GetParam(kMix##i)->Value();\
68-
SendControlMsgFromDelegate(kCtrlTagPlot##i, kPlotMix, sizeof(double), &paramDVal);
71+
SendControlMsgFromDelegate(kCtrlTagPlot##i, kPlotMix, sizeof(double), &paramDVal);\
72+
paramDVal = GetParam(kClip##i)->Value();\
73+
SendControlMsgFromDelegate(kCtrlTagPlot##i, kPlotClip, sizeof(double), &paramDVal);
6974

7075

7176
void waveProcessor::OnIdle()

waveProcessor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "IControls.h"
44
#include "AudioModules.h"
55
#include "UiControlModules.h"
6-
#define SHAPE(x) kPreGain##x, kPostGain##x, kWaveType##x, kMix##x
6+
#define SHAPE(x) kPreGain##x, kPostGain##x, kWaveType##x, kMix##x, kClip##x
77
enum EParams
88
{
99
SHAPE(0),
@@ -40,7 +40,7 @@ class waveProcessor final : public Plugin
4040

4141

4242
private:
43-
#define SHAPER(x) Shaper(GetParam(kPreGain##x), GetParam(kPostGain##x), GetParam(kMix##x), GetParam(kWaveType##x))
43+
#define SHAPER(x) Shaper(GetParam(kPreGain##x), GetParam(kPostGain##x), GetParam(kMix##x), GetParam(kWaveType##x), GetParam(kClip##x))
4444
Shaper mShaper[3] = {SHAPER(0), SHAPER(1), SHAPER(2)};
4545
VuMeter* mVuMeters[3] = {new VuMeter(), new VuMeter(), new VuMeter()};
4646
};

0 commit comments

Comments
 (0)