aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/LinuxVST/src/ChromeOxide/ChromeOxideProc.cpp
blob: 6c2d99f533657825f5d79173c51b10ef3cb77813 (plain) (blame)
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
/* ========================================
 *  ChromeOxide - ChromeOxide.h
 *  Copyright (c) 2016 airwindows, All rights reserved
 * ======================================== */

#ifndef __ChromeOxide_H
#include "ChromeOxide.h"
#endif

void ChromeOxide::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames) 
{
    float* in1  =  inputs[0];
    float* in2  =  inputs[1];
    float* out1 = outputs[0];
    float* out2 = outputs[1];

	double overallscale = 1.0;
	overallscale /= 44100.0;
	overallscale *= getSampleRate();

	double bassSampleL;
	double bassSampleR;
	double randy;
	double bias = B/1.31578947368421;
	double intensity = 0.9+pow(A,2);
	double iirAmount = pow(1.0-(intensity/(10+(bias*4.0))),2)/overallscale;
	//make 10 higher for less trashy sound in high settings
	double bridgerectifier;
	double trebleGainTrim = 1.0;
	double indrive = 1.0;
	double densityA = (intensity*80)+1.0;
	double noise = intensity/(1.0+bias);
	double bassGainTrim = 1.0;
	double glitch = 0.0;
	bias *= overallscale;
	noise *= overallscale;
	
	if (intensity > 1.0)
	{
		glitch = intensity-1.0;
		indrive = intensity*intensity;
		bassGainTrim /= (intensity*intensity);
		trebleGainTrim = (intensity+1.0)/2.0;
	}
	//everything runs off Intensity now
    
    while (--sampleFrames >= 0)
    {
		long double inputSampleL = *in1;
		long double inputSampleR = *in2;
		if (fabs(inputSampleL)<1.18e-37) inputSampleL = fpd * 1.18e-37;
		if (fabs(inputSampleR)<1.18e-37) inputSampleR = fpd * 1.18e-37;
		
		inputSampleL *= indrive;
		inputSampleR *= indrive;
		bassSampleL = inputSampleL;
		bassSampleR = inputSampleR;
		
		if (flip)
		{
			iirSampleAL = (iirSampleAL * (1 - iirAmount)) + (inputSampleL * iirAmount);
			iirSampleAR = (iirSampleAR * (1 - iirAmount)) + (inputSampleR * iirAmount);
			inputSampleL -= iirSampleAL;
			inputSampleR -= iirSampleAR;
		}
		else
		{
			iirSampleBL = (iirSampleBL * (1 - iirAmount)) + (inputSampleL * iirAmount);
			iirSampleBR = (iirSampleBR * (1 - iirAmount)) + (inputSampleR * iirAmount);
			inputSampleL -= iirSampleBL;
			inputSampleR -= iirSampleBR;
		}
		//highpass section
		
		
		bassSampleL -= (inputSampleL * (fabs(inputSampleL) * glitch) * (fabs(inputSampleL) * glitch) );
		bassSampleR -= (inputSampleR * (fabs(inputSampleR) * glitch) * (fabs(inputSampleR) * glitch) );
		//overdrive the bass channel
		
		if (flip)
		{
			iirSampleCL = (iirSampleCL * (1 - iirAmount)) + (bassSampleL * iirAmount);
			iirSampleCR = (iirSampleCR * (1 - iirAmount)) + (bassSampleR * iirAmount);
			bassSampleL = iirSampleCL;
			bassSampleR = iirSampleCR;
		}
		else
		{
			iirSampleDL = (iirSampleDL * (1 - iirAmount)) + (bassSampleL * iirAmount);
			iirSampleDR = (iirSampleDR * (1 - iirAmount)) + (bassSampleR * iirAmount);
			bassSampleL = iirSampleDL;
			bassSampleR = iirSampleDR;
		}
		//bass filter same as high but only after the clipping
		flip = !flip;
		
		bridgerectifier = inputSampleL;
		//insanity check
		randy = bias+((rand()/(double)RAND_MAX)*noise);
		if ((randy >= 0.0)&&(randy < 1.0)) bridgerectifier = (inputSampleL * randy)+(secondSampleL * (1.0-randy));
		if ((randy >= 1.0)&&(randy < 2.0)) bridgerectifier = (secondSampleL * (randy-1.0))+(thirdSampleL * (2.0-randy));
		if ((randy >= 2.0)&&(randy < 3.0)) bridgerectifier = (thirdSampleL * (randy-2.0))+(fourthSampleL * (3.0-randy));
		if ((randy >= 3.0)&&(randy < 4.0)) bridgerectifier = (fourthSampleL * (randy-3.0))+(fifthSampleL * (4.0-randy));
		fifthSampleL = fourthSampleL;
		fourthSampleL = thirdSampleL;
		thirdSampleL = secondSampleL;
		secondSampleL = inputSampleL;
		//high freq noise/flutter section
		
		inputSampleL = bridgerectifier;
		//apply overall, or just to the distorted bit? if the latter, below says 'fabs bridgerectifier'

		bridgerectifier = inputSampleR;
		//insanity check
		randy = bias+((rand()/(double)RAND_MAX)*noise);
		if ((randy >= 0.0)&&(randy < 1.0)) bridgerectifier = (inputSampleR * randy)+(secondSampleR * (1.0-randy));
		if ((randy >= 1.0)&&(randy < 2.0)) bridgerectifier = (secondSampleR * (randy-1.0))+(thirdSampleR * (2.0-randy));
		if ((randy >= 2.0)&&(randy < 3.0)) bridgerectifier = (thirdSampleR * (randy-2.0))+(fourthSampleR * (3.0-randy));
		if ((randy >= 3.0)&&(randy < 4.0)) bridgerectifier = (fourthSampleR * (randy-3.0))+(fifthSampleR * (4.0-randy));
		fifthSampleR = fourthSampleR;
		fourthSampleR = thirdSampleR;
		thirdSampleR = secondSampleR;
		secondSampleR = inputSampleR;
		//high freq noise/flutter section
		
		inputSampleR = bridgerectifier;
		//apply overall, or just to the distorted bit? if the latter, below says 'fabs bridgerectifier'
		
		bridgerectifier = fabs(inputSampleL)*densityA;
		if (bridgerectifier > 1.57079633) bridgerectifier = 1.57079633;
		//max value for sine function
		bridgerectifier = sin(bridgerectifier);
		if (inputSampleL > 0) inputSampleL = bridgerectifier/densityA;
		else inputSampleL = -bridgerectifier/densityA;
		//blend according to densityA control

		bridgerectifier = fabs(inputSampleR)*densityA;
		if (bridgerectifier > 1.57079633) bridgerectifier = 1.57079633;
		//max value for sine function
		bridgerectifier = sin(bridgerectifier);
		if (inputSampleR > 0) inputSampleR = bridgerectifier/densityA;
		else inputSampleR = -bridgerectifier/densityA;
		//blend according to densityA control
		
		inputSampleL *= trebleGainTrim;
		inputSampleR *= trebleGainTrim;
		bassSampleL *= bassGainTrim;
		bassSampleR *= bassGainTrim;
		inputSampleL += bassSampleL;
		inputSampleR += bassSampleR;
		//that simple.
		
		//begin 32 bit stereo floating point dither
		int expon; frexpf((float)inputSampleL, &expon);
		fpd ^= fpd << 13; fpd ^= fpd >> 17; fpd ^= fpd << 5;
		inputSampleL += ((double(fpd)-uint32_t(0x7fffffff)) * 5.5e-36l * pow(2,expon+62));
		frexpf((float)inputSampleR, &expon);
		fpd ^= fpd << 13; fpd ^= fpd >> 17; fpd ^= fpd << 5;
		inputSampleR += ((double(fpd)-uint32_t(0x7fffffff)) * 5.5e-36l * pow(2,expon+62));
		//end 32 bit stereo floating point dither
		
		*out1 = inputSampleL;
		*out2 = inputSampleR;

		*in1++;
		*in2++;
		*out1++;
		*out2++;
    }
}

