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
parameter_interpolator.hpp
Go to the documentation of this file.
1// sbl/dsp/primitives/parameter_interpolator.hpp — RAII parameter interpolator
2//
3// Linearly interpolates a parameter across a render block to eliminate
4// zipper noise from step changes. The destructor writes the final value
5// back to the state pointer, ensuring the next block picks up exactly
6// where this one left off.
7//
8// Inspired by Mutable Instruments stmlib ParameterInterpolator (MIT).
9//
10// Usage:
11// float cutoff_state = 1000.0f;
12//
13// void render(float* out, size_t n) {
14// sbl::dsp::ParameterInterpolator cutoff(&cutoff_state, new_cutoff, n);
15// for (size_t i = 0; i < n; ++i) {
16// filter.set_cutoff(cutoff.next());
17// out[i] = filter.process(in[i]);
18// }
19// } // destructor stores final value
20
21#ifndef SBL_DSP_PRIMITIVES_PARAMETER_INTERPOLATOR_HPP_
22#define SBL_DSP_PRIMITIVES_PARAMETER_INTERPOLATOR_HPP_
23
24#include <cstdint>
25
26namespace sbl::dsp::primitives {
27
29public:
30 /// @note All public methods are ISR-safe — bounded computation, no I/O.
31
32 /**
33 * @brief Construct interpolator for a block
34 * @param state Pointer to persistent state (read on entry, written on exit)
35 * @param target Target value for end of block
36 * @param frames Number of samples in the block (must be > 0)
37 */
38 ParameterInterpolator(float* state, float target, uint16_t frames)
39 : state_(state)
40 , value_(*state)
41 , increment_((target - *state) / static_cast<float>(frames))
42 {}
43
44 /** @brief Non-owning constructor (no write-back on destruction) */
45 ParameterInterpolator(float start, float target, uint16_t frames)
46 : state_(nullptr)
47 , value_(start)
48 , increment_((target - start) / static_cast<float>(frames))
49 {}
50
52 if (state_) {
53 *state_ = value_;
54 }
55 }
56
57 // Non-copyable (owns state pointer)
60
61 /** @brief Get next interpolated value */
62 float next() {
63 float v = value_;
64 value_ += increment_;
65 return v;
66 }
67
68 /** @brief Current value (without advancing) */
69 float value() const { return value_; }
70
71 /** @brief Per-sample increment */
72 float increment() const { return increment_; }
73
74private:
75 float* state_;
76 float value_;
77 float increment_;
78};
79
80} // namespace sbl::dsp::primitives
81
82#endif // SBL_DSP_PRIMITIVES_PARAMETER_INTERPOLATOR_HPP_
ParameterInterpolator(float start, float target, uint16_t frames)
Non-owning constructor (no write-back on destruction)
ParameterInterpolator(float *state, float target, uint16_t frames)
Construct interpolator for a block.
float value() const
Current value (without advancing)
ParameterInterpolator(const ParameterInterpolator &)=delete
ParameterInterpolator & operator=(const ParameterInterpolator &)=delete
Stateful, single-concern building blocks.