Skip to content

Commit fd13f09

Browse files
committed
added lfo feature
1 parent 99d17c3 commit fd13f09

File tree

7 files changed

+298
-62
lines changed

7 files changed

+298
-62
lines changed

AudioModules.cpp

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,110 @@
11
#include "AudioModules.h"
22

33

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) {}
4+
Shaper::Shaper(iplug::IParam* pre, iplug::IParam* post, iplug::IParam* mix, iplug::IParam* wavetype, iplug::IParam* clip,
5+
iplug::IParam* pregainLfo, iplug::IParam* postgainLfo, iplug::IParam* mixLfo, iplug::IParam* clipLfo, Lfo* lfoPtr)
6+
: pregainParam(pre)
7+
, postgainParam(post)
8+
, mixParam(mix)
9+
, waveParam(wavetype)
10+
, clipParam(clip)
11+
, preGainLfoParam(pregainLfo)
12+
, postGainLfoParam(postgainLfo)
13+
, mixLfoParam(mixLfo)
14+
, clipLfoParam(clipLfo)
15+
, lfo(lfoPtr)
16+
{}
717

818
double Shaper::process(double x){
9-
double procesed = algo(x * pregain) * postgain;
10-
if (clip < 5 && std::abs(procesed) > clip)
19+
const double lfoVal = lfo->getVal();
20+
const double pregainL = pregain + lfoVal * pregainLfo;
21+
const double postgainL = postgain + lfoVal* postgainLfo;
22+
const double mixL = std::max(std::min(this->mix + lfoVal * mixLfo, 1.), 0.);
23+
const double clipL = std::max(this->clip + lfoVal * clipLfo, 0.);
24+
double procesed = algo(x * pregainL) * postgainL;
25+
if (clipL < 5 && std::abs(procesed) > clipL)
1126
{
12-
procesed = procesed/abs(procesed) * clip;
27+
procesed = procesed / abs(procesed) * clipL;
1328
}
14-
15-
return procesed * mix + x * (1. - mix);
29+
return procesed * mixL + x * (1. - mixL);
1630
}
1731
void Shaper::updateParams() {
1832
pregain = pregainParam->Value() / 100.;
33+
pregainLfo = preGainLfoParam->Value() / 100.;
1934
postgain = postgainParam->Value() / 100.;
35+
postgainLfo = postGainLfoParam->Value() / 100.;
2036
mix = mixParam->Value() / 100.;
21-
wave = static_cast<Waveform>(waveParam->Int());
37+
mixLfo = mixLfoParam->Value() / 100.;
2238
clip = clipParam->Value() / 100.;
39+
clipLfo = clipLfoParam->Value() / 100.;
40+
wave = static_cast<Waveform>(waveParam->Int());
2341
algo = myAlgos.getAlgo(wave);
2442
}
2543

44+
45+
double Lfo::getRatio(KLfoTimes timeSig)
46+
{
47+
switch (timeSig)
48+
{
49+
case t2beat:
50+
return 2;
51+
case t3_2beat:
52+
return 3. / 2.;
53+
case t1beat:
54+
return 1;
55+
case t3_4beat:
56+
return 3. / 4.;
57+
case t2_3beat:
58+
return 2. / 3.;
59+
case t1_2beat:
60+
return 1. / 2.;
61+
case t1_3beat:
62+
return 1. / 3.;
63+
case t1_4beat:
64+
return 1. / 4.;
65+
case t1_6beat:
66+
return 1. / 6.;
67+
case t1_8beat:
68+
return 1. / 8.;
69+
case t1_16beat:
70+
return 1. / 16.;
71+
default:
72+
return 1;
73+
}
74+
}
75+
76+
void Lfo::updateParams(double sampleRate, double samplesPerBeat, double samplePos)
77+
{
78+
if (isHostClockedParam->Bool())
79+
{
80+
wavelength = getRatio(static_cast<KLfoTimes>(ratioParam->Value())) * samplesPerBeat;
81+
if (samplePos != -1)
82+
{
83+
phase = std::fmod(samplePos, wavelength);
84+
}
85+
}
86+
else
87+
{
88+
wavelength = sampleRate / freqParam->Value();
89+
}
90+
amp = ampParam->Value();
91+
offset = offsetParam->Value() * wavelength;
92+
}
93+
94+
double Lfo::getVal() {
95+
double constexpr pi = 3.141592653589793238462643383279502;
96+
return std::sin(2 * pi * (phase + offset) / wavelength) * amp;
97+
}
98+
void Lfo::process() {
99+
phase += 1;
100+
if (phase >= wavelength)
101+
{
102+
phase = 0;
103+
}
104+
105+
}
106+
107+
26108
VuMeter::VuMeter() {
27109
Voltage.store(0.0);
28110
}

