-
Notifications
You must be signed in to change notification settings - Fork 30
/
MoogPatch.hpp
252 lines (210 loc) · 5.98 KB
/
MoogPatch.hpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
////////////////////////////////////////////////////////////////////////////////////////////////////
/*
DESCRIPTION:
Moog Ladder filter, formed by a cascade of four one pole sections, and a "Oberheim style" multiplexer.
The multiplexer allows to design a LPF or HPF filter (or anything in between ? test at your own risks !)
This implementation follows closely the one proposed by
Antti Huovilainen and Vesa Välimäki
in the paper:
NEW APPROACHES TO DIGITAL SUBTRACTIVE SYNTHESIS
Helsinki University of Technology Laboratory of Acoustics and Audio Signal Processing
With the additon of a Drive parameter to excite a bit more the non linear component.
LICENSE:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* created by the OWL team 2014 */
////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __MoogPatch_hpp__
#define __MoogPatch_hpp__
#include "StompBox.h"
/**
* Moog Ladder filter class
*/
enum fType
{
LPF=0,
HPF
};
class MoogLadder
{
public:
MoogLadder();
~MoogLadder();
fType type ;
void setType(fType t) ;
void setCoeffs(float w0) ; // for LPF, HPF
void process(int numSamples, float* buffer, float w0, float res, float drive, float masterGain) ;
float processLadder(float input,float x1, float y1);
void setMutiplexer();
float nonLinear(float x); // tanh approx
private:
float A, B, C, D, E ; // multiplexer coeffs
float a1, b0, b1 ; // one pole section coeffs
float pw0; // previous w0
float pres; // previous res
float pmasterGain; // previous master Gain
float in1, out1, out2, out3, out4;
float comp;
};
MoogLadder::MoogLadder(){
in1 = 0.f;
out1 = 0.f;
out2 = 0.f;
out3 = 0.f;
out4 = 0.f;
A = 0.f;
B = 0;
C = 0;
D = 0;
E = 0;
pres=0;
pw0=0.f;
pmasterGain=0.f;
}
MoogLadder::~MoogLadder(){
}
void MoogLadder::setType(fType t){
type = t;
}
void MoogLadder::setMutiplexer(){
switch (type)
{
case HPF:
A=1.0;
B=-4.0;
C=6.0;
D=-4.0;
E=1.0;
comp=0.0;
break;
case LPF:
A=0.0;
B=0.0;
C=0.0;
D=0.0;
E=1.0;
comp=0.5;
break;
default:
A=0.0;
B=0.0;
C=0.0;
D=0.0;
E=1.0;
comp=0.5;
break;
}
}
void MoogLadder::setCoeffs(float w0){
// w0 = 2pi * f/fc
float g=1-expf(-w0);
a1=1-g;
b0=g/1.3;
b1=g*0.23076923076;
}
float MoogLadder::processLadder(float input, float x1, float y1){
float output = a1*y1 + b0*input + b1*x1;
return output;
}
float MoogLadder::nonLinear(float x){
// tanh approx
if (x<-3)
return -1;
else if (x>3)
return 1;
else
return x * ( 27 + x*x ) / ( 27 + 9*x*x );
}
void MoogLadder::process(int numSamples, float *buffer, float w0, float res, float drive, float masterGain){
int N = numSamples-1;
float w0i;
float resi;
float masterGaini;
float a,b,c,d,e,output;
for (int i=0;i<numSamples;i++){
// compute perSample w0, res, gain
if (N>0){
w0i = (pw0*(N-i)+i*w0)/N;
resi =(pres*(N-i)+i*res)/N;
masterGaini=(pmasterGain*(N-i)+i*masterGain)/N;
}
else {
w0i = w0;
resi=res;
}
// update coeffs per sample
setCoeffs(w0i);
// compute sections
float in = nonLinear(buffer[i]* drive);
a=in-4*resi*(nonLinear(out4)-comp*in);
b=processLadder(a, in1, out1);
c=processLadder(b, out1, out2);
d=processLadder(c,out2,out3);
e=processLadder(d,out3,out4);
// Multiplexer
output = A*a + B*b + C*c + D*d + E*e ;
buffer[i] = masterGaini * output / powf(drive,0.3f);
// state variables update
in1=a;
out1=b;
out2=c;
out3=d;
out4=e;
}
// state variables update 2
pw0=w0;
pres=res;
pmasterGain=masterGain;
}
/**
* Moog OWL Patch
*/
class MoogPatch : public Patch {
public:
MoogPatch() {
registerParameter(PARAMETER_A, "Cutoff");
registerParameter(PARAMETER_B, "Resonance");
registerParameter(PARAMETER_C, "Drive");
registerParameter(PARAMETER_D, "Master");
registerParameter(PARAMETER_E, "Cutoff Modulation");
ladder.setType(LPF);
ladder.setMutiplexer();
ladder.setCoeffs(0.f);
}
void processAudio(AudioBuffer &buffer){
float wn = 2*M_PI*getFrequency()/getSampleRate();
float* buf = buffer.getSamples(0);
ladder.process(buffer.getSize(), buf, wn, getQ(), getDrive(), getMasterGain());
}
private:
MoogLadder ladder; // Moog filter
float getFrequency() {
float expr = 1 - getParameterValue(PARAMETER_E);
float f = expr*getParameterValue(PARAMETER_A);
// param_A = 0 <-> f=40 Hz;
// param_A = 1 <-> f=20040 Hz;
return 2*powf(10,3*f+1)+40;
}
float getQ(){
float q = getParameterValue(PARAMETER_B);
// param_B = 0 <-> Q=0.5
// param_B = 1 <-> Q=1.1
return q*1.1+0.1;
}
float getDrive(){
return 1+80*getParameterValue(PARAMETER_C)*getParameterValue(PARAMETER_C);
}
float getMasterGain(){
return getParameterValue(PARAMETER_D)*1.5;
}
};
#endif // __MoogPatch_hpp__