clang format

This commit is contained in:
Chris
2023-08-12 09:49:26 +02:00
parent 537fba98c4
commit 044f42374b
22 changed files with 3090 additions and 3216 deletions

View File

@@ -1,2 +1,25 @@
---
BasedOnStyle: LLVM BasedOnStyle: LLVM
ColumnLimit: 140 ColumnLimit: "120"
ConstructorInitializerIndentWidth: "0"
IndentWidth: "4"
TabWidth: "4"
UseTab: Always
AccessModifierOffset: "-4"
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortBlocksOnASingleLine: Always
AllowShortLoopsOnASingleLine: true
AllowShortEnumsOnASingleLine: false
AlwaysBreakTemplateDeclarations: Yes
PackConstructorInitializers: Never
BreakConstructorInitializers: BeforeComma
PointerAlignment: Left
ReferenceAlignment: Left
ConstructorInitializerIndentWidth: 4
SpaceBeforeCpp11BracedList: true
SeparateDefinitionBlocks: Always
BreakBeforeBraces: WebKit
EmptyLineBeforeAccessModifier: LogicalBlock
Cpp11BracedListStyle: true

View File

@@ -5,96 +5,117 @@ namespace trnr {
// Clipper based on ClipOnly2 by Chris Johnson // Clipper based on ClipOnly2 by Chris Johnson
class aw_cliponly2 { class aw_cliponly2 {
public: public:
aw_cliponly2() { aw_cliponly2()
samplerate = 44100; {
samplerate = 44100;
lastSampleL = 0.0; lastSampleL = 0.0;
wasPosClipL = false; wasPosClipL = false;
wasNegClipL = false; wasNegClipL = false;
lastSampleR = 0.0; lastSampleR = 0.0;
wasPosClipR = false; wasPosClipR = false;
wasNegClipR = false; wasNegClipR = false;
for (int x = 0; x < 16; x++) {intermediateL[x] = 0.0; intermediateR[x] = 0.0;} for (int x = 0; x < 16; x++) {
//this is reset: values being initialized only once. Startup values, whatever they are. intermediateL[x] = 0.0;
} intermediateR[x] = 0.0;
}
// this is reset: values being initialized only once. Startup values, whatever they are.
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void process_block(double** inputs, double** outputs, long sample_frames) { void process_block(double** inputs, double** outputs, long sample_frames)
double* in1 = inputs[0]; {
double* in2 = inputs[1]; double* in1 = inputs[0];
double* out1 = outputs[0]; double* in2 = inputs[1];
double* out2 = outputs[1]; double* out1 = outputs[0];
double* out2 = outputs[1];
double overallscale = 1.0; double overallscale = 1.0;
overallscale /= 44100.0; overallscale /= 44100.0;
overallscale *= samplerate; overallscale *= samplerate;
int spacing = floor(overallscale); //should give us working basic scaling, usually 2 or 4 int spacing = floor(overallscale); // should give us working basic scaling, usually 2 or 4
if (spacing < 1) spacing = 1; if (spacing > 16) spacing = 16; if (spacing < 1) spacing = 1;
if (spacing > 16) spacing = 16;
while (--sample_frames >= 0) while (--sample_frames >= 0) {
{ double inputSampleL = *in1;
double inputSampleL = *in1; double inputSampleR = *in2;
double inputSampleR = *in2;
//begin ClipOnly2 stereo as a little, compressed chunk that can be dropped into code // 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 (inputSampleL > 4.0) inputSampleL = 4.0;
if (wasPosClipL == true) { //current will be over if (inputSampleL < -4.0) inputSampleL = -4.0;
if (inputSampleL<lastSampleL) lastSampleL=0.7058208+(inputSampleL*0.2609148); if (wasPosClipL == true) { // current will be over
else lastSampleL = 0.2491717+(lastSampleL*0.7390851); if (inputSampleL < lastSampleL) lastSampleL = 0.7058208 + (inputSampleL * 0.2609148);
} wasPosClipL = false; else lastSampleL = 0.2491717 + (lastSampleL * 0.7390851);
if (inputSampleL>0.9549925859) {wasPosClipL=true;inputSampleL=0.7058208+(lastSampleL*0.2609148);} }
if (wasNegClipL == true) { //current will be -over wasPosClipL = false;
if (inputSampleL > lastSampleL) lastSampleL=-0.7058208+(inputSampleL*0.2609148); if (inputSampleL > 0.9549925859) {
else lastSampleL=-0.2491717+(lastSampleL*0.7390851); wasPosClipL = true;
} wasNegClipL = false; inputSampleL = 0.7058208 + (lastSampleL * 0.2609148);
if (inputSampleL<-0.9549925859) {wasNegClipL=true;inputSampleL=-0.7058208+(lastSampleL*0.2609148);} }
intermediateL[spacing] = inputSampleL; if (wasNegClipL == true) { // current will be -over
inputSampleL = lastSampleL; //Latency is however many samples equals one 44.1k sample if (inputSampleL > lastSampleL) lastSampleL = -0.7058208 + (inputSampleL * 0.2609148);
for (int x = spacing; x > 0; x--) intermediateL[x-1] = intermediateL[x]; else lastSampleL = -0.2491717 + (lastSampleL * 0.7390851);
lastSampleL = intermediateL[0]; //run a little buffer to handle this }
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 (inputSampleR > 4.0) inputSampleR = 4.0;
if (wasPosClipR == true) { //current will be over if (inputSampleR < -4.0) inputSampleR = -4.0;
if (inputSampleR<lastSampleR) lastSampleR=0.7058208+(inputSampleR*0.2609148); if (wasPosClipR == true) { // current will be over
else lastSampleR = 0.2491717+(lastSampleR*0.7390851); if (inputSampleR < lastSampleR) lastSampleR = 0.7058208 + (inputSampleR * 0.2609148);
} wasPosClipR = false; else lastSampleR = 0.2491717 + (lastSampleR * 0.7390851);
if (inputSampleR>0.9549925859) {wasPosClipR=true;inputSampleR=0.7058208+(lastSampleR*0.2609148);} }
if (wasNegClipR == true) { //current will be -over wasPosClipR = false;
if (inputSampleR > lastSampleR) lastSampleR=-0.7058208+(inputSampleR*0.2609148); if (inputSampleR > 0.9549925859) {
else lastSampleR=-0.2491717+(lastSampleR*0.7390851); wasPosClipR = true;
} wasNegClipR = false; inputSampleR = 0.7058208 + (lastSampleR * 0.2609148);
if (inputSampleR<-0.9549925859) {wasNegClipR=true;inputSampleR=-0.7058208+(lastSampleR*0.2609148);} }
intermediateR[spacing] = inputSampleR; if (wasNegClipR == true) { // current will be -over
inputSampleR = lastSampleR; //Latency is however many samples equals one 44.1k sample if (inputSampleR > lastSampleR) lastSampleR = -0.7058208 + (inputSampleR * 0.2609148);
for (int x = spacing; x > 0; x--) intermediateR[x-1] = intermediateR[x]; else lastSampleR = -0.2491717 + (lastSampleR * 0.7390851);
lastSampleR = intermediateR[0]; //run a little buffer to handle this }
//end ClipOnly2 stereo as a little, compressed chunk that can be dropped into code 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
*out1 = inputSampleL; *out1 = inputSampleL;
*out2 = inputSampleR; *out2 = inputSampleR;
in1++; in1++;
in2++; in2++;
out1++; out1++;
out2++; out2++;
} }
} }
private: private:
double samplerate; double samplerate;
double lastSampleL; double lastSampleL;
double intermediateL[16]; double intermediateL[16];
bool wasPosClipL; bool wasPosClipL;
bool wasNegClipL; bool wasNegClipL;
double lastSampleR; double lastSampleR;
double intermediateR[16]; double intermediateR[16];
bool wasPosClipR; bool wasPosClipR;
bool wasNegClipR; //Stereo ClipOnly2 bool wasNegClipR; // Stereo ClipOnly2
//default stuff // default stuff
}; };
} } // namespace trnr

View File

@@ -6,92 +6,103 @@ namespace trnr {
// soft clipper based on ClipSoftly by Chris Johnson // soft clipper based on ClipSoftly by Chris Johnson
class aw_clipsoftly { class aw_clipsoftly {
public: public:
aw_clipsoftly() { aw_clipsoftly()
samplerate = 44100; {
samplerate = 44100;
lastSampleL = 0.0; lastSampleL = 0.0;
lastSampleR = 0.0; lastSampleR = 0.0;
for (int x = 0; x < 16; x++) {intermediateL[x] = 0.0; intermediateR[x] = 0.0;} for (int x = 0; x < 16; x++) {
fpdL = 1.0; while (fpdL < 16386) fpdL = rand()*UINT32_MAX; intermediateL[x] = 0.0;
fpdR = 1.0; while (fpdR < 16386) fpdR = rand()*UINT32_MAX; intermediateR[x] = 0.0;
//this is reset: values being initialized only once. Startup values, whatever they are. }
} fpdL = 1.0;
while (fpdL < 16386) fpdL = rand() * UINT32_MAX;
fpdR = 1.0;
while (fpdR < 16386) fpdR = rand() * UINT32_MAX;
// this is reset: values being initialized only once. Startup values, whatever they are.
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void process_block(double** inputs, double** outputs, long sample_frames) { void process_block(double** inputs, double** outputs, long sample_frames)
double* in1 = inputs[0]; {
double* in2 = inputs[1]; double* in1 = inputs[0];
double* out1 = outputs[0]; double* in2 = inputs[1];
double* out2 = outputs[1]; double* out1 = outputs[0];
double* out2 = outputs[1];
double overallscale = 1.0; double overallscale = 1.0;
overallscale /= 44100.0; overallscale /= 44100.0;
overallscale *= samplerate; overallscale *= samplerate;
int spacing = floor(overallscale); //should give us working basic scaling, usually 2 or 4 int spacing = floor(overallscale); // should give us working basic scaling, usually 2 or 4
if (spacing < 1) spacing = 1; if (spacing > 16) spacing = 16; if (spacing < 1) spacing = 1;
if (spacing > 16) spacing = 16;
while (--sample_frames >= 0) while (--sample_frames >= 0) {
{ double inputSampleL = *in1;
double inputSampleL = *in1; double inputSampleR = *in2;
double inputSampleR = *in2; if (fabs(inputSampleL) < 1.18e-23) inputSampleL = fpdL * 1.18e-17;
if (fabs(inputSampleL)<1.18e-23) inputSampleL = fpdL * 1.18e-17; if (fabs(inputSampleR) < 1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (fabs(inputSampleR)<1.18e-23) inputSampleR = fpdR * 1.18e-17;
double softSpeed = fabs(inputSampleL); double softSpeed = fabs(inputSampleL);
if (softSpeed < 1.0) softSpeed = 1.0; else softSpeed = 1.0/softSpeed; if (softSpeed < 1.0) softSpeed = 1.0;
if (inputSampleL > 1.57079633) inputSampleL = 1.57079633; else softSpeed = 1.0 / softSpeed;
if (inputSampleL < -1.57079633) inputSampleL = -1.57079633; if (inputSampleL > 1.57079633) inputSampleL = 1.57079633;
inputSampleL = sin(inputSampleL)*0.9549925859; //scale to what cliponly uses if (inputSampleL < -1.57079633) inputSampleL = -1.57079633;
inputSampleL = (inputSampleL*softSpeed)+(lastSampleL*(1.0-softSpeed)); inputSampleL = sin(inputSampleL) * 0.9549925859; // scale to what cliponly uses
inputSampleL = (inputSampleL * softSpeed) + (lastSampleL * (1.0 - softSpeed));
softSpeed = fabs(inputSampleR); softSpeed = fabs(inputSampleR);
if (softSpeed < 1.0) softSpeed = 1.0; else softSpeed = 1.0/softSpeed; if (softSpeed < 1.0) softSpeed = 1.0;
if (inputSampleR > 1.57079633) inputSampleR = 1.57079633; else softSpeed = 1.0 / softSpeed;
if (inputSampleR < -1.57079633) inputSampleR = -1.57079633; if (inputSampleR > 1.57079633) inputSampleR = 1.57079633;
inputSampleR = sin(inputSampleR)*0.9549925859; //scale to what cliponly uses if (inputSampleR < -1.57079633) inputSampleR = -1.57079633;
inputSampleR = (inputSampleR*softSpeed)+(lastSampleR*(1.0-softSpeed)); inputSampleR = sin(inputSampleR) * 0.9549925859; // scale to what cliponly uses
inputSampleR = (inputSampleR * softSpeed) + (lastSampleR * (1.0 - softSpeed));
intermediateL[spacing] = inputSampleL; intermediateL[spacing] = inputSampleL;
inputSampleL = lastSampleL; //Latency is however many samples equals one 44.1k sample inputSampleL = lastSampleL; // Latency is however many samples equals one 44.1k sample
for (int x = spacing; x > 0; x--) intermediateL[x-1] = intermediateL[x]; for (int x = spacing; x > 0; x--) intermediateL[x - 1] = intermediateL[x];
lastSampleL = intermediateL[0]; //run a little buffer to handle this lastSampleL = intermediateL[0]; // run a little buffer to handle this
intermediateR[spacing] = inputSampleR; intermediateR[spacing] = inputSampleR;
inputSampleR = lastSampleR; //Latency is however many samples equals one 44.1k sample inputSampleR = lastSampleR; // Latency is however many samples equals one 44.1k sample
for (int x = spacing; x > 0; x--) intermediateR[x-1] = intermediateR[x]; for (int x = spacing; x > 0; x--) intermediateR[x - 1] = intermediateR[x];
lastSampleR = intermediateR[0]; //run a little buffer to handle this lastSampleR = intermediateR[0]; // run a little buffer to handle this
//begin 64 bit stereo floating point dither // begin 64 bit stereo floating point dither
//int expon; frexp((double)inputSampleL, &expon); // int expon; frexp((double)inputSampleL, &expon);
fpdL ^= fpdL << 13; fpdL ^= fpdL >> 17; fpdL ^= fpdL << 5; fpdL ^= fpdL << 13;
//inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62)); fpdL ^= fpdL >> 17;
//frexp((double)inputSampleR, &expon); fpdL ^= fpdL << 5;
fpdR ^= fpdR << 13; fpdR ^= fpdR >> 17; fpdR ^= fpdR << 5; // inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
//inputSampleR += ((double(fpdR)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62)); // frexp((double)inputSampleR, &expon);
//end 64 bit stereo floating point dither 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; *out1 = inputSampleL;
*out2 = inputSampleR; *out2 = inputSampleR;
in1++; in1++;
in2++; in2++;
out1++; out1++;
out2++; out2++;
} }
} }
private: private:
double samplerate; double samplerate;
double lastSampleL; double lastSampleL;
double intermediateL[16]; double intermediateL[16];
double lastSampleR; double lastSampleR;
double intermediateR[16]; double intermediateR[16];
uint32_t fpdL; uint32_t fpdL;
uint32_t fpdR; uint32_t fpdR;
//default stuff // default stuff
}; };
} } // namespace trnr

View File

@@ -6,168 +6,181 @@ namespace trnr {
// modeled tube preamp based on tube2 by Chris Johnson // modeled tube preamp based on tube2 by Chris Johnson
class aw_tube2 { class aw_tube2 {
public: public:
aw_tube2() { aw_tube2()
samplerate = 44100; {
samplerate = 44100;
A = 0.5; A = 0.5;
B = 0.5; B = 0.5;
previousSampleA = 0.0; previousSampleA = 0.0;
previousSampleB = 0.0; previousSampleB = 0.0;
previousSampleC = 0.0; previousSampleC = 0.0;
previousSampleD = 0.0; previousSampleD = 0.0;
previousSampleE = 0.0; previousSampleE = 0.0;
previousSampleF = 0.0; previousSampleF = 0.0;
fpdL = 1.0; while (fpdL < 16386) fpdL = rand()*UINT32_MAX; fpdL = 1.0;
fpdR = 1.0; while (fpdR < 16386) fpdR = rand()*UINT32_MAX; while (fpdL < 16386) fpdL = rand() * UINT32_MAX;
//this is reset: values being initialized only once. Startup values, whatever they are. fpdR = 1.0;
} while (fpdR < 16386) fpdR = rand() * UINT32_MAX;
// this is reset: values being initialized only once. Startup values, whatever they are.
}
void set_input(double value) { void set_input(double value) { A = clamp(value); }
A = clamp(value);
}
void set_tube(double value) { void set_tube(double value) { B = clamp(value); }
B = clamp(value);
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void process_block(double **inputs, double **outputs, long sampleframes) { void process_block(double** inputs, double** outputs, long sampleframes)
double* in1 = inputs[0]; {
double* in2 = inputs[1]; double* in1 = inputs[0];
double* out1 = outputs[0]; double* in2 = inputs[1];
double* out2 = outputs[1]; double* out1 = outputs[0];
double* out2 = outputs[1];
double overallscale = 1.0; double overallscale = 1.0;
overallscale /= 44100.0; overallscale /= 44100.0;
overallscale *= samplerate; overallscale *= samplerate;
double inputPad = A; double inputPad = A;
double iterations = 1.0-B; double iterations = 1.0 - B;
int powerfactor = (9.0*iterations)+1; int powerfactor = (9.0 * iterations) + 1;
double asymPad = (double)powerfactor; double asymPad = (double)powerfactor;
double gainscaling = 1.0/(double)(powerfactor+1); double gainscaling = 1.0 / (double)(powerfactor + 1);
double outputscaling = 1.0 + (1.0/(double)(powerfactor)); double outputscaling = 1.0 + (1.0 / (double)(powerfactor));
while (--sampleframes >= 0) while (--sampleframes >= 0) {
{ double inputSampleL = *in1;
double inputSampleL = *in1; double inputSampleR = *in2;
double inputSampleR = *in2; if (fabs(inputSampleL) < 1.18e-23) inputSampleL = fpdL * 1.18e-17;
if (fabs(inputSampleL)<1.18e-23) inputSampleL = fpdL * 1.18e-17; if (fabs(inputSampleR) < 1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (fabs(inputSampleR)<1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (inputPad < 1.0) { if (inputPad < 1.0) {
inputSampleL *= inputPad; inputSampleL *= inputPad;
inputSampleR *= inputPad; inputSampleR *= inputPad;
} }
if (overallscale > 1.9) { if (overallscale > 1.9) {
double stored = inputSampleL; double stored = inputSampleL;
inputSampleL += previousSampleA; previousSampleA = stored; inputSampleL *= 0.5; inputSampleL += previousSampleA;
stored = inputSampleR; previousSampleA = stored;
inputSampleR += previousSampleB; previousSampleB = stored; inputSampleR *= 0.5; inputSampleL *= 0.5;
} //for high sample rates on this plugin we are going to do a simple average stored = inputSampleR;
inputSampleR += previousSampleB;
previousSampleB = stored;
inputSampleR *= 0.5;
} // for high sample rates on this plugin we are going to do a simple average
if (inputSampleL > 1.0) inputSampleL = 1.0; if (inputSampleL > 1.0) inputSampleL = 1.0;
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 (inputSampleR < -1.0) inputSampleR = -1.0; if (inputSampleR < -1.0) inputSampleR = -1.0;
//flatten bottom, point top of sine waveshaper L // flatten bottom, point top of sine waveshaper L
inputSampleL /= asymPad; inputSampleL /= asymPad;
double sharpen = -inputSampleL; double sharpen = -inputSampleL;
if (sharpen > 0.0) sharpen = 1.0+sqrt(sharpen); if (sharpen > 0.0) sharpen = 1.0 + sqrt(sharpen);
else sharpen = 1.0-sqrt(-sharpen); else sharpen = 1.0 - sqrt(-sharpen);
inputSampleL -= inputSampleL*fabs(inputSampleL)*sharpen*0.25; inputSampleL -= inputSampleL * fabs(inputSampleL) * sharpen * 0.25;
//this will take input from exactly -1.0 to 1.0 max // this will take input from exactly -1.0 to 1.0 max
inputSampleL *= asymPad; inputSampleL *= asymPad;
//flatten bottom, point top of sine waveshaper R // flatten bottom, point top of sine waveshaper R
inputSampleR /= asymPad; inputSampleR /= asymPad;
sharpen = -inputSampleR; sharpen = -inputSampleR;
if (sharpen > 0.0) sharpen = 1.0+sqrt(sharpen); if (sharpen > 0.0) sharpen = 1.0 + sqrt(sharpen);
else sharpen = 1.0-sqrt(-sharpen); else sharpen = 1.0 - sqrt(-sharpen);
inputSampleR -= inputSampleR*fabs(inputSampleR)*sharpen*0.25; inputSampleR -= inputSampleR * fabs(inputSampleR) * sharpen * 0.25;
//this will take input from exactly -1.0 to 1.0 max // this will take input from exactly -1.0 to 1.0 max
inputSampleR *= asymPad; inputSampleR *= asymPad;
//end first asym section: later boosting can mitigate the extreme // end first asym section: later boosting can mitigate the extreme
//softclipping of one side of the wave // softclipping of one side of the wave
//and we are asym clipping more when Tube is cranked, to compensate // and we are asym clipping more when Tube is cranked, to compensate
//original Tube algorithm: powerfactor widens the more linear region of the wave // original Tube algorithm: powerfactor widens the more linear region of the wave
double factor = inputSampleL; //Left channel double factor = inputSampleL; // Left channel
for (int x = 0; x < powerfactor; x++) factor *= inputSampleL; for (int x = 0; x < powerfactor; x++) factor *= inputSampleL;
if ((powerfactor % 2 == 1) && (inputSampleL != 0.0)) factor = (factor/inputSampleL)*fabs(inputSampleL); if ((powerfactor % 2 == 1) && (inputSampleL != 0.0)) factor = (factor / inputSampleL) * fabs(inputSampleL);
factor *= gainscaling; factor *= gainscaling;
inputSampleL -= factor; inputSampleL -= factor;
inputSampleL *= outputscaling; inputSampleL *= outputscaling;
factor = inputSampleR; //Right channel factor = inputSampleR; // Right channel
for (int x = 0; x < powerfactor; x++) factor *= inputSampleR; for (int x = 0; x < powerfactor; x++) factor *= inputSampleR;
if ((powerfactor % 2 == 1) && (inputSampleR != 0.0)) factor = (factor/inputSampleR)*fabs(inputSampleR); if ((powerfactor % 2 == 1) && (inputSampleR != 0.0)) factor = (factor / inputSampleR) * fabs(inputSampleR);
factor *= gainscaling; factor *= gainscaling;
inputSampleR -= factor; inputSampleR -= factor;
inputSampleR *= outputscaling; inputSampleR *= outputscaling;
if (overallscale > 1.9) { if (overallscale > 1.9) {
double stored = inputSampleL; double stored = inputSampleL;
inputSampleL += previousSampleC; previousSampleC = stored; inputSampleL *= 0.5; inputSampleL += previousSampleC;
stored = inputSampleR; previousSampleC = stored;
inputSampleR += previousSampleD; previousSampleD = stored; inputSampleR *= 0.5; inputSampleL *= 0.5;
} //for high sample rates on this plugin we are going to do a simple average stored = inputSampleR;
//end original Tube. Now we have a boosted fat sound peaking at 0dB exactly inputSampleR += previousSampleD;
previousSampleD = stored;
inputSampleR *= 0.5;
} // for high sample rates on this plugin we are going to do a simple average
// end original Tube. Now we have a boosted fat sound peaking at 0dB exactly
//hysteresis and spiky fuzz L // hysteresis and spiky fuzz L
double slew = previousSampleE - inputSampleL; double slew = previousSampleE - inputSampleL;
if (overallscale > 1.9) { if (overallscale > 1.9) {
double stored = inputSampleL; double stored = inputSampleL;
inputSampleL += previousSampleE; previousSampleE = stored; inputSampleL *= 0.5; inputSampleL += previousSampleE;
} else previousSampleE = inputSampleL; //for this, need previousSampleC always previousSampleE = stored;
if (slew > 0.0) slew = 1.0+(sqrt(slew)*0.5); inputSampleL *= 0.5;
else slew = 1.0-(sqrt(-slew)*0.5); } else previousSampleE = inputSampleL; // for this, need previousSampleC always
inputSampleL -= inputSampleL*fabs(inputSampleL)*slew*gainscaling; if (slew > 0.0) slew = 1.0 + (sqrt(slew) * 0.5);
//reusing gainscaling that's part of another algorithm else slew = 1.0 - (sqrt(-slew) * 0.5);
if (inputSampleL > 0.52) inputSampleL = 0.52; inputSampleL -= inputSampleL * fabs(inputSampleL) * slew * gainscaling;
if (inputSampleL < -0.52) inputSampleL = -0.52; // reusing gainscaling that's part of another algorithm
inputSampleL *= 1.923076923076923; if (inputSampleL > 0.52) inputSampleL = 0.52;
//hysteresis and spiky fuzz R if (inputSampleL < -0.52) inputSampleL = -0.52;
slew = previousSampleF - inputSampleR; inputSampleL *= 1.923076923076923;
if (overallscale > 1.9) { // hysteresis and spiky fuzz R
double stored = inputSampleR; slew = previousSampleF - inputSampleR;
inputSampleR += previousSampleF; previousSampleF = stored; inputSampleR *= 0.5; if (overallscale > 1.9) {
} else previousSampleF = inputSampleR; //for this, need previousSampleC always double stored = inputSampleR;
if (slew > 0.0) slew = 1.0+(sqrt(slew)*0.5); inputSampleR += previousSampleF;
else slew = 1.0-(sqrt(-slew)*0.5); previousSampleF = stored;
inputSampleR -= inputSampleR*fabs(inputSampleR)*slew*gainscaling; inputSampleR *= 0.5;
//reusing gainscaling that's part of another algorithm } else previousSampleF = inputSampleR; // for this, need previousSampleC always
if (inputSampleR > 0.52) inputSampleR = 0.52; if (slew > 0.0) slew = 1.0 + (sqrt(slew) * 0.5);
if (inputSampleR < -0.52) inputSampleR = -0.52; else slew = 1.0 - (sqrt(-slew) * 0.5);
inputSampleR *= 1.923076923076923; inputSampleR -= inputSampleR * fabs(inputSampleR) * slew * gainscaling;
//end hysteresis and spiky fuzz section // reusing gainscaling that's part of another algorithm
if (inputSampleR > 0.52) inputSampleR = 0.52;
if (inputSampleR < -0.52) inputSampleR = -0.52;
inputSampleR *= 1.923076923076923;
// end hysteresis and spiky fuzz section
//begin 64 bit stereo floating point dither // begin 64 bit stereo floating point dither
//int expon; frexp((double)inputSampleL, &expon); // int expon; frexp((double)inputSampleL, &expon);
fpdL ^= fpdL << 13; fpdL ^= fpdL >> 17; fpdL ^= fpdL << 5; fpdL ^= fpdL << 13;
//inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62)); fpdL ^= fpdL >> 17;
//frexp((double)inputSampleR, &expon); fpdL ^= fpdL << 5;
fpdR ^= fpdR << 13; fpdR ^= fpdR >> 17; fpdR ^= fpdR << 5; // inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
//inputSampleR += ((double(fpdR)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62)); // frexp((double)inputSampleR, &expon);
//end 64 bit stereo floating point dither 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; *out1 = inputSampleL;
*out2 = inputSampleR; *out2 = inputSampleR;
in1++; in1++;
in2++; in2++;
out1++; out1++;
out2++; out2++;
} }
} }
private: private:
double samplerate; double samplerate;
double previousSampleA; double previousSampleA;
double previousSampleB; double previousSampleB;
double previousSampleC; double previousSampleC;
double previousSampleD; double previousSampleD;
@@ -176,18 +189,19 @@ private:
uint32_t fpdL; uint32_t fpdL;
uint32_t fpdR; uint32_t fpdR;
//default stuff // default stuff
float A; float A;
float B; float B;
double clamp(double& value) { double clamp(double& value)
if (value > 1) { {
value = 1; if (value > 1) {
} else if (value < 0) { value = 1;
value = 0; } else if (value < 0) {
} value = 0;
return value; }
} return value;
}
}; };
} } // namespace trnr

View File

@@ -5,43 +5,44 @@ namespace trnr {
// mulaw companding based on code by Emilie Gillet / Mutable Instruments // mulaw companding based on code by Emilie Gillet / Mutable Instruments
class mulaw { class mulaw {
public: public:
int8_t encode_samples(int16_t pcm_val) { int8_t encode_samples(int16_t pcm_val)
int16_t mask; {
int16_t seg; int16_t mask;
uint8_t uval; int16_t seg;
pcm_val = pcm_val >> 2; uint8_t uval;
if (pcm_val < 0) { pcm_val = pcm_val >> 2;
pcm_val = -pcm_val; if (pcm_val < 0) {
mask = 0x7f; pcm_val = -pcm_val;
} else { mask = 0x7f;
mask = 0xff; } else {
} mask = 0xff;
if (pcm_val > 8159) pcm_val = 8159; }
pcm_val += (0x84 >> 2); if (pcm_val > 8159) pcm_val = 8159;
pcm_val += (0x84 >> 2);
if (pcm_val <= 0x3f) seg = 0; if (pcm_val <= 0x3f) seg = 0;
else if (pcm_val <= 0x7f) seg = 1; else if (pcm_val <= 0x7f) seg = 1;
else if (pcm_val <= 0xff) seg = 2; else if (pcm_val <= 0xff) seg = 2;
else if (pcm_val <= 0x1ff) seg = 3; else if (pcm_val <= 0x1ff) seg = 3;
else if (pcm_val <= 0x3ff) seg = 4; else if (pcm_val <= 0x3ff) seg = 4;
else if (pcm_val <= 0x7ff) seg = 5; else if (pcm_val <= 0x7ff) seg = 5;
else if (pcm_val <= 0xfff) seg = 6; else if (pcm_val <= 0xfff) seg = 6;
else if (pcm_val <= 0x1fff) seg = 7; else if (pcm_val <= 0x1fff) seg = 7;
else seg = 8; else seg = 8;
if (seg >= 8) if (seg >= 8) return static_cast<uint8_t>(0x7f ^ mask);
return static_cast<uint8_t>(0x7f ^ mask); else {
else { uval = static_cast<uint8_t>((seg << 4) | ((pcm_val >> (seg + 1)) & 0x0f));
uval = static_cast<uint8_t>((seg << 4) | ((pcm_val >> (seg + 1)) & 0x0f)); return (uval ^ mask);
return (uval ^ mask); }
} }
}
int16_t decode_samples(uint8_t u_val) { int16_t decode_samples(uint8_t u_val)
int16_t t; {
u_val = ~u_val; int16_t t;
t = ((u_val & 0xf) << 3) + 0x84; u_val = ~u_val;
t <<= ((unsigned)u_val & 0x70) >> 4; t = ((u_val & 0xf) << 3) + 0x84;
return ((u_val & 0x80) ? (0x84 - t) : (t - 0x84)); t <<= ((unsigned)u_val & 0x70) >> 4;
} return ((u_val & 0x80) ? (0x84 - t) : (t - 0x84));
}
}; };
} } // namespace trnr

View File

@@ -1,93 +1,108 @@
#pragma once #pragma once
#include <stdlib.h>
#include <cstdint>
#include <cmath> #include <cmath>
#include <cstdint>
#include <stdlib.h>
namespace trnr { namespace trnr {
// ulaw compansion based on code by Chris Johnson // ulaw compansion based on code by Chris Johnson
class ulaw { class ulaw {
public: public:
ulaw() { ulaw()
fpd_l = 1.0; while (fpd_l < 16386) fpd_l = rand()*UINT32_MAX; {
fpd_r = 1.0; while (fpd_r < 16386) fpd_r = rand()*UINT32_MAX; fpd_l = 1.0;
} while (fpd_l < 16386) fpd_l = rand() * UINT32_MAX;
fpd_r = 1.0;
while (fpd_r < 16386) fpd_r = rand() * UINT32_MAX;
}
void encode_samples(double& input_sample_l, double& input_sample_r) { void encode_samples(double& input_sample_l, double& input_sample_r)
{
// ulaw encoding // ulaw encoding
static int noisesource_l = 0; static int noisesource_l = 0;
static int noisesource_r = 850010; static int noisesource_r = 850010;
int residue; int residue;
double applyresidue; double applyresidue;
noisesource_l = noisesource_l % 1700021; noisesource_l++; noisesource_l = noisesource_l % 1700021;
residue = noisesource_l * noisesource_l; noisesource_l++;
residue = residue % 170003; residue *= residue; residue = noisesource_l * noisesource_l;
residue = residue % 17011; residue *= residue; residue = residue % 170003;
residue = residue % 1709; residue *= residue; residue *= residue;
residue = residue % 173; residue *= residue; residue = residue % 17011;
residue = residue % 17; residue *= residue;
applyresidue = residue; residue = residue % 1709;
applyresidue *= 0.00000001; residue *= residue;
applyresidue *= 0.00000001; residue = residue % 173;
input_sample_l += applyresidue; residue *= residue;
if (input_sample_l<1.2e-38 && -input_sample_l<1.2e-38) { residue = residue % 17;
input_sample_l -= applyresidue; applyresidue = residue;
} applyresidue *= 0.00000001;
applyresidue *= 0.00000001;
input_sample_l += applyresidue;
if (input_sample_l < 1.2e-38 && -input_sample_l < 1.2e-38) { input_sample_l -= applyresidue; }
noisesource_r = noisesource_r % 1700021; noisesource_r++; noisesource_r = noisesource_r % 1700021;
residue = noisesource_r * noisesource_r; noisesource_r++;
residue = residue % 170003; residue *= residue; residue = noisesource_r * noisesource_r;
residue = residue % 17011; residue *= residue; residue = residue % 170003;
residue = residue % 1709; residue *= residue; residue *= residue;
residue = residue % 173; residue *= residue; residue = residue % 17011;
residue = residue % 17; residue *= residue;
applyresidue = residue; residue = residue % 1709;
applyresidue *= 0.00000001; residue *= residue;
applyresidue *= 0.00000001; residue = residue % 173;
input_sample_r += applyresidue; residue *= residue;
if (input_sample_r<1.2e-38 && -input_sample_r<1.2e-38) { residue = residue % 17;
input_sample_r -= applyresidue; applyresidue = residue;
} applyresidue *= 0.00000001;
applyresidue *= 0.00000001;
input_sample_r += applyresidue;
if (input_sample_r < 1.2e-38 && -input_sample_r < 1.2e-38) { input_sample_r -= applyresidue; }
if (input_sample_l > 1.0) input_sample_l = 1.0; if (input_sample_l > 1.0) input_sample_l = 1.0;
if (input_sample_l < -1.0) input_sample_l = -1.0; if (input_sample_l < -1.0) input_sample_l = -1.0;
if (input_sample_r > 1.0) input_sample_r = 1.0; if (input_sample_r > 1.0) input_sample_r = 1.0;
if (input_sample_r < -1.0) input_sample_r = -1.0; if (input_sample_r < -1.0) input_sample_r = -1.0;
if (input_sample_l > 0) input_sample_l = log(1.0+(255*fabs(input_sample_l))) / log(256); if (input_sample_l > 0) input_sample_l = log(1.0 + (255 * fabs(input_sample_l))) / log(256);
if (input_sample_l < 0) input_sample_l = -log(1.0+(255*fabs(input_sample_l))) / log(256); if (input_sample_l < 0) input_sample_l = -log(1.0 + (255 * fabs(input_sample_l))) / log(256);
if (input_sample_r > 0) input_sample_r = log(1.0+(255*fabs(input_sample_r))) / log(256); if (input_sample_r > 0) input_sample_r = log(1.0 + (255 * fabs(input_sample_r))) / log(256);
if (input_sample_r < 0) input_sample_r = -log(1.0+(255*fabs(input_sample_r))) / log(256); if (input_sample_r < 0) input_sample_r = -log(1.0 + (255 * fabs(input_sample_r))) / log(256);
} }
void decode_samples(double& input_sample_l, double& input_sample_r) { void decode_samples(double& input_sample_l, double& input_sample_r)
{
// ulaw decoding // ulaw decoding
if (fabs(input_sample_l)<1.18e-23) input_sample_l = fpd_l * 1.18e-17; if (fabs(input_sample_l) < 1.18e-23) input_sample_l = fpd_l * 1.18e-17;
if (fabs(input_sample_r)<1.18e-23) input_sample_r = fpd_r * 1.18e-17; if (fabs(input_sample_r) < 1.18e-23) input_sample_r = fpd_r * 1.18e-17;
if (input_sample_l > 1.0) input_sample_l = 1.0; if (input_sample_l > 1.0) input_sample_l = 1.0;
if (input_sample_l < -1.0) input_sample_l = -1.0; if (input_sample_l < -1.0) input_sample_l = -1.0;
if (input_sample_r > 1.0) input_sample_r = 1.0; if (input_sample_r > 1.0) input_sample_r = 1.0;
if (input_sample_r < -1.0) input_sample_r = -1.0; if (input_sample_r < -1.0) input_sample_r = -1.0;
if (input_sample_l > 0) input_sample_l = (pow(256,fabs(input_sample_l))-1.0) / 255; if (input_sample_l > 0) input_sample_l = (pow(256, fabs(input_sample_l)) - 1.0) / 255;
if (input_sample_l < 0) input_sample_l = -(pow(256,fabs(input_sample_l))-1.0) / 255; if (input_sample_l < 0) input_sample_l = -(pow(256, fabs(input_sample_l)) - 1.0) / 255;
if (input_sample_r > 0) input_sample_r = (pow(256,fabs(input_sample_r))-1.0) / 255; if (input_sample_r > 0) input_sample_r = (pow(256, fabs(input_sample_r)) - 1.0) / 255;
if (input_sample_r < 0) input_sample_r = -(pow(256,fabs(input_sample_r))-1.0) / 255; if (input_sample_r < 0) input_sample_r = -(pow(256, fabs(input_sample_r)) - 1.0) / 255;
// 64 bit stereo floating point dither // 64 bit stereo floating point dither
fpd_l ^= fpd_l << 13; fpd_l ^= fpd_l >> 17; fpd_l ^= fpd_l << 5; fpd_l ^= fpd_l << 13;
fpd_r ^= fpd_r << 13; fpd_r ^= fpd_r >> 17; fpd_r ^= fpd_r << 5; fpd_l ^= fpd_l >> 17;
} fpd_l ^= fpd_l << 5;
fpd_r ^= fpd_r << 13;
fpd_r ^= fpd_r >> 17;
fpd_r ^= fpd_r << 5;
}
private: private:
uint32_t fpd_l; uint32_t fpd_l;
uint32_t fpd_r; uint32_t fpd_r;
}; };
} } // namespace trnr

View File

@@ -6,266 +6,284 @@ namespace trnr {
// compressor based on pop2 by Chris Johnson // compressor based on pop2 by Chris Johnson
class aw_pop2 { class aw_pop2 {
public: public:
aw_pop2() { aw_pop2()
samplerate = 44100; {
samplerate = 44100;
A = 0.5; A = 0.5;
B = 0.5; B = 0.5;
C = 0.5; C = 0.5;
D = 0.5; D = 0.5;
E = 1.0; E = 1.0;
fpdL = 1.0; while (fpdL < 16386) fpdL = rand()*UINT32_MAX; fpdL = 1.0;
fpdR = 1.0; while (fpdR < 16386) fpdR = rand()*UINT32_MAX; while (fpdL < 16386) fpdL = rand() * UINT32_MAX;
fpdR = 1.0;
while (fpdR < 16386) fpdR = rand() * UINT32_MAX;
lastSampleL = 0.0; lastSampleL = 0.0;
wasPosClipL = false; wasPosClipL = false;
wasNegClipL = false; wasNegClipL = false;
lastSampleR = 0.0; lastSampleR = 0.0;
wasPosClipR = false; wasPosClipR = false;
wasNegClipR = false; wasNegClipR = false;
for (int x = 0; x < 16; x++) {intermediateL[x] = 0.0; intermediateR[x] = 0.0;} for (int x = 0; x < 16; x++) {
intermediateL[x] = 0.0;
intermediateR[x] = 0.0;
}
muVaryL = 0.0; muVaryL = 0.0;
muAttackL = 0.0; muAttackL = 0.0;
muNewSpeedL = 1000.0; muNewSpeedL = 1000.0;
muSpeedAL = 1000.0; muSpeedAL = 1000.0;
muSpeedBL = 1000.0; muSpeedBL = 1000.0;
muCoefficientAL = 1.0; muCoefficientAL = 1.0;
muCoefficientBL = 1.0; muCoefficientBL = 1.0;
muVaryR = 0.0; muVaryR = 0.0;
muAttackR = 0.0; muAttackR = 0.0;
muNewSpeedR = 1000.0; muNewSpeedR = 1000.0;
muSpeedAR = 1000.0; muSpeedAR = 1000.0;
muSpeedBR = 1000.0; muSpeedBR = 1000.0;
muCoefficientAR = 1.0; muCoefficientAR = 1.0;
muCoefficientBR = 1.0; muCoefficientBR = 1.0;
flip = false; flip = false;
//this is reset: values being initialized only once. Startup values, whatever they are. // this is reset: values being initialized only once. Startup values, whatever they are.
} }
void set_compression(double value) { void set_compression(double value) { A = clamp(value); }
A = clamp(value);
}
void set_attack(double value) { void set_attack(double value) { B = clamp(value); }
B = clamp(value);
}
void set_release(double value) { void set_release(double value) { C = clamp(value); }
C = clamp(value);
}
void set_drive(double value) { void set_drive(double value) { D = clamp(value); }
D = clamp(value);
}
void set_drywet(double value) { void set_drywet(double value) { E = clamp(value); }
E = clamp(value);
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void process_block(double **inputs, double **outputs, long sampleframes) { void process_block(double** inputs, double** outputs, long sampleframes)
double* in1 = inputs[0]; {
double* in2 = inputs[1]; double* in1 = inputs[0];
double* out1 = outputs[0]; double* in2 = inputs[1];
double* out2 = outputs[1]; double* out1 = outputs[0];
double* out2 = outputs[1];
double overallscale = 1.0; double overallscale = 1.0;
overallscale /= 44100.0; overallscale /= 44100.0;
overallscale *= samplerate; overallscale *= samplerate;
int spacing = floor(overallscale); //should give us working basic scaling, usually 2 or 4 int spacing = floor(overallscale); // should give us working basic scaling, usually 2 or 4
if (spacing < 1) spacing = 1; if (spacing > 16) spacing = 16; if (spacing < 1) spacing = 1;
if (spacing > 16) spacing = 16;
double threshold = 1.0 - ((1.0-pow(1.0-A,2))*0.9); double threshold = 1.0 - ((1.0 - pow(1.0 - A, 2)) * 0.9);
double attack = ((pow(B,4)*100000.0)+10.0)*overallscale; double attack = ((pow(B, 4) * 100000.0) + 10.0) * overallscale;
double release = ((pow(C,5)*2000000.0)+20.0)*overallscale; double release = ((pow(C, 5) * 2000000.0) + 20.0) * overallscale;
double maxRelease = release * 4.0; double maxRelease = release * 4.0;
double muPreGain = 1.0/threshold; double muPreGain = 1.0 / threshold;
double muMakeupGain = sqrt(1.0 / threshold)*D; double muMakeupGain = sqrt(1.0 / threshold) * D;
double wet = E; double wet = E;
//compressor section // compressor section
while (--sampleframes >= 0) while (--sampleframes >= 0) {
{ double inputSampleL = *in1;
double inputSampleL = *in1; double inputSampleR = *in2;
double inputSampleR = *in2; if (fabs(inputSampleL) < 1.18e-23) inputSampleL = fpdL * 1.18e-17;
if (fabs(inputSampleL)<1.18e-23) inputSampleL = fpdL * 1.18e-17; if (fabs(inputSampleR) < 1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (fabs(inputSampleR)<1.18e-23) inputSampleR = fpdR * 1.18e-17; double drySampleL = inputSampleL;
double drySampleL = inputSampleL; double drySampleR = inputSampleR;
double drySampleR = inputSampleR;
//begin compressor section // begin compressor section
inputSampleL *= muPreGain; inputSampleL *= muPreGain;
inputSampleR *= muPreGain; inputSampleR *= muPreGain;
//adjust coefficients for L // adjust coefficients for L
if (flip) { if (flip) {
if (fabs(inputSampleL) > threshold) { if (fabs(inputSampleL) > threshold) {
muVaryL = threshold / fabs(inputSampleL); muVaryL = threshold / fabs(inputSampleL);
muAttackL = sqrt(fabs(muSpeedAL)); muAttackL = sqrt(fabs(muSpeedAL));
muCoefficientAL = muCoefficientAL * (muAttackL-1.0); muCoefficientAL = muCoefficientAL * (muAttackL - 1.0);
if (muVaryL < threshold) muCoefficientAL = muCoefficientAL + threshold; if (muVaryL < threshold) muCoefficientAL = muCoefficientAL + threshold;
else muCoefficientAL = muCoefficientAL + muVaryL; else muCoefficientAL = muCoefficientAL + muVaryL;
muCoefficientAL = muCoefficientAL / muAttackL; muCoefficientAL = muCoefficientAL / muAttackL;
muNewSpeedL = muSpeedAL * (muSpeedAL-1.0); muNewSpeedL = muSpeedAL * (muSpeedAL - 1.0);
muNewSpeedL = muNewSpeedL + release; muNewSpeedL = muNewSpeedL + release;
muSpeedAL = muNewSpeedL / muSpeedAL; muSpeedAL = muNewSpeedL / muSpeedAL;
if (muSpeedAL > maxRelease) muSpeedAL = maxRelease; if (muSpeedAL > maxRelease) muSpeedAL = maxRelease;
} else { } else {
muCoefficientAL = muCoefficientAL * ((muSpeedAL * muSpeedAL)-1.0); muCoefficientAL = muCoefficientAL * ((muSpeedAL * muSpeedAL) - 1.0);
muCoefficientAL = muCoefficientAL + 1.0; muCoefficientAL = muCoefficientAL + 1.0;
muCoefficientAL = muCoefficientAL / (muSpeedAL * muSpeedAL); muCoefficientAL = muCoefficientAL / (muSpeedAL * muSpeedAL);
muNewSpeedL = muSpeedAL * (muSpeedAL-1.0); muNewSpeedL = muSpeedAL * (muSpeedAL - 1.0);
muNewSpeedL = muNewSpeedL + attack; muNewSpeedL = muNewSpeedL + attack;
muSpeedAL = muNewSpeedL / muSpeedAL;} muSpeedAL = muNewSpeedL / muSpeedAL;
} else { }
if (fabs(inputSampleL) > threshold) { } else {
muVaryL = threshold / fabs(inputSampleL); if (fabs(inputSampleL) > threshold) {
muAttackL = sqrt(fabs(muSpeedBL)); muVaryL = threshold / fabs(inputSampleL);
muCoefficientBL = muCoefficientBL * (muAttackL-1); muAttackL = sqrt(fabs(muSpeedBL));
if (muVaryL < threshold) muCoefficientBL = muCoefficientBL + threshold; muCoefficientBL = muCoefficientBL * (muAttackL - 1);
else muCoefficientBL = muCoefficientBL + muVaryL; if (muVaryL < threshold) muCoefficientBL = muCoefficientBL + threshold;
muCoefficientBL = muCoefficientBL / muAttackL; else muCoefficientBL = muCoefficientBL + muVaryL;
muNewSpeedL = muSpeedBL * (muSpeedBL-1.0); muCoefficientBL = muCoefficientBL / muAttackL;
muNewSpeedL = muNewSpeedL + release; muNewSpeedL = muSpeedBL * (muSpeedBL - 1.0);
muSpeedBL = muNewSpeedL / muSpeedBL; muNewSpeedL = muNewSpeedL + release;
if (muSpeedBL > maxRelease) muSpeedBL = maxRelease; muSpeedBL = muNewSpeedL / muSpeedBL;
} else { if (muSpeedBL > maxRelease) muSpeedBL = maxRelease;
muCoefficientBL = muCoefficientBL * ((muSpeedBL * muSpeedBL)-1.0); } else {
muCoefficientBL = muCoefficientBL + 1.0; muCoefficientBL = muCoefficientBL * ((muSpeedBL * muSpeedBL) - 1.0);
muCoefficientBL = muCoefficientBL / (muSpeedBL * muSpeedBL); muCoefficientBL = muCoefficientBL + 1.0;
muNewSpeedL = muSpeedBL * (muSpeedBL-1.0); muCoefficientBL = muCoefficientBL / (muSpeedBL * muSpeedBL);
muNewSpeedL = muNewSpeedL + attack; muNewSpeedL = muSpeedBL * (muSpeedBL - 1.0);
muSpeedBL = muNewSpeedL / muSpeedBL; muNewSpeedL = muNewSpeedL + attack;
} muSpeedBL = muNewSpeedL / muSpeedBL;
} }
//got coefficients, adjusted speeds for L }
// got coefficients, adjusted speeds for L
//adjust coefficients for R // adjust coefficients for R
if (flip) { if (flip) {
if (fabs(inputSampleR) > threshold) { if (fabs(inputSampleR) > threshold) {
muVaryR = threshold / fabs(inputSampleR); muVaryR = threshold / fabs(inputSampleR);
muAttackR = sqrt(fabs(muSpeedAR)); muAttackR = sqrt(fabs(muSpeedAR));
muCoefficientAR = muCoefficientAR * (muAttackR-1.0); muCoefficientAR = muCoefficientAR * (muAttackR - 1.0);
if (muVaryR < threshold) muCoefficientAR = muCoefficientAR + threshold; if (muVaryR < threshold) muCoefficientAR = muCoefficientAR + threshold;
else muCoefficientAR = muCoefficientAR + muVaryR; else muCoefficientAR = muCoefficientAR + muVaryR;
muCoefficientAR = muCoefficientAR / muAttackR; muCoefficientAR = muCoefficientAR / muAttackR;
muNewSpeedR = muSpeedAR * (muSpeedAR-1.0); muNewSpeedR = muSpeedAR * (muSpeedAR - 1.0);
muNewSpeedR = muNewSpeedR + release; muNewSpeedR = muNewSpeedR + release;
muSpeedAR = muNewSpeedR / muSpeedAR; muSpeedAR = muNewSpeedR / muSpeedAR;
if (muSpeedAR > maxRelease) muSpeedAR = maxRelease; if (muSpeedAR > maxRelease) muSpeedAR = maxRelease;
} else { } else {
muCoefficientAR = muCoefficientAR * ((muSpeedAR * muSpeedAR)-1.0); muCoefficientAR = muCoefficientAR * ((muSpeedAR * muSpeedAR) - 1.0);
muCoefficientAR = muCoefficientAR + 1.0; muCoefficientAR = muCoefficientAR + 1.0;
muCoefficientAR = muCoefficientAR / (muSpeedAR * muSpeedAR); muCoefficientAR = muCoefficientAR / (muSpeedAR * muSpeedAR);
muNewSpeedR = muSpeedAR * (muSpeedAR-1.0); muNewSpeedR = muSpeedAR * (muSpeedAR - 1.0);
muNewSpeedR = muNewSpeedR + attack; muNewSpeedR = muNewSpeedR + attack;
muSpeedAR = muNewSpeedR / muSpeedAR; muSpeedAR = muNewSpeedR / muSpeedAR;
} }
} else { } else {
if (fabs(inputSampleR) > threshold) { if (fabs(inputSampleR) > threshold) {
muVaryR = threshold / fabs(inputSampleR); muVaryR = threshold / fabs(inputSampleR);
muAttackR = sqrt(fabs(muSpeedBR)); muAttackR = sqrt(fabs(muSpeedBR));
muCoefficientBR = muCoefficientBR * (muAttackR-1); muCoefficientBR = muCoefficientBR * (muAttackR - 1);
if (muVaryR < threshold) muCoefficientBR = muCoefficientBR + threshold; if (muVaryR < threshold) muCoefficientBR = muCoefficientBR + threshold;
else muCoefficientBR = muCoefficientBR + muVaryR; else muCoefficientBR = muCoefficientBR + muVaryR;
muCoefficientBR = muCoefficientBR / muAttackR; muCoefficientBR = muCoefficientBR / muAttackR;
muNewSpeedR = muSpeedBR * (muSpeedBR-1.0); muNewSpeedR = muSpeedBR * (muSpeedBR - 1.0);
muNewSpeedR = muNewSpeedR + release; muNewSpeedR = muNewSpeedR + release;
muSpeedBR = muNewSpeedR / muSpeedBR; muSpeedBR = muNewSpeedR / muSpeedBR;
if (muSpeedBR > maxRelease) muSpeedBR = maxRelease; if (muSpeedBR > maxRelease) muSpeedBR = maxRelease;
} else { } else {
muCoefficientBR = muCoefficientBR * ((muSpeedBR * muSpeedBR)-1.0); muCoefficientBR = muCoefficientBR * ((muSpeedBR * muSpeedBR) - 1.0);
muCoefficientBR = muCoefficientBR + 1.0; muCoefficientBR = muCoefficientBR + 1.0;
muCoefficientBR = muCoefficientBR / (muSpeedBR * muSpeedBR); muCoefficientBR = muCoefficientBR / (muSpeedBR * muSpeedBR);
muNewSpeedR = muSpeedBR * (muSpeedBR-1.0); muNewSpeedR = muSpeedBR * (muSpeedBR - 1.0);
muNewSpeedR = muNewSpeedR + attack; muNewSpeedR = muNewSpeedR + attack;
muSpeedBR = muNewSpeedR / muSpeedBR; muSpeedBR = muNewSpeedR / muSpeedBR;
} }
} }
//got coefficients, adjusted speeds for R // got coefficients, adjusted speeds for R
if (flip) { if (flip) {
inputSampleL *= pow(muCoefficientAL,2); inputSampleL *= pow(muCoefficientAL, 2);
inputSampleR *= pow(muCoefficientAR,2); inputSampleR *= pow(muCoefficientAR, 2);
} else { } else {
inputSampleL *= pow(muCoefficientBL,2); inputSampleL *= pow(muCoefficientBL, 2);
inputSampleR *= pow(muCoefficientBR,2); inputSampleR *= pow(muCoefficientBR, 2);
} }
inputSampleL *= muMakeupGain; inputSampleL *= muMakeupGain;
inputSampleR *= muMakeupGain; inputSampleR *= muMakeupGain;
flip = !flip; flip = !flip;
//end compressor section // end compressor section
//begin ClipOnly2 stereo as a little, compressed chunk that can be dropped into code // 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 (inputSampleL > 4.0) inputSampleL = 4.0;
if (wasPosClipL == true) { //current will be over if (inputSampleL < -4.0) inputSampleL = -4.0;
if (inputSampleL<lastSampleL) lastSampleL=0.7058208+(inputSampleL*0.2609148); if (wasPosClipL == true) { // current will be over
else lastSampleL = 0.2491717+(lastSampleL*0.7390851); if (inputSampleL < lastSampleL) lastSampleL = 0.7058208 + (inputSampleL * 0.2609148);
} wasPosClipL = false; else lastSampleL = 0.2491717 + (lastSampleL * 0.7390851);
if (inputSampleL>0.9549925859) {wasPosClipL=true;inputSampleL=0.7058208+(lastSampleL*0.2609148);} }
if (wasNegClipL == true) { //current will be -over wasPosClipL = false;
if (inputSampleL > lastSampleL) lastSampleL=-0.7058208+(inputSampleL*0.2609148); if (inputSampleL > 0.9549925859) {
else lastSampleL=-0.2491717+(lastSampleL*0.7390851); wasPosClipL = true;
} wasNegClipL = false; inputSampleL = 0.7058208 + (lastSampleL * 0.2609148);
if (inputSampleL<-0.9549925859) {wasNegClipL=true;inputSampleL=-0.7058208+(lastSampleL*0.2609148);} }
intermediateL[spacing] = inputSampleL; if (wasNegClipL == true) { // current will be -over
inputSampleL = lastSampleL; //Latency is however many samples equals one 44.1k sample if (inputSampleL > lastSampleL) lastSampleL = -0.7058208 + (inputSampleL * 0.2609148);
for (int x = spacing; x > 0; x--) intermediateL[x-1] = intermediateL[x]; else lastSampleL = -0.2491717 + (lastSampleL * 0.7390851);
lastSampleL = intermediateL[0]; //run a little buffer to handle this }
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 (inputSampleR > 4.0) inputSampleR = 4.0;
if (wasPosClipR == true) { //current will be over if (inputSampleR < -4.0) inputSampleR = -4.0;
if (inputSampleR<lastSampleR) lastSampleR=0.7058208+(inputSampleR*0.2609148); if (wasPosClipR == true) { // current will be over
else lastSampleR = 0.2491717+(lastSampleR*0.7390851); if (inputSampleR < lastSampleR) lastSampleR = 0.7058208 + (inputSampleR * 0.2609148);
} wasPosClipR = false; else lastSampleR = 0.2491717 + (lastSampleR * 0.7390851);
if (inputSampleR>0.9549925859) {wasPosClipR=true;inputSampleR=0.7058208+(lastSampleR*0.2609148);} }
if (wasNegClipR == true) { //current will be -over wasPosClipR = false;
if (inputSampleR > lastSampleR) lastSampleR=-0.7058208+(inputSampleR*0.2609148); if (inputSampleR > 0.9549925859) {
else lastSampleR=-0.2491717+(lastSampleR*0.7390851); wasPosClipR = true;
} wasNegClipR = false; inputSampleR = 0.7058208 + (lastSampleR * 0.2609148);
if (inputSampleR<-0.9549925859) {wasNegClipR=true;inputSampleR=-0.7058208+(lastSampleR*0.2609148);} }
intermediateR[spacing] = inputSampleR; if (wasNegClipR == true) { // current will be -over
inputSampleR = lastSampleR; //Latency is however many samples equals one 44.1k sample if (inputSampleR > lastSampleR) lastSampleR = -0.7058208 + (inputSampleR * 0.2609148);
for (int x = spacing; x > 0; x--) intermediateR[x-1] = intermediateR[x]; else lastSampleR = -0.2491717 + (lastSampleR * 0.7390851);
lastSampleR = intermediateR[0]; //run a little buffer to handle this }
//end ClipOnly2 stereo as a little, compressed chunk that can be dropped into code 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
if (wet<1.0) { if (wet < 1.0) {
inputSampleL = (drySampleL*(1.0-wet))+(inputSampleL*wet); inputSampleL = (drySampleL * (1.0 - wet)) + (inputSampleL * wet);
inputSampleR = (drySampleR*(1.0-wet))+(inputSampleR*wet); inputSampleR = (drySampleR * (1.0 - wet)) + (inputSampleR * wet);
} }
//begin 64 bit stereo floating point dither // begin 64 bit stereo floating point dither
//int expon; frexp((double)inputSampleL, &expon); // int expon; frexp((double)inputSampleL, &expon);
fpdL ^= fpdL << 13; fpdL ^= fpdL >> 17; fpdL ^= fpdL << 5; fpdL ^= fpdL << 13;
//inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62)); fpdL ^= fpdL >> 17;
//frexp((double)inputSampleR, &expon); fpdL ^= fpdL << 5;
fpdR ^= fpdR << 13; fpdR ^= fpdR >> 17; fpdR ^= fpdR << 5; // inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
//inputSampleR += ((double(fpdR)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62)); // frexp((double)inputSampleR, &expon);
//end 64 bit stereo floating point dither 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; *out1 = inputSampleL;
*out2 = inputSampleR; *out2 = inputSampleR;
in1++; in1++;
in2++; in2++;
out1++; out1++;
out2++; out2++;
} }
} }
private: private:
double samplerate; double samplerate;
uint32_t fpdL; uint32_t fpdL;
uint32_t fpdR; uint32_t fpdR;
//default stuff // default stuff
double muVaryL; double muVaryL;
double muAttackL; double muAttackL;
@@ -292,21 +310,22 @@ private:
double lastSampleR; double lastSampleR;
double intermediateR[16]; double intermediateR[16];
bool wasPosClipR; bool wasPosClipR;
bool wasNegClipR; //Stereo ClipOnly2 bool wasNegClipR; // Stereo ClipOnly2
float A; float A;
float B; float B;
float C; float C;
float D; float D;
float E; //parameters. Always 0-1, and we scale/alter them elsewhere. float E; // parameters. Always 0-1, and we scale/alter them elsewhere.
double clamp(double& value) { double clamp(double& value)
if (value > 1) { {
value = 1; if (value > 1) {
} else if (value < 0) { value = 1;
value = 0; } else if (value < 0) {
} value = 0;
return value; }
} return value;
}
}; };
} } // namespace trnr

File diff suppressed because it is too large Load Diff

View File

@@ -1,80 +1,77 @@
#pragma once #pragma once
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h>
#include <array> #include <array>
#include <math.h>
namespace trnr { namespace trnr {
class chebyshev { class chebyshev {
public: public:
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void process_sample(double& input, double frequency) { void process_sample(double& input, double frequency)
{
if (frequency >= 20000.f) { if (frequency >= 20000.f) { frequency = 20000.f; }
frequency = 20000.f;
}
// First calculate the prewarped digital frequency : // First calculate the prewarped digital frequency :
auto K = tanf(M_PI * frequency / samplerate); auto K = tanf(M_PI * frequency / samplerate);
// Now we calc some Coefficients : // Now we calc some Coefficients :
auto sg = sinh(passband_ripple); auto sg = sinh(passband_ripple);
auto cg = cosh(passband_ripple); auto cg = cosh(passband_ripple);
cg *= cg; cg *= cg;
std::array<double, 4> coeff; std::array<double, 4> coeff;
coeff[0] = 1 / (cg - 0.85355339059327376220042218105097); coeff[0] = 1 / (cg - 0.85355339059327376220042218105097);
coeff[1] = K * coeff[0] * sg * 1.847759065022573512256366378792; coeff[1] = K * coeff[0] * sg * 1.847759065022573512256366378792;
coeff[2] = 1 / (cg - 0.14644660940672623779957781894758); coeff[2] = 1 / (cg - 0.14644660940672623779957781894758);
coeff[3] = K * coeff[2] * sg * 0.76536686473017954345691996806; coeff[3] = K * coeff[2] * sg * 0.76536686473017954345691996806;
K *= K; // (just to optimize it a little bit) K *= K; // (just to optimize it a little bit)
// Calculate the first biquad: // Calculate the first biquad:
a0 = 1 / (coeff[1] + K + coeff[0]); a0 = 1 / (coeff[1] + K + coeff[0]);
a1 = 2 * (coeff[0] - K) * a0; a1 = 2 * (coeff[0] - K) * a0;
a2 = (coeff[1] - K - coeff[0]) * a0; a2 = (coeff[1] - K - coeff[0]) * a0;
b0 = a0 * K; b0 = a0 * K;
b1 = 2 * b0; b1 = 2 * b0;
b2 = b0; b2 = b0;
// Calculate the second biquad: // Calculate the second biquad:
a3 = 1 / (coeff[3] + K + coeff[2]); a3 = 1 / (coeff[3] + K + coeff[2]);
a4 = 2 * (coeff[2] - K) * a3; a4 = 2 * (coeff[2] - K) * a3;
a5 = (coeff[3] - K - coeff[2]) * a3; a5 = (coeff[3] - K - coeff[2]) * a3;
b3 = a3 * K; b3 = a3 * K;
b4 = 2 * b3; b4 = 2 * b3;
b5 = b3; b5 = b3;
// Then calculate the output as follows: // Then calculate the output as follows:
auto Stage1 = b0 * input + state0; auto Stage1 = b0 * input + state0;
state0 = b1 * input + a1 * Stage1 + state1; state0 = b1 * input + a1 * Stage1 + state1;
state1 = b2 * input + a2 * Stage1; state1 = b2 * input + a2 * Stage1;
input = b3 * Stage1 + state2; input = b3 * Stage1 + state2;
state2 = b4 * Stage1 + a4 * input + state3; state2 = b4 * Stage1 + a4 * input + state3;
state3 = b5 * Stage1 + a5 * input; state3 = b5 * Stage1 + a5 * input;
} }
private: private:
double samplerate = 0; double samplerate = 0;
double a0 = 0; double a0 = 0;
double a1 = 0; double a1 = 0;
double a2 = 0; double a2 = 0;
double a3 = 0; double a3 = 0;
double a4 = 0; double a4 = 0;
double a5 = 0; double a5 = 0;
double b0 = 0; double b0 = 0;
double b1 = 0; double b1 = 0;
double b2 = 0; double b2 = 0;
double b3 = 0; double b3 = 0;
double b4 = 0; double b4 = 0;
double b5 = 0; double b5 = 0;
double state0 = 0; double state0 = 0;
double state1 = 0; double state1 = 0;
double state2 = 0; double state2 = 0;
double state3 = 0; double state3 = 0;
double passband_ripple = 1; double passband_ripple = 1;
}; };
} } // namespace trnr

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h>
#include <array> #include <array>
#include <math.h>
#include <vector> #include <vector>
namespace trnr { namespace trnr {
@@ -9,306 +9,272 @@ template <typename t_sample>
// Bandpass filter based on YBandpass by Chris Johnson // Bandpass filter based on YBandpass by Chris Johnson
class ybandpass { class ybandpass {
public: public:
ybandpass(double _samplerate) ybandpass(double _samplerate)
: samplerate { _samplerate } : samplerate {_samplerate}
, A { 0.1f } , A {0.1f}
, B { 1.0f } , B {1.0f}
, C { 0.0f } , C {0.0f}
, D { 0.1f } , D {0.1f}
, E { 0.9f } , E {0.9f}
, F { 1.0f } , F {1.0f}
, fpdL { 0 } , fpdL {0}
, fpdR { 0 } , fpdR {0}
, biquad { 0 } , biquad {0}
{ {
for (int x = 0; x < biq_total; x++) { for (int x = 0; x < biq_total; x++) { biquad[x] = 0.0; }
biquad[x] = 0.0; powFactorA = 1.0;
} powFactorB = 1.0;
powFactorA = 1.0; inTrimA = 0.1;
powFactorB = 1.0; inTrimB = 0.1;
inTrimA = 0.1; outTrimA = 1.0;
inTrimB = 0.1; outTrimB = 1.0;
outTrimA = 1.0; for (int x = 0; x < fix_total; x++) {
outTrimB = 1.0; fixA[x] = 0.0;
for (int x = 0; x < fix_total; x++) { fixB[x] = 0.0;
fixA[x] = 0.0; }
fixB[x] = 0.0;
}
fpdL = 1.0; fpdL = 1.0;
while (fpdL < 16386) while (fpdL < 16386) fpdL = rand() * UINT32_MAX;
fpdL = rand() * UINT32_MAX; fpdR = 1.0;
fpdR = 1.0; while (fpdR < 16386) fpdR = rand() * UINT32_MAX;
while (fpdR < 16386) }
fpdR = rand() * UINT32_MAX;
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void set_drive(float value) void set_drive(float value) { A = value * 0.9 + 0.1; }
{
A = value * 0.9 + 0.1;
}
void set_frequency(float value)
{
B = value;
}
void set_resonance(float value)
{
C = value;
}
void set_edge(float value)
{
D = value;
}
void set_output(float value)
{
E = value;
}
void set_mix(float value)
{
F = value;
}
void processblock(t_sample** inputs, t_sample** outputs, int blockSize)
{
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
int inFramesToProcess = blockSize; void set_frequency(float value) { B = value; }
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= samplerate;
inTrimA = inTrimB; void set_resonance(float value) { C = value; }
inTrimB = A * 10.0;
biquad[biq_freq] = pow(B, 3) * 20000.0; void set_edge(float value) { D = value; }
if (biquad[biq_freq] < 15.0)
biquad[biq_freq] = 15.0;
biquad[biq_freq] /= samplerate;
biquad[biq_reso] = (pow(C, 2) * 15.0) + 0.5571;
biquad[biq_aA0] = biquad[biq_aB0];
// biquad[biq_aA1] = biquad[biq_aB1];
biquad[biq_aA2] = biquad[biq_aB2];
biquad[biq_bA1] = biquad[biq_bB1];
biquad[biq_bA2] = biquad[biq_bB2];
// previous run through the buffer is still in the filter, so we move it
// to the A section and now it's the new starting point.
double K = tan(M_PI * biquad[biq_freq]);
double norm = 1.0 / (1.0 + K / biquad[biq_reso] + K * K);
biquad[biq_aB0] = K / biquad[biq_reso] * norm;
// biquad[biq_aB1] = 0.0; //bandpass can simplify the biquad kernel: leave out this multiply
biquad[biq_aB2] = -biquad[biq_aB0];
biquad[biq_bB1] = 2.0 * (K * K - 1.0) * norm;
biquad[biq_bB2] = (1.0 - K / biquad[biq_reso] + K * K) * norm;
// for the coefficient-interpolated biquad filter
powFactorA = powFactorB; void set_output(float value) { E = value; }
powFactorB = pow(D + 0.9, 4);
// 1.0 == target neutral void set_mix(float value) { F = value; }
outTrimA = outTrimB; void processblock(t_sample** inputs, t_sample** outputs, int blockSize)
outTrimB = E; {
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
double wet = F; int inFramesToProcess = blockSize;
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= samplerate;
fixA[fix_freq] = fixB[fix_freq] = 20000.0 / samplerate; inTrimA = inTrimB;
fixA[fix_reso] = fixB[fix_reso] = 0.7071; // butterworth Q inTrimB = A * 10.0;
K = tan(M_PI * fixA[fix_freq]); biquad[biq_freq] = pow(B, 3) * 20000.0;
norm = 1.0 / (1.0 + K / fixA[fix_reso] + K * K); if (biquad[biq_freq] < 15.0) biquad[biq_freq] = 15.0;
fixA[fix_a0] = fixB[fix_a0] = K * K * norm; biquad[biq_freq] /= samplerate;
fixA[fix_a1] = fixB[fix_a1] = 2.0 * fixA[fix_a0]; biquad[biq_reso] = (pow(C, 2) * 15.0) + 0.5571;
fixA[fix_a2] = fixB[fix_a2] = fixA[fix_a0]; biquad[biq_aA0] = biquad[biq_aB0];
fixA[fix_b1] = fixB[fix_b1] = 2.0 * (K * K - 1.0) * norm; // biquad[biq_aA1] = biquad[biq_aB1];
fixA[fix_b2] = fixB[fix_b2] = (1.0 - K / fixA[fix_reso] + K * K) * norm; biquad[biq_aA2] = biquad[biq_aB2];
// for the fixed-position biquad filter biquad[biq_bA1] = biquad[biq_bB1];
biquad[biq_bA2] = biquad[biq_bB2];
// previous run through the buffer is still in the filter, so we move it
// to the A section and now it's the new starting point.
double K = tan(M_PI * biquad[biq_freq]);
double norm = 1.0 / (1.0 + K / biquad[biq_reso] + K * K);
biquad[biq_aB0] = K / biquad[biq_reso] * norm;
// biquad[biq_aB1] = 0.0; //bandpass can simplify the biquad kernel: leave out this multiply
biquad[biq_aB2] = -biquad[biq_aB0];
biquad[biq_bB1] = 2.0 * (K * K - 1.0) * norm;
biquad[biq_bB2] = (1.0 - K / biquad[biq_reso] + K * K) * norm;
// for the coefficient-interpolated biquad filter
for (int s = 0; s < blockSize; s++) { powFactorA = powFactorB;
double inputSampleL = *in1; powFactorB = pow(D + 0.9, 4);
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;
double temp = (double)s / inFramesToProcess; // 1.0 == target neutral
biquad[biq_a0] = (biquad[biq_aA0] * temp) + (biquad[biq_aB0] * (1.0 - temp));
// biquad[biq_a1] = (biquad[biq_aA1]*temp)+(biquad[biq_aB1]*(1.0-temp));
biquad[biq_a2] = (biquad[biq_aA2] * temp) + (biquad[biq_aB2] * (1.0 - temp));
biquad[biq_b1] = (biquad[biq_bA1] * temp) + (biquad[biq_bB1] * (1.0 - temp));
biquad[biq_b2] = (biquad[biq_bA2] * temp) + (biquad[biq_bB2] * (1.0 - temp));
// this is the interpolation code for the biquad
double powFactor = (powFactorA * temp) + (powFactorB * (1.0 - temp));
double inTrim = (inTrimA * temp) + (inTrimB * (1.0 - temp));
double outTrim = (outTrimA * temp) + (outTrimB * (1.0 - temp));
inputSampleL *= inTrim; outTrimA = outTrimB;
inputSampleR *= inTrim; outTrimB = E;
temp = (inputSampleL * fixA[fix_a0]) + fixA[fix_sL1]; double wet = F;
fixA[fix_sL1] = (inputSampleL * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sL2];
fixA[fix_sL2] = (inputSampleL * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixA[fix_a0]) + fixA[fix_sR1];
fixA[fix_sR1] = (inputSampleR * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sR2];
fixA[fix_sR2] = (inputSampleR * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
// encode/decode courtesy of torridgristle under the MIT license fixA[fix_freq] = fixB[fix_freq] = 20000.0 / samplerate;
if (inputSampleL > 1.0) fixA[fix_reso] = fixB[fix_reso] = 0.7071; // butterworth Q
inputSampleL = 1.0;
else if (inputSampleL > 0.0)
inputSampleL = 1.0 - pow(1.0 - inputSampleL, powFactor);
if (inputSampleL < -1.0)
inputSampleL = -1.0;
else if (inputSampleL < 0.0)
inputSampleL = -1.0 + pow(1.0 + inputSampleL, powFactor);
if (inputSampleR > 1.0)
inputSampleR = 1.0;
else if (inputSampleR > 0.0)
inputSampleR = 1.0 - pow(1.0 - inputSampleR, powFactor);
if (inputSampleR < -1.0)
inputSampleR = -1.0;
else if (inputSampleR < 0.0)
inputSampleR = -1.0 + pow(1.0 + inputSampleR, powFactor);
temp = (inputSampleL * biquad[biq_a0]) + biquad[biq_sL1]; K = tan(M_PI * fixA[fix_freq]);
biquad[biq_sL1] = -(temp * biquad[biq_b1]) + biquad[biq_sL2]; norm = 1.0 / (1.0 + K / fixA[fix_reso] + K * K);
biquad[biq_sL2] = (inputSampleL * biquad[biq_a2]) - (temp * biquad[biq_b2]); fixA[fix_a0] = fixB[fix_a0] = K * K * norm;
inputSampleL = temp; // coefficient interpolating biquad filter fixA[fix_a1] = fixB[fix_a1] = 2.0 * fixA[fix_a0];
temp = (inputSampleR * biquad[biq_a0]) + biquad[biq_sR1]; fixA[fix_a2] = fixB[fix_a2] = fixA[fix_a0];
biquad[biq_sR1] = -(temp * biquad[biq_b1]) + biquad[biq_sR2]; fixA[fix_b1] = fixB[fix_b1] = 2.0 * (K * K - 1.0) * norm;
biquad[biq_sR2] = (inputSampleR * biquad[biq_a2]) - (temp * biquad[biq_b2]); fixA[fix_b2] = fixB[fix_b2] = (1.0 - K / fixA[fix_reso] + K * K) * norm;
inputSampleR = temp; // coefficient interpolating biquad filter // for the fixed-position biquad filter
// encode/decode courtesy of torridgristle under the MIT license for (int s = 0; s < blockSize; s++) {
if (inputSampleL > 1.0) double inputSampleL = *in1;
inputSampleL = 1.0; double inputSampleR = *in2;
else if (inputSampleL > 0.0) if (fabs(inputSampleL) < 1.18e-23) inputSampleL = fpdL * 1.18e-17;
inputSampleL = 1.0 - pow(1.0 - inputSampleL, (1.0 / powFactor)); if (fabs(inputSampleR) < 1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (inputSampleL < -1.0) double drySampleL = inputSampleL;
inputSampleL = -1.0; double drySampleR = inputSampleR;
else if (inputSampleL < 0.0)
inputSampleL = -1.0 + pow(1.0 + inputSampleL, (1.0 / powFactor));
if (inputSampleR > 1.0)
inputSampleR = 1.0;
else if (inputSampleR > 0.0)
inputSampleR = 1.0 - pow(1.0 - inputSampleR, (1.0 / powFactor));
if (inputSampleR < -1.0)
inputSampleR = -1.0;
else if (inputSampleR < 0.0)
inputSampleR = -1.0 + pow(1.0 + inputSampleR, (1.0 / powFactor));
inputSampleL *= outTrim; double temp = (double)s / inFramesToProcess;
inputSampleR *= outTrim; biquad[biq_a0] = (biquad[biq_aA0] * temp) + (biquad[biq_aB0] * (1.0 - temp));
// biquad[biq_a1] = (biquad[biq_aA1]*temp)+(biquad[biq_aB1]*(1.0-temp));
biquad[biq_a2] = (biquad[biq_aA2] * temp) + (biquad[biq_aB2] * (1.0 - temp));
biquad[biq_b1] = (biquad[biq_bA1] * temp) + (biquad[biq_bB1] * (1.0 - temp));
biquad[biq_b2] = (biquad[biq_bA2] * temp) + (biquad[biq_bB2] * (1.0 - temp));
// this is the interpolation code for the biquad
double powFactor = (powFactorA * temp) + (powFactorB * (1.0 - temp));
double inTrim = (inTrimA * temp) + (inTrimB * (1.0 - temp));
double outTrim = (outTrimA * temp) + (outTrimB * (1.0 - temp));
temp = (inputSampleL * fixB[fix_a0]) + fixB[fix_sL1]; inputSampleL *= inTrim;
fixB[fix_sL1] = (inputSampleL * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sL2]; inputSampleR *= inTrim;
fixB[fix_sL2] = (inputSampleL * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixB[fix_a0]) + fixB[fix_sR1];
fixB[fix_sR1] = (inputSampleR * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sR2];
fixB[fix_sR2] = (inputSampleR * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
if (wet < 1.0) { temp = (inputSampleL * fixA[fix_a0]) + fixA[fix_sL1];
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0 - wet)); fixA[fix_sL1] = (inputSampleL * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sL2];
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0 - wet)); fixA[fix_sL2] = (inputSampleL * fixA[fix_a2]) - (temp * fixA[fix_b2]);
} inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixA[fix_a0]) + fixA[fix_sR1];
fixA[fix_sR1] = (inputSampleR * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sR2];
fixA[fix_sR2] = (inputSampleR * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
// begin 32 bit stereo floating point dither // encode/decode courtesy of torridgristle under the MIT license
int expon; if (inputSampleL > 1.0) inputSampleL = 1.0;
frexpf((float)inputSampleL, &expon); else if (inputSampleL > 0.0) inputSampleL = 1.0 - pow(1.0 - inputSampleL, powFactor);
fpdL ^= fpdL << 13; if (inputSampleL < -1.0) inputSampleL = -1.0;
fpdL ^= fpdL >> 17; else if (inputSampleL < 0.0) inputSampleL = -1.0 + pow(1.0 + inputSampleL, powFactor);
fpdL ^= fpdL << 5; if (inputSampleR > 1.0) inputSampleR = 1.0;
inputSampleL += ((double(fpdL) - uint32_t(0x7fffffff)) * 5.5e-36l * pow(2, expon + 62)); else if (inputSampleR > 0.0) inputSampleR = 1.0 - pow(1.0 - inputSampleR, powFactor);
frexpf((float)inputSampleR, &expon); if (inputSampleR < -1.0) inputSampleR = -1.0;
fpdR ^= fpdR << 13; else if (inputSampleR < 0.0) inputSampleR = -1.0 + pow(1.0 + inputSampleR, powFactor);
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; temp = (inputSampleL * biquad[biq_a0]) + biquad[biq_sL1];
*out2 = inputSampleR; biquad[biq_sL1] = -(temp * biquad[biq_b1]) + biquad[biq_sL2];
biquad[biq_sL2] = (inputSampleL * biquad[biq_a2]) - (temp * biquad[biq_b2]);
inputSampleL = temp; // coefficient interpolating biquad filter
temp = (inputSampleR * biquad[biq_a0]) + biquad[biq_sR1];
biquad[biq_sR1] = -(temp * biquad[biq_b1]) + biquad[biq_sR2];
biquad[biq_sR2] = (inputSampleR * biquad[biq_a2]) - (temp * biquad[biq_b2]);
inputSampleR = temp; // coefficient interpolating biquad filter
in1++; // encode/decode courtesy of torridgristle under the MIT license
in2++; if (inputSampleL > 1.0) inputSampleL = 1.0;
out1++; else if (inputSampleL > 0.0) inputSampleL = 1.0 - pow(1.0 - inputSampleL, (1.0 / powFactor));
out2++; if (inputSampleL < -1.0) inputSampleL = -1.0;
} else if (inputSampleL < 0.0) inputSampleL = -1.0 + pow(1.0 + inputSampleL, (1.0 / powFactor));
} if (inputSampleR > 1.0) inputSampleR = 1.0;
else if (inputSampleR > 0.0) inputSampleR = 1.0 - pow(1.0 - inputSampleR, (1.0 / powFactor));
if (inputSampleR < -1.0) inputSampleR = -1.0;
else if (inputSampleR < 0.0) inputSampleR = -1.0 + pow(1.0 + inputSampleR, (1.0 / powFactor));
inputSampleL *= outTrim;
inputSampleR *= outTrim;
temp = (inputSampleL * fixB[fix_a0]) + fixB[fix_sL1];
fixB[fix_sL1] = (inputSampleL * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sL2];
fixB[fix_sL2] = (inputSampleL * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixB[fix_a0]) + fixB[fix_sR1];
fixB[fix_sR1] = (inputSampleR * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sR2];
fixB[fix_sR2] = (inputSampleR * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
if (wet < 1.0) {
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0 - wet));
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0 - wet));
}
// 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++;
}
}
private: private:
double samplerate; double samplerate;
enum {
biq_freq,
biq_reso,
biq_a0,
biq_a1,
biq_a2,
biq_b1,
biq_b2,
biq_aA0,
biq_aA1,
biq_aA2,
biq_bA1,
biq_bA2,
biq_aB0,
biq_aB1,
biq_aB2,
biq_bB1,
biq_bB2,
biq_sL1,
biq_sL2,
biq_sR1,
biq_sR2,
biq_total
}; // coefficient interpolating biquad filter, stereo
std::array<double, biq_total> biquad;
double powFactorA; enum {
double powFactorB; biq_freq,
double inTrimA; biq_reso,
double inTrimB; biq_a0,
double outTrimA; biq_a1,
double outTrimB; biq_a2,
biq_b1,
biq_b2,
biq_aA0,
biq_aA1,
biq_aA2,
biq_bA1,
biq_bA2,
biq_aB0,
biq_aB1,
biq_aB2,
biq_bB1,
biq_bB2,
biq_sL1,
biq_sL2,
biq_sR1,
biq_sR2,
biq_total
}; // coefficient interpolating biquad filter, stereo
enum { std::array<double, biq_total> biquad;
fix_freq,
fix_reso,
fix_a0,
fix_a1,
fix_a2,
fix_b1,
fix_b2,
fix_sL1,
fix_sL2,
fix_sR1,
fix_sR2,
fix_total
}; // fixed frequency biquad filter for ultrasonics, stereo
std::array<double, fix_total> fixA;
std::array<double, fix_total> fixB;
uint32_t fpdL; double powFactorA;
uint32_t fpdR; double powFactorB;
// default stuff double inTrimA;
double inTrimB;
double outTrimA;
double outTrimB;
float A; enum {
float B; fix_freq,
float C; fix_reso,
float D; fix_a0,
float E; fix_a1,
float F; // parameters. Always 0-1, and we scale/alter them elsewhere. fix_a2,
fix_b1,
fix_b2,
fix_sL1,
fix_sL2,
fix_sR1,
fix_sR2,
fix_total
}; // fixed frequency biquad filter for ultrasonics, stereo
std::array<double, fix_total> fixA;
std::array<double, fix_total> fixB;
uint32_t fpdL;
uint32_t fpdR;
// default stuff
float A;
float B;
float C;
float D;
float E;
float F; // parameters. Always 0-1, and we scale/alter them elsewhere.
}; };
} } // namespace trnr

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h>
#include <array> #include <array>
#include <math.h>
#include <vector> #include <vector>
namespace trnr { namespace trnr {
@@ -9,306 +9,272 @@ template <typename t_sample>
// Highpass filter based on YHighpass by Chris Johnson // Highpass filter based on YHighpass by Chris Johnson
class yhighpass { class yhighpass {
public: public:
yhighpass(double _samplerate) yhighpass(double _samplerate)
: samplerate { _samplerate } : samplerate {_samplerate}
, A { 0.1f } , A {0.1f}
, B { 1.0f } , B {1.0f}
, C { 0.0f } , C {0.0f}
, D { 0.1f } , D {0.1f}
, E { 0.9f } , E {0.9f}
, F { 1.0f } , F {1.0f}
, fpdL { 0 } , fpdL {0}
, fpdR { 0 } , fpdR {0}
, biquad { 0 } , biquad {0}
{ {
for (int x = 0; x < biq_total; x++) { for (int x = 0; x < biq_total; x++) { biquad[x] = 0.0; }
biquad[x] = 0.0; powFactorA = 1.0;
} powFactorB = 1.0;
powFactorA = 1.0; inTrimA = 0.1;
powFactorB = 1.0; inTrimB = 0.1;
inTrimA = 0.1; outTrimA = 1.0;
inTrimB = 0.1; outTrimB = 1.0;
outTrimA = 1.0; for (int x = 0; x < fix_total; x++) {
outTrimB = 1.0; fixA[x] = 0.0;
for (int x = 0; x < fix_total; x++) { fixB[x] = 0.0;
fixA[x] = 0.0; }
fixB[x] = 0.0;
}
fpdL = 1.0; fpdL = 1.0;
while (fpdL < 16386) while (fpdL < 16386) fpdL = rand() * UINT32_MAX;
fpdL = rand() * UINT32_MAX; fpdR = 1.0;
fpdR = 1.0; while (fpdR < 16386) fpdR = rand() * UINT32_MAX;
while (fpdR < 16386) }
fpdR = rand() * UINT32_MAX;
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void set_drive(float value) void set_drive(float value) { A = value * 0.9 + 0.1; }
{
A = value * 0.9 + 0.1;
}
void set_frequency(float value)
{
B = value;
}
void set_resonance(float value)
{
C = value;
}
void set_edge(float value)
{
D = value;
}
void set_output(float value)
{
E = value;
}
void set_mix(float value)
{
F = value;
}
void processblock(t_sample** inputs, t_sample** outputs, int blockSize)
{
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
int inFramesToProcess = blockSize; void set_frequency(float value) { B = value; }
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= samplerate;
inTrimA = inTrimB; void set_resonance(float value) { C = value; }
inTrimB = A * 10.0;
biquad[biq_freq] = pow(B, 3) * 20000.0; void set_edge(float value) { D = value; }
if (biquad[biq_freq] < 15.0)
biquad[biq_freq] = 15.0;
biquad[biq_freq] /= samplerate;
biquad[biq_reso] = (pow(C, 2) * 15.0) + 0.5571;
biquad[biq_aA0] = biquad[biq_aB0];
biquad[biq_aA1] = biquad[biq_aB1];
biquad[biq_aA2] = biquad[biq_aB2];
biquad[biq_bA1] = biquad[biq_bB1];
biquad[biq_bA2] = biquad[biq_bB2];
// previous run through the buffer is still in the filter, so we move it
// to the A section and now it's the new starting point.
double K = tan(M_PI * biquad[biq_freq]);
double norm = 1.0 / (1.0 + K / biquad[biq_reso] + K * K);
biquad[biq_aB0] = norm;
biquad[biq_aB1] = -2.0 * biquad[biq_aB0];
biquad[biq_aB2] = biquad[biq_aB0];
biquad[biq_bB1] = 2.0 * (K * K - 1.0) * norm;
biquad[biq_bB2] = (1.0 - K / biquad[biq_reso] + K * K) * norm;
// for the coefficient-interpolated biquad filter
powFactorA = powFactorB; void set_output(float value) { E = value; }
powFactorB = pow(D + 0.9, 4);
// 1.0 == target neutral void set_mix(float value) { F = value; }
outTrimA = outTrimB; void processblock(t_sample** inputs, t_sample** outputs, int blockSize)
outTrimB = E; {
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
double wet = F; int inFramesToProcess = blockSize;
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= samplerate;
fixA[fix_freq] = fixB[fix_freq] = 20000.0 / samplerate; inTrimA = inTrimB;
fixA[fix_reso] = fixB[fix_reso] = 0.7071; // butterworth Q inTrimB = A * 10.0;
K = tan(M_PI * fixA[fix_freq]); biquad[biq_freq] = pow(B, 3) * 20000.0;
norm = 1.0 / (1.0 + K / fixA[fix_reso] + K * K); if (biquad[biq_freq] < 15.0) biquad[biq_freq] = 15.0;
fixA[fix_a0] = fixB[fix_a0] = K * K * norm; biquad[biq_freq] /= samplerate;
fixA[fix_a1] = fixB[fix_a1] = 2.0 * fixA[fix_a0]; biquad[biq_reso] = (pow(C, 2) * 15.0) + 0.5571;
fixA[fix_a2] = fixB[fix_a2] = fixA[fix_a0]; biquad[biq_aA0] = biquad[biq_aB0];
fixA[fix_b1] = fixB[fix_b1] = 2.0 * (K * K - 1.0) * norm; biquad[biq_aA1] = biquad[biq_aB1];
fixA[fix_b2] = fixB[fix_b2] = (1.0 - K / fixA[fix_reso] + K * K) * norm; biquad[biq_aA2] = biquad[biq_aB2];
// for the fixed-position biquad filter biquad[biq_bA1] = biquad[biq_bB1];
biquad[biq_bA2] = biquad[biq_bB2];
// previous run through the buffer is still in the filter, so we move it
// to the A section and now it's the new starting point.
double K = tan(M_PI * biquad[biq_freq]);
double norm = 1.0 / (1.0 + K / biquad[biq_reso] + K * K);
biquad[biq_aB0] = norm;
biquad[biq_aB1] = -2.0 * biquad[biq_aB0];
biquad[biq_aB2] = biquad[biq_aB0];
biquad[biq_bB1] = 2.0 * (K * K - 1.0) * norm;
biquad[biq_bB2] = (1.0 - K / biquad[biq_reso] + K * K) * norm;
// for the coefficient-interpolated biquad filter
for (int s = 0; s < blockSize; s++) { powFactorA = powFactorB;
double inputSampleL = *in1; powFactorB = pow(D + 0.9, 4);
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;
double temp = (double)s / inFramesToProcess; // 1.0 == target neutral
biquad[biq_a0] = (biquad[biq_aA0] * temp) + (biquad[biq_aB0] * (1.0 - temp));
biquad[biq_a1] = (biquad[biq_aA1] * temp) + (biquad[biq_aB1] * (1.0 - temp));
biquad[biq_a2] = (biquad[biq_aA2] * temp) + (biquad[biq_aB2] * (1.0 - temp));
biquad[biq_b1] = (biquad[biq_bA1] * temp) + (biquad[biq_bB1] * (1.0 - temp));
biquad[biq_b2] = (biquad[biq_bA2] * temp) + (biquad[biq_bB2] * (1.0 - temp));
// this is the interpolation code for the biquad
double powFactor = (powFactorA * temp) + (powFactorB * (1.0 - temp));
double inTrim = (inTrimA * temp) + (inTrimB * (1.0 - temp));
double outTrim = (outTrimA * temp) + (outTrimB * (1.0 - temp));
inputSampleL *= inTrim; outTrimA = outTrimB;
inputSampleR *= inTrim; outTrimB = E;
temp = (inputSampleL * fixA[fix_a0]) + fixA[fix_sL1]; double wet = F;
fixA[fix_sL1] = (inputSampleL * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sL2];
fixA[fix_sL2] = (inputSampleL * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixA[fix_a0]) + fixA[fix_sR1];
fixA[fix_sR1] = (inputSampleR * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sR2];
fixA[fix_sR2] = (inputSampleR * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
// encode/decode courtesy of torridgristle under the MIT license fixA[fix_freq] = fixB[fix_freq] = 20000.0 / samplerate;
if (inputSampleL > 1.0) fixA[fix_reso] = fixB[fix_reso] = 0.7071; // butterworth Q
inputSampleL = 1.0;
else if (inputSampleL > 0.0)
inputSampleL = 1.0 - pow(1.0 - inputSampleL, powFactor);
if (inputSampleL < -1.0)
inputSampleL = -1.0;
else if (inputSampleL < 0.0)
inputSampleL = -1.0 + pow(1.0 + inputSampleL, powFactor);
if (inputSampleR > 1.0)
inputSampleR = 1.0;
else if (inputSampleR > 0.0)
inputSampleR = 1.0 - pow(1.0 - inputSampleR, powFactor);
if (inputSampleR < -1.0)
inputSampleR = -1.0;
else if (inputSampleR < 0.0)
inputSampleR = -1.0 + pow(1.0 + inputSampleR, powFactor);
temp = (inputSampleL * biquad[biq_a0]) + biquad[biq_sL1]; K = tan(M_PI * fixA[fix_freq]);
biquad[biq_sL1] = (inputSampleL * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sL2]; norm = 1.0 / (1.0 + K / fixA[fix_reso] + K * K);
biquad[biq_sL2] = (inputSampleL * biquad[biq_a2]) - (temp * biquad[biq_b2]); fixA[fix_a0] = fixB[fix_a0] = K * K * norm;
inputSampleL = temp; // coefficient interpolating biquad filter fixA[fix_a1] = fixB[fix_a1] = 2.0 * fixA[fix_a0];
temp = (inputSampleR * biquad[biq_a0]) + biquad[biq_sR1]; fixA[fix_a2] = fixB[fix_a2] = fixA[fix_a0];
biquad[biq_sR1] = (inputSampleR * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sR2]; fixA[fix_b1] = fixB[fix_b1] = 2.0 * (K * K - 1.0) * norm;
biquad[biq_sR2] = (inputSampleR * biquad[biq_a2]) - (temp * biquad[biq_b2]); fixA[fix_b2] = fixB[fix_b2] = (1.0 - K / fixA[fix_reso] + K * K) * norm;
inputSampleR = temp; // coefficient interpolating biquad filter // for the fixed-position biquad filter
// encode/decode courtesy of torridgristle under the MIT license for (int s = 0; s < blockSize; s++) {
if (inputSampleL > 1.0) double inputSampleL = *in1;
inputSampleL = 1.0; double inputSampleR = *in2;
else if (inputSampleL > 0.0) if (fabs(inputSampleL) < 1.18e-23) inputSampleL = fpdL * 1.18e-17;
inputSampleL = 1.0 - pow(1.0 - inputSampleL, (1.0 / powFactor)); if (fabs(inputSampleR) < 1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (inputSampleL < -1.0) double drySampleL = inputSampleL;
inputSampleL = -1.0; double drySampleR = inputSampleR;
else if (inputSampleL < 0.0)
inputSampleL = -1.0 + pow(1.0 + inputSampleL, (1.0 / powFactor));
if (inputSampleR > 1.0)
inputSampleR = 1.0;
else if (inputSampleR > 0.0)
inputSampleR = 1.0 - pow(1.0 - inputSampleR, (1.0 / powFactor));
if (inputSampleR < -1.0)
inputSampleR = -1.0;
else if (inputSampleR < 0.0)
inputSampleR = -1.0 + pow(1.0 + inputSampleR, (1.0 / powFactor));
inputSampleL *= outTrim; double temp = (double)s / inFramesToProcess;
inputSampleR *= outTrim; biquad[biq_a0] = (biquad[biq_aA0] * temp) + (biquad[biq_aB0] * (1.0 - temp));
biquad[biq_a1] = (biquad[biq_aA1] * temp) + (biquad[biq_aB1] * (1.0 - temp));
biquad[biq_a2] = (biquad[biq_aA2] * temp) + (biquad[biq_aB2] * (1.0 - temp));
biquad[biq_b1] = (biquad[biq_bA1] * temp) + (biquad[biq_bB1] * (1.0 - temp));
biquad[biq_b2] = (biquad[biq_bA2] * temp) + (biquad[biq_bB2] * (1.0 - temp));
// this is the interpolation code for the biquad
double powFactor = (powFactorA * temp) + (powFactorB * (1.0 - temp));
double inTrim = (inTrimA * temp) + (inTrimB * (1.0 - temp));
double outTrim = (outTrimA * temp) + (outTrimB * (1.0 - temp));
temp = (inputSampleL * fixB[fix_a0]) + fixB[fix_sL1]; inputSampleL *= inTrim;
fixB[fix_sL1] = (inputSampleL * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sL2]; inputSampleR *= inTrim;
fixB[fix_sL2] = (inputSampleL * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixB[fix_a0]) + fixB[fix_sR1];
fixB[fix_sR1] = (inputSampleR * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sR2];
fixB[fix_sR2] = (inputSampleR * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
if (wet < 1.0) { temp = (inputSampleL * fixA[fix_a0]) + fixA[fix_sL1];
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0 - wet)); fixA[fix_sL1] = (inputSampleL * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sL2];
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0 - wet)); fixA[fix_sL2] = (inputSampleL * fixA[fix_a2]) - (temp * fixA[fix_b2]);
} inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixA[fix_a0]) + fixA[fix_sR1];
fixA[fix_sR1] = (inputSampleR * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sR2];
fixA[fix_sR2] = (inputSampleR * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
// begin 32 bit stereo floating point dither // encode/decode courtesy of torridgristle under the MIT license
int expon; if (inputSampleL > 1.0) inputSampleL = 1.0;
frexpf((float)inputSampleL, &expon); else if (inputSampleL > 0.0) inputSampleL = 1.0 - pow(1.0 - inputSampleL, powFactor);
fpdL ^= fpdL << 13; if (inputSampleL < -1.0) inputSampleL = -1.0;
fpdL ^= fpdL >> 17; else if (inputSampleL < 0.0) inputSampleL = -1.0 + pow(1.0 + inputSampleL, powFactor);
fpdL ^= fpdL << 5; if (inputSampleR > 1.0) inputSampleR = 1.0;
inputSampleL += ((double(fpdL) - uint32_t(0x7fffffff)) * 5.5e-36l * pow(2, expon + 62)); else if (inputSampleR > 0.0) inputSampleR = 1.0 - pow(1.0 - inputSampleR, powFactor);
frexpf((float)inputSampleR, &expon); if (inputSampleR < -1.0) inputSampleR = -1.0;
fpdR ^= fpdR << 13; else if (inputSampleR < 0.0) inputSampleR = -1.0 + pow(1.0 + inputSampleR, powFactor);
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; temp = (inputSampleL * biquad[biq_a0]) + biquad[biq_sL1];
*out2 = inputSampleR; biquad[biq_sL1] = (inputSampleL * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sL2];
biquad[biq_sL2] = (inputSampleL * biquad[biq_a2]) - (temp * biquad[biq_b2]);
inputSampleL = temp; // coefficient interpolating biquad filter
temp = (inputSampleR * biquad[biq_a0]) + biquad[biq_sR1];
biquad[biq_sR1] = (inputSampleR * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sR2];
biquad[biq_sR2] = (inputSampleR * biquad[biq_a2]) - (temp * biquad[biq_b2]);
inputSampleR = temp; // coefficient interpolating biquad filter
in1++; // encode/decode courtesy of torridgristle under the MIT license
in2++; if (inputSampleL > 1.0) inputSampleL = 1.0;
out1++; else if (inputSampleL > 0.0) inputSampleL = 1.0 - pow(1.0 - inputSampleL, (1.0 / powFactor));
out2++; if (inputSampleL < -1.0) inputSampleL = -1.0;
} else if (inputSampleL < 0.0) inputSampleL = -1.0 + pow(1.0 + inputSampleL, (1.0 / powFactor));
} if (inputSampleR > 1.0) inputSampleR = 1.0;
else if (inputSampleR > 0.0) inputSampleR = 1.0 - pow(1.0 - inputSampleR, (1.0 / powFactor));
if (inputSampleR < -1.0) inputSampleR = -1.0;
else if (inputSampleR < 0.0) inputSampleR = -1.0 + pow(1.0 + inputSampleR, (1.0 / powFactor));
inputSampleL *= outTrim;
inputSampleR *= outTrim;
temp = (inputSampleL * fixB[fix_a0]) + fixB[fix_sL1];
fixB[fix_sL1] = (inputSampleL * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sL2];
fixB[fix_sL2] = (inputSampleL * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixB[fix_a0]) + fixB[fix_sR1];
fixB[fix_sR1] = (inputSampleR * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sR2];
fixB[fix_sR2] = (inputSampleR * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
if (wet < 1.0) {
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0 - wet));
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0 - wet));
}
// 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++;
}
}
private: private:
double samplerate; double samplerate;
enum {
biq_freq,
biq_reso,
biq_a0,
biq_a1,
biq_a2,
biq_b1,
biq_b2,
biq_aA0,
biq_aA1,
biq_aA2,
biq_bA1,
biq_bA2,
biq_aB0,
biq_aB1,
biq_aB2,
biq_bB1,
biq_bB2,
biq_sL1,
biq_sL2,
biq_sR1,
biq_sR2,
biq_total
}; // coefficient interpolating biquad filter, stereo
std::array<double, biq_total> biquad;
double powFactorA; enum {
double powFactorB; biq_freq,
double inTrimA; biq_reso,
double inTrimB; biq_a0,
double outTrimA; biq_a1,
double outTrimB; biq_a2,
biq_b1,
biq_b2,
biq_aA0,
biq_aA1,
biq_aA2,
biq_bA1,
biq_bA2,
biq_aB0,
biq_aB1,
biq_aB2,
biq_bB1,
biq_bB2,
biq_sL1,
biq_sL2,
biq_sR1,
biq_sR2,
biq_total
}; // coefficient interpolating biquad filter, stereo
enum { std::array<double, biq_total> biquad;
fix_freq,
fix_reso,
fix_a0,
fix_a1,
fix_a2,
fix_b1,
fix_b2,
fix_sL1,
fix_sL2,
fix_sR1,
fix_sR2,
fix_total
}; // fixed frequency biquad filter for ultrasonics, stereo
std::array<double, fix_total> fixA;
std::array<double, fix_total> fixB;
uint32_t fpdL; double powFactorA;
uint32_t fpdR; double powFactorB;
// default stuff double inTrimA;
double inTrimB;
double outTrimA;
double outTrimB;
float A; enum {
float B; fix_freq,
float C; fix_reso,
float D; fix_a0,
float E; fix_a1,
float F; // parameters. Always 0-1, and we scale/alter them elsewhere. fix_a2,
fix_b1,
fix_b2,
fix_sL1,
fix_sL2,
fix_sR1,
fix_sR2,
fix_total
}; // fixed frequency biquad filter for ultrasonics, stereo
std::array<double, fix_total> fixA;
std::array<double, fix_total> fixB;
uint32_t fpdL;
uint32_t fpdR;
// default stuff
float A;
float B;
float C;
float D;
float E;
float F; // parameters. Always 0-1, and we scale/alter them elsewhere.
}; };
} } // namespace trnr

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h>
#include <array> #include <array>
#include <math.h>
#include <vector> #include <vector>
namespace trnr { namespace trnr {
@@ -9,306 +9,272 @@ template <typename t_sample>
// Lowpass filter based on YLowpass by Chris Johnson // Lowpass filter based on YLowpass by Chris Johnson
class ylowpass { class ylowpass {
public: public:
ylowpass(double _samplerate) ylowpass(double _samplerate)
: samplerate { _samplerate } : samplerate {_samplerate}
, A { 0.1f } , A {0.1f}
, B { 1.0f } , B {1.0f}
, C { 0.0f } , C {0.0f}
, D { 0.1f } , D {0.1f}
, E { 0.9f } , E {0.9f}
, F { 1.0f } , F {1.0f}
, fpdL { 0 } , fpdL {0}
, fpdR { 0 } , fpdR {0}
, biquad { 0 } , biquad {0}
{ {
for (int x = 0; x < biq_total; x++) { for (int x = 0; x < biq_total; x++) { biquad[x] = 0.0; }
biquad[x] = 0.0; powFactorA = 1.0;
} powFactorB = 1.0;
powFactorA = 1.0; inTrimA = 0.1;
powFactorB = 1.0; inTrimB = 0.1;
inTrimA = 0.1; outTrimA = 1.0;
inTrimB = 0.1; outTrimB = 1.0;
outTrimA = 1.0; for (int x = 0; x < fix_total; x++) {
outTrimB = 1.0; fixA[x] = 0.0;
for (int x = 0; x < fix_total; x++) { fixB[x] = 0.0;
fixA[x] = 0.0; }
fixB[x] = 0.0;
}
fpdL = 1.0; fpdL = 1.0;
while (fpdL < 16386) while (fpdL < 16386) fpdL = rand() * UINT32_MAX;
fpdL = rand() * UINT32_MAX; fpdR = 1.0;
fpdR = 1.0; while (fpdR < 16386) fpdR = rand() * UINT32_MAX;
while (fpdR < 16386) }
fpdR = rand() * UINT32_MAX;
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void set_drive(float value) void set_drive(float value) { A = value * 0.9 + 0.1; }
{
A = value * 0.9 + 0.1; void set_frequency(float value) { B = value; }
}
void set_frequency(float value) void set_resonance(float value) { C = value; }
{
B = value; void set_edge(float value) { D = value; }
}
void set_resonance(float value) void set_output(float value) { E = value; }
{
C = value; void set_mix(float value) { F = value; }
}
void set_edge(float value) void processblock(t_sample** inputs, t_sample** outputs, int blockSize)
{ {
D = value; t_sample* in1 = inputs[0];
}
void set_output(float value)
{
E = value;
}
void set_mix(float value)
{
F = value;
}
void processblock(t_sample** inputs, t_sample** outputs, int blockSize)
{
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1]; t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0]; t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1]; t_sample* out2 = outputs[1];
int inFramesToProcess = blockSize; int inFramesToProcess = blockSize;
double overallscale = 1.0; double overallscale = 1.0;
overallscale /= 44100.0; overallscale /= 44100.0;
overallscale *= samplerate; overallscale *= samplerate;
inTrimA = inTrimB; inTrimA = inTrimB;
inTrimB = A * 10.0; inTrimB = A * 10.0;
biquad[biq_freq] = pow(B, 3) * 20000.0; biquad[biq_freq] = pow(B, 3) * 20000.0;
if (biquad[biq_freq] < 15.0) if (biquad[biq_freq] < 15.0) biquad[biq_freq] = 15.0;
biquad[biq_freq] = 15.0; biquad[biq_freq] /= samplerate;
biquad[biq_freq] /= samplerate; biquad[biq_reso] = (pow(C, 2) * 15.0) + 0.5571;
biquad[biq_reso] = (pow(C, 2) * 15.0) + 0.5571; biquad[biq_aA0] = biquad[biq_aB0];
biquad[biq_aA0] = biquad[biq_aB0]; biquad[biq_aA1] = biquad[biq_aB1];
biquad[biq_aA1] = biquad[biq_aB1]; biquad[biq_aA2] = biquad[biq_aB2];
biquad[biq_aA2] = biquad[biq_aB2]; biquad[biq_bA1] = biquad[biq_bB1];
biquad[biq_bA1] = biquad[biq_bB1]; biquad[biq_bA2] = biquad[biq_bB2];
biquad[biq_bA2] = biquad[biq_bB2]; // previous run through the buffer is still in the filter, so we move it
// previous run through the buffer is still in the filter, so we move it // to the A section and now it's the new starting point.
// to the A section and now it's the new starting point. double K = tan(M_PI * biquad[biq_freq]);
double K = tan(M_PI * biquad[biq_freq]); double norm = 1.0 / (1.0 + K / biquad[biq_reso] + K * K);
double norm = 1.0 / (1.0 + K / biquad[biq_reso] + K * K); biquad[biq_aB0] = K * K * norm;
biquad[biq_aB0] = K * K * norm; biquad[biq_aB1] = 2.0 * biquad[biq_aB0];
biquad[biq_aB1] = 2.0 * biquad[biq_aB0]; biquad[biq_aB2] = biquad[biq_aB0];
biquad[biq_aB2] = biquad[biq_aB0]; biquad[biq_bB1] = 2.0 * (K * K - 1.0) * norm;
biquad[biq_bB1] = 2.0 * (K * K - 1.0) * norm; biquad[biq_bB2] = (1.0 - K / biquad[biq_reso] + K * K) * norm;
biquad[biq_bB2] = (1.0 - K / biquad[biq_reso] + K * K) * norm; // for the coefficient-interpolated biquad filter
// for the coefficient-interpolated biquad filter
powFactorA = powFactorB; powFactorA = powFactorB;
powFactorB = pow(D + 0.9, 4); powFactorB = pow(D + 0.9, 4);
// 1.0 == target neutral // 1.0 == target neutral
outTrimA = outTrimB; outTrimA = outTrimB;
outTrimB = E; outTrimB = E;
double wet = F; double wet = F;
fixA[fix_freq] = fixB[fix_freq] = 20000.0 / samplerate; fixA[fix_freq] = fixB[fix_freq] = 20000.0 / samplerate;
fixA[fix_reso] = fixB[fix_reso] = 0.7071; // butterworth Q fixA[fix_reso] = fixB[fix_reso] = 0.7071; // butterworth Q
K = tan(M_PI * fixA[fix_freq]); K = tan(M_PI * fixA[fix_freq]);
norm = 1.0 / (1.0 + K / fixA[fix_reso] + K * K); norm = 1.0 / (1.0 + K / fixA[fix_reso] + K * K);
fixA[fix_a0] = fixB[fix_a0] = K * K * norm; fixA[fix_a0] = fixB[fix_a0] = K * K * norm;
fixA[fix_a1] = fixB[fix_a1] = 2.0 * fixA[fix_a0]; fixA[fix_a1] = fixB[fix_a1] = 2.0 * fixA[fix_a0];
fixA[fix_a2] = fixB[fix_a2] = fixA[fix_a0]; fixA[fix_a2] = fixB[fix_a2] = fixA[fix_a0];
fixA[fix_b1] = fixB[fix_b1] = 2.0 * (K * K - 1.0) * norm; fixA[fix_b1] = fixB[fix_b1] = 2.0 * (K * K - 1.0) * norm;
fixA[fix_b2] = fixB[fix_b2] = (1.0 - K / fixA[fix_reso] + K * K) * norm; fixA[fix_b2] = fixB[fix_b2] = (1.0 - K / fixA[fix_reso] + K * K) * norm;
// for the fixed-position biquad filter // for the fixed-position biquad filter
for (int s = 0; s < blockSize; s++) { for (int s = 0; s < blockSize; s++) {
double inputSampleL = *in1; double inputSampleL = *in1;
double inputSampleR = *in2; double inputSampleR = *in2;
if (fabs(inputSampleL) < 1.18e-23) if (fabs(inputSampleL) < 1.18e-23) inputSampleL = fpdL * 1.18e-17;
inputSampleL = fpdL * 1.18e-17; if (fabs(inputSampleR) < 1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (fabs(inputSampleR) < 1.18e-23) double drySampleL = inputSampleL;
inputSampleR = fpdR * 1.18e-17; double drySampleR = inputSampleR;
double drySampleL = inputSampleL;
double drySampleR = inputSampleR;
double temp = (double)s / inFramesToProcess; double temp = (double)s / inFramesToProcess;
biquad[biq_a0] = (biquad[biq_aA0] * temp) + (biquad[biq_aB0] * (1.0 - temp)); biquad[biq_a0] = (biquad[biq_aA0] * temp) + (biquad[biq_aB0] * (1.0 - temp));
biquad[biq_a1] = (biquad[biq_aA1] * temp) + (biquad[biq_aB1] * (1.0 - temp)); biquad[biq_a1] = (biquad[biq_aA1] * temp) + (biquad[biq_aB1] * (1.0 - temp));
biquad[biq_a2] = (biquad[biq_aA2] * temp) + (biquad[biq_aB2] * (1.0 - temp)); biquad[biq_a2] = (biquad[biq_aA2] * temp) + (biquad[biq_aB2] * (1.0 - temp));
biquad[biq_b1] = (biquad[biq_bA1] * temp) + (biquad[biq_bB1] * (1.0 - temp)); biquad[biq_b1] = (biquad[biq_bA1] * temp) + (biquad[biq_bB1] * (1.0 - temp));
biquad[biq_b2] = (biquad[biq_bA2] * temp) + (biquad[biq_bB2] * (1.0 - temp)); biquad[biq_b2] = (biquad[biq_bA2] * temp) + (biquad[biq_bB2] * (1.0 - temp));
// this is the interpolation code for the biquad // this is the interpolation code for the biquad
double powFactor = (powFactorA * temp) + (powFactorB * (1.0 - temp)); double powFactor = (powFactorA * temp) + (powFactorB * (1.0 - temp));
double inTrim = (inTrimA * temp) + (inTrimB * (1.0 - temp)); double inTrim = (inTrimA * temp) + (inTrimB * (1.0 - temp));
double outTrim = (outTrimA * temp) + (outTrimB * (1.0 - temp)); double outTrim = (outTrimA * temp) + (outTrimB * (1.0 - temp));
inputSampleL *= inTrim; inputSampleL *= inTrim;
inputSampleR *= inTrim; inputSampleR *= inTrim;
temp = (inputSampleL * fixA[fix_a0]) + fixA[fix_sL1]; temp = (inputSampleL * fixA[fix_a0]) + fixA[fix_sL1];
fixA[fix_sL1] = (inputSampleL * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sL2]; fixA[fix_sL1] = (inputSampleL * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sL2];
fixA[fix_sL2] = (inputSampleL * fixA[fix_a2]) - (temp * fixA[fix_b2]); fixA[fix_sL2] = (inputSampleL * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixA[fix_a0]) + fixA[fix_sR1]; temp = (inputSampleR * fixA[fix_a0]) + fixA[fix_sR1];
fixA[fix_sR1] = (inputSampleR * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sR2]; fixA[fix_sR1] = (inputSampleR * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sR2];
fixA[fix_sR2] = (inputSampleR * fixA[fix_a2]) - (temp * fixA[fix_b2]); fixA[fix_sR2] = (inputSampleR * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics inputSampleR = temp; // fixed biquad filtering ultrasonics
// encode/decode courtesy of torridgristle under the MIT license // encode/decode courtesy of torridgristle under the MIT license
if (inputSampleL > 1.0) if (inputSampleL > 1.0) inputSampleL = 1.0;
inputSampleL = 1.0; else if (inputSampleL > 0.0) inputSampleL = 1.0 - pow(1.0 - inputSampleL, powFactor);
else if (inputSampleL > 0.0) if (inputSampleL < -1.0) inputSampleL = -1.0;
inputSampleL = 1.0 - pow(1.0 - inputSampleL, powFactor); else if (inputSampleL < 0.0) inputSampleL = -1.0 + pow(1.0 + inputSampleL, powFactor);
if (inputSampleL < -1.0) if (inputSampleR > 1.0) inputSampleR = 1.0;
inputSampleL = -1.0; else if (inputSampleR > 0.0) inputSampleR = 1.0 - pow(1.0 - inputSampleR, powFactor);
else if (inputSampleL < 0.0) if (inputSampleR < -1.0) inputSampleR = -1.0;
inputSampleL = -1.0 + pow(1.0 + inputSampleL, powFactor); else if (inputSampleR < 0.0) inputSampleR = -1.0 + pow(1.0 + inputSampleR, powFactor);
if (inputSampleR > 1.0)
inputSampleR = 1.0;
else if (inputSampleR > 0.0)
inputSampleR = 1.0 - pow(1.0 - inputSampleR, powFactor);
if (inputSampleR < -1.0)
inputSampleR = -1.0;
else if (inputSampleR < 0.0)
inputSampleR = -1.0 + pow(1.0 + inputSampleR, powFactor);
temp = (inputSampleL * biquad[biq_a0]) + biquad[biq_sL1]; temp = (inputSampleL * biquad[biq_a0]) + biquad[biq_sL1];
biquad[biq_sL1] = (inputSampleL * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sL2]; biquad[biq_sL1] = (inputSampleL * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sL2];
biquad[biq_sL2] = (inputSampleL * biquad[biq_a2]) - (temp * biquad[biq_b2]); biquad[biq_sL2] = (inputSampleL * biquad[biq_a2]) - (temp * biquad[biq_b2]);
inputSampleL = temp; // coefficient interpolating biquad filter inputSampleL = temp; // coefficient interpolating biquad filter
temp = (inputSampleR * biquad[biq_a0]) + biquad[biq_sR1]; temp = (inputSampleR * biquad[biq_a0]) + biquad[biq_sR1];
biquad[biq_sR1] = (inputSampleR * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sR2]; biquad[biq_sR1] = (inputSampleR * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sR2];
biquad[biq_sR2] = (inputSampleR * biquad[biq_a2]) - (temp * biquad[biq_b2]); biquad[biq_sR2] = (inputSampleR * biquad[biq_a2]) - (temp * biquad[biq_b2]);
inputSampleR = temp; // coefficient interpolating biquad filter inputSampleR = temp; // coefficient interpolating biquad filter
// encode/decode courtesy of torridgristle under the MIT license // encode/decode courtesy of torridgristle under the MIT license
if (inputSampleL > 1.0) if (inputSampleL > 1.0) inputSampleL = 1.0;
inputSampleL = 1.0; else if (inputSampleL > 0.0) inputSampleL = 1.0 - pow(1.0 - inputSampleL, (1.0 / powFactor));
else if (inputSampleL > 0.0) if (inputSampleL < -1.0) inputSampleL = -1.0;
inputSampleL = 1.0 - pow(1.0 - inputSampleL, (1.0 / powFactor)); else if (inputSampleL < 0.0) inputSampleL = -1.0 + pow(1.0 + inputSampleL, (1.0 / powFactor));
if (inputSampleL < -1.0) if (inputSampleR > 1.0) inputSampleR = 1.0;
inputSampleL = -1.0; else if (inputSampleR > 0.0) inputSampleR = 1.0 - pow(1.0 - inputSampleR, (1.0 / powFactor));
else if (inputSampleL < 0.0) if (inputSampleR < -1.0) inputSampleR = -1.0;
inputSampleL = -1.0 + pow(1.0 + inputSampleL, (1.0 / powFactor)); else if (inputSampleR < 0.0) inputSampleR = -1.0 + pow(1.0 + inputSampleR, (1.0 / powFactor));
if (inputSampleR > 1.0)
inputSampleR = 1.0;
else if (inputSampleR > 0.0)
inputSampleR = 1.0 - pow(1.0 - inputSampleR, (1.0 / powFactor));
if (inputSampleR < -1.0)
inputSampleR = -1.0;
else if (inputSampleR < 0.0)
inputSampleR = -1.0 + pow(1.0 + inputSampleR, (1.0 / powFactor));
inputSampleL *= outTrim; inputSampleL *= outTrim;
inputSampleR *= outTrim; inputSampleR *= outTrim;
temp = (inputSampleL * fixB[fix_a0]) + fixB[fix_sL1]; temp = (inputSampleL * fixB[fix_a0]) + fixB[fix_sL1];
fixB[fix_sL1] = (inputSampleL * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sL2]; fixB[fix_sL1] = (inputSampleL * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sL2];
fixB[fix_sL2] = (inputSampleL * fixB[fix_a2]) - (temp * fixB[fix_b2]); fixB[fix_sL2] = (inputSampleL * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixB[fix_a0]) + fixB[fix_sR1]; temp = (inputSampleR * fixB[fix_a0]) + fixB[fix_sR1];
fixB[fix_sR1] = (inputSampleR * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sR2]; fixB[fix_sR1] = (inputSampleR * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sR2];
fixB[fix_sR2] = (inputSampleR * fixB[fix_a2]) - (temp * fixB[fix_b2]); fixB[fix_sR2] = (inputSampleR * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics inputSampleR = temp; // fixed biquad filtering ultrasonics
if (wet < 1.0) { if (wet < 1.0) {
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0 - wet)); inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0 - wet));
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0 - wet)); inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0 - wet));
} }
// begin 32 bit stereo floating point dither // begin 32 bit stereo floating point dither
int expon; int expon;
frexpf((float)inputSampleL, &expon); frexpf((float)inputSampleL, &expon);
fpdL ^= fpdL << 13; fpdL ^= fpdL << 13;
fpdL ^= fpdL >> 17; fpdL ^= fpdL >> 17;
fpdL ^= fpdL << 5; fpdL ^= fpdL << 5;
inputSampleL += ((double(fpdL) - uint32_t(0x7fffffff)) * 5.5e-36l * pow(2, expon + 62)); inputSampleL += ((double(fpdL) - uint32_t(0x7fffffff)) * 5.5e-36l * pow(2, expon + 62));
frexpf((float)inputSampleR, &expon); frexpf((float)inputSampleR, &expon);
fpdR ^= fpdR << 13; fpdR ^= fpdR << 13;
fpdR ^= fpdR >> 17; fpdR ^= fpdR >> 17;
fpdR ^= fpdR << 5; fpdR ^= fpdR << 5;
inputSampleR += ((double(fpdR) - uint32_t(0x7fffffff)) * 5.5e-36l * pow(2, expon + 62)); inputSampleR += ((double(fpdR) - uint32_t(0x7fffffff)) * 5.5e-36l * pow(2, expon + 62));
// end 32 bit stereo floating point dither // end 32 bit stereo floating point dither
*out1 = inputSampleL; *out1 = inputSampleL;
*out2 = inputSampleR; *out2 = inputSampleR;
in1++; in1++;
in2++; in2++;
out1++; out1++;
out2++; out2++;
} }
} }
private: private:
double samplerate; double samplerate;
enum {
biq_freq,
biq_reso,
biq_a0,
biq_a1,
biq_a2,
biq_b1,
biq_b2,
biq_aA0,
biq_aA1,
biq_aA2,
biq_bA1,
biq_bA2,
biq_aB0,
biq_aB1,
biq_aB2,
biq_bB1,
biq_bB2,
biq_sL1,
biq_sL2,
biq_sR1,
biq_sR2,
biq_total
}; // coefficient interpolating biquad filter, stereo
std::array<double, biq_total> biquad;
double powFactorA; enum {
double powFactorB; biq_freq,
double inTrimA; biq_reso,
double inTrimB; biq_a0,
double outTrimA; biq_a1,
double outTrimB; biq_a2,
biq_b1,
biq_b2,
biq_aA0,
biq_aA1,
biq_aA2,
biq_bA1,
biq_bA2,
biq_aB0,
biq_aB1,
biq_aB2,
biq_bB1,
biq_bB2,
biq_sL1,
biq_sL2,
biq_sR1,
biq_sR2,
biq_total
}; // coefficient interpolating biquad filter, stereo
enum { std::array<double, biq_total> biquad;
fix_freq,
fix_reso,
fix_a0,
fix_a1,
fix_a2,
fix_b1,
fix_b2,
fix_sL1,
fix_sL2,
fix_sR1,
fix_sR2,
fix_total
}; // fixed frequency biquad filter for ultrasonics, stereo
std::array<double, fix_total> fixA;
std::array<double, fix_total> fixB;
uint32_t fpdL; double powFactorA;
uint32_t fpdR; double powFactorB;
// default stuff double inTrimA;
double inTrimB;
double outTrimA;
double outTrimB;
float A; enum {
float B; fix_freq,
float C; fix_reso,
float D; fix_a0,
float E; fix_a1,
float F; // parameters. Always 0-1, and we scale/alter them elsewhere. fix_a2,
fix_b1,
fix_b2,
fix_sL1,
fix_sL2,
fix_sR1,
fix_sR2,
fix_total
}; // fixed frequency biquad filter for ultrasonics, stereo
std::array<double, fix_total> fixA;
std::array<double, fix_total> fixB;
uint32_t fpdL;
uint32_t fpdR;
// default stuff
float A;
float B;
float C;
float D;
float E;
float F; // parameters. Always 0-1, and we scale/alter them elsewhere.
}; };
} } // namespace trnr

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include <math.h>
#include <array> #include <array>
#include <math.h>
#include <vector> #include <vector>
namespace trnr { namespace trnr {
@@ -9,306 +9,272 @@ template <typename t_sample>
// Notch filter based on YNotch by Chris Johnson // Notch filter based on YNotch by Chris Johnson
class ynotch { class ynotch {
public: public:
ynotch(double _samplerate) ynotch(double _samplerate)
: samplerate { _samplerate } : samplerate {_samplerate}
, A { 0.1f } , A {0.1f}
, B { 1.0f } , B {1.0f}
, C { 0.0f } , C {0.0f}
, D { 0.1f } , D {0.1f}
, E { 0.9f } , E {0.9f}
, F { 1.0f } , F {1.0f}
, fpdL { 0 } , fpdL {0}
, fpdR { 0 } , fpdR {0}
, biquad { 0 } , biquad {0}
{ {
for (int x = 0; x < biq_total; x++) { for (int x = 0; x < biq_total; x++) { biquad[x] = 0.0; }
biquad[x] = 0.0; powFactorA = 1.0;
} powFactorB = 1.0;
powFactorA = 1.0; inTrimA = 0.1;
powFactorB = 1.0; inTrimB = 0.1;
inTrimA = 0.1; outTrimA = 1.0;
inTrimB = 0.1; outTrimB = 1.0;
outTrimA = 1.0; for (int x = 0; x < fix_total; x++) {
outTrimB = 1.0; fixA[x] = 0.0;
for (int x = 0; x < fix_total; x++) { fixB[x] = 0.0;
fixA[x] = 0.0; }
fixB[x] = 0.0;
}
fpdL = 1.0; fpdL = 1.0;
while (fpdL < 16386) while (fpdL < 16386) fpdL = rand() * UINT32_MAX;
fpdL = rand() * UINT32_MAX; fpdR = 1.0;
fpdR = 1.0; while (fpdR < 16386) fpdR = rand() * UINT32_MAX;
while (fpdR < 16386) }
fpdR = rand() * UINT32_MAX;
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { samplerate = _samplerate; }
samplerate = _samplerate;
}
void set_drive(float value) void set_drive(float value) { A = value * 0.9 + 0.1; }
{
A = value * 0.9 + 0.1;
}
void set_frequency(float value)
{
B = value;
}
void set_resonance(float value)
{
C = value;
}
void set_edge(float value)
{
D = value;
}
void set_output(float value)
{
E = value;
}
void set_mix(float value)
{
F = value;
}
void processblock(t_sample** inputs, t_sample** outputs, int blockSize)
{
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
int inFramesToProcess = blockSize; void set_frequency(float value) { B = value; }
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= samplerate;
inTrimA = inTrimB; void set_resonance(float value) { C = value; }
inTrimB = A * 10.0;
biquad[biq_freq] = pow(B, 3) * 20000.0; void set_edge(float value) { D = value; }
if (biquad[biq_freq] < 15.0)
biquad[biq_freq] = 15.0;
biquad[biq_freq] /= samplerate;
biquad[biq_reso] = (pow(C, 2) * 15.0) + 0.0001;
biquad[biq_aA0] = biquad[biq_aB0];
biquad[biq_aA1] = biquad[biq_aB1];
biquad[biq_aA2] = biquad[biq_aB2];
biquad[biq_bA1] = biquad[biq_bB1];
biquad[biq_bA2] = biquad[biq_bB2];
// previous run through the buffer is still in the filter, so we move it
// to the A section and now it's the new starting point.
double K = tan(M_PI * biquad[biq_freq]);
double norm = 1.0 / (1.0 + K / biquad[biq_reso] + K * K);
biquad[biq_aB0] = (1.0 + K * K) * norm;
biquad[biq_aB1] = 2.0 * (K * K - 1) * norm;
biquad[biq_aB2] = biquad[biq_aB0];
biquad[biq_bB1] = biquad[biq_aB1];
biquad[biq_bB2] = (1.0 - K / biquad[biq_reso] + K * K) * norm;
// for the coefficient-interpolated biquad filter
powFactorA = powFactorB; void set_output(float value) { E = value; }
powFactorB = pow(D + 0.9, 4);
// 1.0 == target neutral void set_mix(float value) { F = value; }
outTrimA = outTrimB; void processblock(t_sample** inputs, t_sample** outputs, int blockSize)
outTrimB = E; {
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
double wet = F; int inFramesToProcess = blockSize;
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= samplerate;
fixA[fix_freq] = fixB[fix_freq] = 20000.0 / samplerate; inTrimA = inTrimB;
fixA[fix_reso] = fixB[fix_reso] = 0.7071; // butterworth Q inTrimB = A * 10.0;
K = tan(M_PI * fixA[fix_freq]); biquad[biq_freq] = pow(B, 3) * 20000.0;
norm = 1.0 / (1.0 + K / fixA[fix_reso] + K * K); if (biquad[biq_freq] < 15.0) biquad[biq_freq] = 15.0;
fixA[fix_a0] = fixB[fix_a0] = K * K * norm; biquad[biq_freq] /= samplerate;
fixA[fix_a1] = fixB[fix_a1] = 2.0 * fixA[fix_a0]; biquad[biq_reso] = (pow(C, 2) * 15.0) + 0.0001;
fixA[fix_a2] = fixB[fix_a2] = fixA[fix_a0]; biquad[biq_aA0] = biquad[biq_aB0];
fixA[fix_b1] = fixB[fix_b1] = 2.0 * (K * K - 1.0) * norm; biquad[biq_aA1] = biquad[biq_aB1];
fixA[fix_b2] = fixB[fix_b2] = (1.0 - K / fixA[fix_reso] + K * K) * norm; biquad[biq_aA2] = biquad[biq_aB2];
// for the fixed-position biquad filter biquad[biq_bA1] = biquad[biq_bB1];
biquad[biq_bA2] = biquad[biq_bB2];
// previous run through the buffer is still in the filter, so we move it
// to the A section and now it's the new starting point.
double K = tan(M_PI * biquad[biq_freq]);
double norm = 1.0 / (1.0 + K / biquad[biq_reso] + K * K);
biquad[biq_aB0] = (1.0 + K * K) * norm;
biquad[biq_aB1] = 2.0 * (K * K - 1) * norm;
biquad[biq_aB2] = biquad[biq_aB0];
biquad[biq_bB1] = biquad[biq_aB1];
biquad[biq_bB2] = (1.0 - K / biquad[biq_reso] + K * K) * norm;
// for the coefficient-interpolated biquad filter
for (int s = 0; s < blockSize; s++) { powFactorA = powFactorB;
double inputSampleL = *in1; powFactorB = pow(D + 0.9, 4);
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;
double temp = (double)s / inFramesToProcess; // 1.0 == target neutral
biquad[biq_a0] = (biquad[biq_aA0] * temp) + (biquad[biq_aB0] * (1.0 - temp));
biquad[biq_a1] = (biquad[biq_aA1] * temp) + (biquad[biq_aB1] * (1.0 - temp));
biquad[biq_a2] = (biquad[biq_aA2] * temp) + (biquad[biq_aB2] * (1.0 - temp));
biquad[biq_b1] = (biquad[biq_bA1] * temp) + (biquad[biq_bB1] * (1.0 - temp));
biquad[biq_b2] = (biquad[biq_bA2] * temp) + (biquad[biq_bB2] * (1.0 - temp));
// this is the interpolation code for the biquad
double powFactor = (powFactorA * temp) + (powFactorB * (1.0 - temp));
double inTrim = (inTrimA * temp) + (inTrimB * (1.0 - temp));
double outTrim = (outTrimA * temp) + (outTrimB * (1.0 - temp));
inputSampleL *= inTrim; outTrimA = outTrimB;
inputSampleR *= inTrim; outTrimB = E;
temp = (inputSampleL * fixA[fix_a0]) + fixA[fix_sL1]; double wet = F;
fixA[fix_sL1] = (inputSampleL * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sL2];
fixA[fix_sL2] = (inputSampleL * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixA[fix_a0]) + fixA[fix_sR1];
fixA[fix_sR1] = (inputSampleR * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sR2];
fixA[fix_sR2] = (inputSampleR * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
// encode/decode courtesy of torridgristle under the MIT license fixA[fix_freq] = fixB[fix_freq] = 20000.0 / samplerate;
if (inputSampleL > 1.0) fixA[fix_reso] = fixB[fix_reso] = 0.7071; // butterworth Q
inputSampleL = 1.0;
else if (inputSampleL > 0.0)
inputSampleL = 1.0 - pow(1.0 - inputSampleL, powFactor);
if (inputSampleL < -1.0)
inputSampleL = -1.0;
else if (inputSampleL < 0.0)
inputSampleL = -1.0 + pow(1.0 + inputSampleL, powFactor);
if (inputSampleR > 1.0)
inputSampleR = 1.0;
else if (inputSampleR > 0.0)
inputSampleR = 1.0 - pow(1.0 - inputSampleR, powFactor);
if (inputSampleR < -1.0)
inputSampleR = -1.0;
else if (inputSampleR < 0.0)
inputSampleR = -1.0 + pow(1.0 + inputSampleR, powFactor);
temp = (inputSampleL * biquad[biq_a0]) + biquad[biq_sL1]; K = tan(M_PI * fixA[fix_freq]);
biquad[biq_sL1] = (inputSampleL * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sL2]; norm = 1.0 / (1.0 + K / fixA[fix_reso] + K * K);
biquad[biq_sL2] = (inputSampleL * biquad[biq_a2]) - (temp * biquad[biq_b2]); fixA[fix_a0] = fixB[fix_a0] = K * K * norm;
inputSampleL = temp; // coefficient interpolating biquad filter fixA[fix_a1] = fixB[fix_a1] = 2.0 * fixA[fix_a0];
temp = (inputSampleR * biquad[biq_a0]) + biquad[biq_sR1]; fixA[fix_a2] = fixB[fix_a2] = fixA[fix_a0];
biquad[biq_sR1] = (inputSampleR * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sR2]; fixA[fix_b1] = fixB[fix_b1] = 2.0 * (K * K - 1.0) * norm;
biquad[biq_sR2] = (inputSampleR * biquad[biq_a2]) - (temp * biquad[biq_b2]); fixA[fix_b2] = fixB[fix_b2] = (1.0 - K / fixA[fix_reso] + K * K) * norm;
inputSampleR = temp; // coefficient interpolating biquad filter // for the fixed-position biquad filter
// encode/decode courtesy of torridgristle under the MIT license for (int s = 0; s < blockSize; s++) {
if (inputSampleL > 1.0) double inputSampleL = *in1;
inputSampleL = 1.0; double inputSampleR = *in2;
else if (inputSampleL > 0.0) if (fabs(inputSampleL) < 1.18e-23) inputSampleL = fpdL * 1.18e-17;
inputSampleL = 1.0 - pow(1.0 - inputSampleL, (1.0 / powFactor)); if (fabs(inputSampleR) < 1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (inputSampleL < -1.0) double drySampleL = inputSampleL;
inputSampleL = -1.0; double drySampleR = inputSampleR;
else if (inputSampleL < 0.0)
inputSampleL = -1.0 + pow(1.0 + inputSampleL, (1.0 / powFactor));
if (inputSampleR > 1.0)
inputSampleR = 1.0;
else if (inputSampleR > 0.0)
inputSampleR = 1.0 - pow(1.0 - inputSampleR, (1.0 / powFactor));
if (inputSampleR < -1.0)
inputSampleR = -1.0;
else if (inputSampleR < 0.0)
inputSampleR = -1.0 + pow(1.0 + inputSampleR, (1.0 / powFactor));
inputSampleL *= outTrim; double temp = (double)s / inFramesToProcess;
inputSampleR *= outTrim; biquad[biq_a0] = (biquad[biq_aA0] * temp) + (biquad[biq_aB0] * (1.0 - temp));
biquad[biq_a1] = (biquad[biq_aA1] * temp) + (biquad[biq_aB1] * (1.0 - temp));
biquad[biq_a2] = (biquad[biq_aA2] * temp) + (biquad[biq_aB2] * (1.0 - temp));
biquad[biq_b1] = (biquad[biq_bA1] * temp) + (biquad[biq_bB1] * (1.0 - temp));
biquad[biq_b2] = (biquad[biq_bA2] * temp) + (biquad[biq_bB2] * (1.0 - temp));
// this is the interpolation code for the biquad
double powFactor = (powFactorA * temp) + (powFactorB * (1.0 - temp));
double inTrim = (inTrimA * temp) + (inTrimB * (1.0 - temp));
double outTrim = (outTrimA * temp) + (outTrimB * (1.0 - temp));
temp = (inputSampleL * fixB[fix_a0]) + fixB[fix_sL1]; inputSampleL *= inTrim;
fixB[fix_sL1] = (inputSampleL * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sL2]; inputSampleR *= inTrim;
fixB[fix_sL2] = (inputSampleL * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixB[fix_a0]) + fixB[fix_sR1];
fixB[fix_sR1] = (inputSampleR * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sR2];
fixB[fix_sR2] = (inputSampleR * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
if (wet < 1.0) { temp = (inputSampleL * fixA[fix_a0]) + fixA[fix_sL1];
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0 - wet)); fixA[fix_sL1] = (inputSampleL * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sL2];
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0 - wet)); fixA[fix_sL2] = (inputSampleL * fixA[fix_a2]) - (temp * fixA[fix_b2]);
} inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixA[fix_a0]) + fixA[fix_sR1];
fixA[fix_sR1] = (inputSampleR * fixA[fix_a1]) - (temp * fixA[fix_b1]) + fixA[fix_sR2];
fixA[fix_sR2] = (inputSampleR * fixA[fix_a2]) - (temp * fixA[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
// begin 32 bit stereo floating point dither // encode/decode courtesy of torridgristle under the MIT license
int expon; if (inputSampleL > 1.0) inputSampleL = 1.0;
frexpf((float)inputSampleL, &expon); else if (inputSampleL > 0.0) inputSampleL = 1.0 - pow(1.0 - inputSampleL, powFactor);
fpdL ^= fpdL << 13; if (inputSampleL < -1.0) inputSampleL = -1.0;
fpdL ^= fpdL >> 17; else if (inputSampleL < 0.0) inputSampleL = -1.0 + pow(1.0 + inputSampleL, powFactor);
fpdL ^= fpdL << 5; if (inputSampleR > 1.0) inputSampleR = 1.0;
inputSampleL += ((double(fpdL) - uint32_t(0x7fffffff)) * 5.5e-36l * pow(2, expon + 62)); else if (inputSampleR > 0.0) inputSampleR = 1.0 - pow(1.0 - inputSampleR, powFactor);
frexpf((float)inputSampleR, &expon); if (inputSampleR < -1.0) inputSampleR = -1.0;
fpdR ^= fpdR << 13; else if (inputSampleR < 0.0) inputSampleR = -1.0 + pow(1.0 + inputSampleR, powFactor);
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; temp = (inputSampleL * biquad[biq_a0]) + biquad[biq_sL1];
*out2 = inputSampleR; biquad[biq_sL1] = (inputSampleL * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sL2];
biquad[biq_sL2] = (inputSampleL * biquad[biq_a2]) - (temp * biquad[biq_b2]);
inputSampleL = temp; // coefficient interpolating biquad filter
temp = (inputSampleR * biquad[biq_a0]) + biquad[biq_sR1];
biquad[biq_sR1] = (inputSampleR * biquad[biq_a1]) - (temp * biquad[biq_b1]) + biquad[biq_sR2];
biquad[biq_sR2] = (inputSampleR * biquad[biq_a2]) - (temp * biquad[biq_b2]);
inputSampleR = temp; // coefficient interpolating biquad filter
in1++; // encode/decode courtesy of torridgristle under the MIT license
in2++; if (inputSampleL > 1.0) inputSampleL = 1.0;
out1++; else if (inputSampleL > 0.0) inputSampleL = 1.0 - pow(1.0 - inputSampleL, (1.0 / powFactor));
out2++; if (inputSampleL < -1.0) inputSampleL = -1.0;
} else if (inputSampleL < 0.0) inputSampleL = -1.0 + pow(1.0 + inputSampleL, (1.0 / powFactor));
} if (inputSampleR > 1.0) inputSampleR = 1.0;
else if (inputSampleR > 0.0) inputSampleR = 1.0 - pow(1.0 - inputSampleR, (1.0 / powFactor));
if (inputSampleR < -1.0) inputSampleR = -1.0;
else if (inputSampleR < 0.0) inputSampleR = -1.0 + pow(1.0 + inputSampleR, (1.0 / powFactor));
inputSampleL *= outTrim;
inputSampleR *= outTrim;
temp = (inputSampleL * fixB[fix_a0]) + fixB[fix_sL1];
fixB[fix_sL1] = (inputSampleL * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sL2];
fixB[fix_sL2] = (inputSampleL * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleL = temp; // fixed biquad filtering ultrasonics
temp = (inputSampleR * fixB[fix_a0]) + fixB[fix_sR1];
fixB[fix_sR1] = (inputSampleR * fixB[fix_a1]) - (temp * fixB[fix_b1]) + fixB[fix_sR2];
fixB[fix_sR2] = (inputSampleR * fixB[fix_a2]) - (temp * fixB[fix_b2]);
inputSampleR = temp; // fixed biquad filtering ultrasonics
if (wet < 1.0) {
inputSampleL = (inputSampleL * wet) + (drySampleL * (1.0 - wet));
inputSampleR = (inputSampleR * wet) + (drySampleR * (1.0 - wet));
}
// 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++;
}
}
private: private:
double samplerate; double samplerate;
enum {
biq_freq,
biq_reso,
biq_a0,
biq_a1,
biq_a2,
biq_b1,
biq_b2,
biq_aA0,
biq_aA1,
biq_aA2,
biq_bA1,
biq_bA2,
biq_aB0,
biq_aB1,
biq_aB2,
biq_bB1,
biq_bB2,
biq_sL1,
biq_sL2,
biq_sR1,
biq_sR2,
biq_total
}; // coefficient interpolating biquad filter, stereo
std::array<double, biq_total> biquad;
double powFactorA; enum {
double powFactorB; biq_freq,
double inTrimA; biq_reso,
double inTrimB; biq_a0,
double outTrimA; biq_a1,
double outTrimB; biq_a2,
biq_b1,
biq_b2,
biq_aA0,
biq_aA1,
biq_aA2,
biq_bA1,
biq_bA2,
biq_aB0,
biq_aB1,
biq_aB2,
biq_bB1,
biq_bB2,
biq_sL1,
biq_sL2,
biq_sR1,
biq_sR2,
biq_total
}; // coefficient interpolating biquad filter, stereo
enum { std::array<double, biq_total> biquad;
fix_freq,
fix_reso,
fix_a0,
fix_a1,
fix_a2,
fix_b1,
fix_b2,
fix_sL1,
fix_sL2,
fix_sR1,
fix_sR2,
fix_total
}; // fixed frequency biquad filter for ultrasonics, stereo
std::array<double, fix_total> fixA;
std::array<double, fix_total> fixB;
uint32_t fpdL; double powFactorA;
uint32_t fpdR; double powFactorB;
// default stuff double inTrimA;
double inTrimB;
double outTrimA;
double outTrimB;
float A; enum {
float B; fix_freq,
float C; fix_reso,
float D; fix_a0,
float E; fix_a1,
float F; // parameters. Always 0-1, and we scale/alter them elsewhere. fix_a2,
fix_b1,
fix_b2,
fix_sL1,
fix_sL2,
fix_sR1,
fix_sR2,
fix_total
}; // fixed frequency biquad filter for ultrasonics, stereo
std::array<double, fix_total> fixA;
std::array<double, fix_total> fixB;
uint32_t fpdL;
uint32_t fpdR;
// default stuff
float A;
float B;
float C;
float D;
float E;
float F; // parameters. Always 0-1, and we scale/alter them elsewhere.
}; };
} } // namespace trnr

