forked from geraintluff/jsfx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sandwich Amp.jsfx
294 lines (248 loc) · 7 KB
/
Sandwich Amp.jsfx
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
desc:Sandwich Amp Distortion (by Geraint Luff)
in_pin:Primary Left
in_pin:Primary Right
in_pin:Secondary Left
in_pin:Secondary Right
out_pin:Left
out_pin:Right
slider1:limit_db=-12<-60,0,0.1>-Level (dB)
slider2:asymmetry=0<-1,1,0.01>-Asymmetry
slider3:output_db=0<-12,20,0.1>-Gain (dB)
slider4:distortion_width=0.5<0.1,2,0.01>-Width
slider5:filter_freq=900<50,4000,1>-Filter freq
slider6:filter_gain=20<0,50,0.1>-Filter gain
slider7:bandwidth_octaves=2<0.5,4,0.1>-Filter bandwidth (octaves)
slider8:secondary_gain_db=0<-40,40,0.1>-Secondary gain
import ui-lib.jsfx-inc
import smoother.jsfx-inc
@init
gfx_ext_retina = 1;
freemem = ui_setup(0);
bufferlength = floor(0.1*srate);
inputbuffer = freemem;
outputbuffer = freemem + bufferlength;
freemem += bufferlength*2;
bufferpos = 0;
function tanh(x) (
x >= 20 ? (
1;
) : x <= -20 ? (
-1;
) : (
(exp(2*x) - 1)/(exp(2*x) + 1);
);
);
function sinh(x) (
(exp(x) - exp(-x))*0.5;
);
limit_reference = 1;
offset = 0;
offset_post = 0;
gain = 1;
secondary_gain = 1;
width = width_inv = 1;
filter_a0 = 1;
filter_a1 = 0;
filter_a2 = 0;
filter_b0 = 1;
filter_b1 = 0;
filter_b2 = 0;
function update() local(alpha, A, w0, cos_w0) (
limit_reference = pow(10, smoother_value(limit_db)/20);
offset = smoother_value(asymmetry);
offset_tanh = tanh(offset);
gain = pow(10, smoother_value(output_db)/20);
secondary_gain = pow(10, smoother_value(secondary_gain_db)/20);
width = smoother_value(distortion_width);
width_inv = 1/width;
// Filter coefficients
w0 = 2*$pi*smoother_value(filter_freq)/srate;
cos_w0 = cos(w0);
alpha = sin(w0)*sinh(log(2)/2*smoother_value(bandwidth_octaves)*w0/sin(w0));
A = pow(10, smoother_value(filter_gain)/40);
filter_a0 = 1 + alpha/A;
filter_a1 = -2*cos_w0;
filter_a2 = 1 - alpha/A;
filter_b0 = 1 + alpha*A;
filter_b1 = -2*cos_w0;
filter_b2 = 1 - alpha*A;
);
g_scale = g_scale ? g_scale : 1;
smoother_init(limit_db);
smoother_init(asymmetry);
smoother_init(output_db);
smoother_init(distortion_width);
smoother_init(filter_freq);
smoother_init(filter_gain);
smoother_init(bandwidth_octaves);
smoother_init(secondary_gain_db);
update();
@block
smoothing = smoother_block(limit_db)
+ smoother_block(asymmetry)
+ smoother_block(output_db)
+ smoother_block(distortion_width)
+ smoother_block(filter_freq)
+ smoother_block(filter_gain)
+ smoother_block(bandwidth_octaves)
+ smoother_block(secondary_gain_db);
needs_update ? (
needs_update = 0;
update();
);
@sample
smoothing ? (
smoother_sample(limit_db);
smoother_sample(asymmetry);
smoother_sample(output_db);
smoother_sample(distortion_width);
smoother_sample(filter_freq);
smoother_sample(filter_gain);
smoother_sample(bandwidth_octaves);
smoother_sample(secondary_gain_db);
update();
);
function distort(v) (
limit_reference*(tanh(v/limit_reference - offset) + offset_tanh);
);
function filter(x) local(y) (
y = (filter_b0*x + filter_b1*this.x1 + filter_b2*this.x2 - filter_a1*this.y1 - filter_a2*this.y2)/filter_a0;
this.y2 = this.y1;
this.y1 = y;
this.x2 = this.x1;
this.x1 = x;
y;
);
function inv_filter(x) local(y) (
y = (filter_a0*x + filter_a1*this.x1 + filter_a2*this.x2 - filter_b1*this.y1 - filter_b2*this.y2)/filter_b0;
this.x2 = this.x1;
this.x1 = x;
this.y2 = this.y1;
this.y1 = y;
y;
);
inputbuffer[bufferpos] = (spl0 + spl1)*0.5;
// Add secondary channels
left = spl0 + spl2*secondary_gain;
right = spl1 + spl3*secondary_gain;
// Filter
left = filter_left.filter(left);
right = filter_right.filter(right);
// Inverse width
avg = (left + right)*0.5;
diff = (right - left)*0.5;
left = avg - diff*width_inv;
right = avg + diff*width_inv;
// Distortion
left = distort(left);
right = distort(right);
// Width
avg = (left + right)*0.5;
diff = (right - left)*0.5;
left = avg - diff*width;
right = avg + diff*width;
// Inverse filter
left = filter_left_inv.inv_filter(left);
right = filter_right_inv.inv_filter(right);
// Remove secondary channels
spl0 = gain*(left - spl2*secondary_gain);
spl1 = gain*(right - spl3*secondary_gain);
outputbuffer[bufferpos] = (spl0 + spl1)*0.5;
bufferpos += 1;
bufferpos >= bufferlength ? bufferpos = 0;
@gfx 500 420
function labelled_default_dial(value, low, high, bias, label, formatvalue, format, default) (
ui_push_height(50);
value = control_dial(value, low, high, bias, default);
ui_pop();
ui_push_below(50);
ui_align(0.5, 0.25);
ui_textnumber(formatvalue, format);
ui_pop();
ui_push_above(50);
ui_align(0.5, 0.75);
ui_text(label);
ui_pop();
value;
);
function value_to_y(v) (
ui_top() + min(1, max(0, 0.5*(1 - v*g_scale)))*ui_height();
);
function index_to_x(i, N) (
ui_left() + i/(N - 1)*ui_width();
);
function draw_grid() (
gfx_x = ui_left();
gfx_y = value_to_y(0);
ui_color(255, 255, 255, 0.5);
gfx_lineto(ui_right() - 1, gfx_y);
);
function draw_buffer(buffer) local(i, v) (
ui_push();
ui_retina(1);
gfx_x = ui_left();
gfx_y = value_to_y(buffer[bufferpos]);
i = 0;
while (i < bufferlength) (
v = buffer[(bufferpos + i)%bufferlength];
gfx_lineto(index_to_x(i, bufferlength), value_to_y(v));
i += 1;
);
ui_pop();
);
control_start("main", "default");
ui_screen() == "main" ? (
control_navbar("Amp Distortion", -1, -1);
ui_pad(1);
ui_split_topratio(1/3);
ui_split_leftratio(3/5);
control_group("Distortion");
ui_split_leftratio(1/3);
ui_automate(limit_db, labelled_default_dial(limit_db, -60, 0, -2, "level", limit_db, "%.1fdB", -12));
ui_split_next();
ui_automate(output_db, labelled_default_dial(output_db, -12, 20, 0, "gain", output_db, "%.1fdB", 0));
ui_split_next();
ui_automate(asymmetry, labelled_default_dial(asymmetry, -1, 1, 0, "offset", asymmetry*100, "%i%%", 0));
ui_pop();
ui_pop();
control_group("Width");
ui_split_leftratio(1/2);
ui_automate(distortion_width, labelled_default_dial(distortion_width, 0.1, 2, 0, "width", floor(distortion_width*100 + 0.5), "%i%%", 0.5));
ui_split_next();
ui_automate(secondary_gain_db, labelled_default_dial(secondary_gain_db, -40, 40, 0, "side-gain", secondary_gain_db, "%.1fdB", 0));
ui_pop();
ui_split_next();
control_group("Filter");
ui_split_leftratio(1/3);
ui_automate(filter_freq, labelled_default_dial(filter_freq, 50, 4000, 4, "freq", filter_freq, "%iHz", 900));
ui_split_next();
ui_automate(filter_gain, labelled_default_dial(filter_gain, 0, 50, 0, "gain", filter_gain, "%.1fdB", 20));
ui_split_next();
ui_automate(bandwidth_octaves, labelled_default_dial(bandwidth_octaves, 0.5, 4, 0.4, "bandwidth", bandwidth_octaves, "%.1f oct", 2));
ui_pop();
ui_pop();
g_barwidth = ui_push_width(50);
ui_pad();
g_scale = control_slider_bottom(g_scale, 0.5, 10, log(20));
ui_click() && ui_clickcount() == 2 ? (
g_scale = 1;
);
ui_pop();
ui_push_leftof(g_barwidth);
ui_color(0, 0, 0);
ui_fill();
draw_grid();
ui_color(64, 128, 192);
draw_buffer(inputbuffer);
control_finish_enabled(1);
ui_pop();
ui_push_rightof(g_barwidth);
ui_color(0, 0, 0);
ui_fill();
draw_grid();
ui_color(192, 128, 64);
draw_buffer(outputbuffer);
control_finish_enabled(1);
ui_pop();
) : ui_system();
ui_interacted() ? needs_update = 1;