AudioModules.h

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ enum Waveform
1616
SINEXP,
1717
};
1818

19+
1920
struct Algos
2021
{
2122
static double arctan(double x) { return std::atan(x); }
@@ -63,27 +64,45 @@ struct Algos
6364
}
6465
};
6566

66-
class Shaper
67+
enum KLfoTimes
68+
{
69+
t2beat = 0,
70+
t3_2beat,
71+
t1beat,
72+
t3_4beat,
73+
t2_3beat,
74+
t1_2beat,
75+
t1_3beat,
76+
t1_4beat,
77+
t1_6beat,
78+
t1_8beat,
79+
t1_16beat,
80+
tnum_lfo_times,
81+
};
82+
static std::vector<std::string> lfo_times = {"2/1", "3/2", "1/1", "3/4", "2/3", "1/2", "1/3", "1/4", "1/6", "1/8", "1/16"};
83+
84+
85+
86+
class Lfo
6787
{
6888
public:
69-
Shaper(iplug::IParam* pre, iplug::IParam* post, iplug::IParam* mix, iplug::IParam* wavetype, iplug::IParam* clip);
70-
double process(double x);
71-
void updateParams();
89+
Lfo(iplug::IParam* isHostClocked, iplug::IParam* freq, iplug::IParam* ratio, iplug::IParam* amp, iplug::IParam* offset) :
90+
isHostClockedParam(isHostClocked), freqParam(freq), ratioParam(ratio), ampParam(amp), offsetParam(offset) {}
91+
void process();
92+
double getVal();
93+
static double getRatio(KLfoTimes time);
94+
void updateParams(double sampleRate, double samplesPerBeat, double samplePos);
7295

7396
private:
74-
std::function<double(double)> algo = myAlgos.arctan;
75-
Waveform waveType = ARCTAN;
76-
iplug::IParam* pregainParam = nullptr;
77-
iplug::IParam* postgainParam = nullptr;
78-
iplug::IParam* mixParam = nullptr;
79-
iplug::IParam* waveParam = nullptr;
80-
iplug::IParam* clipParam = nullptr;
81-
Algos myAlgos;
82-
double pregain = 1;
83-
double postgain = 1;
84-
double mix = 0;
85-
double clip = 3.0;
86-
Waveform wave = ARCTAN;
97+
iplug::IParam* isHostClockedParam = nullptr;
98+
iplug::IParam* freqParam = nullptr;
99+
iplug::IParam* ratioParam = nullptr;
100+
iplug::IParam* ampParam = nullptr;
101+
iplug::IParam* offsetParam = nullptr;
102+
double wavelength = 1;
103+
double amp = 1;
104+
double offset = 0;
105+
double phase = 0;
87106
};
88107

89108
class VuMeter
@@ -102,3 +121,43 @@ class VuMeter
102121
};
103122

104123

