DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
LaneCascade.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
32#include "AudioBuffer.h"
33#include "Biquad.h"
34#include "DspMath.h"
35#include "SimdOps.h"
36
37#include <algorithm>
38#include <array>
39#include <initializer_list>
40#include <vector>
41
42namespace dspark {
43
51template <FloatType T, int Lanes = 8>
53{
54 static_assert(Lanes == 2 || Lanes == 4 || Lanes == 8 || Lanes == 16, "Lanes must be 2, 4, 8 or 16");
55
56public:
63 void prepare(int sections, int channels)
64 {
65 steps_ = (std::max(1, sections) + Lanes - 1) / Lanes;
66 sections_ = std::max(1, sections);
67 channels_ = std::max(1, channels);
68 const auto total = static_cast<size_t>(steps_ * Lanes);
69 for (auto* v : { &b0_, &b1_, &b2_, &a1_, &a2_, &db0_, &db1_, &db2_, &da1_, &da2_ })
70 v->assign(total, 0.0);
71 std::fill(b0_.begin(), b0_.end(), 1.0);
72 for (auto* v : { &t0_, &t1_, &t2_, &u1_, &u2_ }) v->assign(total, 0.0);
73 std::fill(t0_.begin(), t0_.end(), 1.0);
74 rampLeft_ = 0;
75 state_.assign(static_cast<size_t>(channels_), std::vector<double>(total * 4, 0.0));
76 pipe_.assign(static_cast<size_t>(channels_), std::array<double, Lanes> {});
77 }
78
79 [[nodiscard]] int getNumSections() const noexcept { return sections_; }
81 [[nodiscard]] static constexpr int getLatency() noexcept { return Lanes - 1; }
82
84 void setCoeffs(int section, const BiquadCoeffs& c) noexcept
85 {
86 const size_t i = slot(section);
87 if (i == npos) return;
88 b0_[i] = t0_[i] = c.b0; b1_[i] = t1_[i] = c.b1; b2_[i] = t2_[i] = c.b2;
89 a1_[i] = u1_[i] = c.a1; a2_[i] = u2_[i] = c.a2;
90 db0_[i] = db1_[i] = db2_[i] = da1_[i] = da2_[i] = 0.0;
91 }
92
97 void setTargetCoeffs(int section, const BiquadCoeffs& c) noexcept
98 {
99 const size_t i = slot(section);
100 if (i == npos) return;
101 t0_[i] = c.b0; t1_[i] = c.b1; t2_[i] = c.b2; u1_[i] = c.a1; u2_[i] = c.a2;
102 }
103
108 void startRamp(int samples) noexcept
109 {
110 const size_t total = b0_.size();
111 if (samples <= 0)
112 {
113 b0_ = t0_; b1_ = t1_; b2_ = t2_; a1_ = u1_; a2_ = u2_;
114 rampLeft_ = 0;
115 return;
116 }
117 const double inv = 1.0 / samples;
118 for (size_t i = 0; i < total; ++i)
119 {
120 db0_[i] = (t0_[i] - b0_[i]) * inv; db1_[i] = (t1_[i] - b1_[i]) * inv; db2_[i] = (t2_[i] - b2_[i]) * inv;
121 da1_[i] = (u1_[i] - a1_[i]) * inv; da2_[i] = (u2_[i] - a2_[i]) * inv;
122 }
123 rampLeft_ = samples;
124 }
125
127 void rampCoeffs(int section, const BiquadCoeffs& c, int samples) noexcept
128 {
129 setTargetCoeffs(section, c);
130 startRamp(samples);
131 }
132
134 [[nodiscard]] BiquadCoeffs getCoeffs(int section) const noexcept
135 {
136 BiquadCoeffs c;
137 const size_t i = slot(section);
138 if (i == npos) return c;
139 c.b0 = b0_[i]; c.b1 = b1_[i]; c.b2 = b2_[i]; c.a1 = a1_[i]; c.a2 = a2_[i];
140 return c;
141 }
142
144 void reset() noexcept
145 {
146 for (auto& s : state_) std::fill(s.begin(), s.end(), 0.0);
147 for (auto& p : pipe_) p.fill(0.0);
148 }
149
151 void processBlock(AudioBufferView<T> buffer) noexcept
152 {
153 const int nCh = std::min(buffer.getNumChannels(), channels_);
154 const int nS = buffer.getNumSamples();
155 for (int n = 0; n < nS; ++n)
156 {
157 for (int ch = 0; ch < nCh; ++ch)
158 {
159 T* data = buffer.getChannel(ch);
160 data[n] = static_cast<T>(step(ch, static_cast<double>(data[n])));
161 }
162 if (rampLeft_ > 0) advanceRamp();
163 }
164 }
165
166private:
167 static constexpr size_t npos = static_cast<size_t>(-1);
168
170 [[nodiscard]] size_t slot(int section) const noexcept
171 {
172 if (section < 0 || section >= steps_ * Lanes) return npos;
173 return static_cast<size_t>((section % steps_) * Lanes + section / steps_);
174 }
175
176 double step(int ch, double x) noexcept
177 {
178 auto& pipe = pipe_[static_cast<size_t>(ch)];
179 double* st = state_[static_cast<size_t>(ch)].data();
180 alignas(64) std::array<double, Lanes> v {};
181 v[0] = x;
182 for (int j = 1; j < Lanes; ++j) v[static_cast<size_t>(j)] = pipe[static_cast<size_t>(j - 1)];
183 constexpr int W = std::min(simd::kVecWidth<double>, Lanes);
184 using O = simd::Vec<double, W>;
185 using V = typename O::V;
186 for (int k = 0; k < steps_; ++k)
187 {
188 const size_t base = static_cast<size_t>(k * Lanes);
189 double* x1 = st + 4 * base;
190 double* x2 = x1 + Lanes;
191 double* y1 = x2 + Lanes;
192 double* y2 = y1 + Lanes;
193 for (int j = 0; j < Lanes; j += W)
194 {
195 const auto i = base + static_cast<size_t>(j);
196 const V in = O::load(v.data() + j);
197 const V px1 = O::load(x1 + j), px2 = O::load(x2 + j);
198 const V py1 = O::load(y1 + j), py2 = O::load(y2 + j);
199 const V ff = O::madd(O::load(b0_.data() + i), in,
200 O::madd(O::load(b1_.data() + i), px1, O::mul(O::load(b2_.data() + i), px2)));
201 const V fb = O::madd(O::load(a1_.data() + i), py1, O::mul(O::load(a2_.data() + i), py2));
202 const V y = O::sub(ff, fb);
203 O::store(x2 + j, px1);
204 O::store(x1 + j, in);
205 O::store(y2 + j, py1);
206 O::store(y1 + j, y);
207 O::store(v.data() + j, y);
208 }
209 }
210 for (int j = 0; j < Lanes; ++j) pipe[static_cast<size_t>(j)] = v[static_cast<size_t>(j)];
211 return v[static_cast<size_t>(Lanes - 1)];
212 }
213
214 void advanceRamp() noexcept
215 {
216 if (--rampLeft_ == 0)
217 {
218 b0_ = t0_; b1_ = t1_; b2_ = t2_; a1_ = u1_; a2_ = u2_; // no allocation: same sizes
219 return;
220 }
221 const size_t total = b0_.size();
222 for (size_t i = 0; i < total; ++i)
223 {
224 b0_[i] += db0_[i]; b1_[i] += db1_[i]; b2_[i] += db2_[i];
225 a1_[i] += da1_[i]; a2_[i] += da2_[i];
226 }
227 }
228
229 int steps_ = 1, sections_ = 1, channels_ = 1;
230 int rampLeft_ = 0;
231 std::vector<double> b0_, b1_, b2_, a1_, a2_, db0_, db1_, db2_, da1_, da2_;
232 std::vector<double> t0_, t1_, t2_, u1_, u2_;
233 std::vector<std::vector<double>> state_;
234 std::vector<std::array<double, Lanes>> pipe_;
235};
236
237} // namespace dspark
Non-owning view over audio channel data.
Definition AudioBuffer.h:53
A cascade of biquads over the lanes of a vector, with latency.
Definition LaneCascade.h:53
int getNumSections() const noexcept
Definition LaneCascade.h:79
void rampCoeffs(int section, const BiquadCoeffs &c, int samples) noexcept
Ramps one section to a target over samples (startRamp() on every section's target).
void prepare(int sections, int channels)
Allocates for a number of sections and channels; every section starts transparent (b0 = 1).
Definition LaneCascade.h:63
void setTargetCoeffs(int section, const BiquadCoeffs &c) noexcept
The coefficients a section ramps to at the next startRamp() (sections not given a target keep the one...
Definition LaneCascade.h:97
void reset() noexcept
Clears the history and the pipeline (the coefficients stay).
void processBlock(AudioBufferView< T > buffer) noexcept
Runs a block in place on the prepared channels (extra ones pass).
void setCoeffs(int section, const BiquadCoeffs &c) noexcept
Sets a section's coefficients at once (ends any ramp of it).
Definition LaneCascade.h:84
static constexpr int getLatency() noexcept
Definition LaneCascade.h:81
void startRamp(int samples) noexcept
Ramps every section linearly from where it is to its target over samples (0: at once); the ramp ends ...
BiquadCoeffs getCoeffs(int section) const noexcept
Main namespace for the DSPark framework.
Stores normalised biquad coefficients (b0, b1, b2, a1, a2), always double.
Definition Biquad.h:107