airwindows/plugins/WinVST/uLawEncode/uLawEncodeProc.cpp
Chris Johnson 448b2472bd uLaw
2018-10-14 19:26:42 -04:00

228 lines
7.4 KiB
C++
Executable file

/* ========================================
* uLawEncode - uLawEncode.h
* Copyright (c) 2016 airwindows, All rights reserved
* ======================================== */
#ifndef __uLawEncode_H
#include "uLawEncode.h"
#endif
void uLawEncode::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 gain = A;
double wet = B;
double dry = 1.0 - wet;
while (--sampleFrames >= 0)
{
long double inputSampleL = *in1;
long double inputSampleR = *in2;
static int noisesourceL = 0;
static int noisesourceR = 850010;
int residue;
double applyresidue;
noisesourceL = noisesourceL % 1700021; noisesourceL++;
residue = noisesourceL * noisesourceL;
residue = residue % 170003; residue *= residue;
residue = residue % 17011; residue *= residue;
residue = residue % 1709; residue *= residue;
residue = residue % 173; residue *= residue;
residue = residue % 17;
applyresidue = residue;
applyresidue *= 0.00000001;
applyresidue *= 0.00000001;
inputSampleL += applyresidue;
if (inputSampleL<1.2e-38 && -inputSampleL<1.2e-38) {
inputSampleL -= applyresidue;
}
noisesourceR = noisesourceR % 1700021; noisesourceR++;
residue = noisesourceR * noisesourceR;
residue = residue % 170003; residue *= residue;
residue = residue % 17011; residue *= residue;
residue = residue % 1709; residue *= residue;
residue = residue % 173; residue *= residue;
residue = residue % 17;
applyresidue = residue;
applyresidue *= 0.00000001;
applyresidue *= 0.00000001;
inputSampleR += applyresidue;
if (inputSampleR<1.2e-38 && -inputSampleR<1.2e-38) {
inputSampleR -= applyresidue;
}
//for live air, we always apply the dither noise. Then, if our result is
//effectively digital black, we'll subtract it auLawEncode. We want a 'air' hiss
double drySampleL = inputSampleL;
double drySampleR = inputSampleR;
if (gain != 1.0) {
inputSampleL *= gain;
inputSampleR *= gain;
}
if (inputSampleL > 1.0) inputSampleL = 1.0;
if (inputSampleL < -1.0) inputSampleL = -1.0;
if (inputSampleR > 1.0) inputSampleR = 1.0;
if (inputSampleR < -1.0) inputSampleR = -1.0;
if (inputSampleL > 0) inputSampleL = log(1.0+(255*fabs(inputSampleL))) / log(256);
if (inputSampleL < 0) inputSampleL = -log(1.0+(255*fabs(inputSampleL))) / log(256);
if (inputSampleR > 0) inputSampleR = log(1.0+(255*fabs(inputSampleR))) / log(256);
if (inputSampleR < 0) inputSampleR = -log(1.0+(255*fabs(inputSampleR))) / log(256);
if (wet !=1.0) {
inputSampleL = (inputSampleL * wet) + (drySampleL * dry);
inputSampleR = (inputSampleR * wet) + (drySampleR * dry);
}
//noise shaping to 32-bit floating point
float fpTemp = inputSampleL;
fpNShapeL += (inputSampleL-fpTemp);
inputSampleL += fpNShapeL;
//if this confuses you look at the wordlength for fpTemp :)
fpTemp = inputSampleR;
fpNShapeR += (inputSampleR-fpTemp);
inputSampleR += fpNShapeR;
//for deeper space and warmth, we try a non-oscillating noise shaping
//that is kind of ruthless: it will forever retain the rounding errors
//except we'll dial it back a hair at the end of every buffer processed
//end noise shaping on 32 bit output
*out1 = inputSampleL;
*out2 = inputSampleR;
*in1++;
*in2++;
*out1++;
*out2++;
}
fpNShapeL *= 0.999999;
fpNShapeR *= 0.999999;
//we will just delicately dial back the FP noise shaping, not even every sample
//this is a good place to put subtle 'no runaway' calculations, though bear in mind
//that it will be called more often when you use shorter sample buffers in the DAW.
//So, very low latency operation will call these calculations more often.
}
void uLawEncode::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 gain = A;
double wet = B;
double dry = 1.0 - wet;
while (--sampleFrames >= 0)
{
long double inputSampleL = *in1;
long double inputSampleR = *in2;
static int noisesourceL = 0;
static int noisesourceR = 850010;
int residue;
double applyresidue;
noisesourceL = noisesourceL % 1700021; noisesourceL++;
residue = noisesourceL * noisesourceL;
residue = residue % 170003; residue *= residue;
residue = residue % 17011; residue *= residue;
residue = residue % 1709; residue *= residue;
residue = residue % 173; residue *= residue;
residue = residue % 17;
applyresidue = residue;
applyresidue *= 0.00000001;
applyresidue *= 0.00000001;
inputSampleL += applyresidue;
if (inputSampleL<1.2e-38 && -inputSampleL<1.2e-38) {
inputSampleL -= applyresidue;
}
noisesourceR = noisesourceR % 1700021; noisesourceR++;
residue = noisesourceR * noisesourceR;
residue = residue % 170003; residue *= residue;
residue = residue % 17011; residue *= residue;
residue = residue % 1709; residue *= residue;
residue = residue % 173; residue *= residue;
residue = residue % 17;
applyresidue = residue;
applyresidue *= 0.00000001;
applyresidue *= 0.00000001;
inputSampleR += applyresidue;
if (inputSampleR<1.2e-38 && -inputSampleR<1.2e-38) {
inputSampleR -= applyresidue;
}
//for live air, we always apply the dither noise. Then, if our result is
//effectively digital black, we'll subtract it auLawEncode. We want a 'air' hiss
double drySampleL = inputSampleL;
double drySampleR = inputSampleR;
if (gain != 1.0) {
inputSampleL *= gain;
inputSampleR *= gain;
}
if (inputSampleL > 1.0) inputSampleL = 1.0;
if (inputSampleL < -1.0) inputSampleL = -1.0;
if (inputSampleR > 1.0) inputSampleR = 1.0;
if (inputSampleR < -1.0) inputSampleR = -1.0;
if (inputSampleL > 0) inputSampleL = log(1.0+(255*fabs(inputSampleL))) / log(256);
if (inputSampleL < 0) inputSampleL = -log(1.0+(255*fabs(inputSampleL))) / log(256);
if (inputSampleR > 0) inputSampleR = log(1.0+(255*fabs(inputSampleR))) / log(256);
if (inputSampleR < 0) inputSampleR = -log(1.0+(255*fabs(inputSampleR))) / log(256);
if (wet !=1.0) {
inputSampleL = (inputSampleL * wet) + (drySampleL * dry);
inputSampleR = (inputSampleR * wet) + (drySampleR * dry);
}
//noise shaping to 64-bit floating point
double fpTemp = inputSampleL;
fpNShapeL += (inputSampleL-fpTemp);
inputSampleL += fpNShapeL;
//if this confuses you look at the wordlength for fpTemp :)
fpTemp = inputSampleR;
fpNShapeR += (inputSampleR-fpTemp);
inputSampleR += fpNShapeR;
//for deeper space and warmth, we try a non-oscillating noise shaping
//that is kind of ruthless: it will forever retain the rounding errors
//except we'll dial it back a hair at the end of every buffer processed
//end noise shaping on 64 bit output
*out1 = inputSampleL;
*out2 = inputSampleR;
*in1++;
*in2++;
*out1++;
*out2++;
}
fpNShapeL *= 0.999999;
fpNShapeR *= 0.999999;
//we will just delicately dial back the FP noise shaping, not even every sample
//this is a good place to put subtle 'no runaway' calculations, though bear in mind
//that it will be called more often when you use shorter sample buffers in the DAW.
//So, very low latency operation will call these calculations more often.
}