forked from Nbickford/REAPERDenoiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfftdconvEfficient.jsfx
415 lines (335 loc) · 11.4 KB
/
fftdconvEfficient.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
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
desc:Convolution Ratio Denoiser
// loosely based off github.com/nbickford/REAPERDenoiser
// This defines a combo box that allows the user to select "Denoise Input" or
// "Record Noise Sample". The default value is 0 (Denoise Input). The maximum
// value is 1 (Record Noise Sample), and it increases in steps of 1.
slider1:0<0,1,1{Denoise Input, Record Noise Sample}>Noise Collection Mode
// This defines a slider that can be varied between 0.0 and 10.0 in steps of
// 0.001, with default value 1.0. (If slider2 is equal to 0.0, this plugin
// shouldn't really do anything to the input audio.)
slider2:1.0<1.0,100.0,0.001>Noise Scale
// This defines a slider that specifies the max level (in dB)
// of the noise that is recorded
slider3:-120<-120,0,0.1>Noise detect min (dB)
// This defines a slider that specifies the minimum level (in dB)
// that a noise is recorded with (useful to filter out silence)
slider4:0<-120,0,0.1>Noise detext max (dB)
// This defines a slider to mix between no noise reduction or
// any amount of noise reduction, up to 100%
slider5:100<0,100,0.5>Wet/dry mix (0 = all dry, 100 = all wet)
slider6:0<0,10,0.01>Decay factor
slider7:1<0.01,1,0.01>Tail Size
// Here we can label our input and output pins. This also tells REAPER how many
// channels we can handle. In this case, the plugin is stereo (a monophonic
// plugin would be simpler, but I almost always use this to denoise stereo
// audio), so we define two input and output pins.
in_pin:Noisy Audio 1
in_pin:Noisy Audio 2
out_pin:Denoised Audio 1
out_pin:Denoised Audio 2
import cookdsp.jsfx-inc
@gfx 1044 400
gfx_set(0, 1, 0);
i = 0;
gi = 10;
idiff = max(1, DSIZE/1024);
loop(min(1024, DSIZE),
gfx_line(gi, 200, gi, 200-50*visBuf[i]);
i += idiff;
gi += 1;
);
@init
// don't add noise to an already silent signal
ext_nodenorm = 1;
ext_noinit = 1;
// function to convert decibel measurement to decimal value
function dBtoDec(value)
(
2^(value/6);
);
// On initialization, initialize all of our variables.
function memalloc_(sz) (
// compensate for cookdsp adding some extra space
// (avoids problems with memory layout when using
// FFT/MDCT and trying to manually keep track of the layout)
__memory_next -= 8;
memalloc(sz);
);
function memalloc_(sz, reinit) (
// compensate for cookdsp adding some extra space
// (avoids problems with memory layout when using
// FFT/MDCT and trying to manually keep track of the layout)
__memory_next -= 8;
memalloc(sz, reinit);
);
// The FFT size will always be constant.
SIZE = 8192;
DSIZE = 2*SIZE;
invSIZE = 1/SIZE;
invDSIZE = 0.5/SIZE;
HALFSIZE = SIZE*0.5;
convBufferL = memalloc_(DSIZE);
convBufferR = memalloc_(DSIZE);
fftOutL = memalloc_(DSIZE);
fftOutR = memalloc_(DSIZE);
visBuf = memalloc_(DSIZE);
window_buf = memalloc_(SIZE);
outBufL = memalloc_(SIZE+1);
outBufR = memalloc_(SIZE+1);
delL = memalloc_(HALFSIZE);
delR = memalloc_(HALFSIZE);
noiseBufferL = memalloc_(HALFSIZE+1, 1);
noiseBufferR = memalloc_(HALFSIZE+1, 1);
// fill window_buf with a hahn window
i = 0;
loop(SIZE,
window_buf[i] = 0.5 - 0.5*cos(2*$pi*i/SIZE);
i += 1;
);
runningSumL = memalloc_(1);
runningSumR = memalloc_(1);
freembuf(__memory_next);
@slider
// A simple function to zero out the noise buffers when switching mode to "Record Noise Sample"
// previousMode should default to 0 on first initialization, but setting it to 0 in @init will cause
// this code to get run again, and the noise profile lost even when switching to "Denoise Input"
slider1 > 0.5 ? (
previousMode > 0.5 ? (
memset(noiseBufferL, 0, SIZE+2);
previousMode = 0;
)
) : previousMode = 1;
noiseLow = dBtoDec(slider3);
noiseHigh = dBtoDec(slider4);
//dryfrac = slider5*0.01;
//wetfrac = 1 - slider5*0.02;
dryfrac = 1 - slider5/100;
wetfrac = slider5/100;
strength = sqr(slider2);
df = exp(slider6);
tail = 0|max(4, min(SIZE, SIZE*slider7));
ss = 1 + sqrt(strength);
invss = 1 / ss;
pdc_delay = HALFSIZE;
pdc_bot_ch = 0;
pdc_top_ch = 2;
@sample
// We'll write a function to denoise a single channel, and then we'll call this
// for each of the channels.
// In this case, we'll pass in the channel number, the four input and output
// tiles, and the current sample.
// We also need to specify which variables will be local to the function (i.e.
// which variables have local instead of global scope).
// Note that channels are zero-indexed (so the left channel is channel 0, and
// the right channel is channel 1).
// Functions can return values, but this one won't return anything.
// Swapping tiles and resetting samplesCollected will be managed by the caller.
function denoiseChannel(channel fftBuffer inBuf outBuf noiseBuffer runningSum convBuffer samplesCollected)
(
// Read out input audio
sample = spl(channel); // You can also use spl0 or spl1.
runningSum[] += sample * sample;
dry = inBuf[samplesCollected];
wet = outBuf[samplesCollected];
inBuf[samplesCollected] = sample;
outBuf[samplesCollected] = outBuf[samplesCollected + HALFSIZE];
fftBuffer[samplesCollected*2] = sample*invSIZE;
fftBuffer[samplesCollected*2+1] = 0;
fftBuffer[samplesCollected*2+SIZE] = 0;
fftBuffer[samplesCollected*2+SIZE+1] = 0;
/*
bandIndex = 0;
loop(HALFSIZE,
outBuf[bandIndex] = outBuf[bandIndex+1] + convBuffer[2*bandIndex] * sample;
bandIndex += 1;
);*/
outVal = wet * wetfrac + dry * dryfrac;
// Reached end of a tile?
samplesCollected >= HALFSIZE-1 ? (
fft(fftBuffer, SIZE);
// If slider1 is greater than 0.5 (i.e. the user selected "Record Noise
// Sample", we store the FFT mangitudes of each of these buffers, but only when the level is correct
runningSumVal = sqrt(runningSum[]*invSIZE);
runningSum[] = 0;
(slider1 > 0.5 && runningSumVal >= noiseLow
&& runningSumVal <= noiseHigh) ?
(
// for each band, compare the norm of the noise in this frame.
// If it is greater than what's already there for this band, then copy
// it into the noiseBuffer
// treat dc and ny bands seperately:
// band 0 (dc)
index = 0;
loop(HALFSIZE+1,
squareMagnitudeNew = sqr(fftBuffer[2*index]) + sqr(fftBuffer[2*index+1]);
squareMagnitudeOld = noiseBuffer[index];
noiseBuffer[index] = 0.25 * squareMagnitudeNew + 0.75 * squareMagnitudeOld;
index += 1;
);
);
// Apply Norbert Weiner's filtering algorithm,
// X(f) = Y(f) * (|Y(f)|^2)/(|Y(f)|^2 + k^2 |N(f)|^2)
// sqr() computes the square of a number, and abs() computes the absolute
// value of a number. We also include a factor of 1/SIZE, to normalize the
// FFT (so that if we don't do any denoising, the input signal is equal to
// the output signal).
// Loop over each band, from bandIndex = 1 to HALFSIZE - 1.
// caluclate attenuationfactor and store in convBuffer as a real valued complex number.
bandIndex = 0;
loop(SIZE,
// Compute |Y(f)|^2 = real(Y(f))^2 + imaginary(Y(f))^2
yNorm = sqr(fftBuffer[bandIndex*2]) + sqr(fftBuffer[2*bandIndex + 1]);
// The same for the noise component:
nNorm = noiseBuffer[min(bandIndex, SIZE-bandIndex)];
attenuationFactor = yNorm / (strength * nNorm + yNorm);
((yNorm + nNorm)*100000000 == 0) ? attenuationFactor = 1;
convBuffer[2*bandIndex] = attenuationFactor;
convBuffer[2*bandIndex+1] = 0;
bandIndex += 1;
);
/*
bandIndex = 0;
loop(DSIZE,
visBuf[bandIndex] = convBuffer[bandIndex];
bandIndex += 1;
);
//*/
// find min of convBuffer
minVal = 2;
bandIndex = 0;
loop(SIZE,
convBuffer[bandIndex] > 0 ? minVal = min(convBuffer[bandIndex], minVal);
bandIndex += 2;
);
// go a bit past the min
minVal *= 0.0000001;
// add a bias to all bands, to prevent taking log of 0,
// and take log
bandIndex = 0;
loop(SIZE,
// calculate log(|H(k)|^2)/4 == log(|H(k)|)/2
convBuffer[bandIndex] = log(convBuffer[bandIndex]+minVal)*invSIZE;
bandIndex += 2;
);
/*
bandIndex = 0;
loop(DSIZE,
visBuf[bandIndex] = convBuffer[bandIndex]*SIZE;
bandIndex += 1;
);
//*/
// convert to cepstrum
ifft(convBuffer, SIZE);
/*
bandIndex = 0;
loop(DSIZE,
visBuf[bandIndex] = convBuffer[bandIndex];
bandIndex += 1;
);
//*/
convBuffer[0] *= 0.5;
/*bandIndex = 1;
loop(SIZE-1,
convBuffer[bandIndex] *= 1;// * window_buf[bandIndex];
bandIndex += 1;
);*/
memset(convBuffer+SIZE, 0, SIZE);
/*
bandIndex = 0;
loop(SIZE,
visBuf[bandIndex] = convBuffer[bandIndex]*SIZE;
bandIndex += 1;
);
//*/
// reverse cepstrum into fft realm
fft(convBuffer, SIZE);
/*
bandIndex = 0;
loop(DSIZE,
visBuf[bandIndex] = convBuffer[bandIndex];
bandIndex += 1;
);
//*/
// apply complex exp to reverse log from before
bandIndex = 0;
loop(SIZE,
// e^(a+bj) = e*a(cos(b) + j*sin(b))
mult = exp(convBuffer[bandIndex])*invSIZE;
ph = convBuffer[bandIndex+1];
convBuffer[bandIndex+1] = mult * sin(ph);
convBuffer[bandIndex] = mult * cos(ph);
bandIndex += 2;
);
// clean convBuffer
ifft(convBuffer, SIZE);
//bandIndex = 0;
//loop(HALFSIZE,
// convBuffer[bandIndex] *= window_buf[bandIndex+HALFSIZE];
//);
/*
bandIndex = 0;
loop(DSIZE,
visBuf[bandIndex] = convBuffer[bandIndex]*SIZE;
bandIndex += 1;
);
//*/
bandIndex = 0;
loop(tail,
w = (0.5+0.5*cos($pi*bandIndex/tail))^df;
convBuffer[bandIndex] *= w;
convBuffer[bandIndex+1] *= w;
bandIndex += 2;
);
// discard second half of the fir
memset(convBuffer + tail, 0, DSIZE - tail);
//bandIndex = 0;
//loop(SIZE,
// convBuffer[bandIndex] *= window_buf[1];
// bandIndex += 2;
//);
//convBuffer[0] = 1;
convBuffer[0] *= invss;
//*
bandIndex = 0;
loop(DSIZE,
visBuf[bandIndex] = convBuffer[bandIndex*0.5]*44*ss;
bandIndex += 1;
);
//*/
fft(convBuffer, SIZE);
/*
bandIndex = 0;
loop(SIZE,
visBuf[bandIndex+SIZE] = convBuffer[bandIndex*2]*SIZE;
bandIndex += 1;
);
//*/
convolve_c(fftBuffer, convBuffer, SIZE);
ifft(fftBuffer, SIZE);
// copy to outBuf
bandIndex = 0;
loop(HALFSIZE,
outBuf[bandIndex] += fftBuffer[bandIndex*2];
bandIndex += 1;
);
loop(HALFSIZE,
outBuf[bandIndex] = fftBuffer[bandIndex*2];
bandIndex += 1;
);
); // samplesCollected >= HALFSIZE-1
outVal;
); // function denoiseChannel(...
// Now, call denoiseChannel for each of the channels.
spl0 = denoiseChannel(0, fftOutL, delL, outBufL, noiseBufferL, runningSumL, convBufferL, samplesCollected);
spl1 = denoiseChannel(1, fftOutR, delR, outBufR, noiseBufferR, runningSumR, convBufferR, samplesCollected);
// Go to the next sample
samplesCollected += 1;
samplesCollected %= HALFSIZE;
@serialize
// Sliders are serialized automatically, so all we have to serialize is the two
// noise buffers. JSFX's serialization works in a clever way: when reading the
// state of the plugin from a serialized version, these functions copy data into
// noiseBufferL and noiseBufferR. But when writing out the state of the plugin,
// they work the other way, copying data out of noiseBufferL and noiseBufferR.
file_var(0, previousMode);
file_mem(0, noiseBufferL, SIZE+2);