mirror of
https://codeberg.org/armin/chromaflock.git
synced 2026-09-01 04:10:47 +02:00
66 lines
2.3 KiB
C++
66 lines
2.3 KiB
C++
#pragma once
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
class Delay {
|
|
public:
|
|
void prepare(double sr) {
|
|
sampleRate = sr;
|
|
int maxSamples = static_cast<int>(sr * 2.0);
|
|
bufferL.resize(maxSamples, 0.0f);
|
|
bufferR.resize(maxSamples, 0.0f);
|
|
writePos = 0;
|
|
}
|
|
|
|
void setParameters(float timeMs, float fb, float mx, bool pingpong = false) {
|
|
timeSamplesF = timeMs * 0.001f * static_cast<float>(sampleRate);
|
|
float bufMax = static_cast<float>(bufferL.size()) - 1.0f;
|
|
timeSamplesF = std::clamp(timeSamplesF, 1.0f, bufMax);
|
|
feedback = std::clamp(fb, 0.0f, 0.95f);
|
|
mix = std::clamp(mx, 0.0f, 1.0f);
|
|
pingPong = pingpong;
|
|
}
|
|
|
|
void process(float& left, float& right) {
|
|
int bufSize = static_cast<int>(bufferL.size());
|
|
|
|
float readPosF = static_cast<float>(writePos) - timeSamplesF;
|
|
while (readPosF < 0.0f) readPosF += static_cast<float>(bufSize);
|
|
|
|
int i0 = static_cast<int>(readPosF) % bufSize;
|
|
int i1 = (i0 + 1) % bufSize;
|
|
float frac = readPosF - std::floor(readPosF);
|
|
|
|
float outL = bufferL[i0] * (1.0f - frac) + bufferL[i1] * frac;
|
|
float outR = bufferR[i0] * (1.0f - frac) + bufferR[i1] * frac;
|
|
|
|
if (pingPong) {
|
|
// Classic ping-pong: the (summed) input enters one line and each
|
|
// echo is bounced to the opposite channel on the next repeat, so the
|
|
// taps alternate L/R. Only the cross-coupled echo is fed into the
|
|
// opposite line (never the dry input) so the bounce is audible even
|
|
// for a mono source where L == R.
|
|
float in = 0.5f * (left + right);
|
|
bufferL[writePos] = in + outR * feedback;
|
|
bufferR[writePos] = outL * feedback;
|
|
} else {
|
|
bufferL[writePos] = left + outL * feedback;
|
|
bufferR[writePos] = right + outR * feedback;
|
|
}
|
|
|
|
writePos = (writePos + 1) % bufSize;
|
|
|
|
left = left * (1.0f - mix) + outL * mix;
|
|
right = right * (1.0f - mix) + outR * mix;
|
|
}
|
|
|
|
private:
|
|
std::vector<float> bufferL, bufferR;
|
|
int writePos = 0;
|
|
double sampleRate = 44100;
|
|
float timeSamplesF = 8820.0f;
|
|
float feedback = 0.3f;
|
|
float mix = 0.25f;
|
|
bool pingPong = false;
|
|
};
|