124+
125+
126+
class Shaper
127+
{
128+
public:
129+
Shaper(iplug::IParam* pre, iplug::IParam* post, iplug::IParam* mix, iplug::IParam* wavetype, iplug::IParam* clip,
130+
iplug::IParam* preGainLfo, iplug::IParam* postGainLfo, iplug::IParam* mixLfo, iplug::IParam* clipLfo, Lfo* lfoPtr);
131+
double process(double x);
132+
void updateParams();
133+
134+
135+
Lfo* lfo = nullptr;
136+
private:
137+
std::function<double(double)> algo = myAlgos.arctan;
138+
Waveform waveType = ARCTAN;
139+
iplug::IParam* pregainParam = nullptr;
140+
iplug::IParam* postgainParam = nullptr;
141+
iplug::IParam* mixParam = nullptr;
142+
iplug::IParam* waveParam = nullptr;
143+
iplug::IParam* clipParam = nullptr;
144+
145+
iplug::IParam* preGainLfoParam = nullptr;
146+
iplug::IParam* postGainLfoParam = nullptr;
147+
iplug::IParam* mixLfoParam = nullptr;
148+
iplug::IParam* clipLfoParam = nullptr;
149+
150+
Algos myAlgos;
151+
152+
double pregain = 1;
153+
double pregainLfo = 0;
154+
double postgain = 1;
155+
double postgainLfo = 0;
156+
double mix = 0;
157+
double mixLfo = 0;
158+
double clip = 3.0;
159+
double clipLfo = 0;
160+
161+
Waveform wave = ARCTAN;
162+
};
163+

UiControlModules.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,30 @@ void dynamicPlot::OnMsgFromDelegate(int msgTag, int dataSize, const void* pData)
9494
};
9595
}
9696

97+
void LfoModuleControl::Draw(IGraphics& g)
98+
{
99+
if (isHostClocked)
100+
{
101+
mRatioControl->Draw(g);
102+
}
103+
else
104+
{
105+
mFreqControl->Draw(g);
106+
}
107+
}
108+
109+
void LfoModuleControl::SetValueFromDelegate(double val, int valIdx) {
110+
isHostClocked = static_cast<bool>(val);
111+
if (isHostClocked)
112+
{
113+
mFreqControl->Hide(true);
114+
mRatioControl->Hide(false);
115+
}
116+
else
117+
{
118+
mFreqControl->Hide(false);
119+
mRatioControl->Hide(true);
120+
}
121+
122+
SetDirty(false);
123+
}

UiControlModules.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,26 @@ class dynamicPlot : public IVPlotControl
4444
Algos mAlgo;
4545
};
4646

47+
class LfoModuleControl : public IControl
48+
{
49+
public:
50+
LfoModuleControl(const IRECT& bounds, IVStyle style, int freqParam, int ratioParam)
51+
: mFreqControl(new IVKnobControl(bounds, freqParam, "Freq", style))
52+
, mRatioControl(new IVKnobControl(bounds, ratioParam, "Ratio", style))
53+
, IControl(bounds)
54+
{
55+
}
56+
void OnAttached() override {
57+
this->GetDelegate()->GetUI()->AttachControl(mFreqControl);
58+
this->GetDelegate()->GetUI()->AttachControl(mRatioControl);
59+
}
60+
61+
void Draw(IGraphics& g) override;
62+
63+
void SetValueFromDelegate(double val, int valIdx) override;
64+
65+
private:
66+
bool isHostClocked = false;
67+
IVKnobControl* mFreqControl;
68+
IVKnobControl* mRatioControl;
69+
};

config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define PLUG_DOES_STATE_CHUNKS 0
2626
#define PLUG_HAS_UI 1
2727
#define PLUG_WIDTH 550
28-
#define PLUG_HEIGHT 420
28+
#define PLUG_HEIGHT 500
2929
#define PLUG_FPS 60
3030
#define PLUG_SHARED_RESOURCES 0
3131
#define PLUG_HOST_RESIZE 0

0 commit comments

Comments
 (0)