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
vca.hpp
Go to the documentation of this file.
1/**
2 * @file vca.hpp
3 * @brief An amplifier: gain, Level and Mod Amt as ports over a linear multiply
4 */
5
6#ifndef SBL_DSP_DYNAMICS_VCA_HPP_
7#define SBL_DSP_DYNAMICS_VCA_HPP_
8
9#include <cmath>
10#include <cstdint>
12#include <sbl/dsp/math/gain.hpp>
14#include <sbl/math/clamp.hpp>
15#include <sbl/math/finite.hpp>
16
17namespace sbl::dsp {
18
19/**
20 * @brief An amplifier: gain, Level and Mod Amt as ports over a linear multiply
21 *
22 * A linear multiplier with an exponential Level control, the two modes Moog
23 * gave the VCA in 1965 (pp. 203-204): the modulation input is linear, so an
24 * envelope's own shape is the loudness curve, and Level is exponential, so
25 * the knob is even in dB. Its ports are process() arguments after the frame
26 * count, in the order PORTS declares (common/transport.hpp):
27 *
28 * - gain, nominally unipolar [0, 1]: the modulation, an envelope. As a block
29 * it is used as given and not clamped; this is a four-quadrant multiplier,
30 * so a bipolar signal at Mod Amt 1 inverts the audio on its negative half
31 * (0-Coast manual p. 26, fig. 49). As a scalar it moves to its value over
32 * RAMP_FRAMES.
33 * - level, in [0, 1], through math::level_gain() over LEVEL_RANGE_DB:
34 * silence at 0, unity at 1. A scalar; the gain it maps to moves linearly
35 * over RAMP_FRAMES.
36 * - mod_amount, in [0, 1], blends the modulation in: the gain per frame is
37 * level * (1 - amount + amount * gain). At 0 the amplifier holds Level
38 * with the modulation ignored, Mavis's VCA MODE switch as a knob (Mavis
39 * manual p. 30); at 1 the modulation sets the whole gain. A scalar, ramped
40 * over RAMP_FRAMES.
41 *
42 * The first call with ports arrives at its values at once. Output does not
43 * depend on block size: every ramp is a fixed number of frames. In
44 * place is fine: out may be in.
45 */
46class Vca {
47public:
48 static constexpr float LEVEL_RANGE_DB = 60.0f; /**< Level's exponential range above silence */
49 static constexpr uint16_t RAMP_FRAMES = 64; /**< a scalar port arrives after this many frames */
50
51 /** @brief Unity gain until ports are given; every scalar port ramps over RAMP_FRAMES. */
52 Vca() {
53 level_gain_.set_length(RAMP_FRAMES);
54 amount_.set_length(RAMP_FRAMES);
56 }
57
58 /** The ports, in the order process() takes them. */
59 static constexpr transport::Port PORTS[] = {
60 {"gain"},
62 {"mod_amount", transport::Spelling::Scalar},
63 };
64
65 /**
66 * @brief One block, the ports in PORTS' order, any trailing ones left off
67 *
68 * gain as a block is used as given; as a scalar it moves to its value over
69 * RAMP_FRAMES. Level and Mod Amt are scalars. The first value a port is
70 * given jumps. A port left off holds its last value, and ramps in progress
71 * continue. In place is fine: out may be in.
72 *
73 * @tparam Ports gain as a block or an arithmetic scalar, then level and mod_amount as scalars
74 * @param in Input buffer, `frames` long
75 * @param[out] out Output buffer, `frames` long
76 * @param frames Frame count
77 * @param ports gain, nominally [0, 1]; level in [0, 1]; mod_amount in [0, 1], clamped. A non-finite scalar gain or mod_amount counts as 0
78 * @context Audio thread; real-time safe.
79 */
80 template<class... Ports>
81 void process(const float* in, float* out, uint16_t frames, Ports... ports) {
82 static_assert(transport::fits_v<Vca, Ports...>,
83 "SBL ERROR: Vca takes gain (a block or a scalar), then level and mod_amount (scalars)");
84 const auto gain = transport::arg<0>(ports...);
85 const auto level = transport::arg<1>(ports...);
86 const auto mod_amount = transport::arg<2>(ports...);
87 if constexpr (transport::is_scalar_v<decltype(level)>) {
88 level_gain_.head(math::level_gain(static_cast<float>(level), LEVEL_RANGE_DB));
89 }
90 if constexpr (transport::is_scalar_v<decltype(mod_amount)>) {
91 amount_.head(sbl::math::clamp01(sbl::math::finite_or(static_cast<float>(mod_amount), 0.0f)));
92 }
93 if constexpr (transport::is_block_v<decltype(gain)>) {
94 for (uint16_t i = 0; i < frames; ++i) {
95 const float lg = level_gain_.step();
96 const float a = amount_.step();
97 out[i] = in[i] * lg * (1.0f - a + a * gain[i]);
98 }
99 if (frames > 0) {
100 gain_.jump(gain[frames - 1]); // a later scalar ramps from where the block left off
101 }
102 } else {
103 if constexpr (transport::is_scalar_v<decltype(gain)>) {
104 const auto g = static_cast<float>(gain);
105 gain_.head(sbl::math::finite_or(g, 0.0f));
106 }
107 for (uint16_t i = 0; i < frames; ++i) {
108 const float lg = level_gain_.step();
109 const float a = amount_.step();
110 const float g = gain_.step();
111 out[i] = in[i] * lg * (1.0f - a + a * g);
112 }
113 }
114 }
115
116 /** @brief Every ramp at its target. */
117 void reset() {
118 level_gain_.jump(level_gain_.target());
119 amount_.jump(amount_.target());
120 gain_.jump(gain_.target());
121 }
122
123 /** @brief Level's gain after the taper, where the ramp is now. */
124 float level_gain() const { return level_gain_.value(); }
125 /** @brief Mod Amt, where the ramp is now. */
126 float mod_amount() const { return amount_.value(); }
127 /** @brief The scalar gain, where the ramp is now, or the last frame of a gain block. */
128 float gain() const { return gain_.value(); }
129
130private:
131 Ramp level_gain_{1.0f};
132 Ramp amount_{1.0f};
133 Ramp gain_{1.0f};
134};
135
136} // namespace sbl::dsp
137
138#endif // SBL_DSP_DYNAMICS_VCA_HPP_
Clamping.
A linear ramp to a target over a fixed number of frames.
Definition ramp.hpp:25
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
An amplifier: gain, Level and Mod Amt as ports over a linear multiply.
Definition vca.hpp:46
void process(const float *in, float *out, uint16_t frames, Ports... ports)
One block, the ports in PORTS' order, any trailing ones left off.
Definition vca.hpp:81
void reset()
Every ramp at its target.
Definition vca.hpp:117
static constexpr transport::Port PORTS[]
Definition vca.hpp:59
static constexpr uint16_t RAMP_FRAMES
Definition vca.hpp:49
float gain() const
The scalar gain, where the ramp is now, or the last frame of a gain block.
Definition vca.hpp:128
float mod_amount() const
Mod Amt, where the ramp is now.
Definition vca.hpp:126
static constexpr float LEVEL_RANGE_DB
Definition vca.hpp:48
float level_gain() const
Level's gain after the taper, where the ramp is now.
Definition vca.hpp:124
Vca()
Unity gain until ports are given; every scalar port ramps over RAMP_FRAMES.
Definition vca.hpp:52
Finiteness of a float, read from its bits, and a fallback for a value that is not finite.
Gain in decibels, and the level taper for an amplifier's Level control.
float level_gain(float level, float range_db)
The level taper: a level in [0, 1] to a gain.
Definition gain.hpp:49
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
A linear ramp to a target over a fixed number of frames.
The processing call's shape as a checked contract.