DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::LadderFilter< T > Class Template Reference

4-pole resonant ladder filter (Moog topology, TPT discretization). More...

#include <LadderFilter.h>

Collaboration diagram for dspark::LadderFilter< T >:

Classes

struct  ChannelState
 
struct  InputCache
 

Public Types

enum class  Mode {
  LP6 , LP12 , LP18 , LP24 ,
  BP12 , HP24
}
 Defines the frequency response mode of the filter output. More...
 
enum class  Saturation { Feedback , Stages , Input }
 Where the filter saturates (see the class notes). More...
 

Public Member Functions

 ~LadderFilter ()=default
 
void prepare (const AudioSpec &spec) noexcept
 Prepares the filter with the current audio environment.
 
void processBlock (AudioBufferView< T > buffer) noexcept
 Processes an audio buffer in-place (thread-safe vs. setters).
 
T processSample (T input, int channel) noexcept
 Processes a single sample on one channel.
 
void reset () noexcept
 Clears the internal integrator state.
 
void setCutoff (T hz) noexcept
 Sets the cutoff frequency (thread-safe, lock-free).
 
void setResonance (T amount) noexcept
 Sets resonance amount (thread-safe, lock-free).
 
void setDrive (T amount) noexcept
 Sets the nonlinear drive amount (thread-safe, lock-free).
 
void setMode (Mode mode) noexcept
 Sets the filter output mode (thread-safe, lock-free).
 
void setSaturation (Saturation model) noexcept
 Selects the saturation model (thread-safe, lock-free).
 
std::vector< uint8_t > getState () const
 Serializes the parameters (setup/UI threads; allocates).
 
bool setState (const uint8_t *data, size_t size)
 Restores parameters from a blob (tolerant; rejects foreign ids).
 
T getCutoff () const noexcept
 
T getResonance () const noexcept
 
T getDrive () const noexcept
 
Mode getMode () const noexcept
 
Saturation getSaturation () const noexcept
 

Static Public Attributes

static constexpr double kMaxResonance = 1.2
 Highest resonance setResonance() accepts (Feedback uses at most 1).
 

Protected Attributes

std::array< ChannelState, kMaxChannels > state_ {}
 
AudioSpec spec_ {}
 
InputCache inputCache_ {}
 
std::atomic< T > cutoff_ {T(1000)}
 
std::atomic< T > resonance_ {T(0)}
 
std::atomic< T > drive_ {T(1)}
 
std::atomic< T > g_ {T(0)}
 
std::atomic< Mode > mode_ {Mode::LP24}
 
std::atomic< Saturation > saturation_ {Saturation::Feedback}
 

Static Protected Attributes

static constexpr int kMaxChannels = 16
 

Detailed Description

template<FloatType T>
class dspark::LadderFilter< T >

4-pole resonant ladder filter (Moog topology, TPT discretization).

Four cascaded one-pole TPT lowpass stages with global feedback. The zero-delay feedback loop is resolved analytically each sample, and the multimode outputs are tap mixes of the cascade. processBlock() snapshots the parameters once per block and dispatches to a per-mode template instantiation, so the sample loop carries no branches.

Three saturation models (setSaturation()):

  • Feedback (default): linear stages, with drive > 1 shaping only the resonance feedback through a bounded tanh. Cheapest (no iteration).
  • Stages: the transistor-ladder model in which every stage saturates, dy_i/dt = wc [phi(y_{i-1}) - phi(y_i)], phi(v) = tanh(drive v) / drive, solved implicitly per sample (detail::ladderStepStages()). Against a 128x oversampled reference of the same equations (48 kHz, 110 Hz full-scale sine, drive 1 and 3, resonance 0 to 0.9) every harmonic above -80 dBc lands within 0.23 dB up to a 2 kHz cutoff and within 1.7 dB at 8 kHz (the frequency-warping limit of any trapezoidal solve at the base rate). It self-oscillates above resonance 1 at a level the stages set: at resonance 1.1 the pitch is within 0.1 cents of the reference up to a 1 kHz cutoff, 1.5 cents at 4 kHz and 7.9 cents at 10 kHz, and the level within 0.07 dB. It changes the character, so it is opt-in. It costs several times the Feedback model (see setSaturation()).
  • Input: linear stages and one saturator on the loop input, y_0 = phi(x - k y_4), run in four sub-steps per sample with the loop closed without delay in each (detail::ladderStepInput()). The sub-steps put the frequency warping of the integrators and the aliasing of the saturator two octaves further up. At 48 kHz, against a 32x oversampled solve of the same equations (110 Hz full-scale sine, drive 1 and 3, resonance 0 to 0.9) every harmonic above -80 dBc lands within 0.05 dB at cutoffs from 250 Hz to 20 kHz. The resonance peak is within 0.25 cents of the analog one up to an 8 kHz cutoff, 0.9 cents at 16 kHz and 1.1 at 20 kHz (the other two models: 3.8, 13 and 18 cents sharp), and with the resonance at 0 the small-signal response is within 0.5 dB of the analog four-pole up to 15 kHz at any cutoff (-0.6 to -2.1 dB at 20 kHz). A full-scale 5 kHz tone at drive 3 and resonance 0.5 aliases at -45 to -61 dBc over cutoffs from 500 Hz to 20 kHz. It costs the same at every cutoff and resonance, below Stages at every setting measured (see setSaturation()). It is a third character, so it is opt-in as well.

