#include "Distortion.h" namespace serum { void DistortionUnit::prepare (double sampleRate, int) { sr = sampleRate; lpL.setSampleRate (sr); lpR.setSampleRate (sr); lpL.setLowpass (8000.0, 0.7071); lpR.setLowpass (8000.0, 0.7071); reset(); } void DistortionUnit::reset() { lpL.clear(); lpR.clear(); } float DistortionUnit::shape (float x, float shapeParam) noexcept { if (shapeParam < 0.33f) return std::tanh (x); if (shapeParam < 0.66f) return juce::jlimit (-1.0f, 1.0f, x); return std::sin (x * 1.5707963267948966f); // sine fold } void DistortionUnit::process (float* l, float* r, int numSamples, const float p[4]) { const float drive = 1.0f + p[0] * 24.0f; const float shapeP = p[1]; const float output = 0.4f + p[3] * 1.2f; // Tone: re-tune the lowpass from 800..16000 Hz. const double toneHz = 800.0 + p[2] * 15200.0; lpL.setLowpass (toneHz, 0.7071); lpR.setLowpass (toneHz, 0.7071); for (int i = 0; i < numSamples; ++i) { l[i] = lpL.process (shape (l[i] * drive, shapeP) * output); r[i] = lpR.process (shape (r[i] * drive, shapeP) * output); } } } // namespace serum