Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
param.hpp
Go to the documentation of this file.
1// sbl/dsp/param.hpp — Float-domain parameter mapping utilities
2//
3// Standalone functions for normalizing inputs and mapping to parameter
4// ranges with optional curve shaping. Complements scaled() on Pot and CvInput.
5//
6// Usage:
7// float t = sbl::dsp::math::cc_to_float(cc_value); // 0-127 → [0, 1]
8// float cutoff = sbl::dsp::math::map_quadratic(t, 100.0f, 15000.0f);
9
10#ifndef SBL_DSP_MATH_PARAM_HPP_
11#define SBL_DSP_MATH_PARAM_HPP_
12
13#include <cstdint>
15
16namespace sbl::dsp::math {
17
18// ─── Normalize to [0.0, 1.0] ────────────────────────────────────
19
20/// Normalize MIDI CC (0-127) to [0.0, 1.0]
21inline constexpr float cc_to_float(uint8_t val) {
22 return static_cast<float>(val) * (1.0f / 127.0f);
23}
24
25/// Normalize 16-bit unsigned (0-65535) to [0.0, 1.0]
26inline constexpr float u16_to_float(uint16_t val) {
27 return static_cast<float>(val) * (1.0f / 65535.0f);
28}
29
30/// Normalize 14-bit MIDI (0-16383) to [0.0, 1.0] (pitch bend, NRPN)
31inline constexpr float u14_to_float(uint16_t val) {
32 return static_cast<float>(val) * (1.0f / 16383.0f);
33}
34
35// ─── Map [0,1] → [min, max] with curve shaping ─────────────────
36
37/// Linear mapping: t → min + (max - min) * t
38inline constexpr float map_linear(float t, float min, float max) {
39 return min + (max - min) * t;
40}
41
42/// Quadratic mapping: t² curve. Good for frequency, cutoff, LFO rate.
43/// More resolution at low values, fast rise at high values.
44inline constexpr float map_quadratic(float t, float min, float max) {
45 return min + (max - min) * t * t;
46}
47
48/// S-curve mapping via Hermite smooth step: t²(3 - 2t).
49/// Slow start, fast middle, slow finish. Good for crossfade morphs.
50inline constexpr float map_scurve(float t, float min, float max) {
51 float s = t * t * (3.0f - 2.0f * t);
52 return min + (max - min) * s;
53}
54
55/// Inverse quadratic mapping: (2t - t²) curve. More resolution at high values.
56/// Good for reverb decay, feedback — parameters where the interesting range
57/// is near the maximum.
58inline constexpr float map_inv_quadratic(float t, float min, float max) {
59 float s = 2.0f * t - t * t;
60 return min + (max - min) * s;
61}
62
63/// Exponential mapping: min * 2^(t * log2(max/min)).
64/// Equal ratio per knob turn — the musically correct curve for time
65/// parameters (attack, release, delay time) and frequency ranges.
66/// Requires min > 0 and max > min.
67inline float map_exponential(float t, float min, float max) {
68 float octaves = __builtin_log2f(max / min); // constant-folded when args are literals
69 return min * fast_exp2f(t * octaves);
70}
71
72} // namespace sbl::dsp::math
73
74#endif // SBL_DSP_MATH_PARAM_HPP_
Fast analytical approximations (Audio Stack — Atoms)
Cross-cutting math used at every audio layer.
Definition clamp.hpp:10
float fast_exp2f(float x)
Definition fast_math.hpp:99
constexpr float map_linear(float t, float min, float max)
Linear mapping: t → min + (max - min) * t.
Definition param.hpp:38
constexpr float u16_to_float(uint16_t val)
Normalize 16-bit unsigned (0-65535) to [0.0, 1.0].
Definition param.hpp:26
constexpr float u14_to_float(uint16_t val)
Normalize 14-bit MIDI (0-16383) to [0.0, 1.0] (pitch bend, NRPN)
Definition param.hpp:31
constexpr float map_inv_quadratic(float t, float min, float max)
Definition param.hpp:58
constexpr float cc_to_float(uint8_t val)
Normalize MIDI CC (0-127) to [0.0, 1.0].
Definition param.hpp:21
constexpr float map_quadratic(float t, float min, float max)
Definition param.hpp:44
constexpr float map_scurve(float t, float min, float max)
Definition param.hpp:50
float map_exponential(float t, float min, float max)
Definition param.hpp:67