DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
MinBlepTable.h
1// DSPark -- Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi -- MIT License
3
4#pragma once
5
19#include "FFT.h"
20
21#include <algorithm>
22#include <cmath>
23#include <cstddef>
24#include <numbers>
25#include <type_traits>
26#include <vector>
27
28namespace dspark {
29
88template <typename T>
90{
91 static_assert(std::is_floating_point_v<T>, "MinBlepTable requires float or double");
92
93public:
95 static constexpr int kTaps = 128;
97 static constexpr int kPhases = 2048;
99 static constexpr int kOversample = kPhases;
101 static constexpr int kTableSize = kTaps * kOversample + 1;
102
109 static const MinBlepTable& instance() noexcept
110 {
111 static const MinBlepTable table;
112 return table;
113 }
114
124 [[nodiscard]] T residual(T t) const noexcept
125 {
126 if (!(t >= T(0)) || !(t < static_cast<T>(kTaps)))
127 return T(0);
128 const auto whole = static_cast<int>(t);
129 const T pos = (t - static_cast<T>(whole)) * static_cast<T>(kPhases);
130 const auto q = std::min(static_cast<int>(pos), kPhases - 1);
131 const T w = pos - static_cast<T>(q);
132 const float* a = rows_.data() + static_cast<size_t>(q) * kTaps;
133 const T ra = static_cast<T>(a[whole]);
134 const T rb = static_cast<T>(a[kTaps + whole]);
135 return ra + w * (rb - ra);
136 }
137
148 void addStep(T* ring, int head, T jump, T frac) const noexcept
149 {
150 addRows(rows_, ring, head, jump, frac);
151 }
152
162 void addDelayedStep(T* ring, int head, T jump, T frac) const noexcept
163 {
164 addRows(rows_, ring, head, jump, frac);
165 addEarly(ring, head, frac, [jump](T) noexcept { return jump; });
166 }
167
183 void addDelayedRamp(T* ring, int head, T slopeJump, T frac) const noexcept
184 {
185 addRows(rampRows_, ring, head, slopeJump, frac);
186 const T delay = dcDelay_;
187 addEarly(ring, head, frac, [slopeJump, delay](T t) noexcept { return slopeJump * (t - delay); });
188 }
189
201 [[nodiscard]] T dcDelay() const noexcept { return dcDelay_; }
202
203private:
205 static constexpr int kCoarse = 64;
207 static constexpr double kBeta = 13.0;
208
209 MinBlepTable() noexcept { build(); }
210
213 [[nodiscard]] static double refine(const double* y, double x) noexcept
214 {
215 const double z = x - 0.5;
216 const double even1 = y[3] + y[2], odd1 = y[3] - y[2];
217 const double even2 = y[4] + y[1], odd2 = y[4] - y[1];
218 const double even3 = y[5] + y[0], odd3 = y[5] - y[0];
219 const double c0 = even1 * 0.42685983409379380 + even2 * 0.07238123511170030
220 + even3 * 0.00075893079450573;
221 const double c1 = odd1 * 0.35831772348893259 + odd2 * 0.20451644554758297
222 + odd3 * 0.00562658797241955;
223 const double c2 = even1 * -0.217009177221292431 + even2 * 0.20051376594086157
224 + even3 * 0.01649541128040211;
225 const double c3 = odd1 * -0.25112715343740988 + odd2 * 0.04223025992200458
226 + odd3 * 0.02488727472995134;
227 const double c4 = even1 * 0.04166946673533273 + even2 * -0.06250420114356986
228 + even3 * 0.02083473440841799;
229 const double c5 = odd1 * 0.08349799235675044 + odd2 * -0.04174912841630993
230 + odd3 * 0.00834987866042734;
231 return ((((c5 * z + c4) * z + c3) * z + c2) * z + c1) * z + c0;
232 }
233
234 void build() noexcept
235 {
236 constexpr int n = kTaps * kCoarse + 1; // 8193 coarse points
237 constexpr size_t fftSize = 131072; // 16x n keeps cepstral aliasing low
238 constexpr double pi = std::numbers::pi_v<double>;
239
240 // -- 1. Linear-phase BLIT: sinc cutting at the base-rate Nyquist
241 // (zeros every kCoarse points), Kaiser windowed, normalised to
242 // unit DC gain so the integrated step settles at 1.
243 auto besselI0 = [](double x) noexcept {
244 double sum = 1.0, term = 1.0;
245 for (int k = 1; k < 200; ++k)
246 {
247 term *= (x / (2.0 * k)) * (x / (2.0 * k));
248 sum += term;
249 if (term < sum * 1e-18) break;
250 }
251 return sum;
252 };
253 std::vector<double> x(fftSize, 0.0);
254 const double center = 0.5 * (n - 1);
255 const double norm = besselI0(kBeta);
256 double dc = 0.0;
257 for (int i = 0; i < n; ++i)
258 {
259 const double t = (static_cast<double>(i) - center) / kCoarse;
260 const double s = (std::abs(t) < 1e-9) ? 1.0 : std::sin(pi * t) / (pi * t);
261 const double r = (static_cast<double>(i) - center) / center;
262 const double w = besselI0(kBeta * std::sqrt(std::max(0.0, 1.0 - r * r))) / norm;
263 x[static_cast<size_t>(i)] = s * w;
264 dc += s * w;
265 }
266 for (int i = 0; i < n; ++i)
267 x[static_cast<size_t>(i)] /= dc;
268
269 // -- 2. Real cepstrum. log|X| is a real, even function of frequency,
270 // so running it through the real inverse FFT (imaginary parts
271 // zero) yields the even cepstrum directly.
272 FFTReal<double> fft(fftSize);
273 std::vector<double> spec(fftSize + 2);
274 std::vector<double> cep(fftSize);
275 fft.forward(x.data(), spec.data());
276
277 const size_t bins = fftSize / 2 + 1;
278 for (size_t k = 0; k < bins; ++k)
279 {
280 const double re = spec[2 * k];
281 const double im = spec[2 * k + 1];
282 // Soft floor just below the window's stopband, blended in power:
283 // hard, deep corners in log|X| make the cepstrum decay like 1/q^2
284 // and alias through the causal fold, which comes back as
285 // percent-level error in the reconstructed step.
286 constexpr double floorMag = 1e-7;
287 const double mag = std::sqrt(re * re + im * im + floorMag * floorMag);
288 spec[2 * k] = std::log(mag);
289 spec[2 * k + 1] = 0.0;
290 }
291 fft.inverse(spec.data(), cep.data());
292
293 // -- 3. Fold onto the causal part (Hilbert relation between log
294 // magnitude and minimum phase): keep q=0 and q=N/2, double
295 // 1..N/2-1, zero the anticausal half.
296 for (size_t q = 1; q < fftSize / 2; ++q)
297 {
298 cep[q] *= 2.0;
299 cep[fftSize - q] = 0.0;
300 }
301
302 // -- 4. Back to the spectrum, exponentiate, back to time: the
303 // minimum-phase BLIT, energy packed at the front.
304 fft.forward(cep.data(), spec.data());
305 for (size_t k = 0; k < bins; ++k)
306 {
307 const double m = std::exp(spec[2 * k]);
308 const double ph = spec[2 * k + 1];
309 spec[2 * k] = m * std::cos(ph);
310 spec[2 * k + 1] = m * std::sin(ph);
311 }
312 fft.inverse(spec.data(), x.data());
313
314 // -- 5. Integrate into a step on the coarse grid, settling exactly
315 // at 1 at the end of the kernel.
316 std::vector<double> step(static_cast<size_t>(n));
317 double acc = 0.0;
318 for (int i = 0; i < n; ++i)
319 {
320 acc += x[static_cast<size_t>(i)];
321 step[static_cast<size_t>(i)] = acc;
322 }
323 const double settle = step[static_cast<size_t>(n - 1)];
324 for (double& s : step) s /= settle;
325
326 // -- 6. Refine the step to the fine grid: the residual at every fine
327 // point of [0, kTaps], pinned to exactly 0 at the kernel's end.
328 // The step is 0 before the event and 1 after the kernel.
329 auto stepAt = [&](long i) noexcept {
330 if (i < 0) return 0.0;
331 if (i >= n) return 1.0;
332 return step[static_cast<size_t>(i)];
333 };
334 constexpr long fineEnd = static_cast<long>(kTaps) * kPhases;
335 constexpr int ratio = kPhases / kCoarse;
336 std::vector<double> r1(static_cast<size_t>(fineEnd + 1), 0.0);
337 for (long fine = 0; fine < fineEnd; ++fine)
338 {
339 const long i = fine / ratio;
340 const double frac = static_cast<double>(fine % ratio) / ratio;
341 double y[6];
342 for (int k = 0; k < 6; ++k) y[k] = stepAt(i - 2 + k);
343 r1[static_cast<size_t>(fine)] = refine(y, frac) - 1.0;
344 }
345
346 // -- 7. The ramp residual: the running trapezoidal integral of the
347 // step residual. It tends to -dcDelay (the band-limited ramp
348 // lags the ideal one), so it is stored plus dcDelay, which
349 // settles exactly at 0 at the kernel's end.
350 std::vector<double> r2(static_cast<size_t>(fineEnd + 1), 0.0);
351 for (long fine = 1; fine <= fineEnd; ++fine)
352 r2[static_cast<size_t>(fine)] = r2[static_cast<size_t>(fine - 1)]
353 + 0.5 * (r1[static_cast<size_t>(fine - 1)] + r1[static_cast<size_t>(fine)]) / kPhases;
354 const double delay = -r2[static_cast<size_t>(fineEnd)];
355 dcDelay_ = static_cast<T>(delay);
356
357 // -- 8. Polyphase rows: row q, tap j holds the value at j + q / kPhases.
358 rows_.assign(static_cast<size_t>(kPhases + 1) * kTaps, 0.0f);
359 rampRows_.assign(static_cast<size_t>(kPhases + 1) * kTaps, 0.0f);
360 for (int q = 0; q <= kPhases; ++q)
361 for (int j = 0; j < kTaps; ++j)
362 {
363 const long fine = static_cast<long>(j) * kPhases + q;
364 const auto at = static_cast<size_t>(q) * kTaps + static_cast<size_t>(j);
365 if (fine >= fineEnd) continue; // both settle at exactly 0
366 rows_[at] = static_cast<float>(r1[static_cast<size_t>(fine)]);
367 rampRows_[at] = static_cast<float>(r2[static_cast<size_t>(fine)] + delay);
368 }
369 }
370
374 template <typename Term>
375 void addEarly(T* ring, int head, T frac, Term term) const noexcept
376 {
377 if (!(frac >= T(0))) return;
378 for (int j = 0; j < kTaps; ++j)
379 {
380 const T t = static_cast<T>(j) + frac;
381 if (!(t < dcDelay_)) break;
382 ring[(head + j) & (kTaps - 1)] += term(t);
383 }
384 }
385
387 static void addRows(const std::vector<float>& rows, T* ring, int head, T scale, T frac) noexcept
388 {
389 if (!(frac >= T(0)) || !(frac < static_cast<T>(kTaps)))
390 return;
391 const auto whole = static_cast<int>(frac);
392 const T pos = (frac - static_cast<T>(whole)) * static_cast<T>(kPhases);
393 const auto q = std::min(static_cast<int>(pos), kPhases - 1);
394 const T w = pos - static_cast<T>(q);
395 const float* a = rows.data() + static_cast<size_t>(q) * kTaps + whole;
396 const float* b = a + kTaps;
397 const int n = kTaps - whole;
398 // Two contiguous runs around the ring's wrap.
399 const int first = std::min(n, kTaps - (head & (kTaps - 1)));
400 T* r = ring + (head & (kTaps - 1));
401 for (int j = 0; j < first; ++j)
402 {
403 const T ra = static_cast<T>(a[j]);
404 r[j] += scale * (ra + w * (static_cast<T>(b[j]) - ra));
405 }
406 for (int j = first; j < n; ++j)
407 {
408 const T ra = static_cast<T>(a[j]);
409 ring[j - first] += scale * (ra + w * (static_cast<T>(b[j]) - ra));
410 }
411 }
412
413 std::vector<float> rows_;
414 std::vector<float> rampRows_;
415 T dcDelay_ = T(0);
416};
417
418} // namespace dspark
Shared minimum-phase band-limited step (minBLEP) residual table.
static constexpr int kPhases
Sub-sample resolution: rows per sample, linearly interpolated.
T dcDelay() const noexcept
Low-frequency delay of the minimum-phase step, in samples.
T residual(T t) const noexcept
Residual of the minimum-phase band-limited step at position t.
static const MinBlepTable & instance() noexcept
Returns the process-wide shared table, building it on first call.
void addDelayedRamp(T *ring, int head, T slopeJump, T frac) const noexcept
Slope-change (kink) correction for a waveform rendered dcDelay() samples late: the minimum-phase band...
static constexpr int kOversample
Sub-sample positions per output sample (the same resolution).
void addStep(T *ring, int head, T jump, T frac) const noexcept
Adds jump * residual(j + frac) to ring[(head + j) & (kTaps - 1)] for every tap j.
static constexpr int kTableSize
Points of the residual's domain [0, kTaps] at that resolution.
void addDelayedStep(T *ring, int head, T jump, T frac) const noexcept
Step correction for a waveform rendered dcDelay() samples late.
static constexpr int kTaps
Correction span in base-rate samples (power of two, ring-buffer friendly).
constexpr int delay
Delay of the projected output behind the newest input, in samples.
Main namespace for the DSPark framework.
constexpr T pi
Pi (3.14159...) for the given floating-point type.
Definition DspMath.h:48