Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
blep_corrector.hpp
Go to the documentation of this file.
1/**
2 * @file blep_corrector.hpp
3 * @brief The correction buffer behind a band-limited edge, over a BLEP kernel
4 */
5
6#ifndef SBL_DSP_OSCILLATORS_BLEP_CORRECTOR_HPP_
7#define SBL_DSP_OSCILLATORS_BLEP_CORRECTOR_HPP_
8
9#include <cstdint>
10#include <type_traits>
12
13/**
14 * @brief Count a correction added by a BlepCorrector
15 *
16 * Increments the counter behind blep_correction_count() when
17 * SBL_PROFILING_ENABLED is defined, and compiles to nothing otherwise.
18 */
19#ifdef SBL_PROFILING_ENABLED
20#define SBL_BLEP_CORRECTION() (++sbl::dsp::detail::blep_correction_counter())
21#else
22#define SBL_BLEP_CORRECTION() ((void)0)
23#endif
24
25namespace sbl::dsp {
26
27#ifdef SBL_PROFILING_ENABLED
28namespace detail {
29
30/**
31 * @brief The count SBL_BLEP_CORRECTION() increments.
32 *
33 * @context Written from the audio thread.
34 */
35inline uint32_t& blep_correction_counter() {
36 static uint32_t count = 0;
37 return count;
38}
39
40} // namespace detail
41#endif
42
43/**
44 * @brief Corrections added by every BlepCorrector since the start
45 *
46 * One for each step() and each corner(): an edge or a slope change that a
47 * core band-limited. Wraps at 2^32. Always 0 in a build without profiling.
48 *
49 * @return The count
50 * @context Any thread; a plain, non-atomic read of a count the audio thread writes.
51 */
52inline uint32_t blep_correction_count() {
53#ifdef SBL_PROFILING_ENABLED
54 return detail::blep_correction_counter();
55#else
56 return 0;
57#endif
58}
59
60namespace detail {
61
62template<class K, class = void>
63struct blep_kernel_has_span : std::false_type {};
64template<class K>
65struct blep_kernel_has_span<K, std::void_t<decltype(K::FRAMES), decltype(K::LATENCY)>> : std::true_type {};
66
67template<class K, class = void>
68struct blep_kernel_has_residuals : std::false_type {};
69template<class K>
70struct blep_kernel_has_residuals<K, std::void_t<decltype(K::step(0.0f).at[0]), decltype(K::slope(0.0f).at[0])>>
71 : std::true_type {};
72
73} // namespace detail
74
75/**
76 * @brief The correction buffer behind a band-limited edge, over a BLEP kernel
77 *
78 * Holds the pending corrections for the frames an event touches and emits
79 * each frame once its corrections are complete, so the output trails the
80 * naive waveform by Kernel::LATENCY frames. Corrections from events closer
81 * than the kernel's span add linearly, which is exact.
82 * History and pending corrections are rings, so a frame moves an index and
83 * not the buffers; the pending ring wraps by a mask, so a kernel's FRAMES is
84 * a power of two.
85 *
86 * Per frame, in this order: step() or corner() for each event that fell in
87 * the frame, with its sub-frame time from math::polyblep_time(), then
88 * process() with the frame's naive sample. An oscillator core, a hard
89 * clipper or an audio-rate sample-and-hold owns one of these; it is below
90 * the signal transport and has no ports.
91 *
92 * The kernel is a policy type (the contract is in dsp/math/polyblep.hpp and
93 * checked below): math::Sinc16, the cores' default, math::Bspline4 and
94 * math::Linear2.
95 *
96 * @tparam Kernel A BLEP kernel whose FRAMES is a power of two
97 *
98 * In a build with profiling, blep_correction_count() is the number of
99 * step() and corner() calls on every corrector, for an app that reports its
100 * events.
101 */
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)");
114
115public:
116 /** @brief Frames the output trails the naive waveform. */
117 static constexpr uint8_t LATENCY = Kernel::LATENCY;
118
119 /**
120 * @brief A value step at a sub-frame time within the frame about to be pushed.
121 *
122 * @param t Fraction of a frame since the step, read at the frame about to be pushed, in [0, 1)
123 * @param height Height of the step
124 * @context Audio thread; real-time safe.
125 */
126 void step(float t, float height) { add(Kernel::step(t), height); }
127
128 /**
129 * @brief A slope change at a sub-frame time within the frame about to be pushed.
130 *
131 * @param t Fraction of a frame since the corner, read at the frame about to be pushed, in [0, 1)
132 * @param delta Change of slope, per frame
133 * @context Audio thread; real-time safe.
134 */
135 void corner(float t, float delta) { add(Kernel::slope(t), delta); }
136
137 /**
138 * @brief Push the frame's naive sample; return the corrected sample LATENCY frames back
139 *
140 * The first LATENCY outputs after a reset are zero.
141 *
142 * @param naive The frame's naive sample
143 * @return The corrected sample LATENCY frames back
144 * @context Audio thread; real-time safe.
145 */
146 float process(float naive) {
147 float out;
148 if constexpr (Kernel::LATENCY == 0) {
149 out = naive + pending_[head_];
150 } else {
151 out = history_[oldest_] + pending_[head_];
152 history_[oldest_] = naive;
153 oldest_ = static_cast<uint8_t>(oldest_ + 1 < Kernel::LATENCY ? oldest_ + 1 : 0);
154 }
155 pending_[head_] = 0.0f;
156 head_ = static_cast<uint8_t>((head_ + 1) & MASK);
157 return out;
158 }
159
160 /** @brief Forget history and pending corrections. */
161 void reset() {
162 for (float& h : history_) {
163 h = 0.0f;
164 }
165 for (float& p : pending_) {
166 p = 0.0f;
167 }
168 oldest_ = 0;
169 head_ = 0;
170 }
171
172private:
173 static constexpr uint8_t MASK = Kernel::FRAMES - 1;
174
175 void add(const math::BlepResidual<Kernel::FRAMES>& residual, float scale) {
177 for (uint8_t i = 0; i < Kernel::FRAMES; ++i) {
178 pending_[(head_ + i) & MASK] += scale * residual.at[i];
179 }
180 }
181
182 /** Naive samples not yet emitted, a ring: frames n - LATENCY .. n - 1, the oldest at oldest_. */
183 float history_[Kernel::LATENCY > 0 ? Kernel::LATENCY : 1] = {};
184 /** Corrections for frames n - LATENCY .. n - LATENCY + FRAMES - 1, a ring from head_. */
185 float pending_[Kernel::FRAMES] = {};
186 uint8_t oldest_ = 0;
187 uint8_t head_ = 0;
188};
189
190} // namespace sbl::dsp
191
192#endif // SBL_DSP_OSCILLATORS_BLEP_CORRECTOR_HPP_
#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.
The audio stack.
Definition dry_wet.hpp:15
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.
Definition polyblep.hpp:49