Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
phase.hpp
Go to the documentation of this file.
1// sbl/dsp/phase.hpp — Phase accumulator for oscillators and LFOs
2//
3// 32-bit phase accumulator with natural wrapping. The full uint32_t range
4// maps to one cycle (0 to just-under-2π). Frequency is set via phase
5// increment per sample.
6//
7// Usage:
8// sbl::dsp::PhaseAccumulator phase;
9// phase.set_increment(freq_to_inc(440.0f));
10// phase.advance(); // single sample
11// uint32_t p = phase.phase(); // read current phase
12//
13// Block processing:
14// uint32_t buf[BLOCK_SIZE];
15// phase.advance(BLOCK_SIZE, buf); // fill buffer with per-sample phases
16
17#pragma once
18
19#include <cstdint>
20
21namespace sbl::dsp {
22
24public:
25 /// @note All public methods are ISR-safe — bounded computation, no I/O.
26
27 /**
28 * @brief Set the phase increment per sample
29 *
30 * inc = frequency_hz * 2^32 / sample_rate
31 * Use freq_to_inc() for Hz input.
32 */
33 void set_increment(uint32_t inc) { increment_ = inc; }
34
35 /** @brief Advance phase by one sample */
36 void advance() { phase_ += increment_; }
37
38 /**
39 * @brief Advance and fill a buffer with per-sample phases
40 *
41 * Each output element is the phase AFTER advancing. Useful for
42 * batch wavetable reads.
43 */
44 void advance(uint16_t frames, uint32_t* out) {
45 for (uint16_t i = 0; i < frames; ++i) {
46 phase_ += increment_;
47 out[i] = phase_;
48 }
49 }
50
51 /** @brief Current phase value */
52 uint32_t phase() const { return phase_; }
53
54 /** @brief Current phase increment */
55 uint32_t increment() const { return increment_; }
56
57 /** @brief Reset phase to zero */
58 void reset() { phase_ = 0; }
59
60 /** @brief Reset phase to a specific value (for sync) */
61 void reset(uint32_t phase) { phase_ = phase; }
62
63 /**
64 * @brief Convert frequency in Hz to phase increment
65 *
66 * @param freq_hz Frequency in Hz (e.g., 440.0f)
67 * @param sr Sample rate (default 48000)
68 * @return Phase increment per sample
69 */
70 static uint32_t freq_to_inc(float freq_hz, float sr = 48000.0f) {
71 return static_cast<uint32_t>((freq_hz / sr) * 4294967296.0f);
72 }
73
74private:
75 uint32_t phase_ = 0;
76 uint32_t increment_ = 0;
77};
78
79} // namespace sbl::dsp
void reset()
Reset phase to zero.
Definition phase.hpp:58
void set_increment(uint32_t inc)
Set the phase increment per sample.
Definition phase.hpp:33
uint32_t increment() const
Current phase increment.
Definition phase.hpp:55
void advance()
Advance phase by one sample.
Definition phase.hpp:36
static uint32_t freq_to_inc(float freq_hz, float sr=48000.0f)
Convert frequency in Hz to phase increment.
Definition phase.hpp:70
void reset(uint32_t phase)
Reset phase to a specific value (for sync)
Definition phase.hpp:61
void advance(uint16_t frames, uint32_t *out)
Advance and fill a buffer with per-sample phases.
Definition phase.hpp:44
uint32_t phase() const
Current phase value.
Definition phase.hpp:52
DSP atoms for audio signal processing.
Definition allpass.hpp:22