View File

@@ -1,113 +1,121 @@
#pragma once #pragma once
#include "ylowpass.h"
#include "yhighpass.h"
#include "ybandpass.h" #include "ybandpass.h"
#include "yhighpass.h"
#include "ylowpass.h"
#include "ynotch.h" #include "ynotch.h"
namespace trnr { namespace trnr {
enum filter_types { enum filter_types {
lowpass = 0, lowpass = 0,
highpass, highpass,
bandpass, bandpass,
notch notch
}; };
template <typename t_sample> template <typename t_sample>
class ysvf { class ysvf {
public: public:
ysvf(double _samplerate = 44100) ysvf(double _samplerate = 44100)
: lowpass { _samplerate } : lowpass {_samplerate}
, highpass { _samplerate } , highpass {_samplerate}
, bandpass { _samplerate } , bandpass {_samplerate}
, notch { _samplerate } , notch {_samplerate}
{} {
}
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate)
lowpass.set_samplerate(_samplerate); {
highpass.set_samplerate(_samplerate); lowpass.set_samplerate(_samplerate);
bandpass.set_samplerate(_samplerate); highpass.set_samplerate(_samplerate);
notch.set_samplerate(_samplerate); bandpass.set_samplerate(_samplerate);
} notch.set_samplerate(_samplerate);
}
void set_filter_type(filter_types type) { void set_filter_type(filter_types type) { filter_type = type; }
filter_type = type;
}
void set_drive(float value) { void set_drive(float value)
lowpass.set_drive(value); {
highpass.set_drive(value); lowpass.set_drive(value);
bandpass.set_drive(value); highpass.set_drive(value);
notch.set_drive(value); bandpass.set_drive(value);
} notch.set_drive(value);
}
void set_frequency(float value) { void set_frequency(float value)
lowpass.set_frequency(value); {
highpass.set_frequency(value); lowpass.set_frequency(value);
bandpass.set_frequency(value); highpass.set_frequency(value);
notch.set_frequency(value); bandpass.set_frequency(value);
} notch.set_frequency(value);
}
void set_resonance(float value) { void set_resonance(float value)
lowpass.set_resonance(value); {
highpass.set_resonance(value); lowpass.set_resonance(value);
bandpass.set_resonance(value); highpass.set_resonance(value);
notch.set_resonance(value); bandpass.set_resonance(value);
} notch.set_resonance(value);
}
void set_edge(float value) { void set_edge(float value)
lowpass.set_edge(value); {
highpass.set_edge(value); lowpass.set_edge(value);
bandpass.set_edge(value); highpass.set_edge(value);
notch.set_edge(value); bandpass.set_edge(value);
} notch.set_edge(value);
}
void set_output(float value) { void set_output(float value)
lowpass.set_output(value); {
highpass.set_output(value); lowpass.set_output(value);
bandpass.set_output(value); highpass.set_output(value);
notch.set_output(value); bandpass.set_output(value);
} notch.set_output(value);
}
void set_mix(float value) { void set_mix(float value)
lowpass.set_mix(value); {
highpass.set_mix(value); lowpass.set_mix(value);
bandpass.set_mix(value); highpass.set_mix(value);
notch.set_mix(value); bandpass.set_mix(value);
} notch.set_mix(value);
}
void process_block(t_sample** inputs, t_sample** outputs, int block_size) { void process_block(t_sample** inputs, t_sample** outputs, int block_size)
{
switch (filter_type) { switch (filter_type) {
case filter_types::lowpass: case filter_types::lowpass:
lowpass.processblock(inputs, outputs, block_size); lowpass.processblock(inputs, outputs, block_size);
break; break;
case filter_types::highpass: case filter_types::highpass:
highpass.processblock(inputs, outputs, block_size); highpass.processblock(inputs, outputs, block_size);
break; break;
case filter_types::bandpass: case filter_types::bandpass:
bandpass.processblock(inputs, outputs, block_size); bandpass.processblock(inputs, outputs, block_size);
break; break;
case filter_types::notch: case filter_types::notch:
notch.processblock(inputs, outputs, block_size); notch.processblock(inputs, outputs, block_size);
break; break;
} }
} }
private: private:
filter_types filter_type; filter_types filter_type;
ylowpass<t_sample> lowpass; ylowpass<t_sample> lowpass;
yhighpass<t_sample> highpass; yhighpass<t_sample> highpass;
ybandpass<t_sample> bandpass; ybandpass<t_sample> bandpass;
ynotch<t_sample> notch; ynotch<t_sample> notch;
double clamp(double& value, double min, double max) { double clamp(double& value, double min, double max)
if (value < min) { {
value = min; if (value < min) {
} else if (value > max) { value = min;
value = max; } else if (value > max) {
} value = max;
return value; }
} return value;
}
}; };
} } // namespace trnr

