49template <
typename SampleType =
float, std::
size_t Voices = 16>
class LadderBankT {
50 static_assert(Voices > 0,
"LadderBankT needs at least one voice");
57 std::fill(std::begin(cutoff_), std::end(cutoff_), SampleType{1000.0f});
64 for (std::size_t v = 0; v < Voices; ++v)
82 resonance_[voice] = std::clamp(
resonance, SampleType{0.0f}, SampleType{1.0f});
87 return voice < Voices ? cutoff_[voice] : SampleType{};
90 return voice < Voices ? resonance_[voice] : SampleType{};
95 return voice < Voices ? g_[voice] : SampleType{};
100 void process(
const SampleType* in, SampleType* out) {
101 SampleType prev[Voices];
102 for (std::size_t v = 0; v < Voices; ++v) {
103 const SampleType input = in[v];
105 resonance_[v] * SampleType{4.0f} * (stage_[3][v] - input * SampleType{0.5f});
106 prev[v] = saturate(input -
feedback);
108 for (
int i = 0;
i < 4; ++
i) {
109 SampleType* stage = stage_[
i];
110 SampleType* saturated = saturated_stage_[
i];
111 for (std::size_t v = 0; v < Voices; ++v) {
113 saturated[v] = saturate(stage[v]);
114 prev[v] = saturated[v];
117 for (std::size_t v = 0; v < Voices; ++v)
118 out[v] = stage_[3][v];
124 SampleType frame[Voices];
125 for (std::size_t n = 0; n < num_samples; ++n) {
126 for (std::size_t v = 0; v < Voices; ++v)
127 frame[v] = buffers[v][n];
129 for (std::size_t v = 0; v < Voices; ++v)
130 buffers[v][n] = frame[v];
138 for (
int i = 0;
i < 4; ++
i) {
139 stage_[
i][voice] = SampleType{0};
140 saturated_stage_[
i][voice] = SampleType{0};
146 for (std::size_t v = 0; v < Voices; ++v)
154 static SampleType saturate(SampleType x) {
155 if constexpr (std::is_same_v<SampleType, float> &&
159 return Filter::template saturate<kDefaultLadderSaturation>(x);
162 void update(std::size_t voice) {
166 SampleType sample_rate_ = SampleType{44100.0f};
167 SampleType cutoff_[
Voices];
168 SampleType resonance_[
Voices] = {};
169 SampleType g_[
Voices] = {};
170 SampleType stage_[4][
Voices] = {};
171 SampleType saturated_stage_[4][
Voices] = {};
Voices independent LadderFilterT voices advanced together.
Definition ladder_bank.hpp:49
LadderFilterT< SampleType > Filter
Definition ladder_bank.hpp:53
void process(const SampleType *in, SampleType *out)
Advance every voice by one sample: out[v] = voice v's output for in[v].
Definition ladder_bank.hpp:100
static constexpr std::size_t voice_count
Definition ladder_bank.hpp:54
void set_sample_rate(SampleType sample_rate)
Set the sample rate every voice runs at, and recompute every voice's coefficient for its current freq...
Definition ladder_bank.hpp:62
float resonance(std::size_t voice) const
Definition ladder_bank.hpp:89
float sample_rate() const
Definition ladder_bank.hpp:68
void set_frequency(std::size_t voice, SampleType hz)
Definition ladder_bank.hpp:72
void set_resonance(std::size_t voice, SampleType resonance)
Definition ladder_bank.hpp:79
SampleType frequency(std::size_t voice) const
Definition ladder_bank.hpp:86
void reset()
Clear every voice's state.
Definition ladder_bank.hpp:145
void reset(std::size_t voice)
Clear one voice's state.
Definition ladder_bank.hpp:135
void process_voices(SampleType *const *buffers, std::size_t num_samples)
Advance every voice by num_samples samples in place, one buffer per voice (buffers[v] holds voice v's...
Definition ladder_bank.hpp:123
SampleType stage_coefficient(std::size_t voice) const
The voice's per-stage one-pole coefficient (Filter::stage_coefficient()).
Definition ladder_bank.hpp:94
LadderBankT()
Definition ladder_bank.hpp:56
Definition ladder_filter.hpp:25
static SampleType stage_coefficient(SampleType cutoff_hz, SampleType sample_rate)
Definition ladder_filter.hpp:75
float tanh_accurate_bits(float x) noexcept
FastMath::tanh_accurate() with its clamps as bit operations; the same value for every input.
Definition fast_math.hpp:743
T snap_to_zero_bits(T x) noexcept
snap_to_zero() as bit operations (see select_bits()).
Definition fast_math.hpp:733
Definition additive_bank.hpp:218
@ feedback
Definition comb_filter.hpp:18
LadderBankT< float, Voices > LadderBank
Definition ladder_bank.hpp:174
@ i
Definition formant_filter_bank.hpp:20
@ accurate
Definition fast_math.hpp:630
constexpr LadderSaturation kDefaultLadderSaturation
Selecting the default ladder saturator at build time:
Definition fast_math.hpp:657