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
ramp.hpp
Go to the documentation of this file.
1/**
2 * @file ramp.hpp
3 * @brief A linear ramp to a target over a fixed number of frames
4 */
5
6#ifndef SBL_DSP_SMOOTHING_RAMP_HPP_
7#define SBL_DSP_SMOOTHING_RAMP_HPP_
8
9#include <cstdint>
10
11namespace sbl::dsp {
12
13/**
14 * @brief A linear ramp to a target over a fixed number of frames
15 *
16 * The de-zipper for a scalar port: a new target
17 * starts a straight line from the current value and arrives after the
18 * ramp's length in frames, whatever the block size. Setting
19 * the same target again does not restart a ramp in progress. head() is
20 * set() for a port: the first value a ramp is given is arrived at at once,
21 * so a component does not sweep from its constructor's value to its first
22 * setting. Below the signal transport; the length is frames, set by the
23 * component that owns it.
24 */
25class Ramp {
26public:
27 /**
28 * @brief A ramp at rest at a value, with a length of 1 frame.
29 *
30 * @param value Initial value and target; it does not count as given for head()
31 */
32 explicit Ramp(float value = 0.0f) : value_(value), target_(value) {}
33
34 /**
35 * @brief Set how many frames a ramp takes.
36 *
37 * @param frames Ramp length in frames; 0 counts as 1
38 */
39 void set_length(uint16_t frames) { length_ = frames == 0 ? 1 : frames; }
40
41 /**
42 * @brief Head for a target from the current value.
43 *
44 * The same target again leaves a ramp in progress alone.
45 *
46 * @param target The value to arrive at after the ramp's length
47 */
48 void set(float target) {
49 if (target == target_) {
50 return;
51 }
52 target_ = target;
53 remaining_ = length_;
54 increment_ = (target_ - value_) / static_cast<float>(length_);
55 }
56
57 /**
58 * @brief set(), except that the first value given since construction, by head() or jump(), is arrived at at once.
59 *
60 * For a scalar port: the first call jumps, later calls ramp.
61 *
62 * @param target The value to head for
63 */
64 void head(float target) {
65 if (given_) {
66 set(target);
67 } else {
68 jump(target);
69 }
70 }
71
72 /**
73 * @brief Arrive at a value at once.
74 *
75 * @param value The new value and target
76 */
77 void jump(float value) {
78 value_ = value;
79 target_ = value;
80 remaining_ = 0;
81 given_ = true;
82 }
83
84 /**
85 * @brief One frame.
86 *
87 * @return The value after this step
88 * @context Audio thread; real-time safe.
89 */
90 float step() {
91 if (remaining_ > 0) {
92 value_ += increment_;
93 if (--remaining_ == 0) {
94 value_ = target_;
95 }
96 }
97 return value_;
98 }
99
100 /** @brief The current value. */
101 float value() const { return value_; }
102 /** @brief The value the ramp heads for. */
103 float target() const { return target_; }
104 /** @brief True while a ramp is in progress. */
105 bool ramping() const { return remaining_ > 0; }
106
107private:
108 float value_;
109 float target_;
110 float increment_ = 0.0f;
111 uint16_t length_ = 1;
112 uint16_t remaining_ = 0;
113 bool given_ = false; /**< a value has been given by head() or jump(); the constructor's does not count */
114};
115
116} // namespace sbl::dsp
117
118#endif // SBL_DSP_SMOOTHING_RAMP_HPP_
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 set(float target)
Head for a target from the current value.
Definition ramp.hpp:48
Ramp(float value=0.0f)
A ramp at rest at a value, with a length of 1 frame.
Definition ramp.hpp:32
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
bool ramping() const
True while a ramp is in progress.
Definition ramp.hpp:105
The audio stack.
Definition dry_wet.hpp:15