View File

@@ -2,24 +2,24 @@
namespace trnr { namespace trnr {
struct ivoice { struct ivoice {
virtual ~ivoice() = default; virtual ~ivoice() = default;
virtual float process_sample() = 0; virtual float process_sample() = 0;
virtual bool is_busy() = 0; virtual bool is_busy() = 0;
virtual void set_samplerate(double samplerate) = 0; virtual void set_samplerate(double samplerate) = 0;
virtual void note_on(int _note, float _velocity) = 0; virtual void note_on(int _note, float _velocity) = 0;
virtual void note_off() = 0; virtual void note_off() = 0;
virtual void modulate_pitch(float _pitch) = 0; virtual void modulate_pitch(float _pitch) = 0;
}; };
// check if a template derives from ivoice // check if a template derives from ivoice
template <class derived> template <class derived>
struct is_convertible { struct is_convertible {
template <class T> template <class T>
static char test(T*); static char test(T*);
template <class T> template <class T>
static double test(...); static double test(...);
static const bool value = sizeof(test<ivoice>(static_cast<derived*>(0))) == 1; static const bool value = sizeof(test<ivoice>(static_cast<derived*>(0))) == 1;
}; };
} } // namespace trnr

View File

@@ -3,46 +3,46 @@
namespace trnr { namespace trnr {
enum midi_event_type { enum midi_event_type {
note_on = 0, note_on = 0,
note_off, note_off,
pitch_wheel, pitch_wheel,
mod_wheel mod_wheel
}; };
class midi_event { class midi_event {
public: public:
midi_event_type type; midi_event_type type;
int offset = 0; int offset = 0;
int midi_note = 0; int midi_note = 0;
float velocity = 1.f; float velocity = 1.f;
double data = 0; double data = 0;
void make_note_on(int _midi_note, float _velocity, int _offset = 0) void make_note_on(int _midi_note, float _velocity, int _offset = 0)
{ {
type = midi_event_type::note_on; type = midi_event_type::note_on;
midi_note = _midi_note; midi_note = _midi_note;
velocity = _velocity; velocity = _velocity;
offset = _offset; offset = _offset;
} }
void make_note_off(int _midi_note, float _velocity, int _offset = 0) void make_note_off(int _midi_note, float _velocity, int _offset = 0)
{ {
type = midi_event_type::note_off; type = midi_event_type::note_off;
midi_note = _midi_note; midi_note = _midi_note;
velocity = _velocity; velocity = _velocity;
offset = _offset; offset = _offset;
} }
void make_pitch_wheel(double _pitch, int _offset = 0) void make_pitch_wheel(double _pitch, int _offset = 0)
{ {
type = midi_event_type::pitch_wheel; type = midi_event_type::pitch_wheel;
data = _pitch; data = _pitch;
} }
void make_mod_wheel(double _mod, int _offset = 0) void make_mod_wheel(double _mod, int _offset = 0)
{ {
type = midi_event_type::pitch_wheel; type = midi_event_type::pitch_wheel;
data = _mod; data = _mod;
} }
}; };
} } // namespace trnr

