1#[derive(Debug, Clone, Copy)]
9#[repr(C)]
10pub struct Param {
11 pub current: f32,
12 pub target: f32,
13 pub step: f32,
15}
16
17impl Param {
18 pub fn new(value: f32) -> Self {
19 Self {
20 current: value,
21 target: value,
22 step: 0.0,
23 }
24 }
25
26 #[inline]
29 pub fn set_target(&mut self, target: f32, ramp_samples: u32) {
30 self.target = target;
31 if ramp_samples == 0 {
32 self.current = target;
33 self.step = 0.0;
34 } else {
35 self.step = (target - self.current) / ramp_samples as f32;
36 }
37 }
38
39 #[inline(always)]
41 pub fn tick(&mut self) {
42 if self.step != 0.0 {
43 self.current += self.step;
44 if (self.step > 0.0 && self.current >= self.target)
46 || (self.step < 0.0 && self.current <= self.target)
47 {
48 self.current = self.target;
49 self.step = 0.0;
50 }
51 }
52 }
53
54 #[inline]
57 pub fn fill_buffer(&mut self, out: &mut [f32]) {
58 for sample in out.iter_mut() {
59 *sample = self.current;
60 self.tick();
61 }
62 }
63}
64
65#[derive(Debug, Clone, Copy)]
68pub struct ParamBlock {
69 pub params: [Param; 8],
70 pub count: usize,
71}
72
73impl ParamBlock {
74 pub fn new() -> Self {
75 Self {
76 params: [Param::new(0.0); 8],
77 count: 0,
78 }
79 }
80
81 pub fn add(&mut self, value: f32) -> usize {
82 let idx = self.count;
83 self.params[idx] = Param::new(value);
84 self.count += 1;
85 idx
86 }
87
88 #[inline(always)]
89 pub fn get(&self, idx: usize) -> &Param {
90 &self.params[idx]
91 }
92
93 #[inline(always)]
94 pub fn get_mut(&mut self, idx: usize) -> &mut Param {
95 &mut self.params[idx]
96 }
97
98 #[inline(always)]
100 pub fn tick_all(&mut self) {
101 for p in self.params[..self.count].iter_mut() {
102 p.tick();
103 }
104 }
105}
106
107impl Default for ParamBlock {
108 fn default() -> Self {
109 Self::new()
110 }
111}