Skip to content

Commit

Permalink
Stop runaway modulators on extreme changes (surge-synthesizer#6863)
Browse files Browse the repository at this point in the history
In extreme (really computer generated) midi sequences you could
force the modulator smoother to runaway since in one case it
would push you outside of [target,value] when interpolating.
Fix that with a clamp; and add an assert in case theres other
future problems.

Closes surge-synthesizer#6835
  • Loading branch information
baconpaul authored Mar 3, 2023
1 parent 41a5fd8 commit 4be8c59
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/common/ModulationSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#pragma once

#include <random>
#include <cassert>

#include "basic_dsp.h"

enum modsrctype
Expand Down Expand Up @@ -484,8 +486,12 @@ template <int NDX = 1> class ControllerModulationSourceVector : public Modulatio
}
else
{
float a = (mode == Modulator::SmoothingMode::FAST_EXP ? 0.99f : 0.9f) * 44100 *
samplerate_inv * b;
// Don't allow us to push outside of [target,value]
// so clamp the interpolator to 0,1
float a =
std::clamp((mode == Modulator::SmoothingMode::FAST_EXP ? 0.99f : 0.9f) *
44100 * samplerate_inv * b,
0.f, 1.f);

value[idx] = (1 - a) * value[idx] + a * target[idx];
}
Expand Down Expand Up @@ -516,11 +522,8 @@ template <int NDX = 1> class ControllerModulationSourceVector : public Modulatio
value[idx] = target[idx];
}

// GitHub issue #6835
if (std::isinf(value[idx]))
{
value[idx] = 0.f;
}
// Just in case #6835 sneaks back
assert(!std::isnan(value[idx]) && !std::isinf(value[idx]));
}
}

Expand Down

0 comments on commit 4be8c59

Please sign in to comment.