View File

@@ -12,82 +12,78 @@ namespace trnr {
template <typename t_voice, typename t_sample> template <typename t_voice, typename t_sample>
class midi_synth : public voice_allocator<t_voice, t_sample> { class midi_synth : public voice_allocator<t_voice, t_sample> {
public: public:
midi_synth(int _n_voices) midi_synth(int _n_voices)
: m_voices_active { false } : m_voices_active {false}
{ {
// checks whether template derives from ivoice // checks whether template derives from ivoice
typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1]; typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1];
} }
void set_samplerate_blocksize(double _samplerate, int _block_size) void set_samplerate_blocksize(double _samplerate, int _block_size)
{ {
m_block_size = _block_size; m_block_size = _block_size;
voice_allocator<t_voice, t_sample>::set_samplerate(_samplerate); voice_allocator<t_voice, t_sample>::set_samplerate(_samplerate);
} }
void process_block(t_sample** _outputs, int _n_frames) void process_block(t_sample** _outputs, int _n_frames)
{ {
// sample accurate event handling based on the iPlug2 synth by Oli Larkin // sample accurate event handling based on the iPlug2 synth by Oli Larkin
if (m_voices_active || !m_event_queue.empty()) { if (m_voices_active || !m_event_queue.empty()) {
int block_size = m_block_size; int block_size = m_block_size;
int samples_remaining = _n_frames; int samples_remaining = _n_frames;
int start_index = 0; int start_index = 0;
while (samples_remaining > 0) { while (samples_remaining > 0) {
if (samples_remaining < block_size) if (samples_remaining < block_size) block_size = samples_remaining;
block_size = samples_remaining;
while (!m_event_queue.empty()) { while (!m_event_queue.empty()) {
midi_event event = m_event_queue.front(); midi_event event = m_event_queue.front();
// we assume the messages are in chronological order. If we find one later than the current block we are done. // we assume the messages are in chronological order. If we find one later than the current block we
if (event.offset > start_index + block_size) // are done.
break; if (event.offset > start_index + block_size) break;
// send performance messages to the voice allocator // send performance messages to the voice allocator
// message offset is relative to the start of this process_samples() block // message offset is relative to the start of this process_samples() block
event.offset -= start_index; event.offset -= start_index;
voice_allocator<t_voice, t_sample>::add_event(event); voice_allocator<t_voice, t_sample>::add_event(event);
m_event_queue.erase(m_event_queue.begin()); m_event_queue.erase(m_event_queue.begin());
} }
voice_allocator<t_voice, t_sample>::process_samples(_outputs, start_index, block_size); voice_allocator<t_voice, t_sample>::process_samples(_outputs, start_index, block_size);
samples_remaining -= block_size; samples_remaining -= block_size;
start_index += block_size; start_index += block_size;
} }
m_voices_active = voice_allocator<t_voice, t_sample>::voices_active(); m_voices_active = voice_allocator<t_voice, t_sample>::voices_active();
flush_event_queue(_n_frames); flush_event_queue(_n_frames);
} else { } else {
for (int s = 0; s < _n_frames; s++) { for (int s = 0; s < _n_frames; s++) {
_outputs[0][s] = 0.; _outputs[0][s] = 0.;
_outputs[1][s] = 0.; _outputs[1][s] = 0.;
} }
} }
} }
void add_event(midi_event event) void add_event(midi_event event)
{ {
if (event.type == midi_event_type::note_on) if (event.type == midi_event_type::note_on) m_voices_active = true;
m_voices_active = true;
m_event_queue.push_back(event); m_event_queue.push_back(event);
} }
void flush_event_queue(int frames) void flush_event_queue(int frames)
{ {
for (int i = 0; i < m_event_queue.size(); i++) { for (int i = 0; i < m_event_queue.size(); i++) { m_event_queue.at(i).offset -= frames; }
m_event_queue.at(i).offset -= frames; }
}
}
private: private:
std::vector<midi_event> m_event_queue; std::vector<midi_event> m_event_queue;
int m_block_size; int m_block_size;
bool m_voices_active; bool m_voices_active;
}; };
} } // namespace trnr

