#include "CMLSDistortion.h"

CMLSDistortion::CMLSDistortion() {}

void CMLSDistortion::prepare(const juce::dsp::ProcessSpec& spec) {
    inputGain.prepare(spec);
    outputGain.prepare(spec);
    inputGain.setGainDecibels(0.0f);
    outputGain.setGainDecibels(0.0f);
}

void CMLSDistortion::reset() {
    inputGain.reset();
    outputGain.reset();
}

void CMLSDistortion::process(const juce::dsp::ProcessContextReplacing<float>& context) {
    auto& block = context.getOutputBlock();
    inputGain.process(context);

    for (size_t ch = 0; ch < block.getNumChannels(); ++ch) {
        auto* data = block.getChannelPointer(ch);
        for (size_t i = 0; i < block.getNumSamples(); ++i) {
            float dry = data[i];
            float wet = std::tanh(data[i] * drive);  // soft clipping
            data[i] = dry * (1.0f - mix) + wet * mix; // apply mix
        }
    }

    outputGain.process(context);
}

void CMLSDistortion::setDrive(float newDrive) {
    drive = newDrive;
}

void CMLSDistortion::setMix(float newMix)
{
    mix = juce::jlimit(0.0f, 1.0f, newMix); // between 0.0 and 1.0
}