6#ifndef SBL_DSP_OSCILLATORS_PM_PULSE_HPP_
7#define SBL_DSP_OSCILLATORS_PM_PULSE_HPP_
91 for (
Ramp* r : {&pitch_, &width_, &anchor_, &index_}) {
115 tune_ = tune_ < 0.0f ? 0.0f : tune_;
138 template<
class... Ports>
139 void process(
float* out, uint16_t frames, Ports... ports) {
141 "SBL ERROR: PmPulse takes pitch, width, anchor and index (blocks or scalars), then phase_mod "
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...);
150 aim(anchor_, anchor);
153 const bool settled = !transport::is_block_v<
decltype(pitch)> && !pitch_.
ramping();
155 const float settled_step = settled ? accumulator_.
set_step(frequency_for(settled_pitch)) : 0.0f;
156 for (uint16_t i = 0; i < frames; ++i) {
158 const float f = settled ? settled_step : accumulator_.
set_step(frequency_for(p));
159 const float phase_now = accumulator_.
advance();
163 const float depth =
index_cycles(at(index, index_, i), p + corner_octaves_);
165 if constexpr (transport::is_block_v<
decltype(phase_mod)>) {
166 input_.
process(phase_mod[i], m);
170 out[i] = render_frame(phase_now, f, depth, m);
172 settle(pitch_, pitch, frames);
173 settle(width_, width, frames);
174 settle(anchor_, anchor, frames);
175 settle(index_, index, frames);
180 accumulator_.
reset();
183 for (
Ramp* r : {&pitch_, &width_, &anchor_, &index_}) {
184 r->jump(r->target());
195 static constexpr float BELOW_ANY_CORNER = -1024.0f;
197 float frequency_for(
float pitch)
const {
return pm_frequency(tune_, pitch); }
200 float render_frame(
float phase_now,
float f,
float depth,
const float (&m)[
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;
207 return modulated ? core_.
process(phase_now, f, offsets) : core_.
process(phase_now, f, 0.0f);
212 static void aim(Ramp& ramp, A arg) {
213 if constexpr (transport::is_scalar_v<A>) {
214 ramp.head(
static_cast<float>(arg));
220 static float at(A arg, Ramp& ramp, uint16_t i) {
221 if constexpr (transport::is_block_v<A>) {
230 static void settle(Ramp& ramp, A arg, uint16_t frames) {
231 if constexpr (transport::is_block_v<A>) {
233 ramp.jump(arg[frames - 1]);
239 float corner_octaves_ = BELOW_ANY_CORNER;
240 float frequency_ = 0.0f;
245 PhaseAccumulator accumulator_{};
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
A pulse oscillator with phase modulation: pitch, width, anchor, index and the PM signal over a PhaseP...
static constexpr uint8_t SUBSTEPS
static constexpr float TAMING_PER_OCTAVE
void reset()
Phase and offset to zero, the PM signal's history cleared, every ramp at its target,...
static constexpr transport::Port PORTS[]
The ports, in the order process() takes them.
static constexpr uint8_t PM_LATENCY
float frequency() const
The frequency of the last frame, cycles per frame, as the accumulator took it.
static constexpr float MAX_INDEX
static constexpr float TAMING_CORNER
float phase() const
The accumulator's phase after the last frame, before the offset.
void process(float *out, uint16_t frames, Ports... ports)
One block, the ports in PORTS' order, any trailing ones left off.
static constexpr uint16_t RAMP_FRAMES
static constexpr float MAX_FREQUENCY
PmPulse()
No Tune and no PM until set; every scalar port ramps over RAMP_FRAMES.
static constexpr uint8_t LATENCY
static float index_cycles(float index, float octaves_above_corner)
The PM depth in cycles for an index and a pitch.
static constexpr transport::Parameter< PmPulse > PARAMETERS[]
The parameters, for tools.
void set_tune(float cycles_per_frame)
Parameter: the frequency at zero pitch.
A linear ramp to a target over a fixed number of frames.
float value() const
The current value.
bool ramping() const
True while a ramp is in progress.
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).
constexpr float PM_TAMING_CORNER
Cycles per frame: where pm_taming()'s reduction starts, 1.05 kHz at 48 kHz.
constexpr uint8_t PM_SUBSTEPS
float pm_frequency(float tune, float pitch)
The step for a pitch in octaves over a tune.
constexpr float PM_MAX_FREQUENCY
float pm_taming(float octaves_above_corner)
The share of the depth kept at a pitch over PM_TAMING_CORNER.
float pm_depth(float index)
Cycles of phase offset per unit of the PM signal for a bipolar index.
constexpr float PM_TAMING_PER_OCTAVE
constexpr uint8_t PM_LATENCY
constexpr float PM_MAX_INDEX
float finite_or(float x, float fallback)
x, or fallback when x is an infinity or a NaN.
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.