6#ifndef SBL_DSP_OSCILLATORS_PHASE_PULSE_HPP_
7#define SBL_DSP_OSCILLATORS_PHASE_PULSE_HPP_
96template<
class Kernel = math::Sinc16>
100 static constexpr uint8_t
LATENCY = Kernel::LATENCY;
134 const float offsets[1] = {
offset};
155 template<u
int8_t SUBSTEPS>
156 float process(
float phase,
float step,
const float (&offsets)[SUBSTEPS]) {
157 return process(
phase, step, offsets, [](
float,
float) {});
178 template<u
int8_t SUBSTEPS,
class OnEdge>
179 float process(
float phase,
float step,
const float (&offsets)[SUBSTEPS], OnEdge&& on_edge) {
180 static_assert(SUBSTEPS > 0,
"SBL ERROR: a frame has at least one part");
181 const float rise0 =
rise(width_, anchor_);
182 const float fall0 =
fall(width_, anchor_);
183 width_ = width_target_;
184 anchor_ = anchor_target_;
185 const float rise1 =
rise(width_, anchor_);
186 const float fall1 =
fall(width_, anchor_);
187 float rise_from = rise0;
188 float fall_from = fall0;
191 const float rise_to = s.
last ? rise1 : rise0 + (rise1 - rise0) * s.
through;
192 const float fall_to = s.
last ? fall1 : fall0 + (fall1 - fall0) * s.
through;
193 edge(s, rise_from, rise_to, 2.0f, on_edge);
194 edge(s, fall_from, fall_to, -2.0f, on_edge);
198 level_ = naive(walk_.
phase(), rise1, fall1);
199 return corrector_.process(level_);
236 template<
class... Ports>
237 void process(
float* out, uint16_t frames, Ports... ports) {
239 "SBL ERROR: PhasePulse takes phase and step (blocks), then width, anchor and offset "
240 "(blocks or scalars)");
241 const auto phase = transport::arg<0>(ports...);
242 const auto step = transport::arg<1>(ports...);
243 const auto width = transport::arg<2>(ports...);
244 const auto anchor = transport::arg<3>(ports...);
245 const auto offset = transport::arg<4>(ports...);
246 if constexpr (transport::is_scalar_v<
decltype(
width)>) {
249 if constexpr (transport::is_scalar_v<
decltype(
anchor)>) {
252 for (uint16_t i = 0; i < frames; ++i) {
253 if constexpr (transport::is_block_v<
decltype(
width)>) {
256 if constexpr (transport::is_block_v<
decltype(
anchor)>) {
259 if constexpr (transport::is_block_v<
decltype(
offset)>) {
261 }
else if constexpr (transport::is_scalar_v<
decltype(
offset)>) {
277 width_ = width_target_;
278 anchor_ = anchor_target_;
280 level_ = naive(walk_.
phase(),
rise(width_, anchor_),
fall(width_, anchor_));
287 float level()
const {
return level_; }
291 float width()
const {
return width_target_; }
293 float anchor()
const {
return anchor_target_; }
332 template<
class OnEdge>
333 void edge(
const PhaseWalk::Segment& s,
float c0,
float c1,
float height, OnEdge& on_edge) {
335 const float change = direction > 0 ? height : -height;
336 corrector_.step(t, change);
343 float width_target_ = 0.5f;
345 float anchor_target_ = 0.0f;
346 float anchor_ = 0.0f;
347 float level_ = -1.0f;
349 BlepCorrector<Kernel> corrector_{};
The correction buffer behind a band-limited edge, over a BLEP kernel.
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.
float process(float phase, float step, const float(&offsets)[SUBSTEPS])
One frame in SUBSTEPS equal parts, the offset given at the end of each.
void process(float *out, uint16_t frames, Ports... ports)
A buffer of frames: phase and step per frame, then width, anchor and offset, which may be left off.
float process(float phase, float step, const float(&offsets)[SUBSTEPS], OnEdge &&on_edge)
The frame call above, reporting each edge the phase crossed.
float width() const
The width set for the next frame.
float anchor() const
The anchor set for the next frame.
float process(float phase, float step)
One frame with no offset: an offset carried before moves back to zero, as phase modulation.
static float advance(float phase, float step)
Advance a phase by a signed step and wrap it into [0, 1)
static constexpr float rise(float width, float anchor)
The phase where the pulse rises.
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.
static constexpr float MAX_STEP
float phase() const
The phase the core read last frame, the offset included.
void set_anchor(float anchor)
Set where width acts. Takes effect next frame as moving edges.
float offset() const
The offset the core read last frame, in cycles.
float level() const
The naive level at the end of the last frame, +1 or -1: the pulse before its corrections,...
void set_width(float fraction_of_cycle)
Set the pulse width. Takes effect next frame as moving edges.
static constexpr uint8_t LATENCY
static constexpr transport::Port PORTS[]
The ports, in the order the buffer call takes them; width and anchor set what their setters set.
static constexpr float fall(float width, float anchor)
The phase where the pulse falls.
static float wrap(float x)
Wrap into [0, 1); a value that rounds up to 1 is 0.
void reset(float phase=0.0f, float offset=0.0f)
Move to a phase plus an offset, the offset kept as the last part's.
static constexpr float MAX_STEP
void frame(float phase, float step, const float(&offsets)[SUBSTEPS], OnSegment &&on_segment)
One frame in SUBSTEPS equal parts, each given to on_segment as a Segment.
float phase() const
The phase the last part ended on, the offset included, in [0, 1).
static void crossings(const Segment &s, float c0, float c1, OnCrossing &&on_crossing)
Report every image of a threshold crossed by a segment while the threshold moved c0 -> c1.
float offset() const
The offset the last part ended on, in cycles.
Finiteness of a float, read from its bits, and a fallback for a value that is not finite.
float finite_or(float x, float fallback)
x, or fallback when x is an infinity or a NaN.
constexpr T clamp(T x, T lo, T hi)
x held to [lo, hi]; a NaN goes to lo.
A modulated phase walked frame by frame as segments, and the crossings of moving thresholds on them.
Polynomial BLEP kernels, and the sub-frame time of a crossing.
One part of a frame: the phase moving linearly from start to end plus wraps cycles.
The processing call's shape as a checked contract.