6#ifndef SBL_DSP_OSCILLATORS_BLEP_OSC_HPP_
7#define SBL_DSP_OSCILLATORS_BLEP_OSC_HPP_
11#include <initializer_list>
74template<
class Kernel = math::Sinc16>
80 static constexpr uint8_t
LATENCY = Kernel::LATENCY;
84 for (
Ramp* r : {&saw_, &pulse_, &triangle_, &peak_, &knee_}) {
141 void set_blend(
float saw,
float pulse,
float triangle) {
154 const float f = frequency_;
155 const float p0 = phase_;
157 const bool wrapped = p >= 1.0f;
163 const float ws = saw_.
step();
164 const float wp = pulse_.
step();
165 const float wt = triangle_.
step();
166 const float w_prev = width_;
167 const float k_prev = peak_now_;
168 const float c_prev = knee_now_;
177 const float triangle_corner = 2.0f / k + 2.0f / (1.0f - k);
178 const float saw_corner = 1.0f / (1.0f - c) - 1.0f / c;
184 corrector_.step(t, ws * -2.0f);
185 corrector_.corner(t, ws * f * -saw_corner);
186 corrector_.step(t, wp * -2.0f);
187 corrector_.corner(t, wt * f * triangle_corner);
189 const bool low_before = p0 < w_prev;
190 const bool low_after = p < w;
191 if (low_before != low_after) {
193 corrector_.step(t, wp * (low_after ? -2.0f : 2.0f));
195 const bool rising_before = p0 < k_prev;
196 const bool rising_after = p < k;
197 if (rising_before != rising_after) {
199 corrector_.corner(t, wt * f * (rising_after ? triangle_corner : -triangle_corner));
201 const bool fast_before = p0 < c_prev;
202 const bool fast_after = p < c;
203 if (fast_before != fast_after) {
205 corrector_.corner(t, ws * f * (fast_after ? -saw_corner : saw_corner));
209 const float saw = p < c ? (-1.0f + p / c) : ((p - c) / (1.0f - c));
210 const float pulse = p < w ? -1.0f : 1.0f;
211 const float triangle = p < k ? (-1.0f + 2.0f * p / k) : (1.0f - 2.0f * (p - k) / (1.0f - k));
212 return corrector_.process(ws * saw + wp * pulse + wt * triangle);
241 template<
class... Ports>
242 void process(
float* out, uint16_t frames, Ports... ports) {
244 "SBL ERROR: BlepOsc takes frequency and pulse_width (blocks or scalars), then saw_knee, saw, "
245 "pulse, triangle and triangle_peak (scalars)");
246 const auto frequency = transport::arg<0>(ports...);
247 const auto width = transport::arg<1>(ports...);
248 const auto knee = transport::arg<2>(ports...);
249 const auto saw = transport::arg<3>(ports...);
250 const auto pulse = transport::arg<4>(ports...);
251 const auto triangle = transport::arg<5>(ports...);
252 const auto peak = transport::arg<6>(ports...);
253 if constexpr (transport::is_scalar_v<
decltype(knee)>) {
256 if constexpr (transport::is_scalar_v<
decltype(saw)>) {
259 if constexpr (transport::is_scalar_v<
decltype(pulse)>) {
262 if constexpr (transport::is_scalar_v<
decltype(triangle)>) {
265 if constexpr (transport::is_scalar_v<
decltype(peak)>) {
268 if constexpr (transport::is_scalar_v<
decltype(
frequency)>) {
271 if constexpr (transport::is_scalar_v<
decltype(width)>) {
274 for (uint16_t i = 0; i < frames; ++i) {
275 if constexpr (transport::is_block_v<
decltype(
frequency)>) {
278 if constexpr (transport::is_block_v<
decltype(width)>) {
293 width_ = width_target_;
294 peak_now_ = peak_.
value();
295 knee_now_ = knee_.
value();
300 float phase()
const {
return phase_; }
306 float frequency_ = 0.0f;
307 float width_target_ = 0.5f;
309 float peak_now_ = 0.5f;
310 float knee_now_ = 0.5f;
313 Ramp triangle_{0.0f};
316 BlepCorrector<Kernel> corrector_{};
The correction buffer behind a band-limited edge, over a BLEP kernel.
The BLEP oscillator core: one phase, three waveform parts, band-limited edges.
void set_pulse_width(float fraction_of_cycle)
Set where the pulse's rising edge falls in the cycle. Takes effect next frame as a moving edge.
void set_blend(float saw, float pulse, float triangle)
Set the weights of the three parts in the sum. Ramp over RAMP_FRAMES.
BlepOsc()
A saw at zero frequency until set; blend, knee and peak changes ramp over RAMP_FRAMES.
static constexpr transport::Port PORTS[]
The ports, in the order the buffer call takes them; each sets the quantity of the setter of its name.
static constexpr uint16_t RAMP_FRAMES
float frequency() const
The frequency, in cycles per frame.
void set_frequency(float cycles_per_frame)
Set the frequency. Takes effect at the next frame.
float phase() const
The phase after the last frame, in [0, 1).
static constexpr uint8_t LATENCY
void set_saw_knee(float fraction_of_cycle)
Set where the saw's phase bends. Ramps over RAMP_FRAMES.
static constexpr float MIN_BREAKPOINT
float process()
One frame.
void set_triangle_peak(float fraction_of_cycle)
Set where the triangle peaks within the cycle. Ramps over RAMP_FRAMES.
static constexpr float MAX_FREQUENCY
void reset()
Phase to zero, ramps to their targets, corrections dropped.
void process(float *out, uint16_t frames, Ports... ports)
A buffer of frames, the ports in PORTS' order, any trailing ones left off.
A linear ramp to a target over a fixed number of frames.
float target() const
The value the ramp heads for.
void set(float target)
Head for a target from the current value.
float value() const
The current value.
void jump(float value)
Arrive at a value at once.
Finiteness of a float, read from its bits, and a fallback for a value that is not finite.
constexpr float polyblep_time(float before, float after, float threshold_before, float threshold_after)
Sub-frame time at which a linear segment crossed a moving threshold.
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.
Polynomial BLEP kernels, and the sub-frame time of a crossing.
A linear ramp to a target over a fixed number of frames.
The processing call's shape as a checked contract.