-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSoftReverb.java
515 lines (446 loc) · 16.9 KB
/
SoftReverb.java
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.gervill;
import java.util.Arrays;
/**
* Reverb effect based on allpass/comb filters. First audio is send to 8
* parelled comb filters and then mixed together and then finally send thru 3
* different allpass filters.
*
* @author Karl Helgason
*/
public final class SoftReverb implements SoftAudioProcessor {
private final static class Delay {
private float[] delaybuffer;
private int rovepos = 0;
Delay() {
delaybuffer = null;
}
public void setDelay(int delay) {
if (delay == 0)
delaybuffer = null;
else
delaybuffer = new float[delay];
rovepos = 0;
}
public void processReplace(float[] inout) {
if (delaybuffer == null)
return;
int len = inout.length;
int rnlen = delaybuffer.length;
int rovepos = this.rovepos;
for (int i = 0; i < len; i++) {
float x = inout[i];
inout[i] = delaybuffer[rovepos];
delaybuffer[rovepos] = x;
if (++rovepos == rnlen)
rovepos = 0;
}
this.rovepos = rovepos;
}
}
private final static class AllPass {
private final float[] delaybuffer;
private final int delaybuffersize;
private int rovepos = 0;
private float feedback;
AllPass(int size) {
delaybuffer = new float[size];
delaybuffersize = size;
}
public void setFeedBack(float feedback) {
this.feedback = feedback;
}
public void processReplace(float inout[]) {
int len = inout.length;
int delaybuffersize = this.delaybuffersize;
int rovepos = this.rovepos;
for (int i = 0; i < len; i++) {
float delayout = delaybuffer[rovepos];
float input = inout[i];
inout[i] = delayout - input;
delaybuffer[rovepos] = input + delayout * feedback;
if (++rovepos == delaybuffersize)
rovepos = 0;
}
this.rovepos = rovepos;
}
public void processReplace(float in[], float out[]) {
int len = in.length;
int delaybuffersize = this.delaybuffersize;
int rovepos = this.rovepos;
for (int i = 0; i < len; i++) {
float delayout = delaybuffer[rovepos];
float input = in[i];
out[i] = delayout - input;
delaybuffer[rovepos] = input + delayout * feedback;
if (++rovepos == delaybuffersize)
rovepos = 0;
}
this.rovepos = rovepos;
}
}
private final static class Comb {
private final float[] delaybuffer;
private final int delaybuffersize;
private int rovepos = 0;
private float feedback;
private float filtertemp = 0;
private float filtercoeff1 = 0;
private float filtercoeff2 = 1;
Comb(int size) {
delaybuffer = new float[size];
delaybuffersize = size;
}
public void setFeedBack(float feedback) {
this.feedback = feedback;
filtercoeff2 = (1 - filtercoeff1)* feedback;
}
public void processMix(float in[], float out[]) {
int len = in.length;
int delaybuffersize = this.delaybuffersize;
int rovepos = this.rovepos;
float filtertemp = this.filtertemp;
float filtercoeff1 = this.filtercoeff1;
float filtercoeff2 = this.filtercoeff2;
for (int i = 0; i < len; i++) {
float delayout = delaybuffer[rovepos];
// One Pole Lowpass Filter
filtertemp = (delayout * filtercoeff2)
+ (filtertemp * filtercoeff1);
out[i] += delayout;
delaybuffer[rovepos] = in[i] + filtertemp;
if (++rovepos == delaybuffersize)
rovepos = 0;
}
this.filtertemp = filtertemp;
this.rovepos = rovepos;
}
public void processReplace(float in[], float out[]) {
int len = in.length;
int delaybuffersize = this.delaybuffersize;
int rovepos = this.rovepos;
float filtertemp = this.filtertemp;
float filtercoeff1 = this.filtercoeff1;
float filtercoeff2 = this.filtercoeff2;
for (int i = 0; i < len; i++) {
float delayout = delaybuffer[rovepos];
// One Pole Lowpass Filter
filtertemp = (delayout * filtercoeff2)
+ (filtertemp * filtercoeff1);
out[i] = delayout;
delaybuffer[rovepos] = in[i] + filtertemp;
if (++rovepos == delaybuffersize)
rovepos = 0;
}
this.filtertemp = filtertemp;
this.rovepos = rovepos;
}
public void setDamp(float val) {
filtercoeff1 = val;
filtercoeff2 = (1 - filtercoeff1)* feedback;
}
}
private float roomsize;
private float damp;
private float gain = 1;
private Delay delay;
private Comb[] combL;
private Comb[] combR;
private AllPass[] allpassL;
private AllPass[] allpassR;
private float[] input;
private float[] out;
private float[] pre1;
private float[] pre2;
private float[] pre3;
private boolean denormal_flip = false;
private boolean mix = true;
private SoftAudioBuffer inputA;
private SoftAudioBuffer left;
private SoftAudioBuffer right;
private boolean dirty = true;
private float dirty_roomsize;
private float dirty_damp;
private float dirty_predelay;
private float dirty_gain;
private float samplerate;
private boolean light = true;
public void init(float samplerate, float controlrate) {
this.samplerate = samplerate;
double freqscale = ((double) samplerate) / 44100.0;
// freqscale = 1.0/ freqscale;
int stereospread = 23;
delay = new Delay();
combL = new Comb[8];
combR = new Comb[8];
combL[0] = new Comb((int) (freqscale * (1116)));
combR[0] = new Comb((int) (freqscale * (1116 + stereospread)));
combL[1] = new Comb((int) (freqscale * (1188)));
combR[1] = new Comb((int) (freqscale * (1188 + stereospread)));
combL[2] = new Comb((int) (freqscale * (1277)));
combR[2] = new Comb((int) (freqscale * (1277 + stereospread)));
combL[3] = new Comb((int) (freqscale * (1356)));
combR[3] = new Comb((int) (freqscale * (1356 + stereospread)));
combL[4] = new Comb((int) (freqscale * (1422)));
combR[4] = new Comb((int) (freqscale * (1422 + stereospread)));
combL[5] = new Comb((int) (freqscale * (1491)));
combR[5] = new Comb((int) (freqscale * (1491 + stereospread)));
combL[6] = new Comb((int) (freqscale * (1557)));
combR[6] = new Comb((int) (freqscale * (1557 + stereospread)));
combL[7] = new Comb((int) (freqscale * (1617)));
combR[7] = new Comb((int) (freqscale * (1617 + stereospread)));
allpassL = new AllPass[4];
allpassR = new AllPass[4];
allpassL[0] = new AllPass((int) (freqscale * (556)));
allpassR[0] = new AllPass((int) (freqscale * (556 + stereospread)));
allpassL[1] = new AllPass((int) (freqscale * (441)));
allpassR[1] = new AllPass((int) (freqscale * (441 + stereospread)));
allpassL[2] = new AllPass((int) (freqscale * (341)));
allpassR[2] = new AllPass((int) (freqscale * (341 + stereospread)));
allpassL[3] = new AllPass((int) (freqscale * (225)));
allpassR[3] = new AllPass((int) (freqscale * (225 + stereospread)));
for (int i = 0; i < allpassL.length; i++) {
allpassL[i].setFeedBack(0.5f);
allpassR[i].setFeedBack(0.5f);
}
/* Init other settings */
globalParameterControlChange(new int[]{0x01 * 128 + 0x01}, 0, 4);
}
public void setInput(int pin, SoftAudioBuffer input) {
if (pin == 0)
inputA = input;
}
public void setOutput(int pin, SoftAudioBuffer output) {
if (pin == 0)
left = output;
if (pin == 1)
right = output;
}
public void setMixMode(boolean mix) {
this.mix = mix;
}
private boolean silent = true;
public void processAudio() {
boolean silent_input = this.inputA.isSilent();
if(!silent_input)
silent = false;
if(silent)
{
if (!mix) {
left.clear();
right.clear();
}
return;
}
float[] inputA = this.inputA.array();
float[] left = this.left.array();
float[] right = this.right == null ? null : this.right.array();
int numsamples = inputA.length;
if (input == null || input.length < numsamples)
input = new float[numsamples];
float again = gain * 0.018f / 2;
denormal_flip = !denormal_flip;
if(denormal_flip)
for (int i = 0; i < numsamples; i++)
input[i] = inputA[i] * again + 1E-20f;
else
for (int i = 0; i < numsamples; i++)
input[i] = inputA[i] * again - 1E-20f;
delay.processReplace(input);
if(light && (right != null))
{
if (pre1 == null || pre1.length < numsamples)
{
pre1 = new float[numsamples];
pre2 = new float[numsamples];
pre3 = new float[numsamples];
}
for (int i = 0; i < allpassL.length; i++)
allpassL[i].processReplace(input);
combL[0].processReplace(input, pre3);
combL[1].processReplace(input, pre3);
combL[2].processReplace(input, pre1);
for (int i = 4; i < combL.length-2; i+=2)
combL[i].processMix(input, pre1);
combL[3].processReplace(input, pre2);;
for (int i = 5; i < combL.length-2; i+=2)
combL[i].processMix(input, pre2);
if (!mix)
{
Arrays.fill(right, 0);
Arrays.fill(left, 0);
}
for (int i = combR.length-2; i < combR.length; i++)
combR[i].processMix(input, right);
for (int i = combL.length-2; i < combL.length; i++)
combL[i].processMix(input, left);
for (int i = 0; i < numsamples; i++)
{
float p = pre1[i] - pre2[i];
float m = pre3[i];
left[i] += m + p;
right[i] += m - p;
}
}
else
{
if (out == null || out.length < numsamples)
out = new float[numsamples];
if (right != null) {
if (!mix)
Arrays.fill(right, 0);
allpassR[0].processReplace(input, out);
for (int i = 1; i < allpassR.length; i++)
allpassR[i].processReplace(out);
for (int i = 0; i < combR.length; i++)
combR[i].processMix(out, right);
}
if (!mix)
Arrays.fill(left, 0);
allpassL[0].processReplace(input, out);
for (int i = 1; i < allpassL.length; i++)
allpassL[i].processReplace(out);
for (int i = 0; i < combL.length; i++)
combL[i].processMix(out, left);
}
if (silent_input) {
silent = true;
for (int i = 0; i < numsamples; i++)
{
float v = left[i];
if(v > 1E-10 || v < -1E-10)
{
silent = false;
break;
}
}
}
}
public void globalParameterControlChange(int[] slothpath, long param,
long value) {
if (slothpath.length == 1) {
if (slothpath[0] == 0x01 * 128 + 0x01) {
if (param == 0) {
if (value == 0) {
// Small Room A small size room with a length
// of 5m or so.
dirty_roomsize = (1.1f);
dirty_damp = (5000);
dirty_predelay = (0);
dirty_gain = (4);
dirty = true;
}
if (value == 1) {
// Medium Room A medium size room with a length
// of 10m or so.
dirty_roomsize = (1.3f);
dirty_damp = (5000);
dirty_predelay = (0);
dirty_gain = (3);
dirty = true;
}
if (value == 2) {
// Large Room A large size room suitable for
// live performances.
dirty_roomsize = (1.5f);
dirty_damp = (5000);
dirty_predelay = (0);
dirty_gain = (2);
dirty = true;
}
if (value == 3) {
// Medium Hall A medium size concert hall.
dirty_roomsize = (1.8f);
dirty_damp = (24000);
dirty_predelay = (0.02f);
dirty_gain = (1.5f);
dirty = true;
}
if (value == 4) {
// Large Hall A large size concert hall
// suitable for a full orchestra.
dirty_roomsize = (1.8f);
dirty_damp = (24000);
dirty_predelay = (0.03f);
dirty_gain = (1.5f);
dirty = true;
}
if (value == 8) {
// Plate A plate reverb simulation.
dirty_roomsize = (1.3f);
dirty_damp = (2500);
dirty_predelay = (0);
dirty_gain = (6);
dirty = true;
}
} else if (param == 1) {
dirty_roomsize = ((float) (Math.exp((value - 40) * 0.025)));
dirty = true;
}
}
}
}
public void processControlLogic() {
if (dirty) {
dirty = false;
setRoomSize(dirty_roomsize);
setDamp(dirty_damp);
setPreDelay(dirty_predelay);
setGain(dirty_gain);
}
}
public void setRoomSize(float value) {
roomsize = 1 - (0.17f / value);
for (int i = 0; i < combL.length; i++) {
combL[i].feedback = roomsize;
combR[i].feedback = roomsize;
}
}
public void setPreDelay(float value) {
delay.setDelay((int)(value * samplerate));
}
public void setGain(float gain) {
this.gain = gain;
}
public void setDamp(float value) {
double x = (value / samplerate) * (2 * Math.PI);
double cx = 2 - Math.cos(x);
damp = (float)(cx - Math.sqrt(cx * cx - 1));
if (damp > 1)
damp = 1;
if (damp < 0)
damp = 0;
// damp = value * 0.4f;
for (int i = 0; i < combL.length; i++) {
combL[i].setDamp(damp);
combR[i].setDamp(damp);
}
}
public void setLightMode(boolean light)
{
this.light = light;
}
}