1 /* ========================================
2 * UnBox - UnBox.h
3 * Copyright (c) 2016 airwindows, All rights reserved
4 * ======================================== */
5
6 #ifndef __UnBox_H
7 #include "UnBox.h"
8 #endif
9
10 namespace UnBox {
11
12
processReplacing(float ** inputs,float ** outputs,VstInt32 sampleFrames)13 void UnBox::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames)
14 {
15 float* in1 = inputs[0];
16 float* in2 = inputs[1];
17 float* out1 = outputs[0];
18 float* out2 = outputs[1];
19
20 double overallscale = 1.0;
21 overallscale /= 44100.0;
22 overallscale *= getSampleRate();
23
24 double input = A*2.0;
25 double unbox = B+1.0;
26 unbox *= unbox; //let's get some more gain into this
27 double iirAmount = (unbox*0.00052)/overallscale;
28 double output = C*2.0;
29
30 double treble = unbox; //averaging taps 1-4
31 double gain = treble;
32 if (gain > 1.0) {e[0] = 1.0; gain -= 1.0;} else {e[0] = gain; gain = 0.0;}
33 if (gain > 1.0) {e[1] = 1.0; gain -= 1.0;} else {e[1] = gain; gain = 0.0;}
34 if (gain > 1.0) {e[2] = 1.0; gain -= 1.0;} else {e[2] = gain; gain = 0.0;}
35 if (gain > 1.0) {e[3] = 1.0; gain -= 1.0;} else {e[3] = gain; gain = 0.0;}
36 if (gain > 1.0) {e[4] = 1.0; gain -= 1.0;} else {e[4] = gain; gain = 0.0;}
37 //there, now we have a neat little moving average with remainders
38 if (treble < 1.0) treble = 1.0;
39 e[0] /= treble;
40 e[1] /= treble;
41 e[2] /= treble;
42 e[3] /= treble;
43 e[4] /= treble;
44 //and now it's neatly scaled, too
45
46 treble = unbox*2.0; //averaging taps 1-8
47 gain = treble;
48 if (gain > 1.0) {f[0] = 1.0; gain -= 1.0;} else {f[0] = gain; gain = 0.0;}
49 if (gain > 1.0) {f[1] = 1.0; gain -= 1.0;} else {f[1] = gain; gain = 0.0;}
50 if (gain > 1.0) {f[2] = 1.0; gain -= 1.0;} else {f[2] = gain; gain = 0.0;}
51 if (gain > 1.0) {f[3] = 1.0; gain -= 1.0;} else {f[3] = gain; gain = 0.0;}
52 if (gain > 1.0) {f[4] = 1.0; gain -= 1.0;} else {f[4] = gain; gain = 0.0;}
53 if (gain > 1.0) {f[5] = 1.0; gain -= 1.0;} else {f[5] = gain; gain = 0.0;}
54 if (gain > 1.0) {f[6] = 1.0; gain -= 1.0;} else {f[6] = gain; gain = 0.0;}
55 if (gain > 1.0) {f[7] = 1.0; gain -= 1.0;} else {f[7] = gain; gain = 0.0;}
56 if (gain > 1.0) {f[8] = 1.0; gain -= 1.0;} else {f[8] = gain; gain = 0.0;}
57 if (gain > 1.0) {f[9] = 1.0; gain -= 1.0;} else {f[9] = gain; gain = 0.0;}
58 //there, now we have a neat little moving average with remainders
59 if (treble < 1.0) treble = 1.0;
60 f[0] /= treble;
61 f[1] /= treble;
62 f[2] /= treble;
63 f[3] /= treble;
64 f[4] /= treble;
65 f[5] /= treble;
66 f[6] /= treble;
67 f[7] /= treble;
68 f[8] /= treble;
69 f[9] /= treble;
70 //and now it's neatly scaled, too
71
72 while (--sampleFrames >= 0)
73 {
74 long double inputSampleL = *in1;
75 long double inputSampleR = *in2;
76
77 if (input != 1.0) {inputSampleL *= input; inputSampleR *= input;}
78
79 static int noisesourceL = 0;
80 static int noisesourceR = 850010;
81 int residue;
82 double applyresidue;
83
84 noisesourceL = noisesourceL % 1700021; noisesourceL++;
85 residue = noisesourceL * noisesourceL;
86 residue = residue % 170003; residue *= residue;
87 residue = residue % 17011; residue *= residue;
88 residue = residue % 1709; residue *= residue;
89 residue = residue % 173; residue *= residue;
90 residue = residue % 17;
91 applyresidue = residue;
92 applyresidue *= 0.00000001;
93 applyresidue *= 0.00000001;
94 inputSampleL += applyresidue;
95 if (inputSampleL<1.2e-38 && -inputSampleL<1.2e-38) {
96 inputSampleL -= applyresidue;
97 }
98
99 noisesourceR = noisesourceR % 1700021; noisesourceR++;
100 residue = noisesourceR * noisesourceR;
101 residue = residue % 170003; residue *= residue;
102 residue = residue % 17011; residue *= residue;
103 residue = residue % 1709; residue *= residue;
104 residue = residue % 173; residue *= residue;
105 residue = residue % 17;
106 applyresidue = residue;
107 applyresidue *= 0.00000001;
108 applyresidue *= 0.00000001;
109 inputSampleR += applyresidue;
110 if (inputSampleR<1.2e-38 && -inputSampleR<1.2e-38) {
111 inputSampleR -= applyresidue;
112 }
113 //for live air, we always apply the dither noise. Then, if our result is
114 //effectively digital black, we'll subtract it aUnBox. We want a 'air' hiss
115 long double drySampleL = inputSampleL;
116 long double drySampleR = inputSampleR;
117
118 aL[4] = aL[3]; aL[3] = aL[2]; aL[2] = aL[1];
119 aL[1] = aL[0]; aL[0] = inputSampleL;
120 inputSampleL *= e[0];
121 inputSampleL += (aL[1] * e[1]);
122 inputSampleL += (aL[2] * e[2]);
123 inputSampleL += (aL[3] * e[3]);
124 inputSampleL += (aL[4] * e[4]);
125 //this is now an average of inputSampleL
126
127 aR[4] = aR[3]; aR[3] = aR[2]; aR[2] = aR[1];
128 aR[1] = aR[0]; aR[0] = inputSampleR;
129 inputSampleR *= e[0];
130 inputSampleR += (aR[1] * e[1]);
131 inputSampleR += (aR[2] * e[2]);
132 inputSampleR += (aR[3] * e[3]);
133 inputSampleR += (aR[4] * e[4]);
134 //this is now an average of inputSampleR
135
136 bL[4] = bL[3]; bL[3] = bL[2]; bL[2] = bL[1];
137 bL[1] = bL[0]; bL[0] = inputSampleL;
138 inputSampleL *= e[0];
139 inputSampleL += (bL[1] * e[1]);
140 inputSampleL += (bL[2] * e[2]);
141 inputSampleL += (bL[3] * e[3]);
142 inputSampleL += (bL[4] * e[4]);
143 //this is now an average of an average of inputSampleL. Two poles
144
145 bR[4] = bR[3]; bR[3] = bR[2]; bR[2] = bR[1];
146 bR[1] = bR[0]; bR[0] = inputSampleR;
147 inputSampleR *= e[0];
148 inputSampleR += (bR[1] * e[1]);
149 inputSampleR += (bR[2] * e[2]);
150 inputSampleR += (bR[3] * e[3]);
151 inputSampleR += (bR[4] * e[4]);
152 //this is now an average of an average of inputSampleR. Two poles
153
154 inputSampleL *= unbox;
155 inputSampleR *= unbox;
156 //clip to 1.2533141373155 to reach maximum output
157 if (inputSampleL > 1.2533141373155) inputSampleL = 1.2533141373155;
158 if (inputSampleL < -1.2533141373155) inputSampleL = -1.2533141373155;
159 inputSampleL = sin(inputSampleL * fabs(inputSampleL)) / ((inputSampleL == 0.0) ?1:fabs(inputSampleL));
160
161 if (inputSampleR > 1.2533141373155) inputSampleR = 1.2533141373155;
162 if (inputSampleR < -1.2533141373155) inputSampleR = -1.2533141373155;
163 inputSampleR = sin(inputSampleR * fabs(inputSampleR)) / ((inputSampleR == 0.0) ?1:fabs(inputSampleR));
164
165 inputSampleL /= unbox;
166 inputSampleR /= unbox;
167 //now we have a distorted inputSample at the correct volume relative to drySample
168
169 long double accumulatorSampleL = (drySampleL - inputSampleL);
170 cL[9] = cL[8]; cL[8] = cL[7]; cL[7] = cL[6]; cL[6] = cL[5];
171 cL[5] = cL[4]; cL[4] = cL[3]; cL[3] = cL[2]; cL[2] = cL[1];
172 cL[1] = cL[0]; cL[0] = accumulatorSampleL;
173 accumulatorSampleL *= f[0];
174 accumulatorSampleL += (cL[1] * f[1]);
175 accumulatorSampleL += (cL[2] * f[2]);
176 accumulatorSampleL += (cL[3] * f[3]);
177 accumulatorSampleL += (cL[4] * f[4]);
178 accumulatorSampleL += (cL[5] * f[5]);
179 accumulatorSampleL += (cL[6] * f[6]);
180 accumulatorSampleL += (cL[7] * f[7]);
181 accumulatorSampleL += (cL[8] * f[8]);
182 accumulatorSampleL += (cL[9] * f[9]);
183 //this is now an average of all the recent variances from dry
184
185 long double accumulatorSampleR = (drySampleR - inputSampleR);
186 cR[9] = cR[8]; cR[8] = cR[7]; cR[7] = cR[6]; cR[6] = cR[5];
187 cR[5] = cR[4]; cR[4] = cR[3]; cR[3] = cR[2]; cR[2] = cR[1];
188 cR[1] = cR[0]; cR[0] = accumulatorSampleR;
189 accumulatorSampleR *= f[0];
190 accumulatorSampleR += (cR[1] * f[1]);
191 accumulatorSampleR += (cR[2] * f[2]);
192 accumulatorSampleR += (cR[3] * f[3]);
193 accumulatorSampleR += (cR[4] * f[4]);
194 accumulatorSampleR += (cR[5] * f[5]);
195 accumulatorSampleR += (cR[6] * f[6]);
196 accumulatorSampleR += (cR[7] * f[7]);
197 accumulatorSampleR += (cR[8] * f[8]);
198 accumulatorSampleR += (cR[9] * f[9]);
199 //this is now an average of all the recent variances from dry
200
201 iirSampleAL = (iirSampleAL * (1 - iirAmount)) + (accumulatorSampleL * iirAmount);
202 accumulatorSampleL -= iirSampleAL;
203 //two poles of IIR
204
205 iirSampleAR = (iirSampleAR * (1 - iirAmount)) + (accumulatorSampleR * iirAmount);
206 accumulatorSampleR -= iirSampleAR;
207 //two poles of IIR
208
209 iirSampleBL = (iirSampleBL * (1 - iirAmount)) + (accumulatorSampleL * iirAmount);
210 accumulatorSampleL -= iirSampleBL;
211 //highpass section
212
213 iirSampleBR = (iirSampleBR * (1 - iirAmount)) + (accumulatorSampleR * iirAmount);
214 accumulatorSampleR -= iirSampleBR;
215 //highpass section
216 //this is now a highpassed average of all the recent variances from dry
217
218 inputSampleL = drySampleL - accumulatorSampleL;
219 inputSampleR = drySampleR - accumulatorSampleR;
220 //we apply it as one operation, to get the result.
221
222 if (output != 1.0) {inputSampleL *= output; inputSampleR *= output;}
223
224 //stereo 32 bit dither, made small and tidy.
225 int expon; frexpf((float)inputSampleL, &expon);
226 long double dither = (rand()/(RAND_MAX*7.737125245533627e+25))*pow(2,expon+62);
227 inputSampleL += (dither-fpNShapeL); fpNShapeL = dither;
228 frexpf((float)inputSampleR, &expon);
229 dither = (rand()/(RAND_MAX*7.737125245533627e+25))*pow(2,expon+62);
230 inputSampleR += (dither-fpNShapeR); fpNShapeR = dither;
231 //end 32 bit dither
232
233 *out1 = inputSampleL;
234 *out2 = inputSampleR;
235
236 in1++;
237 in2++;
238 out1++;
239 out2++;
240 }
241 }
242
processDoubleReplacing(double ** inputs,double ** outputs,VstInt32 sampleFrames)243 void UnBox::processDoubleReplacing(double **inputs, double **outputs, VstInt32 sampleFrames)
244 {
245 double* in1 = inputs[0];
246 double* in2 = inputs[1];
247 double* out1 = outputs[0];
248 double* out2 = outputs[1];
249
250 double overallscale = 1.0;
251 overallscale /= 44100.0;
252 overallscale *= getSampleRate();
253
254 double input = A*2.0;
255 double unbox = B+1.0;
256 unbox *= unbox; //let's get some more gain into this
257 double iirAmount = (unbox*0.00052)/overallscale;
258 double output = C*2.0;
259
260 double treble = unbox; //averaging taps 1-4
261 double gain = treble;
262 if (gain > 1.0) {e[0] = 1.0; gain -= 1.0;} else {e[0] = gain; gain = 0.0;}
263 if (gain > 1.0) {e[1] = 1.0; gain -= 1.0;} else {e[1] = gain; gain = 0.0;}
264 if (gain > 1.0) {e[2] = 1.0; gain -= 1.0;} else {e[2] = gain; gain = 0.0;}
265 if (gain > 1.0) {e[3] = 1.0; gain -= 1.0;} else {e[3] = gain; gain = 0.0;}
266 if (gain > 1.0) {e[4] = 1.0; gain -= 1.0;} else {e[4] = gain; gain = 0.0;}
267 //there, now we have a neat little moving average with remainders
268 if (treble < 1.0) treble = 1.0;
269 e[0] /= treble;
270 e[1] /= treble;
271 e[2] /= treble;
272 e[3] /= treble;
273 e[4] /= treble;
274 //and now it's neatly scaled, too
275
276 treble = unbox*2.0; //averaging taps 1-8
277 gain = treble;
278 if (gain > 1.0) {f[0] = 1.0; gain -= 1.0;} else {f[0] = gain; gain = 0.0;}
279 if (gain > 1.0) {f[1] = 1.0; gain -= 1.0;} else {f[1] = gain; gain = 0.0;}
280 if (gain > 1.0) {f[2] = 1.0; gain -= 1.0;} else {f[2] = gain; gain = 0.0;}
281 if (gain > 1.0) {f[3] = 1.0; gain -= 1.0;} else {f[3] = gain; gain = 0.0;}
282 if (gain > 1.0) {f[4] = 1.0; gain -= 1.0;} else {f[4] = gain; gain = 0.0;}
283 if (gain > 1.0) {f[5] = 1.0; gain -= 1.0;} else {f[5] = gain; gain = 0.0;}
284 if (gain > 1.0) {f[6] = 1.0; gain -= 1.0;} else {f[6] = gain; gain = 0.0;}
285 if (gain > 1.0) {f[7] = 1.0; gain -= 1.0;} else {f[7] = gain; gain = 0.0;}
286 if (gain > 1.0) {f[8] = 1.0; gain -= 1.0;} else {f[8] = gain; gain = 0.0;}
287 if (gain > 1.0) {f[9] = 1.0; gain -= 1.0;} else {f[9] = gain; gain = 0.0;}
288 //there, now we have a neat little moving average with remainders
289 if (treble < 1.0) treble = 1.0;
290 f[0] /= treble;
291 f[1] /= treble;
292 f[2] /= treble;
293 f[3] /= treble;
294 f[4] /= treble;
295 f[5] /= treble;
296 f[6] /= treble;
297 f[7] /= treble;
298 f[8] /= treble;
299 f[9] /= treble;
300 //and now it's neatly scaled, too
301
302 while (--sampleFrames >= 0)
303 {
304 long double inputSampleL = *in1;
305 long double inputSampleR = *in2;
306
307 if (input != 1.0) {inputSampleL *= input; inputSampleR *= input;}
308
309 static int noisesourceL = 0;
310 static int noisesourceR = 850010;
311 int residue;
312 double applyresidue;
313
314 noisesourceL = noisesourceL % 1700021; noisesourceL++;
315 residue = noisesourceL * noisesourceL;
316 residue = residue % 170003; residue *= residue;
317 residue = residue % 17011; residue *= residue;
318 residue = residue % 1709; residue *= residue;
319 residue = residue % 173; residue *= residue;
320 residue = residue % 17;
321 applyresidue = residue;
322 applyresidue *= 0.00000001;
323 applyresidue *= 0.00000001;
324 inputSampleL += applyresidue;
325 if (inputSampleL<1.2e-38 && -inputSampleL<1.2e-38) {
326 inputSampleL -= applyresidue;
327 }
328
329 noisesourceR = noisesourceR % 1700021; noisesourceR++;
330 residue = noisesourceR * noisesourceR;
331 residue = residue % 170003; residue *= residue;
332 residue = residue % 17011; residue *= residue;
333 residue = residue % 1709; residue *= residue;
334 residue = residue % 173; residue *= residue;
335 residue = residue % 17;
336 applyresidue = residue;
337 applyresidue *= 0.00000001;
338 applyresidue *= 0.00000001;
339 inputSampleR += applyresidue;
340 if (inputSampleR<1.2e-38 && -inputSampleR<1.2e-38) {
341 inputSampleR -= applyresidue;
342 }
343 //for live air, we always apply the dither noise. Then, if our result is
344 //effectively digital black, we'll subtract it aUnBox. We want a 'air' hiss
345 long double drySampleL = inputSampleL;
346 long double drySampleR = inputSampleR;
347
348 aL[4] = aL[3]; aL[3] = aL[2]; aL[2] = aL[1];
349 aL[1] = aL[0]; aL[0] = inputSampleL;
350 inputSampleL *= e[0];
351 inputSampleL += (aL[1] * e[1]);
352 inputSampleL += (aL[2] * e[2]);
353 inputSampleL += (aL[3] * e[3]);
354 inputSampleL += (aL[4] * e[4]);
355 //this is now an average of inputSampleL
356
357 aR[4] = aR[3]; aR[3] = aR[2]; aR[2] = aR[1];
358 aR[1] = aR[0]; aR[0] = inputSampleR;
359 inputSampleR *= e[0];
360 inputSampleR += (aR[1] * e[1]);
361 inputSampleR += (aR[2] * e[2]);
362 inputSampleR += (aR[3] * e[3]);
363 inputSampleR += (aR[4] * e[4]);
364 //this is now an average of inputSampleR
365
366 bL[4] = bL[3]; bL[3] = bL[2]; bL[2] = bL[1];
367 bL[1] = bL[0]; bL[0] = inputSampleL;
368 inputSampleL *= e[0];
369 inputSampleL += (bL[1] * e[1]);
370 inputSampleL += (bL[2] * e[2]);
371 inputSampleL += (bL[3] * e[3]);
372 inputSampleL += (bL[4] * e[4]);
373 //this is now an average of an average of inputSampleL. Two poles
374
375 bR[4] = bR[3]; bR[3] = bR[2]; bR[2] = bR[1];
376 bR[1] = bR[0]; bR[0] = inputSampleR;
377 inputSampleR *= e[0];
378 inputSampleR += (bR[1] * e[1]);
379 inputSampleR += (bR[2] * e[2]);
380 inputSampleR += (bR[3] * e[3]);
381 inputSampleR += (bR[4] * e[4]);
382 //this is now an average of an average of inputSampleR. Two poles
383
384 inputSampleL *= unbox;
385 inputSampleR *= unbox;
386 //clip to 1.2533141373155 to reach maximum output
387 if (inputSampleL > 1.2533141373155) inputSampleL = 1.2533141373155;
388 if (inputSampleL < -1.2533141373155) inputSampleL = -1.2533141373155;
389 inputSampleL = sin(inputSampleL * fabs(inputSampleL)) / ((inputSampleL == 0.0) ?1:fabs(inputSampleL));
390
391 if (inputSampleR > 1.2533141373155) inputSampleR = 1.2533141373155;
392 if (inputSampleR < -1.2533141373155) inputSampleR = -1.2533141373155;
393 inputSampleR = sin(inputSampleR * fabs(inputSampleR)) / ((inputSampleR == 0.0) ?1:fabs(inputSampleR));
394
395 inputSampleL /= unbox;
396 inputSampleR /= unbox;
397 //now we have a distorted inputSample at the correct volume relative to drySample
398
399 long double accumulatorSampleL = (drySampleL - inputSampleL);
400 cL[9] = cL[8]; cL[8] = cL[7]; cL[7] = cL[6]; cL[6] = cL[5];
401 cL[5] = cL[4]; cL[4] = cL[3]; cL[3] = cL[2]; cL[2] = cL[1];
402 cL[1] = cL[0]; cL[0] = accumulatorSampleL;
403 accumulatorSampleL *= f[0];
404 accumulatorSampleL += (cL[1] * f[1]);
405 accumulatorSampleL += (cL[2] * f[2]);
406 accumulatorSampleL += (cL[3] * f[3]);
407 accumulatorSampleL += (cL[4] * f[4]);
408 accumulatorSampleL += (cL[5] * f[5]);
409 accumulatorSampleL += (cL[6] * f[6]);
410 accumulatorSampleL += (cL[7] * f[7]);
411 accumulatorSampleL += (cL[8] * f[8]);
412 accumulatorSampleL += (cL[9] * f[9]);
413 //this is now an average of all the recent variances from dry
414
415 long double accumulatorSampleR = (drySampleR - inputSampleR);
416 cR[9] = cR[8]; cR[8] = cR[7]; cR[7] = cR[6]; cR[6] = cR[5];
417 cR[5] = cR[4]; cR[4] = cR[3]; cR[3] = cR[2]; cR[2] = cR[1];
418 cR[1] = cR[0]; cR[0] = accumulatorSampleR;
419 accumulatorSampleR *= f[0];
420 accumulatorSampleR += (cR[1] * f[1]);
421 accumulatorSampleR += (cR[2] * f[2]);
422 accumulatorSampleR += (cR[3] * f[3]);
423 accumulatorSampleR += (cR[4] * f[4]);
424 accumulatorSampleR += (cR[5] * f[5]);
425 accumulatorSampleR += (cR[6] * f[6]);
426 accumulatorSampleR += (cR[7] * f[7]);
427 accumulatorSampleR += (cR[8] * f[8]);
428 accumulatorSampleR += (cR[9] * f[9]);
429 //this is now an average of all the recent variances from dry
430
431 iirSampleAL = (iirSampleAL * (1 - iirAmount)) + (accumulatorSampleL * iirAmount);
432 accumulatorSampleL -= iirSampleAL;
433 //two poles of IIR
434
435 iirSampleAR = (iirSampleAR * (1 - iirAmount)) + (accumulatorSampleR * iirAmount);
436 accumulatorSampleR -= iirSampleAR;
437 //two poles of IIR
438
439 iirSampleBL = (iirSampleBL * (1 - iirAmount)) + (accumulatorSampleL * iirAmount);
440 accumulatorSampleL -= iirSampleBL;
441 //highpass section
442
443 iirSampleBR = (iirSampleBR * (1 - iirAmount)) + (accumulatorSampleR * iirAmount);
444 accumulatorSampleR -= iirSampleBR;
445 //highpass section
446 //this is now a highpassed average of all the recent variances from dry
447
448 inputSampleL = drySampleL - accumulatorSampleL;
449 inputSampleR = drySampleR - accumulatorSampleR;
450 //we apply it as one operation, to get the result.
451
452 if (output != 1.0) {inputSampleL *= output; inputSampleR *= output;}
453
454 //stereo 64 bit dither, made small and tidy.
455 int expon; frexp((double)inputSampleL, &expon);
456 long double dither = (rand()/(RAND_MAX*7.737125245533627e+25))*pow(2,expon+62);
457 dither /= 536870912.0; //needs this to scale to 64 bit zone
458 inputSampleL += (dither-fpNShapeL); fpNShapeL = dither;
459 frexp((double)inputSampleR, &expon);
460 dither = (rand()/(RAND_MAX*7.737125245533627e+25))*pow(2,expon+62);
461 dither /= 536870912.0; //needs this to scale to 64 bit zone
462 inputSampleR += (dither-fpNShapeR); fpNShapeR = dither;
463 //end 64 bit dither
464
465 *out1 = inputSampleL;
466 *out2 = inputSampleR;
467
468 in1++;
469 in2++;
470 out1++;
471 out2++;
472 }
473 }
474
475
476 } // end namespace UnBox
477
478