aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/MacVST/DitherMeTimbers/source/DitherMeTimbersProc.cpp
blob: 7ba84d77e6cf63987393f198bdf54fe38211a11f (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
/* ========================================
 *  DitherMeTimbers - DitherMeTimbers.h
 *  Copyright (c) 2016 airwindows, All rights reserved
 * ======================================== */

#ifndef __DitherMeTimbers_H
#include "DitherMeTimbers.h"
#endif

void DitherMeTimbers::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames) 
{
    float* in1  =  inputs[0];
    float* in2  =  inputs[1];
    float* out1 = outputs[0];
    float* out2 = outputs[1];
    
    while (--sampleFrames >= 0)
    {
		double inputSampleL = *in1 * 8388608.0;
		double inputSampleR = *in2 * 8388608.0;
		double outputSampleL;
		double outputSampleR;
		
		lastSampleL -= (noiseShapingL*0.125);
		lastSampleR -= (noiseShapingR*0.125);

		if ((lastSampleL+lastSampleL) >= (inputSampleL+lastSample2L)) outputSampleL = floor(lastSampleL);
		else outputSampleL = floor(lastSampleL+1.0); //round down or up based on whether it softens treble angles

		if ((lastSampleR+lastSampleR) >= (inputSampleR+lastSample2R)) outputSampleR = floor(lastSampleR);
		else outputSampleR = floor(lastSampleR+1.0); //round down or up based on whether it softens treble angles

		lastSample2L = lastSampleL;
		lastSampleL = inputSampleL; //we retain three samples in a row

		lastSample2R = lastSampleR;
		lastSampleR = inputSampleR; //we retain three samples in a row
		
		noiseShapingL += outputSampleL;
		noiseShapingL -= lastSampleL;
		
		noiseShapingR += outputSampleR;
		noiseShapingR -= lastSampleR;

		*out1 = outputSampleL / 8388608.0;
		*out2 = outputSampleR / 8388608.0;

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

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

    while (--sampleFrames >= 0)
    {
		double inputSampleL = *in1 * 8388608.0;
		double inputSampleR = *in2 * 8388608.0;
		double outputSampleL;
		double outputSampleR;
		
		lastSampleL -= (noiseShapingL*0.125);
		lastSampleR -= (noiseShapingR*0.125);
		
		if ((lastSampleL+lastSampleL) >= (inputSampleL+lastSample2L)) outputSampleL = floor(lastSampleL);
		else outputSampleL = floor(lastSampleL+1.0); //round down or up based on whether it softens treble angles
		
		if ((lastSampleR+lastSampleR) >= (inputSampleR+lastSample2R)) outputSampleR = floor(lastSampleR);
		else outputSampleR = floor(lastSampleR+1.0); //round down or up based on whether it softens treble angles
		
		lastSample2L = lastSampleL;
		lastSampleL = inputSampleL; //we retain three samples in a row
		
		lastSample2R = lastSampleR;
		lastSampleR = inputSampleR; //we retain three samples in a row
		
		noiseShapingL += outputSampleL;
		noiseShapingL -= lastSampleL;
		
		noiseShapingR += outputSampleR;
		noiseShapingR -= lastSampleR;
		
		*out1 = outputSampleL / 8388608.0;
		*out2 = outputSampleR / 8388608.0;

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