Skip to main content

qualia_audio/
semantic.rs

1//! Auditory observations → NQuin-compatible records (epistemic).
2
3use crate::hash::{q_hash, MediaDigest};
4use crate::types::AuditoryEvent;
5
6pub const P_AUDITORY_OBSERVATION: &str = "https://ns.webizen.org/q42/AuditoryObservation";
7pub const P_PROPOSES_SOUND_CLASS: &str = "https://ns.webizen.org/q42/proposesSoundClass";
8pub const P_MODEL_DIGEST: &str = "https://ns.webizen.org/q42/modelDigest";
9pub const P_HUMAN_REJECTS: &str = "https://ns.webizen.org/q42/humanRejects";
10pub const P_HUMAN_CORRECTS: &str = "https://ns.webizen.org/q42/humanCorrectsClass";
11pub const CTX_AUDIO: &str = "https://ns.webizen.org/q42/audio-observation";
12pub const CTX_HUMAN: &str = "https://ns.webizen.org/q42/human-attestation";
13
14pub const MAX_OBS_QUINS: usize = 256;
15
16#[repr(C)]
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub struct AudioQuin {
19    pub subject: u64,
20    pub predicate: u64,
21    pub object: u64,
22    pub context: u64,
23    pub metadata: u64,
24    pub parity: u64,
25}
26
27impl AudioQuin {
28    /// Builder — sets parity.
29    pub fn with_parity(s: u64, p: u64, o: u64, c: u64, m: u64) -> Self {
30        Self {
31            subject: s,
32            predicate: p,
33            object: o,
34            context: c,
35            metadata: m,
36            parity: s ^ p ^ o ^ c ^ m,
37        }
38    }
39}
40
41/// Compiles auditory quins.
42pub fn compile_auditory_quins(
43    media: MediaDigest,
44    events: &[AuditoryEvent],
45    model_hash: u64,
46    out: &mut [AudioQuin],
47) -> usize {
48    if out.is_empty() {
49        return 0;
50    }
51    let mut w = 0usize;
52    out[w] = AudioQuin::with_parity(
53        media.hash,
54        q_hash(P_MODEL_DIGEST),
55        model_hash,
56        q_hash(CTX_AUDIO),
57        media.byte_len,
58    );
59    w += 1;
60    let ctx = q_hash(CTX_AUDIO) ^ model_hash;
61    for e in events {
62        if e.class_hash == 0 && e.confidence_u16 == 0 {
63            continue;
64        }
65        if w + 2 > out.len() {
66            break;
67        }
68        let inst = e.source_hash ^ e.start_frame ^ (e.end_frame.wrapping_mul(0x9e37_79b9));
69        let meta = (e.confidence_u16 as u64)
70            | ((e.start_frame & 0xFFFF) << 16)
71            | ((e.end_frame & 0xFFFF) << 32);
72        out[w] =
73            AudioQuin::with_parity(media.hash, q_hash(P_AUDITORY_OBSERVATION), inst, ctx, meta);
74        w += 1;
75        out[w] = AudioQuin::with_parity(
76            inst,
77            q_hash(P_PROPOSES_SOUND_CLASS),
78            e.class_hash,
79            ctx,
80            e.confidence_u16 as u64,
81        );
82        w += 1;
83    }
84    w
85}
86
87/// Human-reject attestation quin.
88pub fn human_reject_quin(human: u64, instance: u64, reason: u64) -> AudioQuin {
89    AudioQuin::with_parity(
90        human,
91        q_hash(P_HUMAN_REJECTS),
92        instance,
93        q_hash(CTX_HUMAN),
94        reason,
95    )
96}
97
98/// Human-correct attestation quin.
99pub fn human_correct_quin(human: u64, instance: u64, new_class: u64) -> AudioQuin {
100    AudioQuin::with_parity(
101        human,
102        q_hash(P_HUMAN_CORRECTS),
103        new_class,
104        q_hash(CTX_HUMAN) ^ instance,
105        instance,
106    )
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112    use crate::types::AuditoryEvent;
113
114    #[test]
115    fn compile_digest_and_events() {
116        let mut e = AuditoryEvent::empty();
117        e.class_hash = 9;
118        e.confidence_u16 = 1000;
119        e.start_frame = 0;
120        e.end_frame = 100;
121        e.source_hash = 1;
122        let mut out = [AudioQuin::with_parity(0, 0, 0, 0, 0); 8];
123        let n = compile_auditory_quins(
124            MediaDigest {
125                hash: 5,
126                byte_len: 10,
127            },
128            &[e],
129            7,
130            &mut out,
131        );
132        assert_eq!(n, 3);
133        assert_eq!(out[0].predicate, q_hash(P_MODEL_DIGEST));
134    }
135}