Skip to main content

qualia_audio/
generation.rs

1//! Swarm G_audio — reference tone synthesis, consent revoke, simple stem separation.
2//!
3//! Licensed TTS / neural separation remain COMPLETE-WITH-GATE (HA6/HA7).
4
5use crate::hash::q_hash;
6use crate::types::AudioError;
7
8#[derive(Debug, Clone, Copy)]
9pub struct VoiceConsent {
10    pub voice_asset_hash: u64,
11    pub allow_synthesis: bool,
12    pub allow_clone_training: bool,
13    /// Lamport-ish revoke epoch; synthesis denied if `now_epoch < revoked_before`.
14    pub revoked_before_epoch: u32,
15}
16
17impl VoiceConsent {
18    /// Synthesis-only consent.
19    pub fn synthesis_only(voice_id: &str) -> Self {
20        Self {
21            voice_asset_hash: q_hash(voice_id),
22            allow_synthesis: true,
23            allow_clone_training: false,
24            revoked_before_epoch: 0,
25        }
26    }
27
28    /// Denied result.
29    pub fn denied(voice_id: &str) -> Self {
30        Self {
31            voice_asset_hash: q_hash(voice_id),
32            allow_synthesis: false,
33            allow_clone_training: false,
34            revoked_before_epoch: 0,
35        }
36    }
37
38    /// Explicit revoke: future calls with `now_epoch >= revoked_before` still need
39    /// `allow_synthesis`; revoke sets allow_synthesis=false and stamps epoch.
40    pub fn revoke(&mut self, epoch: u32) {
41        self.allow_synthesis = false;
42        self.allow_clone_training = false;
43        self.revoked_before_epoch = epoch;
44    }
45
46    /// `true` when allowed-at.
47    pub fn is_allowed_at(&self, now_epoch: u32) -> bool {
48        self.allow_synthesis
49            && (self.revoked_before_epoch == 0 || now_epoch < self.revoked_before_epoch)
50    }
51}
52
53#[derive(Debug, Clone, Copy)]
54pub struct SynthReceipt {
55    pub model_hash: u64,
56    pub voice_hash: u64,
57    pub seed: u64,
58    pub sample_rate: u32,
59    pub frames: u32,
60    pub is_reference_synth: bool,
61}
62
63const MODEL: &str = "qualia-audio-ref-tone-synth-v1";
64
65/// Synthesize a simple multi-partial tone into mono f32.
66/// **Fails closed** if consent denies synthesis or is revoked.
67pub fn synthesize_reference_tone(
68    consent: VoiceConsent,
69    freq_hz: f32,
70    sample_rate: u32,
71    frames: usize,
72    seed: u64,
73    out: &mut [f32],
74) -> Result<SynthReceipt, AudioError> {
75    if !consent.allow_synthesis {
76        return Err(AudioError::PermissionDenied);
77    }
78    if out.len() < frames || sample_rate == 0 {
79        return Err(AudioError::OutputBufferTooSmall);
80    }
81    let phase0 = (seed as f32 / u64::MAX as f32) * core::f32::consts::TAU;
82    for i in 0..frames {
83        let t = i as f32 / sample_rate as f32;
84        let mut s = 0.0f32;
85        s += 0.5 * (core::f32::consts::TAU * freq_hz * t + phase0).sin();
86        s += 0.2 * (core::f32::consts::TAU * freq_hz * 2.0 * t + phase0).sin();
87        out[i] = s * 0.4;
88    }
89    Ok(SynthReceipt {
90        model_hash: q_hash(MODEL),
91        voice_hash: consent.voice_asset_hash,
92        seed,
93        sample_rate,
94        frames: frames as u32,
95        is_reference_synth: true,
96    })
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn deny_without_consent() {
105        let mut o = [0.0f32; 64];
106        let r = synthesize_reference_tone(VoiceConsent::denied("v1"), 440.0, 16000, 64, 1, &mut o);
107        assert!(matches!(r, Err(AudioError::PermissionDenied)));
108    }
109
110    #[test]
111    fn allow_synth() {
112        let mut o = [0.0f32; 256];
113        let r = synthesize_reference_tone(
114            VoiceConsent::synthesis_only("v1"),
115            440.0,
116            16000,
117            256,
118            2,
119            &mut o,
120        )
121        .unwrap();
122        assert!(r.is_reference_synth);
123        assert!(!r.allow_clone());
124    }
125}
126
127impl SynthReceipt {
128    /// Receipt never elevates clone rights (always false).
129    pub fn allow_clone(&self) -> bool {
130        false
131    }
132}
133
134/// Stem provenance for separation output (not evidence of source identity).
135#[derive(Debug, Clone, Copy)]
136pub struct StemReceipt {
137    pub model_hash: u64,
138    pub source_media_hash: u64,
139    pub stem_class: u64,
140    pub is_reference_separator: bool,
141}
142
143/// Very coarse spectral-band split: low band → "body", high residual → "detail".
144/// Reference separator only — not a licensed demucs-class model.
145pub fn separate_two_stems_reference(
146    mono: &[f32],
147    source_media_hash: u64,
148    body: &mut [f32],
149    detail: &mut [f32],
150) -> Result<(StemReceipt, StemReceipt), AudioError> {
151    let n = mono.len().min(body.len()).min(detail.len());
152    if n == 0 {
153        return Err(AudioError::OutputBufferTooSmall);
154    }
155    let mut z = 0.0f32;
156    let lp = 0.15f32;
157    for i in 0..n {
158        let s = mono[i];
159        z += lp * (s - z);
160        body[i] = z;
161        detail[i] = s - z;
162    }
163    for i in n..body.len() {
164        body[i] = 0.0;
165    }
166    for i in n..detail.len() {
167        detail[i] = 0.0;
168    }
169    let model = q_hash("qualia-audio-ref-sep-v1");
170    Ok((
171        StemReceipt {
172            model_hash: model,
173            source_media_hash,
174            stem_class: q_hash("https://ns.webizen.org/q42/audio/stem/body"),
175            is_reference_separator: true,
176        },
177        StemReceipt {
178            model_hash: model,
179            source_media_hash,
180            stem_class: q_hash("https://ns.webizen.org/q42/audio/stem/detail"),
181            is_reference_separator: true,
182        },
183    ))
184}
185
186#[cfg(test)]
187mod sep_tests {
188    use super::*;
189
190    #[test]
191    fn revoke_blocks_synth() {
192        let mut c = VoiceConsent::synthesis_only("v1");
193        c.revoke(100);
194        assert!(!c.allow_synthesis);
195        let mut o = [0.0f32; 32];
196        assert!(matches!(
197            synthesize_reference_tone(c, 440.0, 16000, 32, 1, &mut o),
198            Err(AudioError::PermissionDenied)
199        ));
200    }
201
202    #[test]
203    fn separation_splits_energy() {
204        let mut mono = [0.0f32; 256];
205        for i in 0..256 {
206            mono[i] = (i as f32 * 0.1).sin() + (i as f32 * 0.7).sin() * 0.3;
207        }
208        let mut body = [0.0f32; 256];
209        let mut detail = [0.0f32; 256];
210        let (a, b) = separate_two_stems_reference(&mono, 42, &mut body, &mut detail).unwrap();
211        assert!(a.is_reference_separator && b.is_reference_separator);
212        let e_b: f32 = body.iter().map(|x| x * x).sum();
213        let e_d: f32 = detail.iter().map(|x| x * x).sum();
214        assert!(e_b > 0.0 && e_d > 0.0);
215    }
216}