apply procedural approach to clipper+tube

This commit is contained in:
2025-10-24 01:59:55 +02:00
parent b7ef24d324
commit 8af07582ae
4 changed files with 313 additions and 332 deletions

View File

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

View File

@@ -1,209 +0,0 @@
#pragma once
#include <cstdlib>
#include <stdint.h>
#include <cmath>
namespace trnr {
// modeled tube preamp based on tube2 by Chris Johnson
class aw_tube2 {
public:
aw_tube2()
{
samplerate = 44100;
A = 0.5;
B = 0.5;
previousSampleA = 0.0;
previousSampleB = 0.0;
previousSampleC = 0.0;
previousSampleD = 0.0;
previousSampleE = 0.0;
previousSampleF = 0.0;
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_input(double value) { A = clamp(value); }
void set_tube(double value) { B = clamp(value); }
void set_samplerate(double _samplerate) { samplerate = _samplerate; }
template <typename t_sample>
void process_block(t_sample** inputs, t_sample** outputs, long sampleframes)
{
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= samplerate;
double inputPad = A;
double iterations = 1.0 - B;
int powerfactor = (9.0 * iterations) + 1;
double asymPad = (double)powerfactor;
double gainscaling = 1.0 / (double)(powerfactor + 1);
double outputscaling = 1.0 + (1.0 / (double)(powerfactor));
while (--sampleframes >= 0) {
double inputSampleL = *in1;
double inputSampleR = *in2;
if (fabs(inputSampleL) < 1.18e-23) inputSampleL = fpdL * 1.18e-17;
if (fabs(inputSampleR) < 1.18e-23) inputSampleR = fpdR * 1.18e-17;
if (inputPad < 1.0) {
inputSampleL *= inputPad;
inputSampleR *= inputPad;
}
if (overallscale > 1.9) {
double stored = inputSampleL;
inputSampleL += previousSampleA;
previousSampleA = stored;
inputSampleL *= 0.5;
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 (inputSampleR > 1.0) inputSampleR = 1.0;
if (inputSampleR < -1.0) inputSampleR = -1.0;
// flatten bottom, point top of sine waveshaper L
inputSampleL /= asymPad;
double sharpen = -inputSampleL;
if (sharpen > 0.0) sharpen = 1.0 + sqrt(sharpen);
else sharpen = 1.0 - sqrt(-sharpen);
inputSampleL -= inputSampleL * fabs(inputSampleL) * sharpen * 0.25;
// this will take input from exactly -1.0 to 1.0 max
inputSampleL *= asymPad;
// flatten bottom, point top of sine waveshaper R
inputSampleR /= asymPad;
sharpen = -inputSampleR;
if (sharpen > 0.0) sharpen = 1.0 + sqrt(sharpen);
else sharpen = 1.0 - sqrt(-sharpen);
inputSampleR -= inputSampleR * fabs(inputSampleR) * sharpen * 0.25;
// this will take input from exactly -1.0 to 1.0 max
inputSampleR *= asymPad;
// end first asym section: later boosting can mitigate the extreme
// softclipping of one side of the wave
// and we are asym clipping more when Tube is cranked, to compensate
// original Tube algorithm: powerfactor widens the more linear region of the wave
double factor = inputSampleL; // Left channel
for (int x = 0; x < powerfactor; x++) factor *= inputSampleL;
if ((powerfactor % 2 == 1) && (inputSampleL != 0.0)) factor = (factor / inputSampleL) * fabs(inputSampleL);
factor *= gainscaling;
inputSampleL -= factor;
inputSampleL *= outputscaling;
factor = inputSampleR; // Right channel
for (int x = 0; x < powerfactor; x++) factor *= inputSampleR;
if ((powerfactor % 2 == 1) && (inputSampleR != 0.0)) factor = (factor / inputSampleR) * fabs(inputSampleR);
factor *= gainscaling;
inputSampleR -= factor;
inputSampleR *= outputscaling;
if (overallscale > 1.9) {
double stored = inputSampleL;
inputSampleL += previousSampleC;
previousSampleC = stored;
inputSampleL *= 0.5;
stored = inputSampleR;
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
double slew = previousSampleE - inputSampleL;
if (overallscale > 1.9) {
double stored = inputSampleL;
inputSampleL += previousSampleE;
previousSampleE = stored;
inputSampleL *= 0.5;
} else previousSampleE = inputSampleL; // for this, need previousSampleC always
if (slew > 0.0) slew = 1.0 + (sqrt(slew) * 0.5);
else slew = 1.0 - (sqrt(-slew) * 0.5);
inputSampleL -= inputSampleL * fabs(inputSampleL) * slew * gainscaling;
// reusing gainscaling that's part of another algorithm
if (inputSampleL > 0.52) inputSampleL = 0.52;
if (inputSampleL < -0.52) inputSampleL = -0.52;
inputSampleL *= 1.923076923076923;
// hysteresis and spiky fuzz R
slew = previousSampleF - inputSampleR;
if (overallscale > 1.9) {
double stored = inputSampleR;
inputSampleR += previousSampleF;
previousSampleF = stored;
inputSampleR *= 0.5;
} else previousSampleF = inputSampleR; // for this, need previousSampleC always
if (slew > 0.0) slew = 1.0 + (sqrt(slew) * 0.5);
else slew = 1.0 - (sqrt(-slew) * 0.5);
inputSampleR -= inputSampleR * fabs(inputSampleR) * slew * gainscaling;
// 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
// int expon; frexp((double)inputSampleL, &expon);
fpdL ^= fpdL << 13;
fpdL ^= fpdL >> 17;
fpdL ^= fpdL << 5;
// inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
// frexp((double)inputSampleR, &expon);
fpdR ^= fpdR << 13;
fpdR ^= fpdR >> 17;
fpdR ^= fpdR << 5;
// inputSampleR += ((double(fpdR)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
// end 64 bit stereo floating point dither
*out1 = inputSampleL;
*out2 = inputSampleR;
in1++;
in2++;
out1++;
out2++;
}
}
private:
double samplerate;
double previousSampleA;
double previousSampleB;
double previousSampleC;
double previousSampleD;
double previousSampleE;
double previousSampleF;
uint32_t fpdL;
uint32_t fpdR;
// default stuff
float A;
float B;
double clamp(double& value)
{
if (value > 1) {
value = 1;
} else if (value < 0) {
value = 0;
}
return value;
}
};
} // namespace trnr

119
clip/clip.h Normal file
View File

@@ -0,0 +1,119 @@
#pragma once
#include <cmath>
#include <cstdlib>
namespace trnr {
// clipper based on ClipOnly2 by Chris Johnson (MIT License)
struct clip {
double samplerate;
double last_sample_l;
double intermediate_l[16];
bool was_pos_clip_l;
bool was_neg_clip_l;
double last_sample_r;
double intermediate_r[16];
bool was_pos_clip_r;
bool was_neg_clip_r;
};
inline void clip_init(clip& c, double _samplerate)
{
c.samplerate = 44100;
c.last_sample_l = 0.0;
c.was_pos_clip_l = false;
c.was_neg_clip_l = false;
c.last_sample_r = 0.0;
c.was_pos_clip_r = false;
c.was_neg_clip_r = false;
for (int x = 0; x < 16; x++) {
c.intermediate_l[x] = 0.0;
c.intermediate_r[x] = 0.0;
}
}
template <typename t_sample>
inline void clip_process_block(clip& c, t_sample** inputs, t_sample** outputs, long sample_frames)
{
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= c.samplerate;
int spacing = floor(overallscale); // should give us working basic scaling, usually 2 or 4
if (spacing < 1) spacing = 1;
if (spacing > 16) spacing = 16;
while (--sample_frames >= 0) {
double input_l = *in1;
double input_r = *in2;
// begin ClipOnly2 stereo as a little, compressed chunk that can be dropped into code
if (input_l > 4.0) input_l = 4.0;
if (input_l < -4.0) input_l = -4.0;
if (c.was_pos_clip_l == true) { // current will be over
if (input_l < c.last_sample_l) c.last_sample_l = 0.7058208 + (input_l * 0.2609148);
else c.last_sample_l = 0.2491717 + (c.last_sample_l * 0.7390851);
}
c.was_pos_clip_l = false;
if (input_l > 0.9549925859) {
c.was_pos_clip_l = true;
input_l = 0.7058208 + (c.last_sample_l * 0.2609148);
}
if (c.was_neg_clip_l == true) { // current will be -over
if (input_l > c.last_sample_l) c.last_sample_l = -0.7058208 + (input_l * 0.2609148);
else c.last_sample_l = -0.2491717 + (c.last_sample_l * 0.7390851);
}
c.was_neg_clip_l = false;
if (input_l < -0.9549925859) {
c.was_neg_clip_l = true;
input_l = -0.7058208 + (c.last_sample_l * 0.2609148);
}
c.intermediate_l[spacing] = input_l;
input_l = c.last_sample_l; // Latency is however many samples equals one 44.1k sample
for (int x = spacing; x > 0; x--) c.intermediate_l[x - 1] = c.intermediate_l[x];
c.last_sample_l = c.intermediate_l[0]; // run a little buffer to handle this
if (input_r > 4.0) input_r = 4.0;
if (input_r < -4.0) input_r = -4.0;
if (c.was_pos_clip_r == true) { // current will be over
if (input_r < c.last_sample_r) c.last_sample_r = 0.7058208 + (input_r * 0.2609148);
else c.last_sample_r = 0.2491717 + (c.last_sample_r * 0.7390851);
}
c.was_pos_clip_r = false;
if (input_r > 0.9549925859) {
c.was_pos_clip_r = true;
input_r = 0.7058208 + (c.last_sample_r * 0.2609148);
}
if (c.was_neg_clip_r == true) { // current will be -over
if (input_r > c.last_sample_r) c.last_sample_r = -0.7058208 + (input_r * 0.2609148);
else c.last_sample_r = -0.2491717 + (c.last_sample_r * 0.7390851);
}
c.was_neg_clip_r = false;
if (input_r < -0.9549925859) {
c.was_neg_clip_r = true;
input_r = -0.7058208 + (c.last_sample_r * 0.2609148);
}
c.intermediate_r[spacing] = input_r;
input_r = c.last_sample_r; // Latency is however many samples equals one 44.1k sample
for (int x = spacing; x > 0; x--) c.intermediate_r[x - 1] = c.intermediate_r[x];
c.last_sample_r = c.intermediate_r[0]; // run a little buffer to handle this
// end ClipOnly2 stereo as a little, compressed chunk that can be dropped into code
*out1 = input_l;
*out2 = input_r;
in1++;
in2++;
out1++;
out2++;
}
}
} // namespace trnr

194
clip/tube.h Normal file
View File

@@ -0,0 +1,194 @@
#pragma once
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stdint.h>
namespace trnr {
// modeled tube preamp based on tube2 by Chris Johnson (MIT License)
struct tube {
double samplerate;
double prev_sample_a;
double prev_sample_b;
double prev_sample_c;
double prev_sample_d;
double prev_sample_e;
double prev_sample_f;
uint32_t fdp_l;
uint32_t fdp_r;
float input_vol;
float tube_amt;
void set_input(double value) { input_vol = std::clamp(value, 0.0, 1.0); }
void set_tube(double value) { tube_amt = std::clamp(value, 0.0, 1.0); }
};
inline void tube_init(tube& t, double samplerate)
{
t.samplerate = 44100;
t.input_vol = 0.5;
t.tube_amt = 0.5;
t.prev_sample_a = 0.0;
t.prev_sample_b = 0.0;
t.prev_sample_c = 0.0;
t.prev_sample_d = 0.0;
t.prev_sample_e = 0.0;
t.prev_sample_f = 0.0;
t.fdp_l = 1.0;
while (t.fdp_l < 16386) t.fdp_l = rand() * UINT32_MAX;
t.fdp_r = 1.0;
while (t.fdp_r < 16386) t.fdp_r = rand() * UINT32_MAX;
}
template <typename t_sample>
inline void tube_process_block(tube& t, t_sample** inputs, t_sample** outputs, long sampleframes)
{
t_sample* in1 = inputs[0];
t_sample* in2 = inputs[1];
t_sample* out1 = outputs[0];
t_sample* out2 = outputs[1];
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= t.samplerate;
double input_pad = t.input_vol;
double iterations = 1.0 - t.tube_amt;
int powerfactor = (9.0 * iterations) + 1;
double asym_pad = (double)powerfactor;
double gainscaling = 1.0 / (double)(powerfactor + 1);
double outputscaling = 1.0 + (1.0 / (double)(powerfactor));
while (--sampleframes >= 0) {
double input_l = *in1;
double input_r = *in2;
if (fabs(input_l) < 1.18e-23) input_l = t.fdp_l * 1.18e-17;
if (fabs(input_r) < 1.18e-23) input_r = t.fdp_r * 1.18e-17;
if (input_pad < 1.0) {
input_l *= input_pad;
input_r *= input_pad;
}
if (overallscale > 1.9) {
double stored = input_l;
input_l += t.prev_sample_a;
t.prev_sample_a = stored;
input_l *= 0.5;
stored = input_r;
input_r += t.prev_sample_b;
t.prev_sample_b = stored;
input_r *= 0.5;
} // for high sample rates on this plugin we are going to do a simple average
if (input_l > 1.0) input_l = 1.0;
if (input_l < -1.0) input_l = -1.0;
if (input_r > 1.0) input_r = 1.0;
if (input_r < -1.0) input_r = -1.0;
// flatten bottom, point top of sine waveshaper L
input_l /= asym_pad;
double sharpen = -input_l;
if (sharpen > 0.0) sharpen = 1.0 + sqrt(sharpen);
else sharpen = 1.0 - sqrt(-sharpen);
input_l -= input_l * fabs(input_l) * sharpen * 0.25;
// this will take input from exactly -1.0 to 1.0 max
input_l *= asym_pad;
// flatten bottom, point top of sine waveshaper R
input_r /= asym_pad;
sharpen = -input_r;
if (sharpen > 0.0) sharpen = 1.0 + sqrt(sharpen);
else sharpen = 1.0 - sqrt(-sharpen);
input_r -= input_r * fabs(input_r) * sharpen * 0.25;
// this will take input from exactly -1.0 to 1.0 max
input_r *= asym_pad;
// end first asym section: later boosting can mitigate the extreme
// softclipping of one side of the wave
// and we are asym clipping more when Tube is cranked, to compensate
// original Tube algorithm: powerfactor widens the more linear region of the wave
double factor = input_l; // Left channel
for (int x = 0; x < powerfactor; x++) factor *= input_l;
if ((powerfactor % 2 == 1) && (input_l != 0.0)) factor = (factor / input_l) * fabs(input_l);
factor *= gainscaling;
input_l -= factor;
input_l *= outputscaling;
factor = input_r; // Right channel
for (int x = 0; x < powerfactor; x++) factor *= input_r;
if ((powerfactor % 2 == 1) && (input_r != 0.0)) factor = (factor / input_r) * fabs(input_r);
factor *= gainscaling;
input_r -= factor;
input_r *= outputscaling;
if (overallscale > 1.9) {
double stored = input_l;
input_l += t.prev_sample_c;
t.prev_sample_c = stored;
input_l *= 0.5;
stored = input_r;
input_r += t.prev_sample_d;
t.prev_sample_d = stored;
input_r *= 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
double slew = t.prev_sample_e - input_l;
if (overallscale > 1.9) {
double stored = input_l;
input_l += t.prev_sample_e;
t.prev_sample_e = stored;
input_l *= 0.5;
} else t.prev_sample_e = input_l; // for this, need previousSampleC always
if (slew > 0.0) slew = 1.0 + (sqrt(slew) * 0.5);
else slew = 1.0 - (sqrt(-slew) * 0.5);
input_l -= input_l * fabs(input_l) * slew * gainscaling;
// reusing gainscaling that's part of another algorithm
if (input_l > 0.52) input_l = 0.52;
if (input_l < -0.52) input_l = -0.52;
input_l *= 1.923076923076923;
// hysteresis and spiky fuzz R
slew = t.prev_sample_f - input_r;
if (overallscale > 1.9) {
double stored = input_r;
input_r += t.prev_sample_f;
t.prev_sample_f = stored;
input_r *= 0.5;
} else t.prev_sample_f = input_r; // for this, need previousSampleC always
if (slew > 0.0) slew = 1.0 + (sqrt(slew) * 0.5);
else slew = 1.0 - (sqrt(-slew) * 0.5);
input_r -= input_r * fabs(input_r) * slew * gainscaling;
// reusing gainscaling that's part of another algorithm
if (input_r > 0.52) input_r = 0.52;
if (input_r < -0.52) input_r = -0.52;
input_r *= 1.923076923076923;
// end hysteresis and spiky fuzz section
// begin 64 bit stereo floating point dither
// int expon; frexp((double)inputSampleL, &expon);
t.fdp_l ^= t.fdp_l << 13;
t.fdp_l ^= t.fdp_l >> 17;
t.fdp_l ^= t.fdp_l << 5;
// inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
// frexp((double)inputSampleR, &expon);
t.fdp_r ^= t.fdp_r << 13;
t.fdp_r ^= t.fdp_r >> 17;
t.fdp_r ^= t.fdp_r << 5;
// inputSampleR += ((double(fpdR)-uint32_t(0x7fffffff)) * 1.1e-44l * pow(2,expon+62));
// end 64 bit stereo floating point dither
*out1 = input_l;
*out2 = input_r;
in1++;
in2++;
out1++;
out2++;
}
}
} // namespace trnr