qualia_core_db/audio/qac/signal/
samples.rs1use super::*;
3impl<'a> SignalView<'a> {
4 pub fn as_bytes(self) -> &'a [u8] {
5 self.payload
6 }
7 pub fn time_at(self, index: u64) -> Result<ExactTime, Error> {
8 if index >= self.records {
9 return Err(Error::Index);
10 }
11 self.timing.ok_or(Error::Clock)?.time_at(index)
12 }
13 pub fn sample(self, record: u64, channel: u32, bin: u32) -> Result<Sample, Error> {
14 if record >= self.records || channel >= self.channels || bin >= self.bins {
15 return Err(Error::Index);
16 }
17 let i = record
18 .checked_mul(self.channels.into())
19 .and_then(|x| x.checked_add(channel.into()))
20 .and_then(|x| x.checked_mul(self.bins.into()))
21 .and_then(|x| x.checked_add(bin.into()))
22 .ok_or(Error::Overflow)?;
23 let start = usize::try_from(
24 i.checked_mul(self.scalar.bytes().into())
25 .ok_or(Error::Overflow)?,
26 )
27 .map_err(|_| Error::Overflow)?;
28 let n = self.scalar.bytes() as usize;
29 decode_sample(
30 self.scalar,
31 self.payload
32 .get(start..start.checked_add(n).ok_or(Error::Overflow)?)
33 .ok_or(Error::Index)?,
34 )
35 }
36}
37pub fn decode_sample(s: Scalar, b: &[u8]) -> Result<Sample, Error> {
40 if b.len() != s.bytes() as usize {
41 return Err(Error::Scalar);
42 }
43 finite(s, b)?;
44 Ok(match s {
45 Scalar::F32 => Sample::F32(f32::from_le_bytes(b.try_into().map_err(|_| Error::Scalar)?)),
46 Scalar::F64 => Sample::F64(f64::from_le_bytes(b.try_into().map_err(|_| Error::Scalar)?)),
47 Scalar::I16 => Sample::I16(i16::from_le_bytes(b.try_into().map_err(|_| Error::Scalar)?)),
48 Scalar::I24 => {
49 if b.len() != 3 {
50 return Err(Error::Scalar);
51 }
52 let x = i32::from_le_bytes([b[0], b[1], b[2], if b[2] & 128 != 0 { 255 } else { 0 }]);
53 Sample::I24(x)
54 }
55 Scalar::I32 => Sample::I32(i32::from_le_bytes(b.try_into().map_err(|_| Error::Scalar)?)),
56 Scalar::ComplexF32 => {
57 if b.len() != 8 {
58 return Err(Error::Scalar);
59 }
60 Sample::ComplexF32 {
61 real: f32::from_le_bytes(b[..4].try_into().unwrap()),
62 imag: f32::from_le_bytes(b[4..].try_into().unwrap()),
63 }
64 }
65 Scalar::U8 => Sample::U8(*b.first().ok_or(Error::Scalar)?),
66 })
67}
68pub(super) fn finite(s: Scalar, payload: &[u8]) -> Result<(), Error> {
69 let width = match s {
70 Scalar::F32 | Scalar::ComplexF32 => 4,
71 Scalar::F64 => 8,
72 _ => return Ok(()),
73 };
74 for raw in payload.chunks_exact(width) {
75 let valid = if width == 4 {
76 f32::from_le_bytes(raw.try_into().unwrap()).is_finite()
77 } else {
78 f64::from_le_bytes(raw.try_into().unwrap()).is_finite()
79 };
80 if !valid {
81 return Err(Error::Nonfinite);
82 }
83 }
84 Ok(())
85}
86
87impl<'a> SignalView<'a> {
88 pub fn mode(self) -> u16 {
89 self.mode
90 }
91 pub fn scalar(self) -> Scalar {
92 self.scalar
93 }
94 pub fn channels(self) -> u32 {
95 self.channels
96 }
97 pub fn bins(self) -> u32 {
98 self.bins
99 }
100 pub fn records(self) -> u64 {
101 self.records
102 }
103 pub fn stream_id(self) -> u32 {
104 self.stream_id
105 }
106 pub fn frame_id(self) -> u32 {
107 self.frame_id
108 }
109 pub fn timing(self) -> Option<Timing> {
110 self.timing
111 }
112 pub fn transform(self) -> Option<crate::audio::qac::metadata::Transform> {
113 self.transform
114 }
115 pub fn window_coefficients_le(&self) -> Option<&'a [u8]> {
116 self.window_coefficients_le
117 }
118 pub fn channel_order(self) -> &'a str {
119 self.channel_order
120 }
121 pub fn purpose(self) -> &'a str {
122 self.purpose
123 }
124 pub fn relation(self) -> &'a str {
125 self.relation
126 }
127 pub fn quantity(self) -> &'a str {
128 self.quantity
129 }
130 pub fn units(self) -> &'a str {
131 self.units
132 }
133 pub fn units_per_stored_sample(self) -> f64 {
134 self.units_per_stored_sample
135 }
136 pub fn stored_offset(self) -> f64 {
137 self.stored_offset
138 }
139}