View File

@@ -4,275 +4,235 @@
namespace trnr { namespace trnr {
enum env_state { enum env_state {
idle = 0, idle = 0,
attack1, attack1,
attack2, attack2,
hold, hold,
decay1, decay1,
decay2, decay2,
sustain, sustain,
release1, release1,
release2 release2
}; };
class tx_envelope { class tx_envelope {
public: public:
env_state state = idle; env_state state = idle;
float attack1_rate = 0; float attack1_rate = 0;
float attack1_level = 0; float attack1_level = 0;
float attack2_rate = 0; float attack2_rate = 0;
float hold_rate = 0; float hold_rate = 0;
float decay1_rate = 0; float decay1_rate = 0;
float decay1_level = 0; float decay1_level = 0;
float decay2_rate = 0; float decay2_rate = 0;
float sustain_level = 0; float sustain_level = 0;
float release1_rate = 0; float release1_rate = 0;
float release1_level = 0; float release1_level = 0;
float release2_rate = 0; float release2_rate = 0;
tx_envelope(bool _retrigger = false) tx_envelope(bool _retrigger = false)
: retrigger { _retrigger } : retrigger {_retrigger}
{ {
} }
float process_sample(bool gate, bool trigger) { float process_sample(bool gate, bool trigger) { return process_sample<float>(gate, trigger, 0, 0); }
return process_sample<float>(gate, trigger, 0, 0); template <typename t_sample>
} float process_sample(bool gate, bool trigger, t_sample _attack_mod, t_sample _decay_mod)
{
template <typename t_sample> size_t attack_mid_x1 = ms_to_samples(attack1_rate + (float)_attack_mod);
float process_sample(bool gate, bool trigger, t_sample _attack_mod, t_sample _decay_mod) { size_t attack_mid_x2 = ms_to_samples(attack2_rate + (float)_attack_mod);
size_t hold_samp = ms_to_samples(hold_rate);
size_t decay_mid_x1 = ms_to_samples(decay1_rate + (float)_decay_mod);
size_t decay_mid_x2 = ms_to_samples(decay2_rate + (float)_decay_mod);
size_t release_mid_x1 = ms_to_samples(release1_rate + (float)_decay_mod);
size_t release_mid_x2 = ms_to_samples(release2_rate + (float)_decay_mod);
size_t attack_mid_x1 = ms_to_samples(attack1_rate + (float)_attack_mod); // if note on is triggered, transition to attack phase
size_t attack_mid_x2 = ms_to_samples(attack2_rate + (float)_attack_mod); if (trigger) {
size_t hold_samp = ms_to_samples(hold_rate); if (retrigger) start_level = 0.f;
size_t decay_mid_x1 = ms_to_samples(decay1_rate + (float)_decay_mod); else start_level = level;
size_t decay_mid_x2 = ms_to_samples(decay2_rate + (float)_decay_mod); phase = 0;
size_t release_mid_x1 = ms_to_samples(release1_rate + (float)_decay_mod); state = attack1;
size_t release_mid_x2 = ms_to_samples(release2_rate + (float)_decay_mod); }
// attack 1st half
if (state == attack1) {
// while in attack phase
if (phase < attack_mid_x1) {
level = lerp(0, start_level, (float)attack_mid_x1, attack1_level, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > attack_mid_x1) { phase = attack_mid_x1; }
// if attack phase is done, transition to decay phase
if (phase == attack_mid_x1) {
state = attack2;
phase = 0;
}
}
// attack 2nd half
if (state == attack2) {
// while in attack phase
if (phase < attack_mid_x2) {
level = lerp(0, attack1_level, (float)attack_mid_x2, 1, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > attack_mid_x2) { phase = attack_mid_x2; }
// if attack phase is done, transition to decay phase
if (phase == attack_mid_x2) {
state = hold;
phase = 0;
}
}
// hold
if (state == hold) {
if (phase < hold_samp) {
level = 1.0;
phase += 1;
}
if (phase > hold_samp) { phase = hold_samp; }
if (phase == hold_samp) {
state = decay1;
phase = 0;
}
}
// decay 1st half
if (state == decay1) {
// while in decay phase
if (phase < decay_mid_x1) {
level = lerp(0, 1, (float)decay_mid_x1, decay1_level, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > decay_mid_x1) { phase = decay_mid_x1; }
// if decay phase is done, transition to sustain phase
if (phase == decay_mid_x1) {
state = decay2;
phase = 0;
}
}
// decay 2nd half
if (state == decay2) {
// while in decay phase
if (phase < decay_mid_x2) {
level = lerp(0, decay1_level, (float)decay_mid_x2, sustain_level, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > decay_mid_x2) { phase = decay_mid_x2; }
// if decay phase is done, transition to sustain phase
if (phase == decay_mid_x2) {
state = sustain;
phase = 0;
level = sustain_level;
}
}
// while sustain phase: if note off is triggered, transition to release phase
if (state == sustain && !gate) {
state = release1;
level = sustain_level;
}
// release 1st half
if (state == release1) {
// while in release phase
if (phase < release_mid_x1) {
level = lerp(0, sustain_level, (float)release_mid_x1, release1_level, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > release_mid_x1) { phase = release_mid_x1; }
// transition to 2nd release half
if (phase == release_mid_x1) {
phase = 0;
state = release2;
}
}
// release 2nd half
if (state == release2) {
// while in release phase
if (phase < release_mid_x2) {
level = lerp(0, release1_level, (float)release_mid_x2, 0, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > release_mid_x2) { phase = release_mid_x2; }
// reset
if (phase == release_mid_x2) {
phase = 0;
state = idle;
level = 0;
}
}
// if note on is triggered, transition to attack phase return smooth(level);
if (trigger) { }
if (retrigger)
start_level = 0.f;
else
start_level = level;
phase = 0;
state = attack1;
}
// attack 1st half
if (state == attack1) {
// while in attack phase
if (phase < attack_mid_x1) {
level = lerp(0, start_level, (float)attack_mid_x1, attack1_level, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > attack_mid_x1) {
phase = attack_mid_x1;
}
// if attack phase is done, transition to decay phase
if (phase == attack_mid_x1) {
state = attack2;
phase = 0;
}
}
// attack 2nd half
if (state == attack2) {
// while in attack phase
if (phase < attack_mid_x2) {
level = lerp(0, attack1_level, (float)attack_mid_x2, 1, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > attack_mid_x2) {
phase = attack_mid_x2;
}
// if attack phase is done, transition to decay phase
if (phase == attack_mid_x2) {
state = hold;
phase = 0;
}
}
// hold
if (state == hold) {
if (phase < hold_samp) {
level = 1.0;
phase += 1;
}
if (phase > hold_samp) {
phase = hold_samp;
}
if (phase == hold_samp) {
state = decay1;
phase = 0;
}
}
// decay 1st half
if (state == decay1) {
// while in decay phase
if (phase < decay_mid_x1) {
level = lerp(0, 1, (float)decay_mid_x1, decay1_level, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > decay_mid_x1) {
phase = decay_mid_x1;
}
// if decay phase is done, transition to sustain phase
if (phase == decay_mid_x1) {
state = decay2;
phase = 0;
}
}
// decay 2nd half
if (state == decay2) {
// while in decay phase
if (phase < decay_mid_x2) {
level = lerp(0, decay1_level, (float)decay_mid_x2, sustain_level, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > decay_mid_x2) {
phase = decay_mid_x2;
}
// if decay phase is done, transition to sustain phase
if (phase == decay_mid_x2) {
state = sustain;
phase = 0;
level = sustain_level;
}
}
// while sustain phase: if note off is triggered, transition to release phase
if (state == sustain && !gate) {
state = release1;
level = sustain_level;
}
// release 1st half
if (state == release1) {
// while in release phase
if (phase < release_mid_x1) {
level = lerp(0, sustain_level, (float)release_mid_x1, release1_level, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > release_mid_x1) {
phase = release_mid_x1;
}
// transition to 2nd release half
if (phase == release_mid_x1) {
phase = 0;
state = release2;
}
}
// release 2nd half
if (state == release2) {
// while in release phase
if (phase < release_mid_x2) {
level = lerp(0, release1_level, (float)release_mid_x2, 0, (float)phase);
phase += 1;
}
// reset phase if parameter was changed
if (phase > release_mid_x2) {
phase = release_mid_x2;
}
// reset
if (phase == release_mid_x2) {
phase = 0;
state = idle;
level = 0;
}
}
return smooth(level); bool is_busy() { return state != 0; }
}
bool is_busy() { return state != 0; } void set_samplerate(double sampleRate) { this->samplerate = sampleRate; }
void set_samplerate(double sampleRate) { // converts the x/y coordinates of the envelope points as a list for graphical representation.
this->samplerate = sampleRate; std::array<float, 18> calc_coordinates(float _max_attack, float _max_decay, float _max_release)
} {
// converts the x/y coordinates of the envelope points as a list for graphical representation. auto scale = [](float _value, float _max) { return powf(_value / _max, 0.25) * _max; };
std::array<float, 18> calc_coordinates(float _max_attack, float _max_decay, float _max_release) {
auto scale = [](float _value, float _max) { float a_x = 0;
return powf(_value / _max, 0.25) * _max; float a_y = 0;
};
float a_x = 0; float b_x = scale(attack1_rate, _max_attack / 2);
float a_y = 0; float b_y = attack1_level;
float b_x = scale(attack1_rate, _max_attack / 2); float c_x = b_x + scale(attack2_rate, _max_attack / 2);
float b_y = attack1_level; float c_y = 1;
float c_x = b_x + scale(attack2_rate, _max_attack / 2); float d_x = c_x + hold_rate;
float c_y = 1; float d_y = 1;
float d_x = c_x + hold_rate; float e_x = d_x + scale(decay1_rate, _max_decay / 2);
float d_y = 1; float e_y = decay1_level;
float e_x = d_x + scale(decay1_rate, _max_decay / 2); float f_x = e_x + scale(decay2_rate, _max_decay / 2);
float e_y = decay1_level; float f_y = sustain_level;
float f_x = e_x + scale(decay2_rate, _max_decay / 2); float g_x = _max_attack + _max_decay;
float f_y = sustain_level; float g_y = sustain_level;
float g_x = _max_attack + _max_decay; float h_x = g_x + scale(release1_rate, _max_decay / 2);
float g_y = sustain_level; float h_y = release1_level;
float h_x = g_x + scale(release1_rate, _max_decay / 2); float i_x = h_x + scale(release2_rate, _max_decay / 2);
float h_y = release1_level; float i_y = 0;
float i_x = h_x + scale(release2_rate, _max_decay / 2); float total = _max_attack + _max_decay + _max_release;
float i_y = 0;
float total = _max_attack + _max_decay + _max_release; return {a_x, a_y, b_x / total, b_y, c_x / total, c_y, d_x / total, d_y, e_x / total, e_y,
f_x / total, f_y, g_x / total, g_y, h_x / total, h_y, i_x / total, i_y};
return { }
a_x,
a_y,
b_x / total,
b_y,
c_x / total,
c_y,
d_x / total,
d_y,
e_x / total,
e_y,
f_x / total,
f_y,
g_x / total,
g_y,
h_x / total,
h_y,
i_x / total,
i_y
};
}
private: private:
double samplerate = 44100.; double samplerate = 44100.;
size_t phase = 0; size_t phase = 0;
float level = 0.f; float level = 0.f;
float start_level = 0.f; float start_level = 0.f;
float h1 = 0.f; float h1 = 0.f;
float h2 = 0.f; float h2 = 0.f;
float h3 = 0.f; float h3 = 0.f;
bool retrigger; bool retrigger;
float lerp(float x1, float y1, float x2, float y2, float x) { return y1 + (((x - x1) * (y2 - y1)) / (x2 - x1)); } float lerp(float x1, float y1, float x2, float y2, float x) { return y1 + (((x - x1) * (y2 - y1)) / (x2 - x1)); }
float smooth(float sample) { float smooth(float sample)
h3 = h2; {
h2 = h1; h3 = h2;
h1 = sample; h2 = h1;
h1 = sample;
return (h1 + h2 + h3) / 3.f; return (h1 + h2 + h3) / 3.f;
} }
size_t ms_to_samples(float ms) { size_t ms_to_samples(float ms) { return static_cast<size_t>(ms * samplerate / 1000.f); }
return static_cast<size_t>(ms * samplerate / 1000.f);
}
}; };
} } // namespace trnr

View File

@@ -1,37 +1,40 @@
#pragma once #pragma once
#include "tx_sineosc.h"
#include "tx_envelope.h" #include "tx_envelope.h"
#include "tx_sineosc.h"
namespace trnr { namespace trnr {
class tx_operator { class tx_operator {
public: public:
tx_operator() tx_operator()
: ratio { 1 } : ratio {1}
, amplitude { 1.0f } , amplitude {1.0f}
{ {
} }
tx_envelope envelope; tx_envelope envelope;
tx_sineosc oscillator; tx_sineosc oscillator;
float ratio; float ratio;
float amplitude; float amplitude;
float process_sample(const bool& gate, const bool& trigger, const float& frequency, const float& velocity, const float& pm = 0) { float process_sample(const bool& gate, const bool& trigger, const float& frequency, const float& velocity,
const float& pm = 0)
{
float env = envelope.process_sample(gate, trigger); float env = envelope.process_sample(gate, trigger);
// drifts and sounds better! // drifts and sounds better!
if (envelope.is_busy()) { if (envelope.is_busy()) {
double osc = oscillator.process_sample(trigger, frequency, pm); double osc = oscillator.process_sample(trigger, frequency, pm);
return osc * env * velocity; return osc * env * velocity;
} else { } else {
return 0.; return 0.;
} }
} }
void set_samplerate(double samplerate) { void set_samplerate(double samplerate)
this->envelope.set_samplerate(samplerate); {
this->oscillator.set_samplerate(samplerate); this->envelope.set_samplerate(samplerate);
} this->oscillator.set_samplerate(samplerate);
}
}; };
} } // namespace trnr

View File

@@ -5,91 +5,84 @@ namespace trnr {
class tx_sineosc { class tx_sineosc {
public: public:
bool phase_reset; bool phase_reset;
tx_sineosc() tx_sineosc()
: samplerate { 44100 } : samplerate {44100}
, phase_resolution { 16.f } , phase_resolution {16.f}
, phase { 0. } , phase {0.}
, history { 0. } , history {0.}
, phase_reset { false } , phase_reset {false}
{ {
} }
void set_phase_resolution(float res) { void set_phase_resolution(float res) { phase_resolution = powf(2, res); }
phase_resolution = powf(2, res);
}
float process_sample(bool trigger, float frequency, float phase_modulation = 0.f) { float process_sample(bool trigger, float frequency, float phase_modulation = 0.f)
if (trigger && phase_reset) { {
phase = 0.0; if (trigger && phase_reset) { phase = 0.0; }
}
float lookup_phase = phase + phase_modulation; float lookup_phase = phase + phase_modulation;
wrap(lookup_phase); wrap(lookup_phase);
phase += frequency / samplerate; phase += frequency / samplerate;
wrap(phase); wrap(phase);
redux(lookup_phase); redux(lookup_phase);
float output = sine(lookup_phase * 4096.); float output = sine(lookup_phase * 4096.);
filter(output); filter(output);
return output; return output;
} }
void set_samplerate(double _samplerate) { void set_samplerate(double _samplerate) { this->samplerate = _samplerate; }
this->samplerate = _samplerate;
}
private: private:
double samplerate; double samplerate;
float phase_resolution; float phase_resolution;
float phase; float phase;
float history; float history;
float sine(float x) { float sine(float x)
// x is scaled 0<=x<4096 {
const float a = -0.40319426317E-08; // x is scaled 0<=x<4096
const float b = 0.21683205691E+03; const float a = -0.40319426317E-08;
const float c = 0.28463350538E-04; const float b = 0.21683205691E+03;
const float d = -0.30774648337E-02; const float c = 0.28463350538E-04;
float y; const float d = -0.30774648337E-02;
float y;
bool negate = false; bool negate = false;
if (x > 2048) { if (x > 2048) {
negate = true; negate = true;
x -= 2048; x -= 2048;
} }
if (x > 1024) if (x > 1024) x = 2048 - x;
x = 2048 - x; y = (a + x) / (b + c * x * x) + d * x;
y = (a + x) / (b + c * x * x) + d * x; if (negate) return (float)(-y);
if (negate) else return (float)y;
return (float)(-y); }
else
return (float)y;
}
float wrap(float& phase) { float wrap(float& phase)
while (phase < 0.) {
phase += 1.; while (phase < 0.) phase += 1.;
while (phase >= 1.) while (phase >= 1.) phase -= 1.;
phase -= 1.;
return phase; return phase;
} }
float filter(float& value) { float filter(float& value)
value = 0.5 * (value + history); {
history = value; value = 0.5 * (value + history);
return value; history = value;
} return value;
}
float redux(float& value) float redux(float& value)
{ {
value = static_cast<int>(value * phase_resolution) / phase_resolution; value = static_cast<int>(value * phase_resolution) / phase_resolution;
return value; return value;
} }
}; };
} } // namespace trnr

View File

@@ -9,183 +9,180 @@ namespace trnr {
class tx_voice : public ivoice { class tx_voice : public ivoice {
public: public:
tx_voice() tx_voice()
: algorithm { 0 } : algorithm {0}
, pitch_env_amt { 0.f } , pitch_env_amt {0.f}
, feedback_amt { 0.f } , feedback_amt {0.f}
, bit_resolution(12.f) , bit_resolution(12.f)
{ {
} }
bool gate = false; bool gate = false;
bool trigger = false; bool trigger = false;
int midi_note = 0; int midi_note = 0;
float velocity = 1.f; float velocity = 1.f;
float additional_pitch_mod = 0.f; // modulates pitch in frequency float additional_pitch_mod = 0.f; // modulates pitch in frequency
int algorithm; int algorithm;
float pitch_env_amt; float pitch_env_amt;
float feedback_amt; float feedback_amt;
float bit_resolution; float bit_resolution;
tx_sineosc feedback_osc; tx_sineosc feedback_osc;
tx_envelope pitch_env; tx_envelope pitch_env;
tx_operator op1; tx_operator op1;
tx_operator op2; tx_operator op2;
tx_operator op3; tx_operator op3;
void note_on(int _note, float _velocity) override void note_on(int _note, float _velocity) override
{ {
this->gate = true; this->gate = true;
this->trigger = true; this->trigger = true;
midi_note = _note; midi_note = _note;
velocity = _velocity; velocity = _velocity;
} }
void note_off() override void note_off() override { this->gate = false; }
{
this->gate = false;
}
// modulates the pitch in semitones // modulates the pitch in semitones
void modulate_pitch(float _pitch) override void modulate_pitch(float _pitch) override { this->pitch_mod = _pitch; }
{
this->pitch_mod = _pitch;
}
float process_sample() override float process_sample() override
{ {
float pitch_env_signal = pitch_env.process_sample(gate, trigger) * pitch_env_amt; float pitch_env_signal = pitch_env.process_sample(gate, trigger) * pitch_env_amt;
float pitched_freq = midi_to_frequency(midi_note + pitch_mod + additional_pitch_mod) + pitch_env_signal; float pitched_freq = midi_to_frequency(midi_note + pitch_mod + additional_pitch_mod) + pitch_env_signal;
float output = 0.f; float output = 0.f;
// mix operator signals according to selected algorithm // mix operator signals according to selected algorithm
switch (algorithm) { switch (algorithm) {
case 0: case 0:
output = calc_algo1(pitched_freq); output = calc_algo1(pitched_freq);
break; break;
case 1: case 1:
output = calc_algo2(pitched_freq); output = calc_algo2(pitched_freq);
break; break;
case 2: case 2:
output = calc_algo3(pitched_freq); output = calc_algo3(pitched_freq);
break; break;
case 3: case 3:
output = calc_algo4(pitched_freq); output = calc_algo4(pitched_freq);
break; break;
default: default:
output = calc_algo1(pitched_freq); output = calc_algo1(pitched_freq);
break; break;
} }
// reset trigger // reset trigger
trigger = false; trigger = false;
return redux(output, bit_resolution); return redux(output, bit_resolution);
} }
bool is_busy() override { return gate || op1.envelope.is_busy() || op2.envelope.is_busy() || op3.envelope.is_busy(); } bool is_busy() override
{
return gate || op1.envelope.is_busy() || op2.envelope.is_busy() || op3.envelope.is_busy();
}
void set_samplerate(double samplerate) override void set_samplerate(double samplerate) override
{ {
pitch_env.set_samplerate(samplerate); pitch_env.set_samplerate(samplerate);
feedback_osc.set_samplerate(samplerate); feedback_osc.set_samplerate(samplerate);
op1.set_samplerate(samplerate); op1.set_samplerate(samplerate);
op2.set_samplerate(samplerate); op2.set_samplerate(samplerate);
op3.set_samplerate(samplerate); op3.set_samplerate(samplerate);
} }
void set_phase_reset(bool phase_reset) void set_phase_reset(bool phase_reset)
{ {
op1.oscillator.phase_reset = phase_reset; op1.oscillator.phase_reset = phase_reset;
op2.oscillator.phase_reset = phase_reset; op2.oscillator.phase_reset = phase_reset;
op3.oscillator.phase_reset = phase_reset; op3.oscillator.phase_reset = phase_reset;
feedback_osc.phase_reset = phase_reset; feedback_osc.phase_reset = phase_reset;
} }
private: private:
const float MOD_INDEX_COEFF = 4.f; const float MOD_INDEX_COEFF = 4.f;
float pitch_mod = 0.f; // modulates pitch in semi-tones float pitch_mod = 0.f; // modulates pitch in semi-tones
float calc_algo1(const float frequency) float calc_algo1(const float frequency)
{ {
float fb_freq = frequency * op3.ratio; float fb_freq = frequency * op3.ratio;
float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF); float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF);
float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index; float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index;
float op3_Freq = frequency * op3.ratio; float op3_Freq = frequency * op3.ratio;
float op3_mod_index = (op3.amplitude * MOD_INDEX_COEFF); float op3_mod_index = (op3.amplitude * MOD_INDEX_COEFF);
float op3_signal = op3.process_sample(gate, trigger, op3_Freq, velocity, fb_signal) * op3_mod_index; float op3_signal = op3.process_sample(gate, trigger, op3_Freq, velocity, fb_signal) * op3_mod_index;
float op2_freq = frequency * op2.ratio; float op2_freq = frequency * op2.ratio;
float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF); float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF);
float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity, op3_signal) * op2_mod_index; float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity, op3_signal) * op2_mod_index;
float op1_freq = frequency * op1.ratio; float op1_freq = frequency * op1.ratio;
return op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal) * op1.amplitude; return op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal) * op1.amplitude;
} }
float calc_algo2(const float frequency) float calc_algo2(const float frequency)
{ {
float fb_freq = frequency * op3.ratio; float fb_freq = frequency * op3.ratio;
float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF); float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF);
float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index; float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index;
float op3_freq = frequency * op3.ratio; float op3_freq = frequency * op3.ratio;
float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3.amplitude; float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3.amplitude;
float op2_freq = frequency * op2.ratio; float op2_freq = frequency * op2.ratio;
float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF); float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF);
float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2_mod_index; float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2_mod_index;
float op1_freq = frequency * op1.ratio; float op1_freq = frequency * op1.ratio;
float op1_signal = op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal) * op1.amplitude; float op1_signal = op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal) * op1.amplitude;
return op1_signal + op3_signal; return op1_signal + op3_signal;
} }
float calc_algo3(const float frequency) float calc_algo3(const float frequency)
{ {
float fb_freq = frequency * op3.ratio; float fb_freq = frequency * op3.ratio;
float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF); float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF);
float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index; float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index;
float op3_freq = frequency * op3.ratio; float op3_freq = frequency * op3.ratio;
float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3.amplitude; float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3.amplitude;
float op2_freq = frequency * op2.ratio; float op2_freq = frequency * op2.ratio;
float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2.amplitude; float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2.amplitude;
float op1_freq = frequency * op1.ratio; float op1_freq = frequency * op1.ratio;
float op1_signal = op1.process_sample(gate, trigger, op1_freq, velocity) * op1.amplitude; float op1_signal = op1.process_sample(gate, trigger, op1_freq, velocity) * op1.amplitude;
return op1_signal + op2_signal + op3_signal; return op1_signal + op2_signal + op3_signal;
} }
float calc_algo4(const float frequency) float calc_algo4(const float frequency)
{ {
float fb_freq = frequency * op3.ratio; float fb_freq = frequency * op3.ratio;
float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF); float fb_mod_index = (feedback_amt * MOD_INDEX_COEFF);
float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index; float fb_signal = feedback_osc.process_sample(trigger, fb_freq) * fb_mod_index;
float op3_freq = frequency * op3.ratio; float op3_freq = frequency * op3.ratio;
float op3_mod_index = (op3.amplitude * MOD_INDEX_COEFF); float op3_mod_index = (op3.amplitude * MOD_INDEX_COEFF);
float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3_mod_index; float op3_signal = op3.process_sample(gate, trigger, op3_freq, velocity, fb_signal) * op3_mod_index;
float op2_freq = frequency * op2.ratio; float op2_freq = frequency * op2.ratio;
float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF); float op2_mod_index = (op2.amplitude * MOD_INDEX_COEFF);
float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2_mod_index; float op2_signal = op2.process_sample(gate, trigger, op2_freq, velocity) * op2_mod_index;
float op1_freq = frequency * op1.ratio; float op1_freq = frequency * op1.ratio;
return op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal + op3_signal) * op1.amplitude; return op1.process_sample(gate, trigger, op1_freq, velocity, op2_signal + op3_signal) * op1.amplitude;
} }
float redux(float& value, float resolution) float redux(float& value, float resolution)
{ {
float res = powf(2, resolution); float res = powf(2, resolution);
value = roundf(value * res) / res; value = roundf(value * res) / res;
return value; return value;
} }
}; };
} } // namespace trnr

