DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
FirBank.h
1// DSPark -- Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi -- MIT License
3
4#pragma once
5
57#include "DspMath.h"
58#include "FFT.h"
59#include "SimdOps.h"
60
61#include <algorithm>
62#include <cstddef>
63#include <memory>
64#include <vector>
65
66namespace dspark {
67
74template <FloatType T>
76{
77public:
78 static constexpr int kMaxInputs = 32;
79
88 void prepare(int kernelLength, int partition, int numInputs, int numKernels)
89 {
90 fft_.reset(); // gates everything else: set last
91 int p = 16;
92 while (p < partition && p < (1 << 20)) p <<= 1;
93 partition_ = p;
94 parts_ = std::clamp((std::max(kernelLength, 1) + p - 1) / p, 1, 1 << 16);
95 inputs_ = std::clamp(numInputs, 1, kMaxInputs);
96 kernels_ = std::max(numKernels, 0);
97 specLen_ = static_cast<size_t>(2 * p + 2);
98
99 const size_t lineLen = static_cast<size_t>(parts_) * specLen_;
100 in_.assign(static_cast<size_t>(inputs_) * static_cast<size_t>(2 * p), T(0));
101 fdl_.assign(static_cast<size_t>(inputs_) * lineLen, T(0));
102 spectra_.assign(static_cast<size_t>(kernels_) * 2u * lineLen, T(0));
103 out_.assign(static_cast<size_t>(kernels_) * static_cast<size_t>(inputs_) * static_cast<size_t>(p), T(0));
104 state_.assign(static_cast<size_t>(kernels_), Kernel {});
105 accum_.assign(specLen_, T(0));
106 block_.assign(static_cast<size_t>(2 * p), T(0));
107 timeOld_.assign(static_cast<size_t>(2 * p), T(0));
108 timeNew_.assign(static_cast<size_t>(2 * p), T(0));
109 pos_ = 0;
110 fdlPos_ = 0;
111 fft_ = std::make_unique<FFTReal<T>>(static_cast<size_t>(2 * p));
112 }
113
115 [[nodiscard]] bool isPrepared() const noexcept { return fft_ != nullptr; }
116
118 [[nodiscard]] int getLatency() const noexcept { return partition_; }
119
121 [[nodiscard]] int getKernelLength() const noexcept { return parts_ * partition_; }
122
124 [[nodiscard]] int getPartition() const noexcept { return partition_; }
125
127 [[nodiscard]] int getNumInputs() const noexcept { return inputs_; }
128
130 [[nodiscard]] int getNumKernels() const noexcept { return kernels_; }
131
143 void setKernel(int kernel, const T* taps, unsigned inputs) noexcept
144 {
145 if (!fft_ || kernel < 0 || kernel >= kernels_ || taps == nullptr) return;
146 Kernel& k = state_[static_cast<size_t>(kernel)];
147 const int set = 1 - k.set; // the one not in use
148 T* spec = spectraOf(kernel, set);
149 const auto p = static_cast<size_t>(partition_);
150 for (int part = 0; part < parts_; ++part)
151 {
152 std::copy_n(taps + static_cast<size_t>(part) * p, p, block_.data());
153 std::fill(block_.begin() + static_cast<std::ptrdiff_t>(p), block_.end(), T(0));
154 fft_->forward(block_.data(), spec + static_cast<size_t>(part) * specLen_);
155 }
156 k.nextMask = inputs & maskOfAll();
157 k.pending = true;
158 }
159
161 void removeKernel(int kernel) noexcept
162 {
163 if (!fft_ || kernel < 0 || kernel >= kernels_) return;
164 Kernel& k = state_[static_cast<size_t>(kernel)];
165 if (k.mask == 0u && !k.pending) return;
166 k.nextMask = 0u;
167 k.pending = true;
168 }
169
171 [[nodiscard]] bool isKernelSilent(int kernel) const noexcept
172 {
173 return kernel < 0 || kernel >= kernels_ || state_[static_cast<size_t>(kernel)].silent;
174 }
175
177 [[nodiscard]] bool isKernelActive(int kernel) const noexcept
178 {
179 if (kernel < 0 || kernel >= kernels_) return false;
180 const Kernel& k = state_[static_cast<size_t>(kernel)];
181 return k.pending ? k.nextMask != 0u : k.mask != 0u;
182 }
183
185 void reset() noexcept
186 {
187 std::fill(in_.begin(), in_.end(), T(0));
188 std::fill(fdl_.begin(), fdl_.end(), T(0));
189 std::fill(out_.begin(), out_.end(), T(0));
190 for (auto& k : state_)
191 if (k.pending)
192 {
193 k.set = 1 - k.set;
194 k.mask = k.nextMask;
195 k.pending = false;
196 }
197 pos_ = 0;
198 fdlPos_ = 0;
199 }
200
202 [[nodiscard]] int room() const noexcept { return partition_ - pos_; }
203
210 [[nodiscard]] const T* tap(int kernel, int input) const noexcept
211 {
212 return out_.data() + (static_cast<size_t>(kernel) * static_cast<size_t>(inputs_) + static_cast<size_t>(input))
213 * static_cast<size_t>(partition_) + static_cast<size_t>(pos_);
214 }
215
222 void push(const T* const* inputs, int n) noexcept
223 {
224 if (!fft_ || n <= 0) return;
225 n = std::min(n, room());
226 const auto p = static_cast<size_t>(partition_);
227 for (int i = 0; i < inputs_; ++i)
228 {
229 T* dest = in_.data() + static_cast<size_t>(i) * 2u * p + p + static_cast<size_t>(pos_);
230 if (inputs[i] != nullptr) std::copy_n(inputs[i], static_cast<size_t>(n), dest);
231 else std::fill_n(dest, static_cast<size_t>(n), T(0));
232 }
233 pos_ += n;
234 if (pos_ == partition_)
235 {
236 hop();
237 pos_ = 0;
238 }
239 }
240
241private:
242 struct Kernel
243 {
244 unsigned mask = 0u;
245 unsigned nextMask = 0u;
246 int set = 0;
247 bool pending = false;
248 bool silent = true;
249 };
250
251 [[nodiscard]] unsigned maskOfAll() const noexcept
252 {
253 return inputs_ >= 32 ? ~0u : ((1u << inputs_) - 1u);
254 }
255
256 [[nodiscard]] T* spectraOf(int kernel, int set) noexcept
257 {
258 return spectra_.data() + (static_cast<size_t>(kernel) * 2u + static_cast<size_t>(set))
259 * static_cast<size_t>(parts_) * specLen_;
260 }
261
263 void convolve(const T* spec, int input, T* time) noexcept
264 {
265 std::fill(accum_.begin(), accum_.end(), T(0));
266 const T* line = fdl_.data() + static_cast<size_t>(input) * static_cast<size_t>(parts_) * specLen_;
267 for (int part = 0; part < parts_; ++part)
268 {
269 const int at = (fdlPos_ - part + parts_) % parts_;
270 simd::complexMulAccum(accum_.data(), line + static_cast<size_t>(at) * specLen_,
271 spec + static_cast<size_t>(part) * specLen_, partition_ + 1);
272 }
273 fft_->inverse(accum_.data(), time);
274 }
275
277 void hop() noexcept
278 {
279 const auto p = static_cast<size_t>(partition_);
280 fdlPos_ = (fdlPos_ + 1) % parts_;
281 for (int i = 0; i < inputs_; ++i)
282 {
283 T* in = in_.data() + static_cast<size_t>(i) * 2u * p;
284 fft_->forward(in, fdl_.data() + (static_cast<size_t>(i) * static_cast<size_t>(parts_)
285 + static_cast<size_t>(fdlPos_)) * specLen_);
286 std::copy_n(in + p, p, in);
287 }
288
289 const T step = T(1) / static_cast<T>(partition_);
290 for (int kernel = 0; kernel < kernels_; ++kernel)
291 {
292 Kernel& k = state_[static_cast<size_t>(kernel)];
293 const unsigned heard = k.mask | (k.pending ? k.nextMask : 0u);
294 T* outs = out_.data() + static_cast<size_t>(kernel) * static_cast<size_t>(inputs_) * p;
295 if (heard == 0u)
296 {
297 if (!k.silent) std::fill_n(outs, static_cast<size_t>(inputs_) * p, T(0));
298 k.silent = true;
299 if (k.pending) { k.set = 1 - k.set; k.pending = false; }
300 continue;
301 }
302 k.silent = false;
303 const T* specOld = spectraOf(kernel, k.set);
304 const T* specNew = spectraOf(kernel, 1 - k.set);
305 for (int i = 0; i < inputs_; ++i)
306 {
307 T* out = outs + static_cast<size_t>(i) * p;
308 const unsigned bit = 1u << i;
309 const bool old = (k.mask & bit) != 0u, coming = k.pending && (k.nextMask & bit) != 0u;
310 if (!k.pending)
311 {
312 if (!old) { std::fill_n(out, p, T(0)); continue; }
313 convolve(specOld, i, timeOld_.data());
314 std::copy_n(timeOld_.data() + p, p, out);
315 continue;
316 }
317 // A kernel comes in (or goes): one partition of crossfade.
318 if (old) convolve(specOld, i, timeOld_.data());
319 if (coming) convolve(specNew, i, timeNew_.data());
320 for (size_t j = 0; j < p; ++j)
321 {
322 const T was = old ? timeOld_[p + j] : T(0);
323 const T is = coming ? timeNew_[p + j] : T(0);
324 out[j] = was + (is - was) * (static_cast<T>(j + 1) * step);
325 }
326 }
327 if (k.pending)
328 {
329 k.set = 1 - k.set;
330 k.mask = k.nextMask;
331 k.pending = false;
332 }
333 }
334 }
335
336 int partition_ = 0, parts_ = 0, inputs_ = 0, kernels_ = 0;
337 size_t specLen_ = 0;
338 int pos_ = 0;
339 int fdlPos_ = 0;
340
341 std::unique_ptr<FFTReal<T>> fft_;
342 std::vector<T> in_;
343 std::vector<T> fdl_;
344 std::vector<T> spectra_;
345 std::vector<T> out_;
346 std::vector<Kernel> state_;
347 std::vector<T> accum_, block_, timeOld_, timeNew_;
348};
349
350} // namespace dspark
Long FIR kernels over shared input lines, by partitioned convolution.
Definition FirBank.h:76
int getNumInputs() const noexcept
Input lines.
Definition FirBank.h:127
int getPartition() const noexcept
Samples per partition.
Definition FirBank.h:124
int getLatency() const noexcept
Samples between push() and tap(): one partition.
Definition FirBank.h:118
static constexpr int kMaxInputs
Input lines a mask can name.
Definition FirBank.h:78
void removeKernel(int kernel) noexcept
Fades a kernel out over the next partition.
Definition FirBank.h:161
void setKernel(int kernel, const T *taps, unsigned inputs) noexcept
Gives a kernel its taps and the lines that hear it.
Definition FirBank.h:143
int room() const noexcept
Samples that can be pushed before the next hop (1 to the partition).
Definition FirBank.h:202
int getKernelLength() const noexcept
Taps of a kernel (a whole number of partitions).
Definition FirBank.h:121
void push(const T *const *inputs, int n) noexcept
Takes n samples of every input line; runs the hop when the partition fills.
Definition FirBank.h:222
void prepare(int kernelLength, int partition, int numInputs, int numKernels)
Allocates the bank (setup thread).
Definition FirBank.h:88
bool isKernelSilent(int kernel) const noexcept
Whether the outputs of a kernel are silence now: it is not playing and nothing of it is left to hear.
Definition FirBank.h:171
bool isKernelActive(int kernel) const noexcept
Whether a kernel is playing, or about to.
Definition FirBank.h:177
const T * tap(int kernel, int input) const noexcept
What a kernel gives for one of its lines: room() samples, one partition late, valid until the next pu...
Definition FirBank.h:210
void reset() noexcept
Clears the memory of the signal. The kernels stay; one that was coming lands at once.
Definition FirBank.h:185
bool isPrepared() const noexcept
Whether prepare() has run.
Definition FirBank.h:115
int getNumKernels() const noexcept
Kernels the bank can hold.
Definition FirBank.h:130
void complexMulAccum(float *DSPARK_RESTRICT accum, const float *DSPARK_RESTRICT a, const float *DSPARK_RESTRICT b, int bins) noexcept
Complex multiply-accumulate over interleaved [re, im, ...] spectra.
Definition SimdOps.h:1234
Main namespace for the DSPark framework.