109 void prepare(
double sampleRate,
int stages,
double cornerHz,
int factor = 1)
111 if (!std::isfinite(sampleRate) || !(sampleRate > 0.0))
return;
113 factor_ = factor == 2 ? 2 : 1;
114 rate_ = fs_ *
static_cast<double>(factor_);
116 buckets_.assign(
static_cast<size_t>(stages_ / 2), 0.0);
136 in_.k = chorus ? std::array<double, 2> { 1.0 / 0.8, 1.0 / 0.8 } : kButterworthK;
138 out_.count = chorus ? 1 : 2;
139 out_.k = chorus ? std::array<double, 2> { 1.0 / 0.6, 0.0 } : kButterworthK;
150 std::fill(buckets_.begin(), buckets_.end(), 0.0);
156 rise1_ = rise2_ = middle1_ = 0.0;
173 if (!std::isfinite(inputHz) || !std::isfinite(outputHz))
return;
174 inCorner_ = std::clamp(inputHz, 200.0, 0.45 * fs_);
175 outCorner_ = std::clamp(outputHz, 200.0, 0.45 * fs_);
176 in_.aim(std::tan(3.141592653589793 * inCorner_ / rate_), inCorner_, snap_ ? 0 : kCornerRampSteps);
177 out_.aim(std::tan(3.141592653589793 * outCorner_ / rate_), outCorner_, snap_ ? 0 : kCornerRampSteps);
190 [[nodiscard]]
int getStages() const noexcept {
return stages_; }
192 [[nodiscard]]
double getCorner() const noexcept {
return inCorner_; }
196 [[nodiscard]]
int getFactor() const noexcept {
return factor_; }
207 [[nodiscard]]
double clockFor(
double delaySeconds)
const noexcept
209 return static_cast<double>(stages_) / (2.0 * std::max(delaySeconds, 1e-5));
227 [[nodiscard]]
double delayFor(
double wantedSeconds)
const noexcept
232 const double filters = in_.delay(rate_) + out_.delay(rate_);
233 const double steps = (precision_ ==
Precision::High ? 3.5 : 0.5) / rate_;
234 const double chainOnly = (wantedSeconds - filters - steps) / (1.0 + 1.0 /
static_cast<double>(stages_));
235 return std::max(chainOnly, 1e-5);
247 const double x = std::isfinite(input) ?
static_cast<double>(input) : 0.0;
248 if (factor_ == 2) (void)chain(lowPass(0.5 * (lastInput_ + x)), delaySeconds);
250 return static_cast<T
>(chain(lowPass(x), delaySeconds));
269 double& outThis,
double& outTwin)
noexcept
271 const double x = std::isfinite(input) ?
static_cast<double>(input) : 0.0;
274 const double mid = lowPass(0.5 * (lastInput_ + x));
275 (void)chain(mid, delayThis);
276 (void)twin.chain(mid, delayTwin);
279 const double filtered = lowPass(x);
280 outThis =
static_cast<double>(
static_cast<T
>(chain(filtered, delayThis)));
281 outTwin =
static_cast<double>(
static_cast<T
>(twin.chain(filtered, delayTwin)));
286 double lowPass(
double x)
noexcept {
return in_.run(x); }
293 [[nodiscard]]
double quintic(
double t,
double x)
const noexcept
295 const double a = t + 2.0, b = t + 1.0, e = t - 1.0, f = t - 2.0, g = t - 3.0;
296 const double ab = a * b, efg = e * f * g;
297 return history_[0] * (b * t * efg) * (-1.0 / 120.0)
298 + history_[1] * (a * t * efg) * (1.0 / 24.0)
299 + history_[2] * (ab * efg) * (-1.0 / 12.0)
300 + history_[3] * (ab * t * f * g) * (1.0 / 12.0)
301 + history_[4] * (ab * t * e * g) * (-1.0 / 24.0)
302 + x * (ab * t * e * f) * (1.0 / 120.0);
306 double chain(
double x,
double delaySeconds)
noexcept
309 const double step = std::min(
clockFor(delaySeconds) / rate_, 64.0);
310 const double start = phase_;
312 const int size =
static_cast<int>(buckets_.size());
325 double area = 0.0, last = 0.0, rise = 0.0, middle = 0.0, fall = 0.0;
326 const auto weigh = [&](
double from,
double to)
noexcept {
327 const double d1 = to - from, d2 = 0.5 * (to * to - from * from),
328 d3 = (to * to * to - from * from * from) * (1.0 / 3.0);
329 rise += held_ * (0.5 * d3);
330 middle += held_ * (0.5 * d1 + d2 - d3);
331 fall += held_ * (0.5 * (d1 - 2.0 * d2 + d3));
333 for (
double tick = std::floor(start) + 1.0; tick <= phase_ && size > 0; tick += 1.0)
335 const double t = step > 0.0 ? (tick - start) / step : 1.0;
336 const double sampled = high ? quintic(t, x) : previousIn_ + t * (x - previousIn_);
337 area += held_ * (t - last);
338 if (high) weigh(last, t);
340 held_ = buckets_[
static_cast<size_t>(head_)];
341 buckets_[
static_cast<size_t>(head_)] = sampled;
342 head_ = head_ + 1 == size ? 0 : head_ + 1;
344 area += held_ * (1.0 - last);
345 if (phase_ >= 1.0) phase_ -= std::floor(phase_);
351 y = rise2_ + middle1_ + fall;
355 history_ = { history_[1], history_[2], history_[3], history_[4], x };
360 static constexpr std::array<double, 2> kButterworthK = { 1.0 / 0.5411961001461970, 1.0 / 1.3065629648763764 };
361 static constexpr int kCornerRampSteps = 32;
368 std::array<double, 2> k = kButterworthK;
370 double g = 0.1, target = 0.1, step = 0.0;
371 double hz = 4000.0, hzTarget = 4000.0, hzStep = 0.0;
373 std::array<double, 2> a1 {};
374 std::array<double, 2> s1 {}, s2 {};
377 void solve() noexcept
379 for (
int i = 0; i < count; ++i) a1[static_cast<size_t>(i)] = 1.0 / (1.0 + g * (g + k[
static_cast<size_t>(i)]));
381 void aim(
double to,
double toHz,
int steps)
noexcept
385 if (steps <= 0) { g = to; hz = toHz; left = 0; solve();
return; }
387 step = (to - g) /
static_cast<double>(steps);
388 hzStep = (toHz - hz) /
static_cast<double>(steps);
390 void clear() noexcept
396 double run(
double x)
noexcept
400 const bool landed = --left == 0;
401 g = landed ? target : g + step;
402 hz = landed ? hzTarget : hz + hzStep;
407 const double m = 0.5 * (x + last);
411 for (
int i = 0; i < count; ++i)
413 const auto j =
static_cast<size_t>(i);
414 const double v1 = a1[j] * (s1[j] + g * (x - s2[j]));
415 const double v2 = s2[j] + g * v1;
416 s1[j] = 2.0 * v1 - s1[j];
417 s2[j] = 2.0 * v2 - s2[j];
423 [[nodiscard]]
double delay(
double rate)
const noexcept
426 for (
int i = 0; i < count; ++i) sum += k[static_cast<size_t>(i)];
427 return sum / (2.0 * 3.141592653589793 * hz) + (mean ? 0.5 / rate : 0.0);
431 double fs_ = 48000.0, rate_ = 48000.0, inCorner_ = 4000.0, outCorner_ = 4000.0;
432 int stages_ = 1024, factor_ = 1;
433 std::vector<double> buckets_;
435 double phase_ = 0.0, previousIn_ = 0.0, held_ = 0.0, lastInput_ = 0.0;
436 double rise1_ = 0.0, rise2_ = 0.0, middle1_ = 0.0;
437 std::array<double, 5> history_ {};
440 LowPass in_ {}, out_ {};