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
exp_mod.hpp
Go to the documentation of this file.
1/**
2 * @file exp_mod.hpp
3 * @brief Exponential modulation converter
4 *
5 * exp_mod() maps a normalized modulation signal, typically [-1.0, 1.0], to a
6 * frequency in Hz as base * 2^(mod * depth), so equal steps of the signal
7 * give equal intervals at any base frequency.
8 *
9 * @code
10 * // Scalar, inside a per-sample loop
11 * float freq = sbl::dsp::math::exp_mod(cutoff_hz, lfo_value, 2.0f);
12 *
13 * // Buffer, over a whole modulation buffer
14 * float freq_buf[48];
15 * sbl::dsp::math::exp_mod(lfo_buf, freq_buf, frames, cutoff_hz, 2.0f);
16 * @endcode
17 */
18
19#ifndef SBL_DSP_MATH_EXP_MOD_HPP_
20#define SBL_DSP_MATH_EXP_MOD_HPP_
21
22#include <cstdint>
23
25
26namespace sbl::dsp::math {
27
28inline constexpr float DEFAULT_LO_HZ = 20.0f; /**< Default low output bound, in Hz */
29inline constexpr float DEFAULT_HI_HZ = 20000.0f; /**< Default high output bound, in Hz */
30
31/**
32 * @brief Apply exponential modulation to a base frequency (scalar).
33 *
34 * Converts a normalized modulation signal to a frequency via
35 * freq = base * 2^(mod * depth).
36 *
37 * At depth = 2.0 and mod = 1.0, output is base * 4 (2 octaves up).
38 * At depth = 2.0 and mod = -1.0, output is base / 4 (2 octaves down).
39 * Equal steps in mod give equal intervals at any base frequency.
40 *
41 * @param base Center frequency in Hz (e.g., filter cutoff)
42 * @param mod Normalized modulation value, typically [-1.0, 1.0]
43 * @param depth Modulation depth in octaves (e.g., 2.0 = +/-2 octaves)
44 * @param lo Minimum output frequency in Hz (default 20)
45 * @param hi Maximum output frequency in Hz (default 20000)
46 * @return Modulated frequency in Hz, clamped to [lo, hi]; a NaN
47 * input is not caught and returns NaN
48 */
49inline float exp_mod(float base, float mod, float depth, float lo = DEFAULT_LO_HZ, float hi = DEFAULT_HI_HZ) {
50 float freq = base * fast_exp2f(mod * depth);
51 if (freq < lo) {
52 freq = lo;
53 }
54 if (freq > hi) {
55 freq = hi;
56 }
57 return freq;
58}
59
60/**
61 * @brief Apply exponential modulation to a buffer.
62 *
63 * Writes the scalar exp_mod() of each frame's mod to freq_out[].
64 *
65 * @param mod Normalized modulation signal buffer, typically [-1.0, 1.0]
66 * @param[out] freq_out Output frequency buffer (Hz)
67 * @param frames Frame count
68 * @param base Center frequency in Hz
69 * @param depth Modulation depth in octaves
70 * @param lo Minimum output frequency in Hz (default 20)
71 * @param hi Maximum output frequency in Hz (default 20000)
72 * @context Audio thread; real-time safe.
73 */
74inline void exp_mod(const float* mod, float* freq_out, uint16_t frames, float base, float depth,
75 float lo = DEFAULT_LO_HZ, float hi = DEFAULT_HI_HZ) {
76 for (uint16_t i = 0; i < frames; ++i) {
77 freq_out[i] = exp_mod(base, mod[i], depth, lo, hi);
78 }
79}
80
81} // namespace sbl::dsp::math
82
83#endif // SBL_DSP_MATH_EXP_MOD_HPP_
Fast analytical approximations of sin, tan, 2^x and tanh.
Math for the audio stack.
Definition crossfade.hpp:18
float fast_exp2f(float x)
Fast 2^x approximation for exponential modulation.
constexpr float DEFAULT_LO_HZ
Definition exp_mod.hpp:28
constexpr float DEFAULT_HI_HZ
Definition exp_mod.hpp:29
float exp_mod(float base, float mod, float depth, float lo=DEFAULT_LO_HZ, float hi=DEFAULT_HI_HZ)
Apply exponential modulation to a base frequency (scalar).
Definition exp_mod.hpp:49