1pub const CONTROL_INTERVAL: i64 = 32;
3
4#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct ControlSpan<'a> {
10 pub from: &'a [f32],
13 pub to: &'a [f32],
15 pub phase: usize,
18}
19
20impl ControlSpan<'_> {
21 #[must_use]
23 pub fn is_steady(&self) -> bool {
24 self.from.len() == self.to.len()
25 && self
26 .from
27 .iter()
28 .zip(self.to)
29 .all(|(from, to)| from.to_bits() == to.to_bits())
30 }
31
32 #[must_use]
35 pub fn progress(&self, frame: usize) -> f32 {
36 progress(self.phase, frame)
37 }
38}
39
40pub(crate) fn progress(phase: usize, frame: usize) -> f32 {
41 const INTERVAL: u8 = 32;
42 const _: () = assert!(INTERVAL as i64 == CONTROL_INTERVAL);
43 let along =
44 u8::try_from(phase.saturating_add(frame)).map_or(INTERVAL, |along| along.min(INTERVAL));
45 f32::from(along) * (1.0 / f32::from(INTERVAL))
46}
47
48#[derive(Clone, Copy, Debug, Eq, PartialEq)]
50pub struct ControlLane {
51 pub node: usize,
53 pub first_point: i64,
56 pub points: usize,
60 pub offset: usize,
63}
64
65#[derive(Clone, Copy, Debug, Default)]
71pub struct Controls<'a> {
72 pub lanes: &'a [ControlLane],
74 pub values: &'a [f32],
76}
77
78impl<'a> Controls<'a> {
79 pub(crate) fn span_at(
80 &self,
81 lane: &ControlLane,
82 sample: i64,
83 params: usize,
84 ) -> ControlSpan<'a> {
85 let last = i64::try_from(lane.points).unwrap_or(i64::MAX) - 1;
86 let point = sample.div_euclid(CONTROL_INTERVAL);
87 let index = |point: i64| point.saturating_sub(lane.first_point).clamp(0, last.max(0));
88 let values = |index: i64| {
89 let start = lane.offset + usize::try_from(index).unwrap_or(0) * params;
90 self.values.get(start..start + params).unwrap_or(&[])
91 };
92 ControlSpan {
93 from: values(index(point)),
94 to: values(index(point.saturating_add(1))),
95 phase: usize::try_from(sample.rem_euclid(CONTROL_INTERVAL)).unwrap_or(0),
96 }
97 }
98}
99
100#[derive(Clone, Copy, Debug)]
101pub(crate) struct Ramp<const N: usize> {
102 from: [f32; N],
103 change: [f32; N],
104 phase: usize,
105}
106
107impl<const N: usize> Ramp<N> {
108 pub(crate) fn between(from: [f32; N], to: [f32; N], phase: usize) -> Self {
109 Self {
110 from,
111 change: core::array::from_fn(|index| to[index] - from[index]),
112 phase,
113 }
114 }
115
116 pub(crate) fn at(&self, frame: usize) -> [f32; N] {
117 let progress = progress(self.phase, frame);
118 core::array::from_fn(|index| self.from[index] + self.change[index] * progress)
119 }
120
121 pub(crate) const fn after(self, frames: usize) -> Self {
122 Self {
123 phase: self.phase.saturating_add(frames),
124 ..self
125 }
126 }
127}