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
phase_walk.hpp
Go to the documentation of this file.
1/**
2 * @file phase_walk.hpp
3 * @brief A modulated phase walked frame by frame as segments, and the crossings of moving thresholds on them
4 */
5
6#ifndef SBL_DSP_OSCILLATORS_PHASE_WALK_HPP_
7#define SBL_DSP_OSCILLATORS_PHASE_WALK_HPP_
8
9#include <cmath>
10#include <cstdint>
11#include <cstring>
13#include <sbl/math/clamp.hpp>
14#include <sbl/math/finite.hpp>
15
16namespace sbl::dsp {
17
18/**
19 * @brief A modulated phase walked frame by frame as segments, and the crossings of moving thresholds on them
20 *
21 * The part of a threshold core that is not about its shape.
22 * Each frame its driver gives the phase, in [0, 1), the signed step that led
23 * there, in cycles, and a phase offset at the end of each of SUBSTEPS equal
24 * parts of the frame. The walk reads the phase plus the
25 * offset, and turns the frame into one Segment per part: the phase moving
26 * linearly from where the last part ended to where this one ends. A core
27 * corrects its events on those segments: where a threshold of its shape,
28 * moving across the frame, is crossed.
29 *
30 * Each segment ends on the frame's phase less the step still ahead of it,
31 * plus the part's offset. Its step is the frame's step times the part's
32 * share, plus the offset's change since the last part. The step says how
33 * many wraps lie between the segment's two ends, round(start + step - end),
34 * so the phase may run forward, backward and through zero, and the ends are
35 * the phases themselves: a core's crossings always agree with the naive
36 * samples it reads at those phases, whatever rounding its driver's
37 * accumulator did.
38 *
39 * Crossings are found on the unwrapped segment against the threshold and its
40 * images one cycle up and down, in either direction and across the wrap, and
41 * every image crossed is reported at its own time. Which side of the
42 * threshold each end lies on is read as `phase >= threshold`, the comparison
43 * a core reads its naive sample with, so a core's corrections sum to its
44 * naive change however the unwrapped end rounds.
45 *
46 * Inputs and their handling, per part:
47 * - A phase outside [0, 1) is wrapped into it. A non-finite phase or step
48 * counts as no motion: the phase holds. A non-finite offset holds the
49 * previous one.
50 * - The step, the offset's change included, is clamped to [-MAX_STEP,
51 * MAX_STEP], one cycle per frame. With threshold jumps of up to one cycle a
52 * frame, a segment holds at most three crossings per threshold. Past the
53 * clamp the segment's two ends stay exact and the motion between them loses
54 * whole cycles, whose crossings go unreported.
55 * - Phase is linear within a part: a crossing is timed on the
56 * segment, so an event born and closed within one part is not seen.
57 *
58 * Below the signal transport: its inputs are
59 * natural units, set by the core that owns it. It holds the last part's phase
60 * and offset; all state advances per frame.
61 *
62 * In a core's frame:
63 * @code
64 * walk_.frame(phase, step, offsets, [&](const PhaseWalk::Segment& s) {
65 * PhaseWalk::crossings(s, from, to, [&](float t, int direction, float speed) { ... });
66 * });
67 * const float naive = shape(walk_.phase());
68 * @endcode
69 */
70class PhaseWalk {
71public:
72 static constexpr float MAX_STEP = 1.0f; /**< cycles per frame, either direction */
73
74 /** @brief One part of a frame: the phase moving linearly from `start` to `end` plus `wraps` cycles. */
75 struct Segment {
76 float start; /**< the phase at the part's start, in [0, 1) */
77 float end; /**< the phase at its end, in [0, 1) */
78 int wraps; /**< whole cycles between them: the motion is end + wraps - start; in [-2, 2] */
79 float part; /**< the part's share of the frame, 1 / SUBSTEPS */
80 float before; /**< the share of the frame after the part */
81 float through; /**< the share of the frame up to the part's end */
82 bool last; /**< the frame's last part, which ends on the frame's phase and offset */
83 };
84
85 /**
86 * @brief One frame in SUBSTEPS equal parts, each given to `on_segment` as a Segment
87 *
88 * @tparam SUBSTEPS Parts per frame, at least 1
89 * @tparam OnSegment A callable taking `const Segment&`
90 * @param phase The frame's phase, in [0, 1); wrapped; a non-finite phase holds
91 * @param step The signed step from the previous frame's phase, in cycles; a non-finite step holds
92 * @param offsets The phase offset at the end of each part, in cycles; `offsets[SUBSTEPS - 1]` is the frame's;
93 * a non-finite offset holds the previous one
94 * @param on_segment Called once per part, in order, as `on_segment(const Segment&)`
95 * @context Audio thread; real-time safe.
96 */
97 template<uint8_t SUBSTEPS, class OnSegment>
98 void frame(float phase, float step, const float (&offsets)[SUBSTEPS], OnSegment&& on_segment) {
99 static_assert(SUBSTEPS > 0, "SBL ERROR: a frame has at least one part");
100 const float part = 1.0f / static_cast<float>(SUBSTEPS);
101 float p = phase_;
102 for (uint8_t k = 1; k <= SUBSTEPS; ++k) {
103 const float o = sbl::math::is_finite(offsets[k - 1]) ? offsets[k - 1] : offset_;
104 const float moved = o - offset_;
105 offset_ = o;
106 const float p0 = p;
107 const float behind = step * (static_cast<float>(SUBSTEPS - k) * part); // the frame's phase is this far on
108 float q = (phase - behind) + o;
109 float s = step * part + moved;
111 q = p0;
112 s = 0.0f;
113 }
114 q = wrap(q);
116 p = q;
117 Segment segment{};
118 segment.start = p0;
119 segment.end = q;
120 segment.wraps = static_cast<int>(std::floor(p0 + s - q + 0.5f)); // rounded, in [-2, 2]
121 segment.part = part;
122 segment.before = static_cast<float>(SUBSTEPS - k) * part;
123 segment.through = static_cast<float>(k) * part;
124 segment.last = k == SUBSTEPS;
125 on_segment(static_cast<const Segment&>(segment));
126 }
127 phase_ = p;
128 }
129
130 /**
131 * @brief Report every image of a threshold crossed by a segment while the threshold moved c0 -> c1
132 *
133 * The threshold's images are c + n for every whole n. Each crossing is given to
134 * `on_crossing(t, direction, speed)`:
135 * - `t`, the fraction of a frame from the crossing to the frame's end, in [0, 1),
136 * the sub-frame time a BlepCorrector takes;
137 * - `direction`, +1 where the phase passed the image upward relative to the
138 * threshold's motion, -1 downward;
139 * - `speed`, the phase's speed relative to the threshold over the segment, in
140 * cycles per frame, signed: positive upward.
141 *
142 * Most segments cross nothing, so which side each end lies on is tested here,
143 * inline in the caller, and the crossings themselves are found out of line.
144 * The inlining is forced with [[gnu::always_inline]].
145 *
146 * @tparam OnCrossing A callable taking `(float t, int direction, float speed)`
147 * @param s The segment
148 * @param c0 The threshold at the segment's start, in cycles
149 * @param c1 The threshold at the segment's end, in cycles
150 * @param on_crossing Called once per image crossed
151 * @context Audio thread; real-time safe.
152 */
153 template<class OnCrossing>
154 [[gnu::always_inline]] static inline void crossings(const Segment& s, float c0, float c1,
155 OnCrossing&& on_crossing) {
156 const int side0 = s.start >= c0 ? 0 : -1; // floor(start - c0)
157 const int side1 = s.wraps + (s.end >= c1 ? 0 : -1); // floor(end + wraps - c1)
158 if (side0 != side1) {
159 crossed(s, c0, c1, side0, side1, on_crossing);
160 }
161 }
162
163 /**
164 * @brief Move to a phase plus an offset, the offset kept as the last part's.
165 *
166 * @param phase Phase in cycles; a non-finite phase counts as 0
167 * @param offset Offset in cycles; a non-finite offset counts as 0
168 */
169 void reset(float phase = 0.0f, float offset = 0.0f) {
170 offset_ = std::isfinite(offset) ? offset : 0.0f;
171 phase_ = std::isfinite(phase) ? wrap(phase + offset_) : wrap(offset_);
172 }
173
174 /** @brief The phase the last part ended on, the offset included, in [0, 1). */
175 float phase() const { return phase_; }
176
177 /** @brief The offset the last part ended on, in cycles. */
178 float offset() const { return offset_; }
179
180 /**
181 * @brief Wrap into [0, 1); a value that rounds up to 1 is 0.
182 *
183 * @param x A finite value
184 * @return x - floor(x), or 0 where that rounds to 1
185 */
186 static float wrap(float x) {
187 const float y = x - std::floor(x);
188 return y < 1.0f ? y : 0.0f;
189 }
190
191private:
192 /** The crossings of a segment whose ends lie on different sides: images side0 + 1 .. side1 upward, or side0 .. side1 + 1 downward. */
193 template<class OnCrossing>
194 [[gnu::noinline]] static void crossed(const Segment& s, float c0, float c1, int side0, int side1,
195 OnCrossing& on_crossing) {
196 const float p1 = s.end + static_cast<float>(s.wraps);
197 // The speed is taken per crossing, so a caller that ignores it does not pay for the divide.
198 for (int n = side0 + 1; n <= side1; ++n) {
199 const auto image = static_cast<float>(n);
200 on_crossing(s.before + s.part * math::polyblep_time(s.start, p1, c0 + image, c1 + image), 1,
201 ((p1 - s.start) - (c1 - c0)) / s.part);
202 }
203 for (int n = side0; n > side1; --n) {
204 const auto image = static_cast<float>(n);
205 on_crossing(s.before + s.part * math::polyblep_time(s.start, p1, c0 + image, c1 + image), -1,
206 ((p1 - s.start) - (c1 - c0)) / s.part);
207 }
208 }
209
210 float phase_ = 0.0f;
211 float offset_ = 0.0f;
212};
213
214} // namespace sbl::dsp
215
216#endif // SBL_DSP_OSCILLATORS_PHASE_WALK_HPP_
Clamping.
A modulated phase walked frame by frame as segments, and the crossings of moving thresholds on them.
static float wrap(float x)
Wrap into [0, 1); a value that rounds up to 1 is 0.
void reset(float phase=0.0f, float offset=0.0f)
Move to a phase plus an offset, the offset kept as the last part's.
static constexpr float MAX_STEP
void frame(float phase, float step, const float(&offsets)[SUBSTEPS], OnSegment &&on_segment)
One frame in SUBSTEPS equal parts, each given to on_segment as a Segment.
float phase() const
The phase the last part ended on, the offset included, in [0, 1).
static void crossings(const Segment &s, float c0, float c1, OnCrossing &&on_crossing)
Report every image of a threshold crossed by a segment while the threshold moved c0 -> c1.
float offset() const
The offset the last part ended on, in cycles.
Finiteness of a float, read from its bits, and a fallback for a value that is not finite.
constexpr float polyblep_time(float before, float after, float threshold_before, float threshold_after)
Sub-frame time at which a linear segment crossed a moving threshold.
Definition polyblep.hpp:242
The audio stack.
Definition dry_wet.hpp:15
bool is_finite(float x)
std::isfinite, read from the exponent's bits.
Definition finite.hpp:27
constexpr T clamp(T x, T lo, T hi)
x held to [lo, hi]; a NaN goes to lo.
Definition clamp.hpp:24
Polynomial BLEP kernels, and the sub-frame time of a crossing.
One part of a frame: the phase moving linearly from start to end plus wraps cycles.