#include "Delay.h" namespace serum { void DelayUnit::prepare (double sampleRate, int) { sr = sampleRate; const int maxDelay = (int) (sr * 2.1); // 2.1 seconds delayL.assign ((size_t) maxDelay, 0.0f); delayR.assign ((size_t) maxDelay, 0.0f); reset(); } void DelayUnit::reset() { std::fill (delayL.begin(), delayL.end(), 0.0f); std::fill (delayR.begin(), delayR.end(), 0.0f); writePos = 0; dampL = dampR = 0.0f; } void DelayUnit::process (float* l, float* r, int numSamples, const float p[4]) { const int len = (int) delayL.size(); if (len < 4) return; const double delaySamples = juce::jlimit (2.0, (double) len - 2.0, maps::delayToMs (p[0]) / 1000.0 * sr); const float feedback = p[1] * 0.92f; const float dampCoef = 1.0f - p[2] * 0.95f; // 1 = no damping const float pingpong = p[3]; for (int i = 0; i < numSamples; ++i) { double readPos = writePos - delaySamples; if (readPos < 0.0) readPos += len; const int i0 = (int) readPos; const int i1 = (i0 + 1) % len; const float frac = (float) (readPos - std::floor (readPos)); float dl = delayL[(size_t) i0] * (1.0f - frac) + delayL[(size_t) i1] * frac; float dr = delayR[(size_t) i0] * (1.0f - frac) + delayR[(size_t) i1] * frac; // Damping lowpass in the feedback path. dampL = dl * (1.0f - dampCoef) + dampL * dampCoef; dampR = dr * (1.0f - dampCoef) + dampR * dampCoef; // Ping-pong: cross-feed between channels. const float feedL = dampR * pingpong + dampL * (1.0f - pingpong); const float feedR = dampL * pingpong + dampR * (1.0f - pingpong); delayL[(size_t) writePos] = l[i] + feedL * feedback; delayR[(size_t) writePos] = r[i] + feedR * feedback; l[i] = dl; r[i] = dr; writePos = (writePos + 1) % len; } } } // namespace serum