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
pm_pulse.hpp
Go to the documentation of this file.
1/**
2 * @file pm_pulse.hpp
3 * @brief A pulse oscillator with phase modulation: pitch, width, anchor, index and the PM signal over a PhasePulse
4 */
5
6#ifndef SBL_DSP_OSCILLATORS_PM_PULSE_HPP_
7#define SBL_DSP_OSCILLATORS_PM_PULSE_HPP_
8
9#include <cmath>
10#include <cstdint>
17#include <sbl/math/clamp.hpp>
18#include <sbl/math/finite.hpp>
19
20namespace sbl::dsp {
21
22/**
23 * @brief A pulse oscillator with phase modulation: pitch, width, anchor, index and the PM signal over a PhasePulse
24 *
25 * The composition owns a PhaseAccumulator and drives a PhasePulse<> with its
26 * phase and step, from the parts in pm.hpp, which a component with cores of its own uses directly. The
27 * accumulator holds the phase as a 32-bit count of the cycle, so it gathers
28 * no rounding error; each frame's step is the frequency rounded to fs / 2^32.
29 * It makes the one conversion from each port to the core's natural units,
30 * pitch through math::frequency(). Its ports
31 * are process() arguments after the frame count, in the order PORTS declares
32 * (common/transport.hpp):
33 *
34 * - pitch, octaves relative to Tune. The frequency is capped at
35 * MAX_FREQUENCY and is never negative; negative steps come from PM.
36 * - width, the fraction of the cycle the pulse is high, [0, 1].
37 * - anchor, where width acts, [-1, 1]: -1 holds the rise at the wrap, 0 the
38 * center, +1 the fall (PhasePulse).
39 * - index, the PM depth, bipolar: index_cycles() maps it to cycles of phase
40 * offset per unit of the PM signal, MAX_INDEX x|x| at full scale, reduced
41 * with pitch above TAMING_CORNER as Plaits reduces its FM amount
42 * (plaits/dsp/engine/fm_engine.cc:76-78). It is not clamped; past 1 the
43 * depth keeps growing, under the core's per-frame bound.
44 * - phase_mod, the PM signal, a block, nominally bipolar and not clamped.
45 * The core's phase offset is the index in cycles times it, given at
46 * SUBSTEPS steps a frame: a SincUpsampler interpolates the signal between
47 * frames, PM_LATENCY frames late, so an offset that curves within a frame is
48 * followed closely. Left off, there is no PM.
49 *
50 * Pitch, width, anchor and index as blocks are used as given, one value per
51 * frame. As scalars each moves to its value over RAMP_FRAMES; the first
52 * scalar a port is given jumps. A port left off holds and a ramp in progress
53 * continues; phase_mod left off is zero.
54 *
55 * Phase is linear within each step of a frame: an edge is
56 * timed on the segment between two steps' phases, so a pulse born and closed
57 * within a step is not seen. Without modulation the core takes the frame as one
58 * segment. The step, the offset's change included, is bounded at the core's
59 * MAX_STEP in each part; with the reduction, a sine at up to twice
60 * the pitch stays inside it at full index at every pitch.
61 *
62 * Tune is a parameter: the frequency at zero pitch in cycles per frame, set
63 * once by the app from Hz and the target's sample rate, so the component
64 * holds no sample rate. TAMING_CORNER is in cycles per frame for
65 * the same reason, 1.05 kHz at 48 kHz: an octave under Plaits' corner.
66 *
67 * Out-of-range port values are handled by the core; a non-finite pitch or
68 * index counts as 0, and a non-finite PM sample holds the last finite one.
69 * Output is the core's, nominally [-1, 1], LATENCY frames late, with the DC
70 * the width and the modulation carry; an app follows it with a DcBlock. On
71 * the default kernel a step overshoots by 8.2 % of its height, so a pulse
72 * reaches 1.16.
73 * Output does not depend on block size: every ramp is a fixed number of
74 * frames.
75 */
76class PmPulse {
77public:
78 using Core = PhasePulse<>; /**< the core, on the default kernel */
79
80 static constexpr uint8_t LATENCY = Core::LATENCY; /**< frames the output trails */
81 static constexpr float MAX_FREQUENCY = PM_MAX_FREQUENCY; /**< cycles per frame */
82 static constexpr float MAX_INDEX = PM_MAX_INDEX; /**< cycles of offset per unit of PM at index 1 */
83 static constexpr float TAMING_CORNER = PM_TAMING_CORNER; /**< cycles per frame */
84 static constexpr float TAMING_PER_OCTAVE = PM_TAMING_PER_OCTAVE; /**< as pm_taming() */
85 static constexpr uint16_t RAMP_FRAMES = 64; /**< a scalar port arrives after this many frames */
86 static constexpr uint8_t SUBSTEPS = PM_SUBSTEPS; /**< offsets given each frame */
87 static constexpr uint8_t PM_LATENCY = sbl::dsp::PM_LATENCY; /**< frames the PM signal trails */
88
89 /** @brief No Tune and no PM until set; every scalar port ramps over RAMP_FRAMES. */
91 for (Ramp* r : {&pitch_, &width_, &anchor_, &index_}) {
92 r->set_length(RAMP_FRAMES);
93 }
94 }
95
96 /**
97 * @brief The PM depth in cycles for an index and a pitch
98 *
99 * @param index The index port's value; bipolar, the sign kept
100 * @param octaves_above_corner The pitch in octaves above TAMING_CORNER; below it there is no reduction
101 * @return pm_depth(index) * pm_taming(octaves_above_corner), in cycles per unit of PM
102 */
103 static float index_cycles(float index, float octaves_above_corner) {
104 return pm_depth(index) * pm_taming(octaves_above_corner);
105 }
106
107 /**
108 * @brief Parameter: the frequency at zero pitch.
109 *
110 * @param cycles_per_frame Tune in cycles per frame; non-finite or negative counts as 0
111 * @context Same thread as process(); real-time safe.
112 */
113 void set_tune(float cycles_per_frame) {
114 tune_ = sbl::math::finite_or(cycles_per_frame, 0.0f);
115 tune_ = tune_ < 0.0f ? 0.0f : tune_;
116 corner_octaves_ = tune_ > 0.0f ? math::octaves_from_ratio(tune_ / TAMING_CORNER) : BELOW_ANY_CORNER;
117 }
118
119 /** @brief The ports, in the order process() takes them. */
120 static constexpr transport::Port PORTS[] = {
121 {"pitch"}, {"width"}, {"anchor"}, {"index"}, {"phase_mod", transport::Spelling::Block},
122 };
123
124 /** @brief The parameters, for tools. */
128
129 /**
130 * @brief One block, the ports in PORTS' order, any trailing ones left off.
131 *
132 * @tparam Ports A block or an arithmetic scalar for each of pitch, width, anchor and index; a block for phase_mod
133 * @param[out] out Output buffer, `frames` long
134 * @param frames Frame count
135 * @param ports pitch in octaves over Tune; width in [0, 1]; anchor in [-1, 1]; index, bipolar; phase_mod, the PM signal
136 * @context Audio thread; real-time safe.
137 */
138 template<class... Ports>
139 void process(float* out, uint16_t frames, Ports... ports) {
140 static_assert(transport::fits_v<PmPulse, Ports...>,
141 "SBL ERROR: PmPulse takes pitch, width, anchor and index (blocks or scalars), then phase_mod "
142 "(a block)");
143 const auto pitch = transport::arg<0>(ports...);
144 const auto width = transport::arg<1>(ports...);
145 const auto anchor = transport::arg<2>(ports...);
146 const auto index = transport::arg<3>(ports...);
147 const auto phase_mod = transport::arg<4>(ports...);
148 aim(pitch_, pitch);
149 aim(width_, width);
150 aim(anchor_, anchor);
151 aim(index_, index);
152 // A settled scalar pitch needs its frequency once a block, not an exp2 a frame.
153 const bool settled = !transport::is_block_v<decltype(pitch)> && !pitch_.ramping();
154 const float settled_pitch = sbl::math::finite_or(pitch_.value(), 0.0f);
155 const float settled_step = settled ? accumulator_.set_step(frequency_for(settled_pitch)) : 0.0f;
156 for (uint16_t i = 0; i < frames; ++i) {
157 const float p = settled ? settled_pitch : sbl::math::finite_or(at(pitch, pitch_, i), 0.0f);
158 const float f = settled ? settled_step : accumulator_.set_step(frequency_for(p));
159 const float phase_now = accumulator_.advance();
160 frequency_ = f;
161 core_.set_width(at(width, width_, i));
162 core_.set_anchor(at(anchor, anchor_, i));
163 const float depth = index_cycles(at(index, index_, i), p + corner_octaves_);
164 float m[SUBSTEPS];
165 if constexpr (transport::is_block_v<decltype(phase_mod)>) {
166 input_.process(phase_mod[i], m);
167 } else {
168 input_.process(m);
169 }
170 out[i] = render_frame(phase_now, f, depth, m);
171 }
172 settle(pitch_, pitch, frames);
173 settle(width_, width, frames);
174 settle(anchor_, anchor, frames);
175 settle(index_, index, frames);
176 }
177
178 /** @brief Phase and offset to zero, the PM signal's history cleared, every ramp at its target, corrections dropped. */
179 void reset() {
180 accumulator_.reset();
181 input_.reset();
182 core_.reset();
183 for (Ramp* r : {&pitch_, &width_, &anchor_, &index_}) {
184 r->jump(r->target());
185 }
186 }
187
188 /** @brief The frequency of the last frame, cycles per frame, as the accumulator took it. */
189 float frequency() const { return frequency_; }
190
191 /** @brief The accumulator's phase after the last frame, before the offset. */
192 float phase() const { return accumulator_.phase(); }
193
194private:
195 static constexpr float BELOW_ANY_CORNER = -1024.0f; /**< octaves: with no Tune, no reduction */
196
197 float frequency_for(float pitch) const { return pm_frequency(tune_, pitch); }
198
199 /** One frame of the core: its offset at each step, or one segment while there is no modulation. */
200 float render_frame(float phase_now, float f, float depth, const float (&m)[SUBSTEPS]) {
201 float offsets[SUBSTEPS];
202 bool modulated = false;
203 for (uint8_t k = 0; k < SUBSTEPS; ++k) {
204 offsets[k] = depth * m[k];
205 modulated = modulated || offsets[k] != 0.0f;
206 }
207 return modulated ? core_.process(phase_now, f, offsets) : core_.process(phase_now, f, 0.0f);
208 }
209
210 /** A scalar heads for its value over RAMP_FRAMES; its first value jumps. */
211 template<class A>
212 static void aim(Ramp& ramp, A arg) {
213 if constexpr (transport::is_scalar_v<A>) {
214 ramp.head(static_cast<float>(arg));
215 }
216 }
217
218 /** A port's value this frame: a block's own, or the ramp's, which holds when the port is left off. */
219 template<class A>
220 static float at(A arg, Ramp& ramp, uint16_t i) {
221 if constexpr (transport::is_block_v<A>) {
222 return arg[i];
223 } else {
224 return ramp.step();
225 }
226 }
227
228 /** After a block, the ramp waits at its last value, so a later scalar moves from there. */
229 template<class A>
230 static void settle(Ramp& ramp, A arg, uint16_t frames) {
231 if constexpr (transport::is_block_v<A>) {
232 if (frames > 0) {
233 ramp.jump(arg[frames - 1]);
234 }
235 }
236 }
237
238 float tune_ = 0.0f;
239 float corner_octaves_ = BELOW_ANY_CORNER; /**< log2(Tune / TAMING_CORNER): pitch plus this is octaves above */
240 float frequency_ = 0.0f;
241 Ramp pitch_{0.0f};
242 Ramp width_{0.5f};
243 Ramp anchor_{0.0f};
244 Ramp index_{0.0f};
245 PhaseAccumulator accumulator_{};
246 PmInput input_{};
247 Core core_{};
248};
249
250} // namespace sbl::dsp
251
252#endif // SBL_DSP_OSCILLATORS_PM_PULSE_HPP_
Clamping.
float advance()
One frame: advance by the step.
float set_step(float cycles_per_frame)
Set the step each later advance() takes.
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.
A band-limited pulse driven by a signed phase, its width placed by a bipolar anchor.
float process(float phase, float step, float offset)
One frame: the corrected sample LATENCY frames back.
void reset(float phase=0.0f, float offset=0.0f)
Move to a phase plus an offset; width and anchor to their targets, corrections dropped.
void set_anchor(float anchor)
Set where width acts. Takes effect next frame as moving edges.
void set_width(float fraction_of_cycle)
Set the pulse width. Takes effect next frame as moving edges.
static constexpr uint8_t LATENCY
void process(float sample, float(&values)[PM_SUBSTEPS])
One frame: a sample in, the frame's values out, the last at the frame's end.
Definition pm.hpp:87
void reset()
The held sample and the upsampler's history to zero.
Definition pm.hpp:101
A pulse oscillator with phase modulation: pitch, width, anchor, index and the PM signal over a PhaseP...
Definition pm_pulse.hpp:76
static constexpr uint8_t SUBSTEPS
Definition pm_pulse.hpp:86
static constexpr float TAMING_PER_OCTAVE
Definition pm_pulse.hpp:84
void reset()
Phase and offset to zero, the PM signal's history cleared, every ramp at its target,...
Definition pm_pulse.hpp:179
static constexpr transport::Port PORTS[]
The ports, in the order process() takes them.
Definition pm_pulse.hpp:120
static constexpr uint8_t PM_LATENCY
Definition pm_pulse.hpp:87
float frequency() const
The frequency of the last frame, cycles per frame, as the accumulator took it.
Definition pm_pulse.hpp:189
static constexpr float MAX_INDEX
Definition pm_pulse.hpp:82
static constexpr float TAMING_CORNER
Definition pm_pulse.hpp:83
float phase() const
The accumulator's phase after the last frame, before the offset.
Definition pm_pulse.hpp:192
void process(float *out, uint16_t frames, Ports... ports)
One block, the ports in PORTS' order, any trailing ones left off.
Definition pm_pulse.hpp:139
static constexpr uint16_t RAMP_FRAMES
Definition pm_pulse.hpp:85
static constexpr float MAX_FREQUENCY
Definition pm_pulse.hpp:81
PmPulse()
No Tune and no PM until set; every scalar port ramps over RAMP_FRAMES.
Definition pm_pulse.hpp:90
PhasePulse<> Core
Definition pm_pulse.hpp:78
static constexpr uint8_t LATENCY
Definition pm_pulse.hpp:80
static float index_cycles(float index, float octaves_above_corner)
The PM depth in cycles for an index and a pitch.
Definition pm_pulse.hpp:103
static constexpr transport::Parameter< PmPulse > PARAMETERS[]
The parameters, for tools.
Definition pm_pulse.hpp:125
void set_tune(float cycles_per_frame)
Parameter: the frequency at zero pitch.
Definition pm_pulse.hpp:113
A linear ramp to a target over a fixed number of frames.
Definition ramp.hpp:25
float value() const
The current value.
Definition ramp.hpp:101
bool ramping() const
True while a ramp is in progress.
Definition ramp.hpp:105
Finiteness of a float, read from its bits, and a fallback for a value that is not finite.
float octaves_from_ratio(float ratio)
The interval of a frequency ratio, in octaves: log2(ratio).
Definition pitch.hpp:43
The audio stack.
Definition dry_wet.hpp:15
constexpr float PM_TAMING_CORNER
Cycles per frame: where pm_taming()'s reduction starts, 1.05 kHz at 48 kHz.
Definition pm.hpp:26
constexpr uint8_t PM_SUBSTEPS
Definition pm.hpp:28
float pm_frequency(float tune, float pitch)
The step for a pitch in octaves over a tune.
Definition pm.hpp:38
constexpr float PM_MAX_FREQUENCY
Definition pm.hpp:23
float pm_taming(float octaves_above_corner)
The share of the depth kept at a pitch over PM_TAMING_CORNER.
Definition pm.hpp:67
float pm_depth(float index)
Cycles of phase offset per unit of the PM signal for a bipolar index.
Definition pm.hpp:50
constexpr float PM_TAMING_PER_OCTAVE
Definition pm.hpp:27
constexpr uint8_t PM_LATENCY
Definition pm.hpp:29
constexpr float PM_MAX_INDEX
Definition pm.hpp:24
float finite_or(float x, float fallback)
x, or fallback when x is an infinity or a NaN.
Definition finite.hpp:36
constexpr bool fits_v
constexpr auto arg(A... args)
A phase advanced by a signed step each frame, held as a 32-bit count of the cycle.
A band-limited pulse driven by a signed phase, its width placed by a bipolar anchor.
Pitch as octaves and ratios: from an interval to a frequency and back.
The parts of a phase-modulated oscillator: the pitch's step, the index's depth, the PM input.
A linear ramp to a target over a fixed number of frames.
The processing call's shape as a checked contract.