Contract notes:

  • Like the analog original, the passband gain drops by 1/(1 + 4*resonance) as resonance rises (about -14 dB at resonance 1). Compensate externally if loudness-matched sweeps are needed.
  • Feedback model: drive saturates the resonance feedback path only: with resonance at 0 it has no effect on the signal. With drive > 1 the bounded tanh stage keeps self-oscillation (resonance = 1) at a finite level; at drive <= 1 the loop is exactly marginal at resonance = 1, so sustained input at the cutoff accumulates without decay - back off resonance slightly or engage drive for stable self-oscillation.
  • Stages model: drive is the level at which the stages saturate (a full-scale signal at drive 1 sits at the tanh(1) knee), at any drive. Its cutoff is limited to 0.45 * fs (detail::kLadderStagesMaxGain).
  • Input model: drive is the level at which the loop input (the input minus the resonance feedback) saturates, at any drive and resonance: with the resonance at 0, a full-scale signal at drive 1 sits at the tanh(1) knee. Above resonance 1 it self-oscillates at a level the saturator sets. Its cutoff reaches 0.499 * fs, like the Feedback model's. The HP24 output is taken from the saturated loop input. The input is interpolated across the sub-steps and the output is their mean, each with a one-pole level correction; the first sample after another model or a reset starts both histories.
  • Channels beyond kMaxChannels (16) pass through unprocessed.
  • Mode and saturation model can be switched while running; the integrator state is continuous.
Template Parameters
TSample type (float or double). Must satisfy FloatType concept.

Definition at line 120 of file LadderFilter.h.

Member Enumeration Documentation

◆ Mode

template<FloatType T>
enum class dspark::LadderFilter::Mode
strong

Defines the frequency response mode of the filter output.

Enumerator
LP6 

1-pole lowpass (6 dB/oct).

LP12 

2-pole lowpass (12 dB/oct).

LP18 

3-pole lowpass (18 dB/oct).

LP24 

4-pole lowpass (24 dB/oct) - classic Moog style.

BP12 

Bandpass (6 dB/oct per side).

HP24 

4-pole highpass (24 dB/oct).

Definition at line 124 of file LadderFilter.h.

◆ Saturation

template<FloatType T>
enum class dspark::LadderFilter::Saturation
strong

Where the filter saturates (see the class notes).

Enumerator
Feedback 

Linear stages, tanh on the resonance feedback (default).

Stages 

Every stage saturates; implicit per-sample solve.

Input 

Linear stages, tanh on the loop input; sub-stepped.

Definition at line 135 of file LadderFilter.h.

Constructor & Destructor Documentation

◆ ~LadderFilter()

template<FloatType T>
dspark::LadderFilter< T >::~LadderFilter ( )
default

Member Function Documentation

◆ getCutoff()

template<FloatType T>
T dspark::LadderFilter< T >::getCutoff ( ) const
inlinenoexcept

Definition at line 437 of file LadderFilter.h.

◆ getDrive()

template<FloatType T>
T dspark::LadderFilter< T >::getDrive ( ) const
inlinenoexcept

Definition at line 439 of file LadderFilter.h.

◆ getMode()

template<FloatType T>
Mode dspark::LadderFilter< T >::getMode ( ) const
inlinenoexcept

