1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Parameter smoothing for click-free transitions.
use serde::{Deserialize, Serialize};
/// One-pole exponential smoother for real-time parameter changes.
///
/// Prevents clicks/artifacts when RPM, load, or other parameters
/// change mid-block by exponentially approaching the target value.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SmoothedParam {
current: f32,
target: f32,
coeff: f32,
}
impl SmoothedParam {
/// Creates a new smoother.
///
/// - `initial`: Starting value.
/// - `smooth_time_s`: Time constant in seconds (63% of the way to target).
/// - `sample_rate`: Audio sample rate.
#[inline]
pub fn new(initial: f32, smooth_time_s: f32, sample_rate: f32) -> Self {
let coeff = if smooth_time_s > 0.0 && sample_rate > 0.0 {
(-1.0 / (smooth_time_s * sample_rate)).exp()
} else {
0.0
};
Self {
current: initial,
target: initial,
coeff,
}
}
/// Sets the target value (approached smoothly).
#[inline]
pub fn set_target(&mut self, target: f32) {
self.target = target;
}
/// Advances one sample and returns the smoothed value.
#[inline]
pub fn next_value(&mut self) -> f32 {
self.current = self.target + self.coeff * (self.current - self.target);
self.current
}
/// Snaps immediately to the target (no smoothing).
#[inline]
pub fn snap(&mut self) {
self.current = self.target;
}
/// Returns the current smoothed value without advancing.
#[must_use]
#[inline]
pub fn current(&self) -> f32 {
self.current
}
/// Returns the target value.
#[must_use]
#[inline]
pub fn target(&self) -> f32 {
self.target
}
/// Returns true if the value has settled (within epsilon of target).
#[must_use]
#[inline]
pub fn is_settled(&self) -> bool {
(self.current - self.target).abs() < 1e-6
}
}
#[cfg(feature = "std")]
impl SmoothedParam {
fn _exp(x: f32) -> f32 {
x.exp()
}
}