Skip to main content

alpha2_model/
domain.rs

1//! The values a numeric place accepts, shared by validation and by operations that write values.
2
3/// An inclusive range of accepted values; an unbounded end is infinite.
4#[derive(Copy, Clone, Debug, PartialEq)]
5pub struct Domain {
6    /// The least value.
7    pub min: f32,
8    /// The greatest value.
9    pub max: f32,
10}
11
12impl Domain {
13    /// Every finite value: positions, scales, rotations.
14    pub const ANY: Self = Self::new(f32::NEG_INFINITY, f32::INFINITY);
15    /// `0..=1`: opacities, roundness, transition progress.
16    pub const UNIT: Self = Self::new(0.0, 1.0);
17    /// `0..`: lengths, sizes, feathers.
18    pub const NON_NEGATIVE: Self = Self::new(0.0, f32::INFINITY);
19    /// `-1..=1`: pans.
20    pub const PAN: Self = Self::new(-1.0, 1.0);
21    /// `0..=MAX_GAIN`: clip and bus gains.
22    pub const GAIN: Self = Self::new(0.0, crate::MAX_GAIN);
23    /// Greater than zero: a ducker's threshold.
24    pub const POSITIVE: Self = Self::new(f32::from_bits(1), f32::INFINITY);
25    /// [`crate::EQ_FREQUENCY_RANGE_HZ`]: equalizer band frequencies.
26    pub const EQ_FREQUENCY_HZ: Self = Self::new(
27        *crate::EQ_FREQUENCY_RANGE_HZ.start(),
28        *crate::EQ_FREQUENCY_RANGE_HZ.end(),
29    );
30    /// [`crate::EQ_Q_RANGE`]: equalizer band resonances.
31    pub const EQ_Q: Self = Self::new(*crate::EQ_Q_RANGE.start(), *crate::EQ_Q_RANGE.end());
32    /// Up to [`crate::MAX_EQ_GAIN_DB`] either way: equalizer band gains.
33    pub const EQ_GAIN_DB: Self = Self::new(-crate::MAX_EQ_GAIN_DB, crate::MAX_EQ_GAIN_DB);
34    /// [`crate::MIN_LEVEL_DB`] to 0: compressor thresholds and ducker depths.
35    pub const LEVEL_DB: Self = Self::new(crate::MIN_LEVEL_DB, 0.0);
36    /// 1 to [`crate::MAX_COMPRESSOR_RATIO`]: compressor ratios.
37    pub const COMPRESSOR_RATIO: Self = Self::new(1.0, crate::MAX_COMPRESSOR_RATIO);
38    /// 0 to [`crate::MAX_KNEE_DB`]: compressor knees.
39    pub const KNEE_DB: Self = Self::new(0.0, crate::MAX_KNEE_DB);
40    /// Up to [`crate::MAX_MAKEUP_DB`] either way: compressor makeup gains.
41    pub const MAKEUP_DB: Self = Self::new(-crate::MAX_MAKEUP_DB, crate::MAX_MAKEUP_DB);
42    /// 0 to [`crate::MAX_EXPANDER_RANGE_DB`]: expander ranges.
43    pub const EXPANDER_RANGE_DB: Self = Self::new(0.0, crate::MAX_EXPANDER_RANGE_DB);
44    /// 0 to [`crate::MAX_DELAY_FEEDBACK`]: delay feedbacks.
45    pub const DELAY_FEEDBACK: Self = Self::new(0.0, crate::MAX_DELAY_FEEDBACK);
46    /// Up to [`crate::MAX_MATRIX_GAIN`] either way: matrix gains.
47    pub const MATRIX_GAIN: Self = Self::new(-crate::MAX_MATRIX_GAIN, crate::MAX_MATRIX_GAIN);
48    /// The points of a polystar.
49    pub const POLYSTAR_POINTS: Self =
50        Self::new(crate::MIN_POLYSTAR_POINTS, crate::MAX_POLYSTAR_POINTS);
51
52    /// The range `min..=max`.
53    #[must_use]
54    pub const fn new(min: f32, max: f32) -> Self {
55        Self { min, max }
56    }
57
58    /// Whether `value` is finite and inside.
59    #[must_use]
60    pub fn contains(self, value: f32) -> bool {
61        value.is_finite() && self.min <= value && value <= self.max
62    }
63
64    /// `value` moved to the nearest end when outside.
65    #[must_use]
66    pub const fn clamp(self, value: f32) -> f32 {
67        value.clamp(self.min, self.max)
68    }
69}