Skip to main content

alpha2_audio/
control.rs

1/// Project samples between the points where a retuned processor is given parameter values.
2pub const CONTROL_INTERVAL: i64 = 32;
3
4/// The settings a retuned processor moves between over one control interval.
5///
6/// The processor is at `from` on the interval's first sample and moves in a straight line
7/// toward `to`, which it would reach on the first sample of the next interval.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct ControlSpan<'a> {
10    /// The values at the control point at or before the next processed sample, one per
11    /// parameter in the processor's documented order.
12    pub from: &'a [f32],
13    /// The values at the next control point.
14    pub to: &'a [f32],
15    /// How many samples past the point of `from` the next processed sample lies, below
16    /// [`CONTROL_INTERVAL`].
17    pub phase: usize,
18}
19
20impl ControlSpan<'_> {
21    /// Whether both ends hold the same values, bit for bit: nothing moves.
22    #[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    /// How far from `from` toward `to` the processor is `frame` samples into the next
33    /// processed block, at most 1.
34    #[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/// Where one node's parameter values lie in a block's [`Controls`].
49#[derive(Clone, Copy, Debug, Eq, PartialEq)]
50pub struct ControlLane {
51    /// The chain node the values retune.
52    pub node: usize,
53    /// The control point of the first values, in units of [`CONTROL_INTERVAL`] of the node's own
54    /// sample position.
55    pub first_point: i64,
56    /// How many points the lane holds: those the block's samples lie at or after, and the one
57    /// after the last of them. A sample before the first point is given the first values and
58    /// one at or after the last is held at the last.
59    pub points: usize,
60    /// Where the lane starts in [`Controls::values`]: [`crate::Processor::control_params`] values
61    /// per point, points ascending.
62    pub offset: usize,
63}
64
65/// Parameter values at the control points one block touches, for the chain nodes it retunes.
66///
67/// A node with a lane runs in pieces cut at the multiples of [`CONTROL_INTERVAL`] of its own
68/// sample position, each given the values of the point at or before it and of the next, so
69/// the settings in effect at a sample do not depend on how the audio is cut into blocks.
70#[derive(Clone, Copy, Debug, Default)]
71pub struct Controls<'a> {
72    /// One lane per retuned node, nodes ascending.
73    pub lanes: &'a [ControlLane],
74    /// The values every lane points into.
75    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}