Skip to main content

qualia_core_db/render/portal_qac/
window.rs

1use super::{Error, Metadata};
2use crate::audio::qac::{
3    metadata::FrequencyOffset,
4    signal::{decode_sample, ExactTime, Sample},
5};
6
7pub(super) struct Window<'a> {
8    pub bytes: &'a [u8],
9    pub metadata: &'a Metadata,
10    pub start: u64,
11    pub count: u64,
12    pub channel: u32,
13}
14impl Window<'_> {
15    fn sample(&self, record: u64, bin: u32) -> Result<Sample, Error> {
16        if record >= self.count || bin >= self.metadata.bins {
17            return Err(Error::Index);
18        }
19        let scalar = self.metadata.scalar;
20        let at = record
21            .checked_mul(self.metadata.channels.into())
22            .and_then(|n| n.checked_add(self.channel.into()))
23            .and_then(|n| n.checked_mul(self.metadata.bins.into()))
24            .and_then(|n| n.checked_add(bin.into()))
25            .and_then(|n| n.checked_mul(scalar.bytes().into()))
26            .ok_or(Error::Overflow)?;
27        let at = usize::try_from(at).map_err(|_| Error::Overflow)?;
28        let bytes = self
29            .bytes
30            .get(
31                at..at
32                    .checked_add(scalar.bytes() as usize)
33                    .ok_or(Error::Overflow)?,
34            )
35            .ok_or(Error::Invariant)?;
36        Ok(decode_sample(scalar, bytes)?)
37    }
38    fn time_at(&self, record: u64) -> Result<ExactTime, Error> {
39        if record >= self.count {
40            return Err(Error::Index);
41        }
42        Ok(self
43            .metadata
44            .timing
45            .time_at(self.start.checked_add(record).ok_or(Error::Overflow)?)?)
46    }
47}
48/// Borrowed time/channel PCM selection. Samples retain their exact scalar type.
49/// Original bytes include all interleaved channels for the selected time range.
50pub struct PcmWindow<'a> {
51    pub(super) inner: Window<'a>,
52}
53impl PcmWindow<'_> {
54    pub fn records(&self) -> u64 {
55        self.inner.count
56    }
57    pub fn start_record(&self) -> u64 {
58        self.inner.start
59    }
60    pub fn channel(&self) -> u32 {
61        self.inner.channel
62    }
63    pub fn metadata(&self) -> &Metadata {
64        self.inner.metadata
65    }
66    pub fn original_interleaved_bytes(&self) -> &[u8] {
67        self.inner.bytes
68    }
69    pub fn sample(&self, record: u64) -> Result<Sample, Error> {
70        self.inner.sample(record, 0)
71    }
72    pub fn time_at(&self, record: u64) -> Result<ExactTime, Error> {
73        self.inner.time_at(record)
74    }
75}
76/// Borrowed frame/channel STFT selection with original full-bin ordering.
77/// Frequency is the signed mathematical FFT offset, never an inferred carrier.
78pub struct StftWindow<'a> {
79    pub(super) inner: Window<'a>,
80}
81impl StftWindow<'_> {
82    pub fn records(&self) -> u64 {
83        self.inner.count
84    }
85    pub fn start_record(&self) -> u64 {
86        self.inner.start
87    }
88    pub fn channel(&self) -> u32 {
89        self.inner.channel
90    }
91    pub fn bins(&self) -> u32 {
92        self.inner.metadata.bins
93    }
94    pub fn metadata(&self) -> &Metadata {
95        self.inner.metadata
96    }
97    pub fn original_interleaved_bytes(&self) -> &[u8] {
98        self.inner.bytes
99    }
100    pub fn sample(&self, frame: u64, bin: u32) -> Result<Sample, Error> {
101        self.inner.sample(frame, bin)
102    }
103    pub fn time_at(&self, frame: u64) -> Result<ExactTime, Error> {
104        self.inner.time_at(frame)
105    }
106    pub fn frequency_offset(&self, bin: u32) -> Result<FrequencyOffset, Error> {
107        Ok(self
108            .inner
109            .metadata
110            .transform
111            .ok_or(Error::Invariant)?
112            .frequency_offset(bin)?)
113    }
114}