6#ifndef SBL_DSP_OSCILLATORS_BLEP_CORRECTOR_HPP_
7#define SBL_DSP_OSCILLATORS_BLEP_CORRECTOR_HPP_
19#ifdef SBL_PROFILING_ENABLED
20#define SBL_BLEP_CORRECTION() (++sbl::dsp::detail::blep_correction_counter())
22#define SBL_BLEP_CORRECTION() ((void)0)
27#ifdef SBL_PROFILING_ENABLED
35inline uint32_t& blep_correction_counter() {
36 static uint32_t count = 0;
53#ifdef SBL_PROFILING_ENABLED
54 return detail::blep_correction_counter();
62template<
class K,
class =
void>
63struct blep_kernel_has_span : std::false_type {};
65struct blep_kernel_has_span<K, std::void_t<decltype(K::FRAMES), decltype(K::LATENCY)>> : std::true_type {};
67template<
class K,
class =
void>
68struct blep_kernel_has_residuals : std::false_type {};
70struct blep_kernel_has_residuals<K, std::void_t<decltype(K::step(0.0f).at[0]), decltype(K::slope(0.0f).at[0])>>
102template<
class Kernel>
104 static_assert(detail::blep_kernel_has_span<Kernel>::value,
105 "SBL ERROR: a BLEP kernel needs `static constexpr uint8_t FRAMES` and `LATENCY`");
106 static_assert(detail::blep_kernel_has_residuals<Kernel>::value,
107 "SBL ERROR: a BLEP kernel needs `step(float t)` and `slope(float t)` returning BlepResidual<FRAMES>");
108 static_assert(Kernel::FRAMES > 0 && Kernel::LATENCY < Kernel::FRAMES,
109 "SBL ERROR: a BLEP kernel's LATENCY lies inside its FRAMES");
110 static_assert(
sizeof(Kernel::step(0.0f).at) /
sizeof(
float) == Kernel::FRAMES,
111 "SBL ERROR: a BLEP kernel's residual has FRAMES entries");
112 static_assert((Kernel::FRAMES & (Kernel::FRAMES - 1)) == 0,
113 "SBL ERROR: a BLEP kernel's FRAMES is a power of two (the pending ring wraps by a mask)");
117 static constexpr uint8_t
LATENCY = Kernel::LATENCY;
126 void step(
float t,
float height) { add(Kernel::step(t), height); }
135 void corner(
float t,
float delta) { add(Kernel::slope(t), delta); }
148 if constexpr (Kernel::LATENCY == 0) {
149 out = naive + pending_[head_];
151 out = history_[oldest_] + pending_[head_];
152 history_[oldest_] = naive;
153 oldest_ =
static_cast<uint8_t
>(oldest_ + 1 < Kernel::LATENCY ? oldest_ + 1 : 0);
155 pending_[head_] = 0.0f;
156 head_ =
static_cast<uint8_t
>((head_ + 1) & MASK);
162 for (
float& h : history_) {
165 for (
float& p : pending_) {
173 static constexpr uint8_t MASK = Kernel::FRAMES - 1;
177 for (uint8_t i = 0; i < Kernel::FRAMES; ++i) {
178 pending_[(head_ + i) & MASK] += scale * residual.
at[i];
183 float history_[Kernel::LATENCY > 0 ? Kernel::LATENCY : 1] = {};
185 float pending_[Kernel::FRAMES] = {};
#define SBL_BLEP_CORRECTION()
Count a correction added by a BlepCorrector.
The correction buffer behind a band-limited edge, over a BLEP kernel.
void step(float t, float height)
A value step at a sub-frame time within the frame about to be pushed.
void corner(float t, float delta)
A slope change at a sub-frame time within the frame about to be pushed.
float process(float naive)
Push the frame's naive sample; return the corrected sample LATENCY frames back.
void reset()
Forget history and pending corrections.
static constexpr uint8_t LATENCY
Frames the output trails the naive waveform.
uint32_t blep_correction_count()
Corrections added by every BlepCorrector since the start.
Polynomial BLEP kernels, and the sub-frame time of a crossing.
A kernel's corrections for the N frames it touches, oldest first.