Skip to main content

qualia_core_db/audio/qac/signal/
model.rs

1use crate::q42::{field_envelope, field_semantics};
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum Error {
4    Envelope(field_envelope::Error),
5    Shared(field_semantics::Error),
6    Unsupported,
7    Shape,
8    Scalar,
9    Unit,
10    Clock,
11    Overflow,
12    Nonfinite,
13    Index,
14    Limit,
15    Profile,
16}
17impl From<field_envelope::Error> for Error {
18    fn from(e: field_envelope::Error) -> Self {
19        Self::Envelope(e)
20    }
21}
22impl From<field_semantics::Error> for Error {
23    fn from(e: field_semantics::Error) -> Self {
24        Self::Shared(e)
25    }
26}
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28#[repr(u32)]
29pub enum Scalar {
30    F32 = 1,
31    F64 = 2,
32    I16 = 3,
33    I24 = 4,
34    I32 = 5,
35    ComplexF32 = 6,
36    U8 = 7,
37}
38impl Scalar {
39    pub fn from_code(n: u32) -> Result<Self, Error> {
40        Ok(match n {
41            1 => Self::F32,
42            2 => Self::F64,
43            3 => Self::I16,
44            4 => Self::I24,
45            5 => Self::I32,
46            6 => Self::ComplexF32,
47            7 => Self::U8,
48            _ => return Err(Error::Scalar),
49        })
50    }
51    pub fn bytes(self) -> u32 {
52        match self {
53            Self::F32 | Self::I32 => 4,
54            Self::F64 | Self::ComplexF32 => 8,
55            Self::I16 => 2,
56            Self::I24 => 3,
57            Self::U8 => 1,
58        }
59    }
60}
61/// Decoded access retains original bits, never normalizes samples.
62#[derive(Clone, Copy, Debug, PartialEq)]
63pub enum Sample {
64    F32(f32),
65    F64(f64),
66    I16(i16),
67    I24(i32),
68    I32(i32),
69    ComplexF32 { real: f32, imag: f32 },
70    U8(u8),
71}
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub struct Rate {
74    pub numerator: u64,
75    pub denominator: u64,
76}
77fn gcd(mut a: u128, mut b: u128) -> u128 {
78    while b != 0 {
79        let r = a % b;
80        a = b;
81        b = r;
82    }
83    a
84}
85impl Rate {
86    pub fn new(n: u64, d: u64) -> Result<Self, Error> {
87        if n == 0
88            || d == 0
89            || n > 1_000_000_000_000_000
90            || d > 1_000_000_000_000
91            || gcd(n.into(), d.into()) != 1
92        {
93            return Err(Error::Clock);
94        }
95        Ok(Self {
96            numerator: n,
97            denominator: d,
98        })
99    }
100    pub fn divided_by(self, n: u32) -> Result<Self, Error> {
101        Self::new(self.numerator, self.denominator)?;
102        if n == 0 {
103            return Err(Error::Clock);
104        }
105        let g = gcd(self.numerator.into(), n.into()) as u64;
106        let num = self.numerator / g;
107        let step = u64::from(n) / g;
108        let den = self.denominator.checked_mul(step).ok_or(Error::Overflow)?;
109        Self::new(num, den)
110    }
111}
112#[derive(Clone, Copy, Debug, PartialEq, Eq)]
113pub struct ExactTime {
114    pub numerator: i128,
115    pub denominator: i128,
116}
117impl ExactTime {
118    fn new(n: i128, d: i128) -> Result<Self, Error> {
119        if d <= 0 {
120            return Err(Error::Clock);
121        }
122        let g = gcd(n.checked_abs().ok_or(Error::Overflow)? as u128, d as u128) as i128;
123        Ok(Self {
124            numerator: n / g,
125            denominator: d / g,
126        })
127    }
128    fn add(self, n: i128, d: i128) -> Result<Self, Error> {
129        let g = gcd(self.denominator as u128, d as u128) as i128;
130        let a = d / g;
131        let b = self.denominator / g;
132        Self::new(
133            self.numerator
134                .checked_mul(a)
135                .and_then(|x| n.checked_mul(b).and_then(|y| x.checked_add(y)))
136                .ok_or(Error::Overflow)?,
137            self.denominator.checked_mul(a).ok_or(Error::Overflow)?,
138        )
139    }
140}
141#[derive(Clone, Copy, Debug, PartialEq, Eq)]
142pub struct Timing {
143    pub scale: u32,
144    pub epoch_seconds: i64,
145    pub epoch_nanoseconds: u32,
146    pub ticks: Rate,
147    pub samples: Rate,
148    pub start_tick: i64,
149}
150impl Timing {
151    pub fn time_at(self, index: u64) -> Result<ExactTime, Error> {
152        Rate::new(self.ticks.numerator, self.ticks.denominator)?;
153        Rate::new(self.samples.numerator, self.samples.denominator)?;
154        if self.scale > 2 || self.epoch_nanoseconds >= 1_000_000_000 {
155            return Err(Error::Clock);
156        }
157        ExactTime::new(self.epoch_seconds.into(), 1)?
158            .add(self.epoch_nanoseconds.into(), 1_000_000_000)?
159            .add(
160                i128::from(self.start_tick)
161                    .checked_mul(self.ticks.denominator.into())
162                    .ok_or(Error::Overflow)?,
163                self.ticks.numerator.into(),
164            )?
165            .add(
166                i128::from(index)
167                    .checked_mul(self.samples.denominator.into())
168                    .ok_or(Error::Overflow)?,
169                self.samples.numerator.into(),
170            )
171    }
172}
173#[derive(Clone, Copy, Debug)]
174pub struct Limits {
175    pub entries: u32,
176    pub samples: u64,
177    pub payload_bytes: u64,
178    pub work: u64,
179}
180impl Default for Limits {
181    fn default() -> Self {
182        Self {
183            entries: 4096,
184            samples: 16_777_216,
185            payload_bytes: 256 << 20,
186            work: 64_000_000,
187        }
188    }
189}