Skip to main content

qualia_core_db/audio/qac/signal/writer/
samples.rs

1use super::*;
2/// Actual original samples. EncodedLe is borrowed byte-exactly, with no alignment
3/// assumption. Native variants serialize finite samples explicitly little endian.
4#[derive(Clone, Copy, Debug)]
5pub enum PcmSamples<'a> {
6    EncodedLe { scalar: Scalar, bytes: &'a [u8] },
7    F32(&'a [f32]),
8    F64(&'a [f64]),
9    I16(&'a [i16]),
10    I24(&'a [i32]),
11    I32(&'a [i32]),
12}
13impl PcmSamples<'_> {
14    pub fn scalar(self) -> Scalar {
15        match self {
16            Self::EncodedLe { scalar, .. } => scalar,
17            Self::F32(_) => Scalar::F32,
18            Self::F64(_) => Scalar::F64,
19            Self::I16(_) => Scalar::I16,
20            Self::I24(_) => Scalar::I24,
21            Self::I32(_) => Scalar::I32,
22        }
23    }
24    pub fn encoded_len(self) -> Result<usize, Error> {
25        if matches!(self.scalar(), Scalar::ComplexF32 | Scalar::U8) {
26            return Err(Error::Scalar);
27        }
28        let count = match self {
29            Self::EncodedLe { bytes, scalar } => {
30                if bytes.len() % scalar.bytes() as usize != 0 {
31                    return Err(Error::Shape);
32                }
33                return Ok(bytes.len());
34            }
35            Self::F32(v) => v.len(),
36            Self::F64(v) => v.len(),
37            Self::I16(v) => v.len(),
38            Self::I24(v) => v.len(),
39            Self::I32(v) => v.len(),
40        };
41        count
42            .checked_mul(self.scalar().bytes() as usize)
43            .ok_or(Error::Overflow)
44    }
45    pub(crate) fn encode_into(self, out: &mut [u8]) -> Result<usize, Error> {
46        let n = self.encoded_len()?;
47        if out.len() < n {
48            return Err(Error::Envelope(env::Error::Extent));
49        }
50        match self {
51            Self::F32(v) => {
52                if v.iter().any(|x| !x.is_finite()) {
53                    return Err(Error::Nonfinite);
54                }
55            }
56            Self::F64(v) => {
57                if v.iter().any(|x| !x.is_finite()) {
58                    return Err(Error::Nonfinite);
59                }
60            }
61            Self::I24(v) => {
62                if v.iter().any(|x| !(-8_388_608..=8_388_607).contains(x)) {
63                    return Err(Error::Scalar);
64                }
65            }
66            _ => {}
67        }
68        let out = &mut out[..n];
69        macro_rules! le {
70            ($v:expr,$n:expr) => {
71                for (dst, value) in out.chunks_exact_mut($n).zip($v) {
72                    dst.copy_from_slice(&value.to_le_bytes());
73                }
74            };
75        }
76        match self {
77            Self::EncodedLe { bytes, .. } => out.copy_from_slice(bytes),
78            Self::F32(v) => le!(v, 4),
79            Self::F64(v) => le!(v, 8),
80            Self::I16(v) => le!(v, 2),
81            Self::I32(v) => le!(v, 4),
82            Self::I24(v) => {
83                for (dst, value) in out.chunks_exact_mut(3).zip(v) {
84                    dst.copy_from_slice(&value.to_le_bytes()[..3]);
85                }
86            }
87        }
88        Ok(n)
89    }
90}