View File

@@ -8,156 +8,136 @@ namespace trnr {
template <typename t_voice, typename t_sample> template <typename t_voice, typename t_sample>
class voice_allocator { class voice_allocator {
public: public:
std::vector<t_voice> voices; std::vector<t_voice> voices;
voice_allocator() voice_allocator()
: voices(4, t_voice()) : voices(4, t_voice())
{ {
// checks whether template derives from ivoice // checks whether template derives from ivoice
typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1]; typedef t_voice assert_at_compile_time[is_convertible<t_voice>::value ? 1 : -1];
} }
void set_voice_count(const int& voice_count) void set_voice_count(const int& voice_count) { voices.resize(voice_count, voices.at(0)); }
{
voices.resize(voice_count, voices.at(0));
}
void note_on(const midi_event& event) void note_on(const midi_event& event)
{ {
t_voice* voice = get_free_voice(event.midi_note); t_voice* voice = get_free_voice(event.midi_note);
if (voice == nullptr) { if (voice == nullptr) { voice = steal_voice(); }
voice = steal_voice();
}
if (voice != nullptr) { if (voice != nullptr) { voice->note_on(event.midi_note, event.velocity); }
voice->note_on(event.midi_note, event.velocity); }
}
}
void note_off(const midi_event& event) void note_off(const midi_event& event)
{ {
for (auto it = voices.begin(); it != voices.end(); it++) { for (auto it = voices.begin(); it != voices.end(); it++) {
if ((*it).midi_note == event.midi_note) { if ((*it).midi_note == event.midi_note) { (*it).note_off(); }
(*it).note_off(); }
} }
}
}
void access(std::function<void(t_voice&)> f) void access(std::function<void(t_voice&)> f) { std::for_each(voices.begin(), voices.end(), f); }
{
std::for_each(voices.begin(), voices.end(), f);
}
void process_samples(t_sample** _outputs, int _start_index, int _block_size) void process_samples(t_sample** _outputs, int _start_index, int _block_size)
{ {
for (int s = _start_index; s < _start_index + _block_size; s++) { for (int s = _start_index; s < _start_index + _block_size; s++) {
process_events(s); process_events(s);
float voices_signal = 0.; float voices_signal = 0.;
std::for_each(voices.begin(), voices.end(), [&voices_signal](t_voice& voice) { std::for_each(voices.begin(), voices.end(),
voices_signal += (voice.process_sample() / 3.); [&voices_signal](t_voice& voice) { voices_signal += (voice.process_sample() / 3.); });
});
_outputs[0][s] = voices_signal; _outputs[0][s] = voices_signal;
_outputs[1][s] = voices_signal; _outputs[1][s] = voices_signal;
} }
} }
void add_event(midi_event event) void add_event(midi_event event) { input_queue.push_back(event); }
{
input_queue.push_back(event);
}
bool voices_active() bool voices_active()
{ {
bool voices_active = false; bool voices_active = false;
for (auto it = voices.begin(); it != voices.end(); it++) { for (auto it = voices.begin(); it != voices.end(); it++) {
bool busy = (*it).is_busy(); bool busy = (*it).is_busy();
voices_active |= busy; voices_active |= busy;
} }
return voices_active; return voices_active;
} }
void set_samplerate(double _samplerate) void set_samplerate(double _samplerate)
{ {
for (int i = 0; i < voices.size(); i++) { for (int i = 0; i < voices.size(); i++) { voices.at(i).set_samplerate(_samplerate); }
voices.at(i).set_samplerate(_samplerate); }
}
}
private: private:
std::vector<midi_event> input_queue; std::vector<midi_event> input_queue;
int index_to_steal = 0; int index_to_steal = 0;
t_voice* get_free_voice(float frequency) t_voice* get_free_voice(float frequency)
{ {
t_voice* voice = nullptr; t_voice* voice = nullptr;
for (auto it = voices.begin(); it != voices.end(); it++) { for (auto it = voices.begin(); it != voices.end(); it++) {
if (!(*it).is_busy()) { if (!(*it).is_busy()) { voice = &*it; }
voice = &*it; }
}
}
return voice; return voice;
} }
t_voice* steal_voice() t_voice* steal_voice()
{ {
t_voice* free_voice = nullptr; t_voice* free_voice = nullptr;
for (auto it = voices.begin(); it != voices.end(); it++) { for (auto it = voices.begin(); it != voices.end(); it++) {
if (!(*it).gate) { if (!(*it).gate) {
free_voice = &*it; free_voice = &*it;
break; break;
} }
} }
if (free_voice == nullptr) { if (free_voice == nullptr) {
free_voice = &voices.at(index_to_steal); free_voice = &voices.at(index_to_steal);
if (index_to_steal < voices.size() - 1) { if (index_to_steal < voices.size() - 1) {
index_to_steal++; index_to_steal++;
} else { } else {
index_to_steal = 0; index_to_steal = 0;
} }
} }
return free_voice; return free_voice;
} }
void process_events(int _start_index) void process_events(int _start_index)
{ {
auto iterator = input_queue.begin(); auto iterator = input_queue.begin();
while (iterator != input_queue.end()) { while (iterator != input_queue.end()) {
midi_event& event = *iterator; midi_event& event = *iterator;
if (event.offset == _start_index) { if (event.offset == _start_index) {
switch (event.type) { switch (event.type) {
case midi_event_type::note_on: case midi_event_type::note_on:
note_on(event); note_on(event);
break; break;
case midi_event_type::note_off: case midi_event_type::note_off:
note_off(event); note_off(event);
break; break;
case midi_event_type::pitch_wheel: case midi_event_type::pitch_wheel:
access([&event](t_voice& voice) { voice.modulate_pitch(event.data); }); access([&event](t_voice& voice) { voice.modulate_pitch(event.data); });
break; break;
default: default:
break; break;
} }
iterator = input_queue.erase(iterator); iterator = input_queue.erase(iterator);
} else { } else {
iterator++; iterator++;
} }
} }
} }
}; };
} } // namespace trnr