159 if (!(spec.sampleRate > 0.0))
return;
161 updateCoefficients();
178 const int nCh = std::min(buffer.getNumChannels(),
kMaxChannels);
179 const int nS = buffer.getNumSamples();
182 const T currentG =
g_.load(std::memory_order_relaxed);
183 const T storedRes =
resonance_.load(std::memory_order_relaxed);
184 const T currentDrive =
drive_.load(std::memory_order_relaxed);
185 const Mode currentMode =
mode_.load(std::memory_order_relaxed);
190 processBlockInput(buffer, nCh, nS,
191 inputCoeffs(
cutoff_.load(std::memory_order_relaxed), storedRes, currentDrive),
196 for (
int ch = 0; ch < nCh; ++ch) leaveInput(
state_[ch]);
200 processBlockStages(buffer, nCh, nS,
207 const T currentRes = std::min(storedRes, T(1));
212 case Mode::LP6: processBlockInternal<Mode::LP6>(buffer, nCh, nS, currentG, currentRes, currentDrive);
break;
213 case Mode::LP12: processBlockInternal<Mode::LP12>(buffer, nCh, nS, currentG, currentRes, currentDrive);
break;
214 case Mode::LP18: processBlockInternal<Mode::LP18>(buffer, nCh, nS, currentG, currentRes, currentDrive);
break;
215 case Mode::LP24: processBlockInternal<Mode::LP24>(buffer, nCh, nS, currentG, currentRes, currentDrive);
break;
216 case Mode::BP12: processBlockInternal<Mode::BP12>(buffer, nCh, nS, currentG, currentRes, currentDrive);
break;
217 case Mode::HP24: processBlockInternal<Mode::HP24>(buffer, nCh, nS, currentG, currentRes, currentDrive);
break;
239 if (channel < 0 || channel >=
kMaxChannels)
return input;
241 const T g =
g_.load(std::memory_order_relaxed);
242 const T res =
resonance_.load(std::memory_order_relaxed);
243 const T drive =
drive_.load(std::memory_order_relaxed);
249 const auto& ic = inputCoeffs(
cutoff_.load(std::memory_order_relaxed), res, drive);
250 switch (
mode_.load(std::memory_order_relaxed))
252 case Mode::LP6:
return inputSample<detail::LadderTap::LP6>(input, s, ic);
253 case Mode::LP12:
return inputSample<detail::LadderTap::LP12>(input, s, ic);
254 case Mode::LP18:
return inputSample<detail::LadderTap::LP18>(input, s, ic);
255 case Mode::LP24:
return inputSample<detail::LadderTap::LP24>(input, s, ic);
256 case Mode::BP12:
return inputSample<detail::LadderTap::BP12>(input, s, ic);
257 case Mode::HP24:
return inputSample<detail::LadderTap::HP24>(input, s, ic);
266 switch (
mode_.load(std::memory_order_relaxed))
268 case Mode::LP6:
return detail::ladderStepStages<detail::LadderTap::LP6>(input, s.
z, sc);
269 case Mode::LP12:
return detail::ladderStepStages<detail::LadderTap::LP12>(input, s.
z, sc);
270 case Mode::LP18:
return detail::ladderStepStages<detail::LadderTap::LP18>(input, s.
z, sc);
271 case Mode::LP24:
return detail::ladderStepStages<detail::LadderTap::LP24>(input, s.
z, sc);
272 case Mode::BP12:
return detail::ladderStepStages<detail::LadderTap::BP12>(input, s.
z, sc);
273 case Mode::HP24:
return detail::ladderStepStages<detail::LadderTap::HP24>(input, s.
z, sc);
278 const BlockCoeffs c = makeBlockCoeffs(g, std::min(res, T(1)));
280 switch (
mode_.load(std::memory_order_relaxed))
282 case Mode::LP6:
return processSampleInternal<Mode::LP6>(input, s, c, drive);
283 case Mode::LP12:
return processSampleInternal<Mode::LP12>(input, s, c, drive);
284 case Mode::LP18:
return processSampleInternal<Mode::LP18>(input, s, c, drive);
285 case Mode::LP24:
return processSampleInternal<Mode::LP24>(input, s, c, drive);
286 case Mode::BP12:
return processSampleInternal<Mode::BP12>(input, s, c, drive);
287 case Mode::HP24:
return processSampleInternal<Mode::HP24>(input, s, c, drive);
323 if (!std::isfinite(hz))
return;
324 cutoff_.store(std::max(hz, T(0)), std::memory_order_relaxed);
325 updateCoefficients();
340 if (!std::isfinite(amount))
return;
342 std::memory_order_relaxed);
356 if (!std::isfinite(amount))
return;
357 drive_.store(std::max(amount, T(0.1)), std::memory_order_relaxed);
366 mode_.store(mode, std::memory_order_relaxed);
404 [[nodiscard]] std::vector<uint8_t>
getState()
const
409 w.
write(
"cutoff",
static_cast<float>(
cutoff_.load(std::memory_order_relaxed)));
410 w.
write(
"resonance",
static_cast<float>(
resonance_.load(std::memory_order_relaxed)));
411 w.
write(
"drive",
static_cast<float>(
drive_.load(std::memory_order_relaxed)));
412 w.
write(
"mode",
static_cast<int32_t
>(
mode_.load(std::memory_order_relaxed)));
413 w.
write(
"saturation",
static_cast<int32_t
>(
saturation_.load(std::memory_order_relaxed)));
430 const int mode = std::clamp(r.
read(
"mode",
static_cast<int32_t
>(
Mode::LP24)),
437 [[nodiscard]] T
getCutoff() const noexcept {
return cutoff_.load(std::memory_order_relaxed); }
439 [[nodiscard]] T
getDrive() const noexcept {
return drive_.load(std::memory_order_relaxed); }
440 [[nodiscard]]
Mode getMode() const noexcept {
return mode_.load(std::memory_order_relaxed); }
453 std::array<T, 4>
z {};
458 std::array<ChannelState, kMaxChannels>
state_ {};
475 std::atomic<T>
g_ {T(0)};
486 void updateCoefficients() noexcept
489 const T clamped = std::clamp(
cutoff_.load(std::memory_order_relaxed),
491 cutoff_.store(clamped, std::memory_order_relaxed);
492 const T preWarpedGain =
static_cast<T
>(std::tan(pi<double> *
static_cast<double>(clamped) /
spec_.
sampleRate));
493 g_.store(preWarpedGain, std::memory_order_relaxed);
497 using BlockCoeffs = detail::LadderCoeffs<T>;
500 [[nodiscard]]
static BlockCoeffs makeBlockCoeffs(T g, T res)
noexcept
506 template <Mode FilterMode>
507 void processBlockInternal(AudioBufferView<T> buffer,
int nCh,
int nS, T g, T res, T drive)
noexcept
511 const BlockCoeffs c = makeBlockCoeffs(g, res);
513 for (
int ch = 0; ch < nCh; ++ch)
515 auto* channelData = buffer.getChannel(ch);
518 std::array<T, 4> z =
state_[ch].z;
520 for (
int i = 0; i < nS; ++i)
525 if (!std::isfinite(z[0] + z[1] + z[2] + z[3])) z.fill(T(0));
536 [[nodiscard]]
const detail::LadderInputCoeffs& inputCoeffs(T cutoff, T res, T drive)
noexcept
539 const bool same = cutoff == cache.
cutoff && res == cache.resonance && drive == cache.drive
546 cache.cutoff = cutoff;
547 cache.resonance = res;
555 static void leaveInput(ChannelState& s)
noexcept
557 if (!s.inputLive)
return;
558 for (
size_t i = 0; i < 4; ++i) s.z[i] =
static_cast<T
>(s.input.q[i]);
573 template <detail::LadderTap Tap>
574 [[nodiscard]] T inputSample(T input, ChannelState& s,
const detail::LadderInputCoeffs& c)
noexcept
576 const bool first = !s.inputLive;
577 if (first) [[unlikely]]
580 for (
size_t i = 0; i < 4; ++i) s.input.q[i] =
static_cast<double>(s.z[i]);
582 s.input.input =
static_cast<double>(input);
585 T y = detail::ladderStepInput<Tap>(input, s.input, c);
586 if (first) [[unlikely]]
588 const double mean = s.input.output / (1.0 + c.post);
589 s.input.output = mean;
590 y =
static_cast<T
>(mean);
596 void processBlockInput(AudioBufferView<T> buffer,
int nCh,
int nS,
597 const detail::LadderInputCoeffs& c,
Mode mode)
noexcept
601 case Mode::LP6: inputLoop<detail::LadderTap::LP6>(buffer, nCh, nS, c);
break;
602 case Mode::LP12: inputLoop<detail::LadderTap::LP12>(buffer, nCh, nS, c);
break;
603 case Mode::LP18: inputLoop<detail::LadderTap::LP18>(buffer, nCh, nS, c);
break;
604 case Mode::LP24: inputLoop<detail::LadderTap::LP24>(buffer, nCh, nS, c);
break;
605 case Mode::BP12: inputLoop<detail::LadderTap::BP12>(buffer, nCh, nS, c);
break;
606 case Mode::HP24: inputLoop<detail::LadderTap::HP24>(buffer, nCh, nS, c);
break;
611 static void recoverInput(detail::LadderInputState& h)
noexcept
613 if (!std::isfinite(((h.q[0] + h.q[1]) + (h.q[2] + h.q[3])) + ((h.input + h.output) + h.ahead)
614 + (h.loopStates + h.loopDrive)))
618 template <detail::LadderTap Tap>
619 void inputLoop(AudioBufferView<T> buffer,
int nCh,
int nS,
const detail::LadderInputCoeffs& c)
noexcept
626 for (; ch + 1 < nCh; ch += 2)
628 auto* a = buffer.getChannel(ch);
629 auto* b = buffer.getChannel(ch + 1);
630 ChannelState& sa =
state_[ch];
631 ChannelState& sb =
state_[ch + 1];
633 if (!sa.inputLive || !sb.inputLive)
635 a[0] = inputSample<Tap>(a[0], sa, c);
636 b[0] = inputSample<Tap>(b[0], sb, c);
640 detail::LadderInputState ha = sa.input, hb = sb.input;
642 detail::ladderStepInputPair<Tap>(a[i], b[i], ha, hb, c);
649 for (; ch < nCh; ++ch)
651 auto* channelData = buffer.getChannel(ch);
652 ChannelState& s =
state_[ch];
656 channelData[0] = inputSample<Tap>(channelData[0], s, c);
659 detail::LadderInputState h = s.input;
661 channelData[i] = detail::ladderStepInput<Tap>(channelData[i], h, c);
668 void processBlockStages(AudioBufferView<T> buffer,
int nCh,
int nS,
669 const detail::LadderStageCoeffs& c,
Mode mode)
noexcept
673 case Mode::LP6: stagesLoop<detail::LadderTap::LP6>(buffer, nCh, nS, c);
break;
674 case Mode::LP12: stagesLoop<detail::LadderTap::LP12>(buffer, nCh, nS, c);
break;
675 case Mode::LP18: stagesLoop<detail::LadderTap::LP18>(buffer, nCh, nS, c);
break;
676 case Mode::LP24: stagesLoop<detail::LadderTap::LP24>(buffer, nCh, nS, c);
break;
677 case Mode::BP12: stagesLoop<detail::LadderTap::BP12>(buffer, nCh, nS, c);
break;
678 case Mode::HP24: stagesLoop<detail::LadderTap::HP24>(buffer, nCh, nS, c);
break;
682 template <detail::LadderTap Tap>
683 void stagesLoop(AudioBufferView<T> buffer,
int nCh,
int nS,
const detail::LadderStageCoeffs& c)
noexcept
689 for (; ch + 1 < nCh; ch += 2)
691 auto* a = buffer.getChannel(ch);
692 auto* b = buffer.getChannel(ch + 1);
693 std::array<T, 4> za =
state_[ch].z, zb =
state_[ch + 1].z;
694 for (
int i = 0; i < nS; ++i)
696 a[i] = detail::ladderStepStages<Tap>(a[i], za, c);
697 b[i] = detail::ladderStepStages<Tap>(b[i], zb, c);
699 if (!std::isfinite(za[0] + za[1] + za[2] + za[3])) za.fill(T(0));
700 if (!std::isfinite(zb[0] + zb[1] + zb[2] + zb[3])) zb.fill(T(0));
704 for (; ch < nCh; ++ch)
706 auto* channelData = buffer.getChannel(ch);
707 std::array<T, 4> z =
state_[ch].z;
708 for (
int i = 0; i < nS; ++i)
709 channelData[i] = detail::ladderStepStages<Tap>(channelData[i], z, c);
711 if (!std::isfinite(z[0] + z[1] + z[2] + z[3])) z.fill(T(0));
737 template <Mode FilterMode>
738 [[nodiscard]] T processSampleInternal(T input, ChannelState& s,
const BlockCoeffs& c, T drive)
noexcept
740 return detail::ladderStep<tapOf(FilterMode)>(input, s.z, c, drive);