Definition at line 440 of file LadderFilter.h.

◆ getResonance()

template<FloatType T>
T dspark::LadderFilter< T >::getResonance ( ) const
inlinenoexcept

Definition at line 438 of file LadderFilter.h.

◆ getSaturation()

template<FloatType T>
Saturation dspark::LadderFilter< T >::getSaturation ( ) const
inlinenoexcept

Definition at line 441 of file LadderFilter.h.

◆ getState()

template<FloatType T>
std::vector< uint8_t > dspark::LadderFilter< T >::getState ( ) const
inline

Serializes the parameters (setup/UI threads; allocates).

Definition at line 404 of file LadderFilter.h.

◆ prepare()

template<FloatType T>
void dspark::LadderFilter< T >::prepare ( const AudioSpec &  spec)
inlinenoexcept

Prepares the filter with the current audio environment.

Invalid specs (sample rate not > 0, NaN included) are ignored and the previous state is kept. Clears the integrator state.

Parameters
specAudio specification including sample rate.

Definition at line 157 of file LadderFilter.h.

◆ processBlock()

template<FloatType T>
void dspark::LadderFilter< T >::processBlock ( AudioBufferView< T >  buffer)
inlinenoexcept

Processes an audio buffer in-place (thread-safe vs. setters).

Takes an atomic snapshot of the current parameters and dispatches to a branchless inner loop instantiated per mode. A non-finite integrator state is cleared at the end of the block, so one NaN input cannot silence the channel until reset().

Parameters
bufferView of the multi-channel audio data to process.

Definition at line 175 of file LadderFilter.h.

◆ processSample()

template<FloatType T>
T dspark::LadderFilter< T >::processSample ( T  input,
int  channel 
)
inlinenoexcept

Processes a single sample on one channel.

Convenience per-sample entry point mirroring Biquad. For bulk processing prefer processBlock(), which snapshots the parameters and derives the loop coefficients once per block instead of per sample.

Note
Unlike processBlock(), no DenormalGuard is installed here (the per-sample FP-environment writes would cost more than the filter itself); per-sample callers should place one DenormalGuard around their processing loop.
Parameters
inputInput sample.
channelChannel index in [0, kMaxChannels).
Returns
Filtered sample (returns input unchanged if channel is out of range).

Definition at line 237 of file LadderFilter.h.

◆ reset()

template<FloatType T>
void dspark::LadderFilter< T >::reset ( )
inlinenoexcept

Clears the internal integrator state.

Should be called when playback stops or continuity is broken to prevent clicks.

Definition at line 298 of file LadderFilter.h.

◆ setCutoff()

template<FloatType T>
void dspark::LadderFilter< T >::setCutoff ( T  hz)
inlinenoexcept

Sets the cutoff frequency (thread-safe, lock-free).

Non-finite values (NaN/Inf) are ignored - they would poison the integrator gain permanently otherwise. Before prepare() the sample rate is unknown, so the raw request is stored and clamped to [20, sampleRate * 0.499] once prepare() runs (getCutoff() then reports the clamped value).

Parameters
hzCutoff frequency in Hz.

Definition at line 321 of file LadderFilter.h.

◆ setDrive()

template<FloatType T>
void dspark::LadderFilter< T >::setDrive ( T  amount)
inlinenoexcept

Sets the nonlinear drive amount (thread-safe, lock-free).

Parameters
amountDrive multiplier, floored at 0.1. Feedback model: values <= 1.0 keep the feedback path linear; > 1.0 saturates it (analog-style resonance compression). Stages model: the level scale of every stage, tanh(drive * v) / drive. Input model: the same scale on the loop input. Non-finite values are ignored.

Definition at line 354 of file LadderFilter.h.

◆ setMode()

template<FloatType T>
void dspark::LadderFilter< T >::setMode ( Mode  mode)
inlinenoexcept

Sets the filter output mode (thread-safe, lock-free).

Parameters
modeTarget frequency response type.

Definition at line 364 of file LadderFilter.h.

◆ setResonance()

template<FloatType T>
void dspark::LadderFilter< T >::setResonance ( T  amount)
inlinenoexcept

Sets resonance amount (thread-safe, lock-free).

