CMLSDistortion.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "CMLSDistortion.h"
  2. CMLSDistortion::CMLSDistortion() {}
  3. void CMLSDistortion::prepare(const juce::dsp::ProcessSpec& spec) {
  4. inputGain.prepare(spec);
  5. outputGain.prepare(spec);
  6. inputGain.setGainDecibels(0.0f);
  7. outputGain.setGainDecibels(0.0f);
  8. }
  9. void CMLSDistortion::reset() {
  10. inputGain.reset();
  11. outputGain.reset();
  12. }
  13. void CMLSDistortion::process(const juce::dsp::ProcessContextReplacing<float>& context) {
  14. auto& block = context.getOutputBlock();
  15. inputGain.process(context);
  16. for (size_t ch = 0; ch < block.getNumChannels(); ++ch) {
  17. auto* data = block.getChannelPointer(ch);
  18. for (size_t i = 0; i < block.getNumSamples(); ++i) {
  19. float dry = data[i];
  20. float wet = std::tanh(data[i] * drive); // soft clipping
  21. data[i] = dry * (1.0f - mix) + wet * mix; // apply mix
  22. }
  23. }
  24. outputGain.process(context);
  25. }
  26. void CMLSDistortion::setDrive(float newDrive) {
  27. drive = newDrive;
  28. }
  29. void CMLSDistortion::setMix(float newMix)
  30. {
  31. mix = juce::jlimit(0.0f, 1.0f, newMix); // between 0.0 and 1.0
  32. }