/* ============================================================================== CMLSDelay.cpp Created: 3 May 2025 5:39:35pm Author: Luigi ============================================================================== */ #include "CMLSDelay.h" CMLSDelay::CMLSDelay() { delayLengthRange = new juce::NormalisableRange(0, MAX_DELAY_LENGTH); feedbackRange = new juce::NormalisableRange(0, MAX_FEEDBACK); this->delayLength = 0.0f; this->feedback = 0.75f; this->delayBufferLength = 1; this->delayReadPosition = 0; this->delayWritePosition = 0; } CMLSDelay::~CMLSDelay() {} void CMLSDelay::reset() { } void CMLSDelay::prepare(const juce::dsp::ProcessSpec& spec) { this->delayBufferLength = (int) 2.0 * spec.sampleRate; if (this->delayBufferLength < 1) { this->delayBufferLength = 1; } this->delayBuffer.setSize(spec.numChannels, this->delayBufferLength); this->delayBuffer.clear(); this->delayReadPosition = (int)(this->delayWritePosition - (this->delayLength * spec.sampleRate) + this->delayBufferLength) % this->delayBufferLength; } void CMLSDelay::process(const juce::dsp::ProcessContextReplacing& context) { auto audioBlock = context.getOutputBlock(); auto numChannels = audioBlock.getNumChannels(); const auto numSamples = audioBlock.getNumSamples(); int dpr, dpw; for (int channel = 0; channel < numChannels; ++channel) { float* channelData = audioBlock.getChannelPointer(channel); float* delayData = this->delayBuffer.getWritePointer(juce::jmin(channel, this->delayBuffer.getNumChannels() - 1)); dpr = this->delayReadPosition; dpw = this->delayWritePosition; for (int sample = 0; sample < numSamples; sample++) { const float in = channelData[sample]; float out = 0.0f; out = ((1-this->dryWetProp) * in + this->dryWetProp * delayData[dpr]); delayData[dpw] = in + (this->feedback * delayData[dpr]); if (++dpr >= this->delayBufferLength) { dpr = 0; } if (++dpw >= this->delayBufferLength) { dpw = 0; } channelData[sample] = out; } } this->delayReadPosition = dpr; this->delayWritePosition = dpw; } void CMLSDelay::setDryWet(float value) { this->dryWetProp = value; } void CMLSDelay::setAmount(float value) { this->delayLength = this->delayLengthRange->convertFrom0to1(value); this->feedback = this->delayLengthRange->convertFrom0to1(value); } const float CMLSDelay::getDryWet() { return this->dryWetProp; } const float CMLSDelay::getAmount() { return this->amount; }