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
cam.hpp
Go to the documentation of this file.
1/**
2 * @file cam.hpp
3 * @brief Cam profile evaluation
4 *
5 * Evaluates LUTE-generated cam transfer function tables.
6 * A cam table maps [0, 1] input to arbitrary float output via precomputed
7 * breakpoint-interpolated lookup tables.
8 *
9 * Rate-agnostic: works at control rate (200 Hz knob updates) or audio rate
10 * (48 kHz modulation reshaping).
11 *
12 * A Cam carries a table and its size together, taken from the table's array
13 * type, so a size cannot be paired with another table. CamPitchAxis maps a
14 * frequency or a pitch onto a cam's input, for a cam drawn over pitch.
15 *
16 * Usage:
17 * @code
18 * #include <sbl/control/cam.hpp>
19 *
20 * // drive_256: a LUTE-generated table of 256 entries plus a guard point
21 * float v = sbl::control::cam_evaluate(drive_256, knob_value);
22 * constexpr sbl::control::Cam DRIVE = sbl::control::Cam::of(drive_256);
23 * sbl::control::cam_evaluate(DRIVE, mod_buf, shaped_buf, frames);
24 *
25 * constexpr sbl::control::CamPitchAxis AXIS{27.5f, 8.0f}; // A0 and eight octaves up
26 * float ratio = sbl::control::cam_evaluate(TRACKING, AXIS.at_frequency(hz));
27 * @endcode
28 */
29
30#ifndef SBL_CONTROL_CAM_HPP_
31#define SBL_CONTROL_CAM_HPP_
32
33#include <cmath>
34#include <cstddef>
35#include <cstdint>
36
37namespace sbl::control {
38
39/** The guard points a cam table carries after its entries (LUTE's `guard_points` for a cam). */
40inline constexpr size_t CAM_GUARD_POINTS = 1;
41
42/**
43 * A cam table and its size: `size` entries and a guard point after them. A
44 * default Cam has no table; cam_evaluate must not be given one.
45 */
46struct Cam {
47 const float* table = nullptr;
48 uint16_t size = 0;
49
50 /** The Cam of a LUTE table, its size read from the array's type. */
51 template<size_t N>
52 static constexpr Cam of(const float (&table)[N]) {
53 static_assert(N > CAM_GUARD_POINTS + 1, "a cam table needs two entries and its guard point");
54 return {table, static_cast<uint16_t>(N - CAM_GUARD_POINTS)};
55 }
56
57 constexpr explicit operator bool() const { return table != nullptr; }
58};
59
60/**
61 * Evaluate a cam table at a single input position.
62 * Input clamped to [0, 1]. Returns linearly interpolated table value.
63 * Table must have at least 1 guard point (size + 1 entries).
64 */
65inline float cam_evaluate(const float* table, uint16_t size, float input) {
66 float clamped = input < 0.0f ? 0.0f : (input > 1.0f ? 1.0f : input);
67 float pos = clamped * static_cast<float>(size - 1);
68 uint16_t idx = static_cast<uint16_t>(pos);
69 float frac = pos - static_cast<float>(idx);
70 return table[idx] + (table[idx + 1] - table[idx]) * frac;
71}
72
73/** Evaluate a cam table across a buffer. */
74inline void cam_evaluate(const float* table, uint16_t size, const float* in, float* out, uint16_t frames) {
75 for (uint16_t i = 0; i < frames; ++i) {
76 out[i] = cam_evaluate(table, size, in[i]);
77 }
78}
79
80/** Evaluate a Cam at a single input position, clamped to [0, 1]. */
81inline float cam_evaluate(const Cam& cam, float input) { return cam_evaluate(cam.table, cam.size, input); }
82
83/** Evaluate a Cam across a buffer. */
84inline void cam_evaluate(const Cam& cam, const float* in, float* out, uint16_t frames) {
85 cam_evaluate(cam.table, cam.size, in, out, frames);
86}
87
88/** Evaluate a LUTE table, its size read from the array's type. */
89template<size_t N>
90inline float cam_evaluate(const float (&table)[N], float input) {
91 return cam_evaluate(Cam::of(table), input);
92}
93
94/**
95 * A cam's input axis over pitch: input 0 at `low`, input 1 `octaves` above
96 * it, even in octaves between. `low` is a frequency in whatever unit the
97 * caller's frequencies are in (Hz, or cycles per frame).
98 */
100 float low;
101 float octaves;
102
103 /** The cam input for a frequency; at or under `low` it is 0. */
104 float at_frequency(float frequency) const { return frequency > low ? std::log2(frequency / low) / octaves : 0.0f; }
105
106 /** The cam input for a pitch in octaves above `low`. */
107 constexpr float at_octaves(float octaves_above_low) const { return octaves_above_low / octaves; }
108};
109
110} // namespace sbl::control
111
112#endif // SBL_CONTROL_CAM_HPP_
The control stack: scheduler, event queue, merge, CC mapping, calibration.
constexpr size_t CAM_GUARD_POINTS
Definition cam.hpp:40
float cam_evaluate(const float *table, uint16_t size, float input)
Definition cam.hpp:65
constexpr float at_octaves(float octaves_above_low) const
Definition cam.hpp:107
float at_frequency(float frequency) const
Definition cam.hpp:104
static constexpr Cam of(const float(&table)[N])
Definition cam.hpp:52