-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDelayPlugin.cpp
More file actions
164 lines (119 loc) · 3.38 KB
/
DelayPlugin.cpp
File metadata and controls
164 lines (119 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "DelayPlugin.h"
#include "IPlug_include_in_plug_src.h"
#include "IControl.h"
#include "resource.h"
const int kNumPrograms = 1;
enum EParams
{
kDelayMS,
kFeedbackPC,
kWetPC,
kNumParams
};
enum ELayout
{
kWidth = GUI_WIDTH,
kHeight = GUI_HEIGHT,
kGainX = 125,
kGainY = 125,
kKnobFrames = 60
};
DelayPlugin::DelayPlugin(IPlugInstanceInfo instanceInfo)
: IPLUG_CTOR(kNumParams, kNumPrograms, instanceInfo)
{
TRACE;
//arguments are: name, defaultVal, minVal, maxVal, step, label
GetParam(kDelayMS)->InitDouble("Delay", 10., 0., 200., 0.01, "Milliseconds");
GetParam(kFeedbackPC)->InitDouble("Feedback", 50., 0., 100.0, 0.01, "%");
GetParam(kWetPC)->InitDouble("Wet/Dry", 50., 0., 100.0, 0.01, "%");
IGraphics* pGraphics = MakeGraphics(this, kWidth, kHeight);
pGraphics->AttachPanelBackground(&COLOR_GRAY);
IBitmap* knob = pGraphics->LoadPointerToBitmap(KNOB_ID, KNOB_FN, kKnobFrames);
pGraphics->AttachControl(new IKnobMultiControl(this, 20, 200, kDelayMS, knob));
pGraphics->AttachControl(new IKnobMultiControl(this, 80, 200, kFeedbackPC, knob));
pGraphics->AttachControl(new IKnobMultiControl(this, 140, 200, kWetPC, knob));
AttachGraphics(pGraphics);
//MakePreset("preset 1", ... );
MakeDefaultPreset((char *) "-", kNumPrograms);
}
DelayPlugin::~DelayPlugin()
{
if(mpBuffer)
{
delete [] mpBuffer;
}
}
void DelayPlugin::ProcessDoubleReplacing(double** inputs, double** outputs, int nFrames)
{
// Mutex is already locked for us.
double* in1 = inputs[0];
double* in2 = inputs[1];
double* out1 = outputs[0];
double* out2 = outputs[1];
for (int s = 0; s < nFrames; ++s, ++in1, ++in2, ++out1, ++out2) // for loop to cycle through frame.
{
//first we read our delayed output
double yn = mpBuffer[mReadIndex];
//if the delay is 0 samples we just feed it the input
if (mDelaySam == 0)
{
yn = *in1;
}
//now we write to out delay buffer
mpBuffer[mWriteIndex] = *in1 + mFeedback * yn;
//.. and then perform the calculation for the output. Notice how the *in is factored by 1 - mWet (which gives the dry level, since wet + dry = 1)
*out1 = ( mWet * yn + (1 - mWet) * *in1 );
//then we increment the write index, wrapping if it goes out of bounds.
++mWriteIndex;
if(mWriteIndex >= mBufferSize)
{
mWriteIndex = 0;
}
//same with thr read index
++mReadIndex;
if(mReadIndex >= mBufferSize)
{
mReadIndex = 0;
}
//because we are working in mono we'll just copy the left output to the right output.
*out2 = *out1;
}
}
void DelayPlugin::Reset()
{
TRACE;
IMutexLock lock(this);
mBufferSize = 2*GetSampleRate();
if(mpBuffer)
{
delete [] mpBuffer;
}
mpBuffer = new double[mBufferSize];
resetDelay();
cookVars();
}
void DelayPlugin::resetDelay()
{
if(mpBuffer)
{
memset(mpBuffer, 0, mBufferSize*sizeof(double));
}
mWriteIndex = 0;
mReadIndex = 0;
}
void DelayPlugin::cookVars()
{
mDelaySam = GetParam(kDelayMS)->Value() * GetSampleRate() / 1000.0;
mFeedback = GetParam(kFeedbackPC)->Value() / 100.0;
mWet = GetParam(kWetPC)->Value() / 100.0;
mReadIndex = mWriteIndex - (int)mDelaySam;
if(mReadIndex < 0)
{
mReadIndex += mBufferSize;
}
}
void DelayPlugin::OnParamChange(int paramIdx)
{
IMutexLock lock(this);
cookVars();
}