Parameters
amountRange [0.0, 1.0] for the Feedback model; values near 1.0 induce self-oscillation (see the class notes on drive for loop stability). The Stages and Input models also take values up to kMaxResonance (1.2), where they self-oscillate at a level their saturation sets; the Feedback model treats them as 1. Clamped to [0, kMaxResonance]; non-finite values are ignored.

Definition at line 338 of file LadderFilter.h.

◆ setSaturation()

template<FloatType T>
void dspark::LadderFilter< T >::setSaturation ( Saturation  model)
inlinenoexcept

Selects the saturation model (thread-safe, lock-free).

Feedback (the default) keeps the stages linear. Stages saturates every stage and solves the loop implicitly: a clipped Newton iteration in double, 2.1 steps on average and at most 7 measured (cap 16). It costs 38 to 100 ns per sample (mono) against 10 to 19 ns for Feedback (MSVC x64, float), rising with cutoff, drive and the noisiness of the input: 54 ns at a 1 kHz cutoff with drive 3; at 15 kHz with drive 3, 54-58 ns for sines and a saw and 98 ns for white noise. Channel pairs are solved together, which lowers the cost per channel by 10-20 % in stereo.

Input saturates the loop input and runs four sub-steps per sample, the same work at every cutoff and resonance: 53 to 74 ns per sample in mono depending on the machine (float; GCC, Clang and MSVC on x64 and GCC on arm64) and 35 to 48 ns per channel for channel pairs, which advance together. A saturator driven past the limit of its tanh skips its division (at drive 10, from 16% less to 4% more). On the same machine and settings that is 1.5 to 4.0 times Feedback and 0.30 to 0.95 times Stages.

The integrator state carries over a switch. Out-of-range values (a wild cast or a corrupt state blob) select Feedback.

Parameters
modelSaturation model.

Definition at line 395 of file LadderFilter.h.

◆ setState()

template<FloatType T>
bool dspark::LadderFilter< T >::setState ( const uint8_t *  data,
size_t  size 
)
inline

Restores parameters from a blob (tolerant; rejects foreign ids).

Missing keys keep their defaults: a blob without "saturation" selects the Feedback model.

Definition at line 423 of file LadderFilter.h.

Member Data Documentation

◆ cutoff_

template<FloatType T>
std::atomic<T> dspark::LadderFilter< T >::cutoff_ {T(1000)}
protected

Definition at line 472 of file LadderFilter.h.

◆ drive_

template<FloatType T>
std::atomic<T> dspark::LadderFilter< T >::drive_ {T(1)}
protected

Definition at line 474 of file LadderFilter.h.

◆ g_

template<FloatType T>
std::atomic<T> dspark::LadderFilter< T >::g_ {T(0)}
protected

Definition at line 475 of file LadderFilter.h.

◆ inputCache_

template<FloatType T>
InputCache dspark::LadderFilter< T >::inputCache_ {}
protected

Definition at line 469 of file LadderFilter.h.

◆ kMaxChannels

template<FloatType T>
constexpr int dspark::LadderFilter< T >::kMaxChannels = 16
staticconstexprprotected

Definition at line 444 of file LadderFilter.h.

◆ kMaxResonance

template<FloatType T>
constexpr double dspark::LadderFilter< T >::kMaxResonance = 1.2
staticconstexpr

Highest resonance setResonance() accepts (Feedback uses at most 1).

Definition at line 143 of file LadderFilter.h.

◆ mode_

template<FloatType T>
std::atomic<Mode> dspark::LadderFilter< T >::mode_ {Mode::LP24}
protected

Definition at line 476 of file LadderFilter.h.

◆ resonance_

template<FloatType T>
std::atomic<T> dspark::LadderFilter< T >::resonance_ {T(0)}
protected

Definition at line 473 of file LadderFilter.h.

◆ saturation_

template<FloatType T>
std::atomic<Saturation> dspark::LadderFilter< T >::saturation_ {Saturation::Feedback}
protected

Definition at line 477 of file LadderFilter.h.

◆ spec_

template<FloatType T>
AudioSpec dspark::LadderFilter< T >::spec_ {}
protected

Definition at line 459 of file LadderFilter.h.

◆ state_

template<FloatType T>
std::array<ChannelState, kMaxChannels> dspark::LadderFilter< T >::state_ {}
protected

Definition at line 458 of file LadderFilter.h.


The documentation for this class was generated from the following file: