Skip to content

Math16

Source: math16.h

BeatPhase

class BeatPhase
src/core/math16.h:124

A resolution- and framerate-independent BPM phase accumulator.

Nine effects hand-rolled this identically, and the shape is subtle enough to be worth owning once: the per-tick product dt * bpm * scale / 60000 rounds to ZERO when dt is under a millisecond (every desktop frame, and an ESP32 running a small fixture fast), so the animation silently freezes. The fix all nine converged on is to accumulate the RAW numerator in 64 bits and divide only at the read: which is what this does.

Usage: one member per animated quantity; call advanceTo(nowMs, rate) once per frame, then read as often as needed. rate is BPM-like: the caller's speed control, whatever its units.

Public Methods

inline void advanceTo(uint32_t nowMs, uint32_t rate) : Accumulate this frame's contribution.

inline uint32_t phase(uint32_t scale) const : The phase scaled by scale and divided late: phase(256) is the uint8 angle form (256 = full turn) the effects use, phase(65536) the angle16 form.

inline uint64_t numerator() const : The undivided numerator, for a caller that scales differently (NoiseEffect multiplies the rate by the grid width before accumulating; DistortionWaves reads one accumulator at two scales for its second axis).

inline void advanceScaled(uint32_t elapsedMs, uint64_t scaledRate) : Feed a pre-scaled product directly: for the callers whose rate already carries a factor.

inline void reset()