1use super::*;
2use crate::q42::field_semantics::{Context, DomainRegistry};
3pub const SIGNAL_SCHEMA: u16 = 210;
4pub const SHAPE_SCHEMA: u16 = 205;
5pub const TRANSFORM_SCHEMA: u16 = 201;
6const SIGNAL: &[FieldRule] = &[
7 r(1, 6, None, true),
8 r(2, 5, Some(1), true),
9 r(3, 5, Some(1), true),
10 r(4, 6, None, true),
11 r(5, 6, None, true),
12];
13const SHAPE: &[FieldRule] = &[
14 r(1, 6, None, true),
15 r(2, 6, None, true),
16 r(14, 2, None, true),
17 r(15, 6, None, true),
18];
19const TRANSFORM: &[FieldRule] = &[
20 r(1, 6, None, true),
21 r(2, 1, Some(1), true),
22 r(3, 1, Some(1), true),
23 r(4, 6, None, true),
24 r(5, 9, Some(1), false),
25 r(6, 6, None, true),
26 r(7, 1, Some(1), true),
27 r(8, 9, Some(1), false),
28 r(9, 6, None, true),
29 r(10, 3, Some(1), true),
30 r(11, 6, None, true),
31 r(12, 6, None, true),
32 r(13, 1, Some(1), true),
33 r(14, 2, Some(1), true),
34 r(15, 2, Some(1), true),
35 r(16, 4, None, false),
36 r(17, 6, None, false),
37];
38const CAL: &[FieldRule] = &[
39 r(1, 6, None, true),
40 r(2, 6, None, true),
41 r(3, 5, Some(1), true),
42 r(4, 5, Some(1), false),
43 r(5, 5, Some(1), false),
44 r(6, 9, Some(1), false),
45 r(7, 9, Some(1), false),
46 r(8, 7, Some(32), false),
47 r(9, 8, Some(1), false),
48 r(10, 8, Some(1), false),
49];
50#[derive(Clone, Copy, Debug, Default)]
52pub struct SignalRegistry;
53impl DomainRegistry for SignalRegistry {
54 fn rules(&self, s: u16, v: u16) -> Option<&'static [FieldRule]> {
55 if v != 1 {
56 return None;
57 }
58 match s {
59 201 => Some(TRANSFORM),
60 202 => Some(CAL),
61 205 => Some(SHAPE),
62 210 => Some(SIGNAL),
63 200 | 203 | 204 | 206..=209 => {
64 crate::audio::qac::profiles::QacProfileRegistry.rules(s, v)
65 }
66 _ => None,
67 }
68 }
69 fn descriptor_target_schema(&self, key: u16) -> Option<u16> {
70 match key {
71 4 => Some(200),
72 5 => Some(201),
73 6 => Some(202),
74 _ => None,
75 }
76 }
77 fn validate_units(&self, units: &str, ctx: &Context<'_, '_>) -> Result<(), sem::Error> {
78 crate::audio::qac::profiles::QacProfileRegistry.validate_units(units, ctx)
79 }
80 fn validate_block(&self, b: Block<'_>, ctx: &Context<'_, '_>) -> Result<(), sem::Error> {
81 ctx.charge_work(
82 (b.fields().count() as u64)
83 .checked_mul(32)
84 .ok_or(sem::Error::Overflow)?,
85 )?;
86 match b.schema_type() {
87 201 => {
88 if let Some(w) = sem::field(b, 16) {
89 ctx.charge_work(w.value().len() as u64)?;
90 }
91 Transform::from_block(b).map_err(|_| sem::Error::Value)?;
92 }
93 205 => {
94 if ![
95 "urn:qualia:qac:pcm-preservation:1",
96 "urn:qualia:qac:stft-preservation:1",
97 "urn:qualia:qac:opaque-original:1",
98 ]
99 .contains(&text(b, 1)?)
100 {
101 return Err(sem::Error::UnsupportedDomain);
102 }
103 if text(b, 2)? != "1" {
104 return Err(sem::Error::Value);
105 }
106 let shape = get(b, 14)?;
107 if shape.element_count() == 0 || shape.element_count() > 3 {
108 return Err(sem::Error::Value);
109 }
110 let mut n = 1u64;
111 for raw in shape.value().chunks_exact(8) {
112 let x = u64::from_le_bytes(raw.try_into().unwrap());
113 if x == 0 {
114 return Err(sem::Error::Value);
115 }
116 n = n.checked_mul(x).ok_or(sem::Error::Overflow)?;
117 }
118 text(b, 15)?;
119 }
120 210 => {
121 text(b, 1)?;
122 text(b, 4)?;
123 let scale = f64v(b, 2)?;
124 let offset = f64v(b, 3)?;
125 if !scale.is_finite() || scale <= 0.0 || !offset.is_finite() {
126 return Err(sem::Error::Value);
127 }
128 match text(b, 5)? {
129 "physical-base-units" | "raw-counts" => {
130 if scale != 1.0 || offset != 0.0 {
131 return Err(sem::Error::Value);
132 }
133 }
134 "digital-unrelated-to-pressure" => {}
135 _ => return Err(sem::Error::Value),
136 }
137 }
138 202 => {
139 text(b, 1)?;
140 text(b, 2)?;
141 if f64v(b, 3)? == 0.0 {
142 return Err(sem::Error::Value);
143 }
144 if sem::field(b, 5).is_some() && f64v(b, 5)? <= 0.0 {
145 return Err(sem::Error::Value);
146 }
147 for key in [9, 10] {
148 if sem::field(b, key).is_some() {
149 ctx.require_schema(u32v(b, key)?, 5)?;
150 }
151 }
152 if sem::field(b, 9).is_some() && sem::field(b, 10).is_some() {
153 let a = ctx.require_schema(u32v(b, 9)?, 5)?;
154 let z = ctx.require_schema(u32v(b, 10)?, 5)?;
155 if u32v(a, 1)? != u32v(z, 1)? {
156 return Err(sem::Error::Time);
157 }
158 let at = u64v(a, 2)? as i64;
159 let zt = u64v(z, 2)? as i64;
160 if at > zt
161 || (at == zt
162 && u128::from(u64v(a, 3)?) * u128::from(u64v(z, 4)?)
163 >= u128::from(u64v(z, 3)?) * u128::from(u64v(a, 4)?))
164 {
165 return Err(sem::Error::Time);
166 }
167 }
168 }
169 200 | 203 | 204 | 206..=209 => {
170 crate::audio::qac::profiles::QacProfileRegistry.validate_block(b, ctx)?;
171 }
172 _ => return Err(sem::Error::UnsupportedDomain),
173 }
174 Ok(())
175 }
176}