void ChromeOxide::processDoubleReplacing(double **inputs, double **outputs, VstInt32 sampleFrames) 
{
    double* in1  =  inputs[0];
    double* in2  =  inputs[1];
    double* out1 = outputs[0];
    double* out2 = outputs[1];

	double overallscale = 1.0;
	overallscale /= 44100.0;
	overallscale *= getSampleRate();
	double bassSampleL;
	double bassSampleR;
	double randy;
	double bias = B/1.31578947368421;
	double intensity = 0.9+pow(A,2);
	double iirAmount = pow(1.0-(intensity/(10+(bias*4.0))),2)/overallscale;
	//make 10 higher for less trashy sound in high settings
	double bridgerectifier;
	double trebleGainTrim = 1.0;
	double indrive = 1.0;
	double densityA = (intensity*80)+1.0;
	double noise = intensity/(1.0+bias);
	double bassGainTrim = 1.0;
	double glitch = 0.0;
	bias *= overallscale;
	noise *= overallscale;
	
	if (intensity > 1.0)
	{
		glitch = intensity-1.0;
		indrive = intensity*intensity;
		bassGainTrim /= (intensity*intensity);
		trebleGainTrim = (intensity+1.0)/2.0;
	}
	//everything runs off Intensity now
    
    while (--sampleFrames >= 0)
    {
		long double inputSampleL = *in1;
		long double inputSampleR = *in2;
		if (fabs(inputSampleL)<1.18e-43) inputSampleL = fpd * 1.18e-43;
		if (fabs(inputSampleR)<1.18e-43) inputSampleR = fpd * 1.18e-43;
		
		inputSampleL *= indrive;
		inputSampleR *= indrive;
		bassSampleL = inputSampleL;
		bassSampleR = inputSampleR;
		
		if (flip)
		{
			iirSampleAL = (iirSampleAL * (1 - iirAmount)) + (inputSampleL * iirAmount);
			iirSampleAR = (iirSampleAR * (1 - iirAmount)) + (inputSampleR * iirAmount);
			inputSampleL -= iirSampleAL;
			inputSampleR -= iirSampleAR;
		}
		else
		{
			iirSampleBL = (iirSampleBL * (1 - iirAmount)) + (inputSampleL * iirAmount);
			iirSampleBR = (iirSampleBR * (1 - iirAmount)) + (inputSampleR * iirAmount);
			inputSampleL -= iirSampleBL;
			inputSampleR -= iirSampleBR;
		}
		//highpass section
		
		
		bassSampleL -= (inputSampleL * (fabs(inputSampleL) * glitch) * (fabs(inputSampleL) * glitch) );
		bassSampleR -= (inputSampleR * (fabs(inputSampleR) * glitch) * (fabs(inputSampleR) * glitch) );
		//overdrive the bass channel
		
		if (flip)
		{
			iirSampleCL = (iirSampleCL * (1 - iirAmount)) + (bassSampleL * iirAmount);
			iirSampleCR = (iirSampleCR * (1 - iirAmount)) + (bassSampleR * iirAmount);
			bassSampleL = iirSampleCL;
			bassSampleR = iirSampleCR;
		}
		else
		{
			iirSampleDL = (iirSampleDL * (1 - iirAmount)) + (bassSampleL * iirAmount);
			iirSampleDR = (iirSampleDR * (1 - iirAmount)) + (bassSampleR * iirAmount);
			bassSampleL = iirSampleDL;
			bassSampleR = iirSampleDR;
		}
		//bass filter same as high but only after the clipping
		flip = !flip;
		
		bridgerectifier = inputSampleL;
		//insanity check
		randy = bias+((rand()/(double)RAND_MAX)*noise);
		if ((randy >= 0.0)&&(randy < 1.0)) bridgerectifier = (inputSampleL * randy)+(secondSampleL * (1.0-randy));
		if ((randy >= 1.0)&&(randy < 2.0)) bridgerectifier = (secondSampleL * (randy-1.0))+(thirdSampleL * (2.0-randy));
		if ((randy >= 2.0)&&(randy < 3.0)) bridgerectifier = (thirdSampleL * (randy-2.0))+(fourthSampleL * (3.0-randy));
		if ((randy >= 3.0)&&(randy < 4.0)) bridgerectifier = (fourthSampleL * (randy-3.0))+(fifthSampleL * (4.0-randy));
		fifthSampleL = fourthSampleL;
		fourthSampleL = thirdSampleL;
		thirdSampleL = secondSampleL;
		secondSampleL = inputSampleL;
		//high freq noise/flutter section
		
		inputSampleL = bridgerectifier;
		//apply overall, or just to the distorted bit? if the latter, below says 'fabs bridgerectifier'
		
		bridgerectifier = inputSampleR;
		//insanity check
		randy = bias+((rand()/(double)RAND_MAX)*noise);
		if ((randy >= 0.0)&&(randy < 1.0)) bridgerectifier = (inputSampleR * randy)+(secondSampleR * (1.0-randy));
		if ((randy >= 1.0)&&(randy < 2.0)) bridgerectifier = (secondSampleR * (randy-1.0))+(thirdSampleR * (2.0-randy));
		if ((randy >= 2.0)&&(randy < 3.0)) bridgerectifier = (thirdSampleR * (randy-2.0))+(fourthSampleR * (3.0-randy));
		if ((randy >= 3.0)&&(randy < 4.0)) bridgerectifier = (fourthSampleR * (randy-3.0))+(fifthSampleR * (4.0-randy));
		fifthSampleR = fourthSampleR;
		fourthSampleR = thirdSampleR;
		thirdSampleR = secondSampleR;
		secondSampleR = inputSampleR;
		//high freq noise/flutter section
		
		inputSampleR = bridgerectifier;
		//apply overall, or just to the distorted bit? if the latter, below says 'fabs bridgerectifier'
		
		bridgerectifier = fabs(inputSampleL)*densityA;
		if (bridgerectifier > 1.57079633) bridgerectifier = 1.57079633;
		//max value for sine function
		bridgerectifier = sin(bridgerectifier);
		if (inputSampleL > 0) inputSampleL = bridgerectifier/densityA;
		else inputSampleL = -bridgerectifier/densityA;
		//blend according to densityA control
		
		bridgerectifier = fabs(inputSampleR)*densityA;
		if (bridgerectifier > 1.57079633) bridgerectifier = 1.57079633;
		//max value for sine function
		bridgerectifier = sin(bridgerectifier);
		if (inputSampleR > 0) inputSampleR = bridgerectifier/densityA;
		else inputSampleR = -bridgerectifier/densityA;
		//blend according to densityA control
		
		inputSampleL *= trebleGainTrim;
		inputSampleR *= trebleGainTrim;
		bassSampleL *= bassGainTrim;
		bassSampleR *= bassGainTrim;
		inputSampleL += bassSampleL;
		inputSampleR += bassSampleR;
		//that simple.
		
		//begin 64 bit stereo floating point dither
		int expon; frexp((double)inputSampleL, &expon);
		fpd ^= fpd << 13; fpd ^= fpd >> 17; fpd ^= fpd << 5;
		inputSampleL += ((double(fpd)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
		frexp((double)inputSampleR, &expon);
		fpd ^= fpd << 13; fpd ^= fpd >> 17; fpd ^= fpd << 5;
		inputSampleR += ((double(fpd)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
		//end 64 bit stereo floating point dither
		
		*out1 = inputSampleL;
		*out2 = inputSampleR;

		*in1++;
		*in2++;
		*out1++;
		*out2++;
    }
}