alpha2-audio 0.10.0

Platform-independent audio DSP core for alpha2.
Documentation
/// Project samples between the points where a retuned processor is given parameter values.
pub const CONTROL_INTERVAL: i64 = 32;

/// The settings a retuned processor moves between over one control interval.
///
/// The processor is at `from` on the interval's first sample and moves in a straight line
/// toward `to`, which it would reach on the first sample of the next interval.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ControlSpan<'a> {
    /// The values at the control point at or before the next processed sample, one per
    /// parameter in the processor's documented order.
    pub from: &'a [f32],
    /// The values at the next control point.
    pub to: &'a [f32],
    /// How many samples past the point of `from` the next processed sample lies, below
    /// [`CONTROL_INTERVAL`].
    pub phase: usize,
}

impl ControlSpan<'_> {
    /// Whether both ends hold the same values, bit for bit: nothing moves.
    #[must_use]
    pub fn is_steady(&self) -> bool {
        self.from.len() == self.to.len()
            && self
                .from
                .iter()
                .zip(self.to)
                .all(|(from, to)| from.to_bits() == to.to_bits())
    }

    /// How far from `from` toward `to` the processor is `frame` samples into the next
    /// processed block, at most 1.
    #[must_use]
    pub fn progress(&self, frame: usize) -> f32 {
        progress(self.phase, frame)
    }
}

pub(crate) fn progress(phase: usize, frame: usize) -> f32 {
    const INTERVAL: u8 = 32;
    const _: () = assert!(INTERVAL as i64 == CONTROL_INTERVAL);
    let along =
        u8::try_from(phase.saturating_add(frame)).map_or(INTERVAL, |along| along.min(INTERVAL));
    f32::from(along) * (1.0 / f32::from(INTERVAL))
}

/// Where one node's parameter values lie in a block's [`Controls`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ControlLane {
    /// The chain node the values retune.
    pub node: usize,
    /// The control point of the first values, in units of [`CONTROL_INTERVAL`] of the node's own
    /// sample position.
    pub first_point: i64,
    /// How many points the lane holds: those the block's samples lie at or after, and the one
    /// after the last of them. A sample before the first point is given the first values and
    /// one at or after the last is held at the last.
    pub points: usize,
    /// Where the lane starts in [`Controls::values`]: [`crate::Processor::control_params`] values
    /// per point, points ascending.
    pub offset: usize,
}

/// Parameter values at the control points one block touches, for the chain nodes it retunes.
///
/// A node with a lane runs in pieces cut at the multiples of [`CONTROL_INTERVAL`] of its own
/// sample position, each given the values of the point at or before it and of the next, so
/// the settings in effect at a sample do not depend on how the audio is cut into blocks.
#[derive(Clone, Copy, Debug, Default)]
pub struct Controls<'a> {
    /// One lane per retuned node, nodes ascending.
    pub lanes: &'a [ControlLane],
    /// The values every lane points into.
    pub values: &'a [f32],
}

impl<'a> Controls<'a> {
    pub(crate) fn span_at(
        &self,
        lane: &ControlLane,
        sample: i64,
        params: usize,
    ) -> ControlSpan<'a> {
        let last = i64::try_from(lane.points).unwrap_or(i64::MAX) - 1;
        let point = sample.div_euclid(CONTROL_INTERVAL);
        let index = |point: i64| point.saturating_sub(lane.first_point).clamp(0, last.max(0));
        let values = |index: i64| {
            let start = lane.offset + usize::try_from(index).unwrap_or(0) * params;
            self.values.get(start..start + params).unwrap_or(&[])
        };
        ControlSpan {
            from: values(index(point)),
            to: values(index(point.saturating_add(1))),
            phase: usize::try_from(sample.rem_euclid(CONTROL_INTERVAL)).unwrap_or(0),
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct Ramp<const N: usize> {
    from: [f32; N],
    change: [f32; N],
    phase: usize,
}

impl<const N: usize> Ramp<N> {
    pub(crate) fn between(from: [f32; N], to: [f32; N], phase: usize) -> Self {
        Self {
            from,
            change: core::array::from_fn(|index| to[index] - from[index]),
            phase,
        }
    }

    pub(crate) fn at(&self, frame: usize) -> [f32; N] {
        let progress = progress(self.phase, frame);
        core::array::from_fn(|index| self.from[index] + self.change[index] * progress)
    }

    pub(crate) const fn after(self, frames: usize) -> Self {
        Self {
            phase: self.phase.saturating_add(frames),
            ..self
        }
    }
}