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
vco.hpp
Go to the documentation of this file.
1/**
2 * @file vco.hpp
3 * @brief An oscillator: Tune, and pitch, Wave and Shape as ports over a BlepOsc
4 */
5
6#ifndef SBL_DSP_OSCILLATORS_VCO_HPP_
7#define SBL_DSP_OSCILLATORS_VCO_HPP_
8
9#include <cmath>
10#include <cstdint>
15#include <sbl/math/clamp.hpp>
16#include <sbl/math/finite.hpp>
17
18namespace sbl::dsp {
19
20/**
21 * @brief An oscillator: Tune, and pitch, Wave and Shape as ports over a BlepOsc
22 *
23 * A component on the signal transport. It owns a BlepOsc<> and makes the one
24 * conversion from each port to the core's natural units. Its ports are
25 * process() arguments after the frame count, in the order PORTS declares
26 * (common/transport.hpp):
27 *
28 * - pitch, octaves relative to Tune, the exponential pitch path: Tune, the
29 * V/Oct input, the MIDI offset and modulation add in octaves, and this is
30 * where the sum becomes a frequency. As a scalar it moves to its value
31 * over RAMP_FRAMES, linearly in octaves, so a stepped control glides
32 * rather than steps. As a block it is used as given, one frequency per
33 * frame: the audio-rate FM path.
34 * - wave, in [0, 1], morphs from the saw at 0 to the pulse at 1. The blend
35 * of the two parts follows pulse_amount(), which takes the saw's even
36 * harmonics down 40 dB per unit of travel.
37 * - shape, in [0, 1], is phase distortion: 0 is the plain waveform, 1 the
38 * strongest bend. It moves the saw's knee and the pulse's width together
39 * from 0.5 down to the core's minimum along knee_for(), halving the knee
40 * every quarter turn, so one control means one thing across the morph.
41 * Wave and Shape are scalars and ramp inside the core over its RAMP_FRAMES.
42 *
43 * Tune is a parameter: the frequency at zero pitch in cycles per frame, set
44 * once by the app from Hz and the target's sample rate, so the component
45 * holds no sample rate.
46 *
47 * Out-of-range port values are clamped; a non-finite value counts as 0.
48 * Output is the core's, nominally [-1, 1] with the overshoot of its kernel
49 * (polyblep.hpp), LATENCY frames late, with the DC
50 * the bend and the pulse carry. Output does
51 * not depend on block size: every ramp is a fixed number of frames.
52 */
53class Vco {
54public:
55 using Core = BlepOsc<>; /**< the core, on the default kernel */
56
57 static constexpr uint8_t LATENCY = Core::LATENCY; /**< frames the output trails */
58 static constexpr float MIN_KNEE = Core::MIN_BREAKPOINT; /**< the knee and width at Shape 1 */
59 static constexpr float WAVE_EVENS_DB_PER_UNIT = 40.0f; /**< how far the saw's even harmonics fall across Wave */
60 static constexpr float SHAPE_OCTAVES = 4.0f; /**< the knee halves this many times across Shape: 0.5 to 1/32 */
61 static constexpr uint16_t RAMP_FRAMES = Core::RAMP_FRAMES; /**< a scalar pitch arrives after this many frames */
62 static constexpr uint16_t CHUNK_FRAMES = 32; /**< frames of per-frame frequency rendered at a time */
63
64 /** @brief No Tune until set; a scalar pitch ramps over RAMP_FRAMES. */
65 Vco() { pitch_.set_length(RAMP_FRAMES); }
66
67 /**
68 * @brief The pulse's weight for a Wave setting, with the saw's weight 1 minus it.
69 *
70 * The saw's even harmonics scale by (1 - w) * 10^(-2w): 40 dB per unit,
71 * and gone at 1.
72 *
73 * @param wave Wave, clamped to [0, 1]; non-finite counts as 0
74 * @return The pulse's weight, in [0, 1]
75 */
76 static float pulse_amount(float wave) {
77 const float w = sbl::math::clamp01(sbl::math::finite_or(wave, 0.0f));
78 const float evens = (1.0f - w) * std::exp2f(-w * WAVE_EVENS_DB_PER_UNIT / 6.0206f); // 10^(-2w)
79 return (1.0f - evens) / (1.0f + evens);
80 }
81
82 /**
83 * @brief The saw's knee and the pulse's width for a Shape setting: 0.5 halved SHAPE_OCTAVES times across the knob.
84 *
85 * @param shape Shape, clamped to [0, 1]; non-finite counts as 0
86 * @return The knee, in [MIN_KNEE, 0.5]
87 */
88 static float knee_for(float shape) {
89 return 0.5f * std::exp2f(-SHAPE_OCTAVES * sbl::math::clamp01(sbl::math::finite_or(shape, 0.0f)));
90 }
91
92 /**
93 * @brief Parameter: the frequency at zero pitch.
94 *
95 * @param cycles_per_frame Tune in cycles per frame; non-finite or negative counts as 0
96 * @context Same thread as process(); real-time safe.
97 */
98 void set_tune(float cycles_per_frame) {
99 tune_ = sbl::math::finite_or(cycles_per_frame, 0.0f);
100 if (tune_ < 0.0f) {
101 tune_ = 0.0f;
102 }
103 }
104
105 /** @brief The ports, in the order process() takes them. */
106 static constexpr transport::Port PORTS[] = {
107 {"pitch"},
110 };
111
112 /** @brief The parameters, for tools. */
116
117 /**
118 * @brief One block, the ports in PORTS' order, any trailing ones left off
119 *
120 * Pitch as a block is used as given, one frequency per frame. As a scalar
121 * it moves to its value over RAMP_FRAMES; the first scalar jumps. Wave and
122 * Shape ramp inside the core. A port left off holds its last value, and
123 * ramps in progress continue.
124 *
125 * @tparam Ports pitch as a block or an arithmetic scalar, then wave and shape as scalars
126 * @param[out] out Output buffer, `frames` long
127 * @param frames Frame count
128 * @param ports pitch in octaves over Tune; wave in [0, 1]; shape in [0, 1]
129 * @context Audio thread; real-time safe.
130 */
131 template<class... Ports>
132 void process(float* out, uint16_t frames, Ports... ports) {
133 static_assert(transport::fits_v<Vco, Ports...>,
134 "SBL ERROR: Vco takes pitch (a block or a scalar), then wave and shape (scalars)");
135 const auto pitch = transport::arg<0>(ports...);
136 const auto wave = transport::arg<1>(ports...);
137 const auto shape = transport::arg<2>(ports...);
138 if constexpr (transport::is_scalar_v<decltype(wave)>) {
139 const float pulse = pulse_amount(static_cast<float>(wave));
140 osc_.set_blend(1.0f - pulse, pulse, 0.0f);
141 }
142 if constexpr (transport::is_scalar_v<decltype(shape)>) {
143 const float knee = knee_for(static_cast<float>(shape));
144 osc_.set_saw_knee(knee);
145 osc_.set_pulse_width(knee);
146 }
147 if constexpr (transport::is_block_v<decltype(pitch)>) {
148 render(out, pitch, frames);
149 } else {
150 if constexpr (transport::is_scalar_v<decltype(pitch)>) {
151 aim(static_cast<float>(pitch));
152 }
153 render(out, frames);
154 }
155 }
156
157 /** @brief Phase to zero, every ramp at its target, corrections dropped. */
158 void reset() {
159 osc_.reset();
160 pitch_.jump(pitch_.target());
161 }
162
163 /** @brief The core's frequency after the last process(), cycles per frame. */
164 float frequency() const { return osc_.frequency(); }
165
166 /** @brief The pitch the ramp is at, octaves. */
167 float pitch() const { return pitch_.value(); }
168
169private:
170 static uint16_t chunk(uint16_t left) { return left < CHUNK_FRAMES ? left : CHUNK_FRAMES; }
171
172 /** Pitch heads for its value over RAMP_FRAMES; the first value jumps. */
173 void aim(float pitch) { pitch_.head(sbl::math::finite_or(pitch, 0.0f)); }
174
175 float frequency_for(float pitch) const { return math::frequency(tune_, sbl::math::finite_or(pitch, 0.0f)); }
176
177 /** Pitch as a block, used as given; the ramp is left where the block ends. */
178 void render(float* out, const float* pitch, uint16_t frames) {
179 uint16_t pos = 0;
180 while (pos < frames) {
181 const uint16_t n = chunk(frames - pos);
182 for (uint16_t i = 0; i < n; ++i) {
183 frequency_[i] = frequency_for(pitch[pos + i]);
184 }
185 osc_.process(out + pos, n, frequency_);
186 pos += n;
187 }
188 if (frames > 0) {
189 pitch_.jump(sbl::math::finite_or(pitch[frames - 1], 0.0f));
190 }
191 }
192
193 /** Per-frame frequency while the pitch ramp moves, the core's own frequency once it has arrived. */
194 void render(float* out, uint16_t frames) {
195 uint16_t pos = 0;
196 while (pos < frames && pitch_.ramping()) {
197 const uint16_t n = chunk(frames - pos);
198 for (uint16_t i = 0; i < n; ++i) {
199 frequency_[i] = frequency_for(pitch_.step());
200 }
201 osc_.process(out + pos, n, frequency_);
202 pos += n;
203 }
204 if (pos < frames) {
205 osc_.set_frequency(frequency_for(pitch_.value()));
206 osc_.process(out + pos, static_cast<uint16_t>(frames - pos));
207 }
208 }
209
210 float tune_ = 0.0f;
211 Ramp pitch_{0.0f};
212 float frequency_[CHUNK_FRAMES]{};
213 Core osc_{};
214};
215
216} // namespace sbl::dsp
217
218#endif // SBL_DSP_OSCILLATORS_VCO_HPP_
The BLEP oscillator core: one phase, three waveform parts, band-limited edges.
Clamping.
The BLEP oscillator core: one phase, three waveform parts, band-limited edges.
Definition blep_osc.hpp:75
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.
Definition blep_osc.hpp:106
void set_blend(float saw, float pulse, float triangle)
Set the weights of the three parts in the sum. Ramp over RAMP_FRAMES.
Definition blep_osc.hpp:141
static constexpr uint16_t RAMP_FRAMES
Definition blep_osc.hpp:79
float frequency() const
The frequency, in cycles per frame.
Definition blep_osc.hpp:302
void set_frequency(float cycles_per_frame)
Set the frequency. Takes effect at the next frame.
Definition blep_osc.hpp:95
static constexpr uint8_t LATENCY
Definition blep_osc.hpp:80
void set_saw_knee(float fraction_of_cycle)
Set where the saw's phase bends. Ramps over RAMP_FRAMES.
Definition blep_osc.hpp:117
static constexpr float MIN_BREAKPOINT
Definition blep_osc.hpp:78
float process()
One frame.
Definition blep_osc.hpp:153
void reset()
Phase to zero, ramps to their targets, corrections dropped.
Definition blep_osc.hpp:286
float step()
One frame.
Definition ramp.hpp:90
float target() const
The value the ramp heads for.
Definition ramp.hpp:103
void head(float target)
set(), except that the first value given since construction, by head() or jump(), is arrived at at on...
Definition ramp.hpp:64
float value() const
The current value.
Definition ramp.hpp:101
void jump(float value)
Arrive at a value at once.
Definition ramp.hpp:77
void set_length(uint16_t frames)
Set how many frames a ramp takes.
Definition ramp.hpp:39
bool ramping() const
True while a ramp is in progress.
Definition ramp.hpp:105
An oscillator: Tune, and pitch, Wave and Shape as ports over a BlepOsc.
Definition vco.hpp:53
void set_tune(float cycles_per_frame)
Parameter: the frequency at zero pitch.
Definition vco.hpp:98
Vco()
No Tune until set; a scalar pitch ramps over RAMP_FRAMES.
Definition vco.hpp:65
static constexpr float MIN_KNEE
Definition vco.hpp:58
void process(float *out, uint16_t frames, Ports... ports)
One block, the ports in PORTS' order, any trailing ones left off.
Definition vco.hpp:132
static constexpr transport::Port PORTS[]
The ports, in the order process() takes them.
Definition vco.hpp:106
float pitch() const
The pitch the ramp is at, octaves.
Definition vco.hpp:167
static float knee_for(float shape)
The saw's knee and the pulse's width for a Shape setting: 0.5 halved SHAPE_OCTAVES times across the k...
Definition vco.hpp:88
static constexpr transport::Parameter< Vco > PARAMETERS[]
The parameters, for tools.
Definition vco.hpp:113
static constexpr uint8_t LATENCY
Definition vco.hpp:57
static constexpr float WAVE_EVENS_DB_PER_UNIT
Definition vco.hpp:59
static constexpr uint16_t CHUNK_FRAMES
Definition vco.hpp:62
void reset()
Phase to zero, every ramp at its target, corrections dropped.
Definition vco.hpp:158
BlepOsc<> Core
Definition vco.hpp:55
float frequency() const
The core's frequency after the last process(), cycles per frame.
Definition vco.hpp:164
static float pulse_amount(float wave)
The pulse's weight for a Wave setting, with the saw's weight 1 minus it.
Definition vco.hpp:76
static constexpr float SHAPE_OCTAVES
Definition vco.hpp:60
static constexpr uint16_t RAMP_FRAMES
Definition vco.hpp:61
Finiteness of a float, read from its bits, and a fallback for a value that is not finite.
float frequency(float reference, float octaves)
A reference frequency moved by an interval in octaves.
Definition pitch.hpp:52
The audio stack.
Definition dry_wet.hpp:15
float finite_or(float x, float fallback)
x, or fallback when x is an infinity or a NaN.
Definition finite.hpp:36
constexpr T clamp01(T x)
x held to [0, 1]; a NaN goes to 0.
Definition clamp.hpp:35
constexpr bool fits_v
Pitch as octaves and ratios: from an interval to a frequency and back.
A linear ramp to a target over a fixed number of frames.
The processing call's shape as a checked contract.