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_accumulator.hpp
Go to the documentation of this file.
1/**
2 * @file phase_accumulator.hpp
3 * @brief A phase advanced by a signed step each frame, held as a 32-bit count of the cycle
4 */
5
6#ifndef SBL_DSP_OSCILLATORS_PHASE_ACCUMULATOR_HPP_
7#define SBL_DSP_OSCILLATORS_PHASE_ACCUMULATOR_HPP_
8
9#include <cmath>
10#include <cstdint>
11
12namespace sbl::dsp {
13
14/**
15 * @brief A phase advanced by a signed step each frame, held as a 32-bit count of the cycle
16 *
17 * The phase is a 32-bit unsigned count, 2^32 counts to the cycle, and each
18 * frame adds the step's count to it. Integer addition is exact and wraps by
19 * itself, so the phase accumulates no rounding error: the one error is the
20 * step's rounding to the nearest count, a frequency resolution of fs / 2^32,
21 * about 11 uHz at 48 kHz, which does not grow with time. A float
22 * accumulator rounds every add instead.
23 *
24 * The interface is float, in natural units: a step in cycles per frame in;
25 * the phase in [0, 1), and the step as it is taken, out. The count stays
26 * inside. Below the signal transport: the component
27 * that owns an accumulator converts its ports to the step.
28 *
29 * The step is signed, so the phase runs forward, backward and through zero,
30 * as PhasePulse reads it. A signed count tells the direction only for steps
31 * under half a cycle a frame, so the step is clamped to MAX_STEP either way;
32 * a pitch alone stays far under it. A non-finite step counts as no motion.
33 * The phase out is the count's top 24 bits, exact in a float and never 1.
34 *
35 * @code
36 * PhaseAccumulator acc;
37 * const float step = acc.set_step(frequency); // when the frequency changes
38 * const float phase = acc.advance(); // each frame
39 * core.process(phase, step, offset);
40 * @endcode
41 */
43public:
44 static constexpr float MAX_STEP = 0.49999997f; /**< cycles per frame either way: the float just under 1/2 */
45
46 /**
47 * @brief Set the step each later advance() takes
48 *
49 * @param cycles_per_frame Step in cycles per frame, signed; clamped to MAX_STEP either way; non-finite counts as 0
50 * @return The step as it will be taken, rounded to the nearest count
51 * @context Same thread as advance(); real-time safe.
52 */
53 float set_step(float cycles_per_frame) {
54 increment_ = count_of(cycles_per_frame);
55 return step();
56 }
57
58 /**
59 * @brief One frame: advance by the step.
60 *
61 * @return The new phase, in [0, 1)
62 * @context Audio thread; real-time safe.
63 */
64 float advance() {
65 count_ += static_cast<uint32_t>(increment_); // modulo 2^32: a backward step wraps down
66 return phase();
67 }
68
69 /** @brief The phase after the last advance, in [0, 1). */
70 float phase() const { return static_cast<float>(count_ >> TOP_SHIFT) * PHASE_PER_TOP_COUNT; }
71
72 /** @brief The step each advance takes, in cycles per frame. */
73 float step() const { return static_cast<float>(increment_) * CYCLES_PER_COUNT; }
74
75 /**
76 * @brief Move to a phase. The step stays.
77 *
78 * @param phase Phase in cycles, wrapped into [0, 1); non-finite counts as 0
79 */
80 void reset(float phase = 0.0f) {
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; // p * 2^32 is exact
83 }
84
85private:
86 static constexpr float COUNTS_PER_CYCLE = 0x1p32f;
87 static constexpr float CYCLES_PER_COUNT = 0x1p-32f;
88 static constexpr float WHOLE_FROM = 0x1p23f; /**< from here up a float holds whole numbers only */
89 static constexpr uint32_t TOP_SHIFT = 8U; /**< the phase out is the count's top 32 - 8 = 24 bits */
90 static constexpr float PHASE_PER_TOP_COUNT = 0x1p-24f; /**< one count of the top 24 bits, in cycles */
91
92 /** A step in cycles per frame as a signed count, clamped and rounded to the nearest. */
93 static int32_t count_of(float cycles_per_frame) {
94 if (!std::isfinite(cycles_per_frame)) {
95 return 0;
96 }
97 float s = cycles_per_frame;
98 s = s > MAX_STEP ? MAX_STEP : (s < -MAX_STEP ? -MAX_STEP : s);
99 const float counts = s * COUNTS_PER_CYCLE; // exact: a power of two; under 2^31 either way
100 if (std::fabs(counts) >= WHOLE_FROM) {
101 return static_cast<int32_t>(counts); // already a whole number; adding 1/2 would round twice
102 }
103 return static_cast<int32_t>(counts + (counts < 0.0f ? -0.5f : 0.5f));
104 }
105
106 uint32_t count_ = 0U;
107 int32_t increment_ = 0;
108};
109
110} // namespace sbl::dsp
111
112#endif // SBL_DSP_OSCILLATORS_PHASE_ACCUMULATOR_HPP_
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.
The audio stack.
Definition dry_wet.hpp:15