mirror of
https://github.com/airwindows/airwindows.git
synced 2026-05-16 06:05:55 -06:00
218 lines
9.5 KiB
C++
Executable file
218 lines
9.5 KiB
C++
Executable file
/* ========================================
|
|
* BeziComp - BeziComp.h
|
|
* Copyright (c) airwindows, Airwindows uses the MIT license
|
|
* ======================================== */
|
|
|
|
#ifndef __BeziComp_H
|
|
#include "BeziComp.h"
|
|
#endif
|
|
|
|
void BeziComp::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames)
|
|
{
|
|
float* in1 = inputs[0];
|
|
float* in2 = inputs[1];
|
|
float* out1 = outputs[0];
|
|
float* out2 = outputs[1];
|
|
|
|
VstInt32 inFramesToProcess = sampleFrames; //vst doesn't give us this as a separate variable so we'll make it
|
|
double overallscale = 1.0;
|
|
overallscale /= 44100.0;
|
|
overallscale *= getSampleRate();
|
|
int spacing = floor(overallscale); //should give us working basic scaling, usually 2 or 4
|
|
if (spacing < 1) spacing = 1; if (spacing > 16) spacing = 16;
|
|
|
|
double bezCThresh = pow(A,2.0) * 64.0;
|
|
double bezMakeUp = sqrt(bezCThresh+1.0);
|
|
double bezRez = (pow(B,6.0)+0.0001)/overallscale; if (bezRez > 1.0) bezRez = 1.0;
|
|
double wet = C;
|
|
|
|
while (--sampleFrames >= 0)
|
|
{
|
|
double inputSampleL = *in1;
|
|
double inputSampleR = *in2;
|
|
if (fabs(inputSampleL)<1.18e-23) inputSampleL = fpdL * 1.18e-17;
|
|
if (fabs(inputSampleR)<1.18e-23) inputSampleR = fpdR * 1.18e-17;
|
|
double drySampleL = inputSampleL;
|
|
double drySampleR = inputSampleR;
|
|
|
|
bezComp[bez_cycle] += bezRez;
|
|
bezComp[bez_SampL] += (fmax(fabs(inputSampleL),fabs(inputSampleR)) * bezRez);
|
|
|
|
if (bezComp[bez_cycle] > 1.0) {
|
|
bezComp[bez_cycle] -= 1.0;
|
|
bezComp[bez_CL] = bezComp[bez_BL];
|
|
bezComp[bez_BL] = bezComp[bez_AL];
|
|
bezComp[bez_AL] = bezComp[bez_SampL];
|
|
bezComp[bez_SampL] = 0.0;
|
|
}
|
|
double CBL = (bezComp[bez_CL]*(1.0-bezComp[bez_cycle]))+(bezComp[bez_BL]*bezComp[bez_cycle]);
|
|
double BAL = (bezComp[bez_BL]*(1.0-bezComp[bez_cycle]))+(bezComp[bez_AL]*bezComp[bez_cycle]);
|
|
double CBAL = (bezComp[bez_BL]+(CBL*(1.0-bezComp[bez_cycle]))+(BAL*bezComp[bez_cycle]))*0.5;
|
|
|
|
inputSampleL *= 1.0-(fmin(CBAL*bezCThresh,1.0));
|
|
inputSampleL *= bezMakeUp;
|
|
inputSampleR *= 1.0-(fmin(CBAL*bezCThresh,1.0));
|
|
inputSampleR *= bezMakeUp;
|
|
|
|
if (wet < 1.0) {
|
|
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0-wet));
|
|
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0-wet));
|
|
}
|
|
|
|
//begin ClipOnly2 stereo as a little, compressed chunk that can be dropped into code
|
|
if (inputSampleL > 4.0) inputSampleL = 4.0; if (inputSampleL < -4.0) inputSampleL = -4.0;
|
|
if (wasPosClipL == true) { //current will be over
|
|
if (inputSampleL<lastSampleL) lastSampleL=0.7058208+(inputSampleL*0.2609148);
|
|
else lastSampleL = 0.2491717+(lastSampleL*0.7390851);
|
|
} wasPosClipL = false;
|
|
if (inputSampleL>0.9549925859) {wasPosClipL=true;inputSampleL=0.7058208+(lastSampleL*0.2609148);}
|
|
if (wasNegClipL == true) { //current will be -over
|
|
if (inputSampleL > lastSampleL) lastSampleL=-0.7058208+(inputSampleL*0.2609148);
|
|
else lastSampleL=-0.2491717+(lastSampleL*0.7390851);
|
|
} wasNegClipL = false;
|
|
if (inputSampleL<-0.9549925859) {wasNegClipL=true;inputSampleL=-0.7058208+(lastSampleL*0.2609148);}
|
|
intermediateL[spacing] = inputSampleL;
|
|
inputSampleL = lastSampleL; //Latency is however many samples equals one 44.1k sample
|
|
for (int x = spacing; x > 0; x--) intermediateL[x-1] = intermediateL[x];
|
|
lastSampleL = intermediateL[0]; //run a little buffer to handle this
|
|
|
|
if (inputSampleR > 4.0) inputSampleR = 4.0; if (inputSampleR < -4.0) inputSampleR = -4.0;
|
|
if (wasPosClipR == true) { //current will be over
|
|
if (inputSampleR<lastSampleR) lastSampleR=0.7058208+(inputSampleR*0.2609148);
|
|
else lastSampleR = 0.2491717+(lastSampleR*0.7390851);
|
|
} wasPosClipR = false;
|
|
if (inputSampleR>0.9549925859) {wasPosClipR=true;inputSampleR=0.7058208+(lastSampleR*0.2609148);}
|
|
if (wasNegClipR == true) { //current will be -over
|
|
if (inputSampleR > lastSampleR) lastSampleR=-0.7058208+(inputSampleR*0.2609148);
|
|
else lastSampleR=-0.2491717+(lastSampleR*0.7390851);
|
|
} wasNegClipR = false;
|
|
if (inputSampleR<-0.9549925859) {wasNegClipR=true;inputSampleR=-0.7058208+(lastSampleR*0.2609148);}
|
|
intermediateR[spacing] = inputSampleR;
|
|
inputSampleR = lastSampleR; //Latency is however many samples equals one 44.1k sample
|
|
for (int x = spacing; x > 0; x--) intermediateR[x-1] = intermediateR[x];
|
|
lastSampleR = intermediateR[0]; //run a little buffer to handle this
|
|
//end ClipOnly2 stereo as a little, compressed chunk that can be dropped into code
|
|
|
|
//begin 32 bit stereo floating point dither
|
|
int expon; frexpf((float)inputSampleL, &expon);
|
|
fpdL ^= fpdL << 13; fpdL ^= fpdL >> 17; fpdL ^= fpdL << 5;
|
|
inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 5.5e-36l * pow(2,expon+62));
|
|
frexpf((float)inputSampleR, &expon);
|
|
fpdR ^= fpdR << 13; fpdR ^= fpdR >> 17; fpdR ^= fpdR << 5;
|
|
inputSampleR += ((double(fpdR)-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 BeziComp::processDoubleReplacing(double **inputs, double **outputs, VstInt32 sampleFrames)
|
|
{
|
|
double* in1 = inputs[0];
|
|
double* in2 = inputs[1];
|
|
double* out1 = outputs[0];
|
|
double* out2 = outputs[1];
|
|
|
|
VstInt32 inFramesToProcess = sampleFrames; //vst doesn't give us this as a separate variable so we'll make it
|
|
double overallscale = 1.0;
|
|
overallscale /= 44100.0;
|
|
overallscale *= getSampleRate();
|
|
int spacing = floor(overallscale); //should give us working basic scaling, usually 2 or 4
|
|
if (spacing < 1) spacing = 1; if (spacing > 16) spacing = 16;
|
|
|
|
double bezCThresh = pow(A,2.0) * 64.0;
|
|
double bezMakeUp = sqrt(bezCThresh+1.0);
|
|
double bezRez = (pow(B,6.0)+0.0001)/overallscale; if (bezRez > 1.0) bezRez = 1.0;
|
|
double wet = C;
|
|
|
|
while (--sampleFrames >= 0)
|
|
{
|
|
double inputSampleL = *in1;
|
|
double inputSampleR = *in2;
|
|
if (fabs(inputSampleL)<1.18e-23) inputSampleL = fpdL * 1.18e-17;
|
|
if (fabs(inputSampleR)<1.18e-23) inputSampleR = fpdR * 1.18e-17;
|
|
double drySampleL = inputSampleL;
|
|
double drySampleR = inputSampleR;
|
|
|
|
bezComp[bez_cycle] += bezRez;
|
|
bezComp[bez_SampL] += (fmax(fabs(inputSampleL),fabs(inputSampleR)) * bezRez);
|
|
|
|
if (bezComp[bez_cycle] > 1.0) {
|
|
bezComp[bez_cycle] -= 1.0;
|
|
bezComp[bez_CL] = bezComp[bez_BL];
|
|
bezComp[bez_BL] = bezComp[bez_AL];
|
|
bezComp[bez_AL] = bezComp[bez_SampL];
|
|
bezComp[bez_SampL] = 0.0;
|
|
}
|
|
double CBL = (bezComp[bez_CL]*(1.0-bezComp[bez_cycle]))+(bezComp[bez_BL]*bezComp[bez_cycle]);
|
|
double BAL = (bezComp[bez_BL]*(1.0-bezComp[bez_cycle]))+(bezComp[bez_AL]*bezComp[bez_cycle]);
|
|
double CBAL = (bezComp[bez_BL]+(CBL*(1.0-bezComp[bez_cycle]))+(BAL*bezComp[bez_cycle]))*0.5;
|
|
|
|
inputSampleL *= 1.0-(fmin(CBAL*bezCThresh,1.0));
|
|
inputSampleL *= bezMakeUp;
|
|
inputSampleR *= 1.0-(fmin(CBAL*bezCThresh,1.0));
|
|
inputSampleR *= bezMakeUp;
|
|
|
|
if (wet < 1.0) {
|
|
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0-wet));
|
|
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0-wet));
|
|
}
|
|
|
|
//begin ClipOnly2 stereo as a little, compressed chunk that can be dropped into code
|
|
if (inputSampleL > 4.0) inputSampleL = 4.0; if (inputSampleL < -4.0) inputSampleL = -4.0;
|
|
if (wasPosClipL == true) { //current will be over
|
|
if (inputSampleL<lastSampleL) lastSampleL=0.7058208+(inputSampleL*0.2609148);
|
|
else lastSampleL = 0.2491717+(lastSampleL*0.7390851);
|
|
} wasPosClipL = false;
|
|
if (inputSampleL>0.9549925859) {wasPosClipL=true;inputSampleL=0.7058208+(lastSampleL*0.2609148);}
|
|
if (wasNegClipL == true) { //current will be -over
|
|
if (inputSampleL > lastSampleL) lastSampleL=-0.7058208+(inputSampleL*0.2609148);
|
|
else lastSampleL=-0.2491717+(lastSampleL*0.7390851);
|
|
} wasNegClipL = false;
|
|
if (inputSampleL<-0.9549925859) {wasNegClipL=true;inputSampleL=-0.7058208+(lastSampleL*0.2609148);}
|
|
intermediateL[spacing] = inputSampleL;
|
|
inputSampleL = lastSampleL; //Latency is however many samples equals one 44.1k sample
|
|
for (int x = spacing; x > 0; x--) intermediateL[x-1] = intermediateL[x];
|
|
lastSampleL = intermediateL[0]; //run a little buffer to handle this
|
|
|
|
if (inputSampleR > 4.0) inputSampleR = 4.0; if (inputSampleR < -4.0) inputSampleR = -4.0;
|
|
if (wasPosClipR == true) { //current will be over
|
|
if (inputSampleR<lastSampleR) lastSampleR=0.7058208+(inputSampleR*0.2609148);
|
|
else lastSampleR = 0.2491717+(lastSampleR*0.7390851);
|
|
} wasPosClipR = false;
|
|
if (inputSampleR>0.9549925859) {wasPosClipR=true;inputSampleR=0.7058208+(lastSampleR*0.2609148);}
|
|
if (wasNegClipR == true) { //current will be -over
|
|
if (inputSampleR > lastSampleR) lastSampleR=-0.7058208+(inputSampleR*0.2609148);
|
|
else lastSampleR=-0.2491717+(lastSampleR*0.7390851);
|
|
} wasNegClipR = false;
|
|
if (inputSampleR<-0.9549925859) {wasNegClipR=true;inputSampleR=-0.7058208+(lastSampleR*0.2609148);}
|
|
intermediateR[spacing] = inputSampleR;
|
|
inputSampleR = lastSampleR; //Latency is however many samples equals one 44.1k sample
|
|
for (int x = spacing; x > 0; x--) intermediateR[x-1] = intermediateR[x];
|
|
lastSampleR = intermediateR[0]; //run a little buffer to handle this
|
|
//end ClipOnly2 stereo as a little, compressed chunk that can be dropped into code
|
|
|
|
//begin 64 bit stereo floating point dither
|
|
//int expon; frexp((double)inputSampleL, &expon);
|
|
fpdL ^= fpdL << 13; fpdL ^= fpdL >> 17; fpdL ^= fpdL << 5;
|
|
//inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
|
|
//frexp((double)inputSampleR, &expon);
|
|
fpdR ^= fpdR << 13; fpdR ^= fpdR >> 17; fpdR ^= fpdR << 5;
|
|
//inputSampleR += ((double(fpdR)-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++;
|
|
}
|
|
}
|