Files
serumalt/Source/EffectUnits/Flanger.cpp
T
biggy e4cea300db chore: land janitor round 1 and graft integration
Round 1 findings and fixes, all previously uncommitted:
- FXProcessor per-slot DSP history repair (HEAD does not compile without it)
- SynthVoice::getModulationValue, editor control-lock guard
- ExactCompare.h naming the deliberate exact float comparisons
- KnobLookAndFeel replacing the never-installed SerumLookAndFeel
- 55 first-party compiler warnings to 0
- AGENT.md renamed to AGENTS.md across Source/
- graft repository context graph and its hooks
- JANITOR_TASKS.md and UI_UX_AUDIT.md records
2026-10-06 08:50:45 +02:00

64 lines
2.0 KiB
C++

#include "Flanger.h"
namespace serum
{
void FlangerUnit::prepare (double sampleRate, int)
{
sr = sampleRate;
const int maxDelay = (int) (sr * 0.03); // 30ms
delayL.assign ((size_t) maxDelay, 0.0f);
delayR.assign ((size_t) maxDelay, 0.0f);
reset();
}
void FlangerUnit::reset()
{
std::fill (delayL.begin(), delayL.end(), 0.0f);
std::fill (delayR.begin(), delayR.end(), 0.0f);
writePos = 0;
lfoPhase = 0.0;
}
void FlangerUnit::process (float* l, float* r, int numSamples, const float p[4])
{
const int len = (int) delayL.size();
if (len < 4)
return;
const double rate = 0.05 + p[0] * 2.0;
const double depth = (0.3 + p[1] * 4.7) * sr / 1000.0; // 0.3..5 ms
const double base = 1.5 * sr / 1000.0;
const float feedback = p[2] * 0.9f;
for (int i = 0; i < numSamples; ++i)
{
const float inL = l[i], inR = r[i];
lfoPhase += juce::MathConstants<double>::twoPi * rate / sr;
if (lfoPhase > juce::MathConstants<double>::twoPi) lfoPhase -= juce::MathConstants<double>::twoPi;
const double mod = std::sin (lfoPhase) * depth;
// base + mod is not guaranteed positive: at high depth the LFO offset
// can exceed the centre delay, so the read position runs ahead of the
// write position and needs the wrap in both directions to stay in range.
double readPos = writePos - (base + mod);
if (readPos < 0.0) readPos += len;
if (readPos >= (double) len) readPos -= len;
const int i0 = (int) readPos;
const int i1 = (i0 + 1) % len;
const float frac = (float) (readPos - std::floor (readPos));
const float outL = delayL[(size_t) i0] * (1.0f - frac) + delayL[(size_t) i1] * frac;
const float outR = delayR[(size_t) i0] * (1.0f - frac) + delayR[(size_t) i1] * frac;
delayL[(size_t) writePos] = inL + outL * feedback;
delayR[(size_t) writePos] = inR + outR * feedback;
l[i] = outL;
r[i] = outR;
writePos = (writePos + 1) % len;
}
}
} // namespace serum