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
pm.hpp
Go to the documentation of this file.
1/**
2 * @file pm.hpp
3 * @brief The parts of a phase-modulated oscillator: the pitch's step, the index's depth, the PM input
4 *
5 * What PmPulse is composed of besides its core, for a component that drives
6 * cores of its own: several on one phase, or two sides with a path between
7 * them. Each part is below the signal transport and takes natural units.
8 */
9
10#ifndef SBL_DSP_OSCILLATORS_PM_HPP_
11#define SBL_DSP_OSCILLATORS_PM_HPP_
12
13#include <cmath>
14#include <cstdint>
15
18#include <sbl/math/clamp.hpp>
19#include <sbl/math/finite.hpp>
20
21namespace sbl::dsp {
22
23inline constexpr float PM_MAX_FREQUENCY = 0.25f; /**< cycles per frame: fs/4, as BlepOsc caps it */
24inline constexpr float PM_MAX_INDEX = 0.5f; /**< cycles of offset per unit of PM at index 1 */
25/** @brief Cycles per frame: where pm_taming()'s reduction starts, 1.05 kHz at 48 kHz. */
26inline constexpr float PM_TAMING_CORNER = 1046.5f / 48000.0f;
27inline constexpr float PM_TAMING_PER_OCTAVE = 0.3f; /**< the reduction's slope before squaring: zero 40 semitones up */
28inline constexpr uint8_t PM_SUBSTEPS = SincUpsampler::FACTOR; /**< offsets a frame */
29inline constexpr uint8_t PM_LATENCY = SincUpsampler::LATENCY; /**< frames the PM signal trails */
30
31/**
32 * @brief The step for a pitch in octaves over a tune.
33 *
34 * @param tune Tune in cycles per frame
35 * @param pitch Pitch in octaves over the tune
36 * @return Step in cycles per frame, in [0, PM_MAX_FREQUENCY]; a NaN gives 0
37 */
38inline float pm_frequency(float tune, float pitch) {
39 return sbl::math::clamp(math::frequency(tune, pitch), 0.0f, PM_MAX_FREQUENCY);
40}
41
42/**
43 * @brief Cycles of phase offset per unit of the PM signal for a bipolar index.
44 *
45 * Quadratic, so most of the control's travel is the shallow end.
46 *
47 * @param index Index, nominally [-1, 1], not clamped; a non-finite index counts as 0
48 * @return PM_MAX_INDEX * index * |index|, in cycles per unit of PM
49 */
50inline float pm_depth(float index) {
51 const float x = sbl::math::finite_or(index, 0.0f);
52 return PM_MAX_INDEX * x * std::fabs(x);
53}
54
55/**
56 * @brief The share of the depth kept at a pitch over PM_TAMING_CORNER.
57 *
58 * The reduction of the depth with pitch that PmPulse applies, as Plaits
59 * reduces its FM amount (plaits/dsp/engine/fm_engine.cc:76-78). Without one,
60 * the PM's sidebands pass the Nyquist frequency at high pitch and fold. A
61 * component multiplies it in, or a curve of its own.
62 *
63 * @param octaves_above_corner Pitch in octaves over PM_TAMING_CORNER
64 * @return (1 - PM_TAMING_PER_OCTAVE * octaves)^2 with the base clamped to [0, 1]: 1 at and under the
65 * corner, 0 from 40 semitones up; a NaN gives 0
66 */
67inline float pm_taming(float octaves_above_corner) {
68 const float r = sbl::math::clamp(1.0f - PM_TAMING_PER_OCTAVE * octaves_above_corner, 0.0f, 1.0f);
69 return r * r;
70}
71
72/**
73 * @brief The PM signal, a sample a frame in and PM_SUBSTEPS values a frame out.
74 *
75 * The values come through a SincUpsampler, PM_LATENCY frames late; a sample
76 * that is not finite holds the last one that was.
77 */
78class PmInput {
79public:
80 /**
81 * @brief One frame: a sample in, the frame's values out, the last at the frame's end.
82 *
83 * @param sample This frame's PM sample; a non-finite sample holds the last finite one
84 * @param[out] values The frame's PM_SUBSTEPS values, PM_LATENCY frames late
85 * @context Audio thread; real-time safe.
86 */
87 void process(float sample, float (&values)[PM_SUBSTEPS]) {
88 last_ = sbl::math::is_finite(sample) ? sample : last_;
89 upsampler_.process(last_, values);
90 }
91
92 /**
93 * @brief One frame with no PM signal: zero in. The held sample is kept for when the signal returns.
94 *
95 * @param[out] values The frame's PM_SUBSTEPS values, PM_LATENCY frames late
96 * @context Audio thread; real-time safe.
97 */
98 void process(float (&values)[PM_SUBSTEPS]) { upsampler_.process(0.0f, values); }
99
100 /** @brief The held sample and the upsampler's history to zero. */
101 void reset() {
102 last_ = 0.0f;
103 upsampler_.reset();
104 }
105
106private:
107 float last_ = 0.0f; /**< the last finite sample */
108 SincUpsampler upsampler_{};
109};
110
111} // namespace sbl::dsp
112
113#endif // SBL_DSP_OSCILLATORS_PM_HPP_
Clamping.
The PM signal, a sample a frame in and PM_SUBSTEPS values a frame out.
Definition pm.hpp:78
void process(float sample, float(&values)[PM_SUBSTEPS])
One frame: a sample in, the frame's values out, the last at the frame's end.
Definition pm.hpp:87
void process(float(&values)[PM_SUBSTEPS])
One frame with no PM signal: zero in. The held sample is kept for when the signal returns.
Definition pm.hpp:98
void reset()
The held sample and the upsampler's history to zero.
Definition pm.hpp:101
A frame-rate signal interpolated at FACTOR steps through each frame, LATENCY frames late.
void process(float sample, float(&out)[FACTOR])
One frame: push a sample and interpolate the frame LATENCY frames back.
void reset()
History to zero.
static constexpr uint8_t FACTOR
static constexpr uint8_t LATENCY
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
constexpr float PM_TAMING_CORNER
Cycles per frame: where pm_taming()'s reduction starts, 1.05 kHz at 48 kHz.
Definition pm.hpp:26
constexpr uint8_t PM_SUBSTEPS
Definition pm.hpp:28
float pm_frequency(float tune, float pitch)
The step for a pitch in octaves over a tune.
Definition pm.hpp:38
constexpr float PM_MAX_FREQUENCY
Definition pm.hpp:23
float pm_taming(float octaves_above_corner)
The share of the depth kept at a pitch over PM_TAMING_CORNER.
Definition pm.hpp:67
float pm_depth(float index)
Cycles of phase offset per unit of the PM signal for a bipolar index.
Definition pm.hpp:50
constexpr float PM_TAMING_PER_OCTAVE
Definition pm.hpp:27
constexpr uint8_t PM_LATENCY
Definition pm.hpp:29
constexpr float PM_MAX_INDEX
Definition pm.hpp:24
float finite_or(float x, float fallback)
x, or fallback when x is an infinity or a NaN.
Definition finite.hpp:36
bool is_finite(float x)
std::isfinite, read from the exponent's bits.
Definition finite.hpp:27
constexpr T clamp(T x, T lo, T hi)
x held to [lo, hi]; a NaN goes to lo.
Definition clamp.hpp:24
Pitch as octaves and ratios: from an interval to a frequency and back.
A frame-rate signal interpolated at FACTOR steps through each frame, LATENCY frames late.