Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
param_interp.hpp
Go to the documentation of this file.
1// sbl/dsp/param_interp.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#pragma once
22
23#include <cstdint>
24
25namespace sbl::dsp {
26
28public:
29 /// @note All public methods are ISR-safe — bounded computation, no I/O.
30
31 /**
32 * @brief Construct interpolator for a block
33 * @param state Pointer to persistent state (read on entry, written on exit)
34 * @param target Target value for end of block
35 * @param frames Number of samples in the block (must be > 0)
36 */
37 ParameterInterpolator(float* state, float target, uint16_t frames)
38 : state_(state)
39 , value_(*state)
40 , increment_((target - *state) / static_cast<float>(frames))
41 {}
42
43 /** @brief Non-owning constructor (no write-back on destruction) */
44 ParameterInterpolator(float start, float target, uint16_t frames)
45 : state_(nullptr)
46 , value_(start)
47 , increment_((target - start) / static_cast<float>(frames))
48 {}
49
51 if (state_) {
52 *state_ = value_;
53 }
54 }
55
56 // Non-copyable (owns state pointer)
59
60 /** @brief Get next interpolated value */
61 float next() {
62 float v = value_;
63 value_ += increment_;
64 return v;
65 }
66
67 /** @brief Current value (without advancing) */
68 float value() const { return value_; }
69
70 /** @brief Per-sample increment */
71 float increment() const { return increment_; }
72
73private:
74 float* state_;
75 float value_;
76 float increment_;
77};
78
79} // namespace sbl::dsp
float value() const
Current value (without advancing)
ParameterInterpolator & operator=(const ParameterInterpolator &)=delete
float next()
Get next interpolated value.
ParameterInterpolator(const ParameterInterpolator &)=delete
float increment() const
Per-sample increment.
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.
DSP atoms for audio signal processing.
Definition allpass.hpp:22