6#ifndef SBL_DSP_OSCILLATORS_PHASE_WALK_HPP_
7#define SBL_DSP_OSCILLATORS_PHASE_WALK_HPP_
97 template<u
int8_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);
102 for (uint8_t k = 1; k <= SUBSTEPS; ++k) {
104 const float moved = o - offset_;
107 const float behind = step * (
static_cast<float>(SUBSTEPS - k) * part);
108 float q = (
phase - behind) + o;
109 float s = step * part + moved;
120 segment.wraps =
static_cast<int>(std::floor(p0 + s - q + 0.5f));
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));
153 template<
class OnCrossing>
155 OnCrossing&& on_crossing) {
156 const int side0 = s.
start >= c0 ? 0 : -1;
157 const int side1 = s.
wraps + (s.
end >= c1 ? 0 : -1);
158 if (side0 != side1) {
159 crossed(s, c0, c1, side0, side1, on_crossing);
175 float phase()
const {
return phase_; }
187 const float y = x - std::floor(x);
188 return y < 1.0f ? y : 0.0f;
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);
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);
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);
211 float offset_ = 0.0f;
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.
bool is_finite(float x)
std::isfinite, read from the exponent's bits.
constexpr T clamp(T x, T lo, T hi)
x held to [lo, hi]; a NaN goes to lo.
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.