6#ifndef SBL_DSP_OSCILLATORS_PHASE_ACCUMULATOR_HPP_
7#define SBL_DSP_OSCILLATORS_PHASE_ACCUMULATOR_HPP_
54 increment_ = count_of(cycles_per_frame);
65 count_ +=
static_cast<uint32_t
>(increment_);
70 float phase()
const {
return static_cast<float>(count_ >> TOP_SHIFT) * PHASE_PER_TOP_COUNT; }
73 float step()
const {
return static_cast<float>(increment_) * CYCLES_PER_COUNT; }
81 const float p = std::isfinite(
phase) ?
phase - std::floor(
phase) : 0.0f;
82 count_ = p < 1.0f ?
static_cast<uint32_t
>(p * COUNTS_PER_CYCLE) : 0U;
86 static constexpr float COUNTS_PER_CYCLE = 0x1p32f;
87 static constexpr float CYCLES_PER_COUNT = 0x1p-32f;
88 static constexpr float WHOLE_FROM = 0x1p23f;
89 static constexpr uint32_t TOP_SHIFT = 8U;
90 static constexpr float PHASE_PER_TOP_COUNT = 0x1p-24f;
93 static int32_t count_of(
float cycles_per_frame) {
94 if (!std::isfinite(cycles_per_frame)) {
97 float s = cycles_per_frame;
99 const float counts = s * COUNTS_PER_CYCLE;
100 if (std::fabs(counts) >= WHOLE_FROM) {
101 return static_cast<int32_t
>(counts);
103 return static_cast<int32_t
>(counts + (counts < 0.0f ? -0.5f : 0.5f));
106 uint32_t count_ = 0U;
107 int32_t increment_ = 0;
A phase advanced by a signed step each frame, held as a 32-bit count of the cycle.
float advance()
One frame: advance by the step.
static constexpr float MAX_STEP
float set_step(float cycles_per_frame)
Set the step each later advance() takes.
float step() const
The step each advance takes, in cycles per frame.
float phase() const
The phase after the last advance, in [0, 1).
void reset(float phase=0.0f)
Move to a phase. The step stays.