Skip to main content

qualia_client_core/
audio_pipeline.rs

1//! Client-facing auditory Ears MVP + later swarm helpers.
2
3use base64::{engine::general_purpose::STANDARD as B64, Engine as _};
4use qualia_audio::capture::{CapturePurpose, CaptureSession};
5use qualia_audio::cross_modal::{
6    frames_to_media_ms, propose_temporal_correlations, TimeIntervalMs,
7};
8use qualia_audio::generation::{synthesize_reference_tone, VoiceConsent};
9use qualia_audio::pipeline::{
10    run_ears_demo, run_ears_on_wav_file, run_ears_weighted, section18_smoke, sonify_demo_to_wav,
11    speech_phone_demo, EarsDemoResult,
12};
13use qualia_audio::semantic::{human_correct_quin, human_reject_quin};
14use serde::Serialize;
15
16#[derive(Debug, Clone, Serialize)]
17pub struct EarsDemoDto {
18    pub sample_rate: u32,
19    pub frames: u32,
20    pub n_events: usize,
21    pub n_quins: usize,
22    pub model_hash: String,
23    pub media_hash: String,
24    pub is_reference: bool,
25    pub mel_frames: usize,
26    pub cqt_peak: f32,
27    pub event_instance_hashes: Vec<String>,
28    pub note: String,
29}
30
31impl From<EarsDemoResult> for EarsDemoDto {
32    fn from(r: EarsDemoResult) -> Self {
33        let instances: Vec<String> = r
34            .events
35            .iter()
36            .map(|e| {
37                format!(
38                    "0x{:016x}",
39                    e.source_hash ^ e.start_frame ^ e.end_frame.wrapping_mul(0x9e37_79b9)
40                )
41            })
42            .collect();
43        Self {
44            sample_rate: r.sample_rate,
45            frames: r.frames,
46            n_events: r.n_events,
47            n_quins: r.n_quins,
48            model_hash: format!("0x{:016x}", r.model_hash),
49            media_hash: format!("0x{:016x}", r.media_hash),
50            is_reference: r.is_reference,
51            mel_frames: r.mel_frames,
52            cqt_peak: r.cqt_peak,
53            event_instance_hashes: instances,
54            note: r.note,
55        }
56    }
57}
58
59/// EARS pipeline demo (self-marking synthetic input).
60pub fn ears_demo(storage_root: Option<&std::path::Path>) -> Result<EarsDemoDto, String> {
61    run_ears_demo(storage_root, 440.0, 16000, 300).map(Into::into)
62}
63
64/// Honest, machine-readable status of one audio capability (for Listen UI honesty chips).
65#[derive(Debug, Clone, Serialize)]
66pub struct AudioCapabilityDto {
67    pub id: String,
68    pub domain: String,
69    pub status: String,
70    pub zero_heap_hot: bool,
71    pub streaming: bool,
72    pub test_name: String,
73    pub note: String,
74}
75
76/// Snapshot the audio capability registry as serializable DTOs.
77pub fn audio_capabilities() -> Vec<AudioCapabilityDto> {
78    qualia_audio::capability_registry::CAPABILITIES
79        .iter()
80        .map(|c| AudioCapabilityDto {
81            id: c.id.to_string(),
82            domain: c.domain.as_str().to_string(),
83            status: c.status.as_str().to_string(),
84            zero_heap_hot: c.zero_heap_hot,
85            streaming: c.streaming,
86            test_name: c.test_name.to_string(),
87            note: c.note.to_string(),
88        })
89        .collect()
90}
91
92#[derive(Debug, Clone, Serialize)]
93pub struct CrossModalDemoDto {
94    pub n_correlations: usize,
95    pub asserts_causality_any: bool,
96    pub note: String,
97}
98
99/// Cross-modal pipeline demo.
100pub fn cross_modal_demo() -> CrossModalDemoDto {
101    let v = [TimeIntervalMs {
102        start_ms: 0,
103        end_ms: 2000,
104        instance: 0x15,
105    }];
106    let a_start = frames_to_media_ms(8000, 16000, 0);
107    let a = [TimeIntervalMs {
108        start_ms: a_start,
109        end_ms: a_start + 1000,
110        instance: 0xA0,
111    }];
112    let mut out = [qualia_audio::AvCorrelationProposal {
113        media_hash: 1,
114        visual_instance: 0,
115        auditory_instance: 0,
116        overlap_start_ms: 0,
117        overlap_end_ms: 0,
118        confidence_u16: 0,
119        asserts_causality: true,
120    }; 8];
121    let n = propose_temporal_correlations(1, &v, &a, &mut out);
122    CrossModalDemoDto {
123        n_correlations: n,
124        asserts_causality_any: out[..n].iter().any(|c| c.asserts_causality),
125        note: "Temporal overlap only — asserts_causality always false.".into(),
126    }
127}
128
129/// Reference-tone synth gated on voice consent.
130pub fn synth_with_consent(allow: bool) -> Result<u32, String> {
131    let c = if allow {
132        VoiceConsent::synthesis_only("demo-voice")
133    } else {
134        VoiceConsent::denied("demo-voice")
135    };
136    let mut o = [0.0f32; 512];
137    let r =
138        synthesize_reference_tone(c, 440.0, 16000, 512, 1, &mut o).map_err(|e| format!("{e:?}"))?;
139    Ok(r.frames)
140}
141
142/// EARS pipeline over a WAV file.
143pub fn ears_from_wav(
144    storage_root: Option<&std::path::Path>,
145    path: &std::path::Path,
146) -> Result<EarsDemoDto, String> {
147    run_ears_on_wav_file(storage_root, path).map(Into::into)
148}
149
150/// Section-18 smoke result as DTO.
151pub fn section18_smoke_dto() -> Result<String, String> {
152    section18_smoke()
153}
154
155/// Appends a human-reject attestation quin for an audio instance.
156pub fn audio_reject_instance(instance_hex: &str) -> Result<String, String> {
157    let inst = parse_hex(instance_hex)?;
158    let q = human_reject_quin(qualia_audio::q_hash("did:webizen:local-principal"), inst, 0);
159    Ok(format!(
160        "reject_quin parity=0x{:016x} instance=0x{:016x} (machine claim retained)",
161        q.parity, inst
162    ))
163}
164
165/// Appends a human-correct attestation quin for an audio instance.
166pub fn audio_correct_instance(instance_hex: &str, new_class_hex: &str) -> Result<String, String> {
167    let inst = parse_hex(instance_hex)?;
168    let cls = parse_hex(new_class_hex)?;
169    let q = human_correct_quin(
170        qualia_audio::q_hash("did:webizen:local-principal"),
171        inst,
172        cls,
173    );
174    Ok(format!(
175        "correct_quin parity=0x{:016x} new_class=0x{:016x}",
176        q.parity, cls
177    ))
178}
179
180fn parse_hex(s: &str) -> Result<u64, String> {
181    let t = s.trim().trim_start_matches("0x").trim_start_matches("0X");
182    u64::from_str_radix(t, 16).map_err(|e| e.to_string())
183}
184
185/// Weighted EARS demo.
186pub fn ears_weighted_demo(storage_root: Option<&std::path::Path>) -> Result<EarsDemoDto, String> {
187    run_ears_weighted(storage_root, 440.0, 16000, 300).map(Into::into)
188}
189
190/// U3-style hear: demo events → WAV data URL + optional file under storage.
191pub fn sonify_ears_demo(
192    storage_root: Option<&std::path::Path>,
193) -> Result<serde_json::Value, String> {
194    let r = run_ears_demo(None, 440.0, 16000, 400)?;
195    let wav = sonify_demo_to_wav(r.sample_rate, &r.events, r.frames as usize)?;
196    let mut path_out = None;
197    if let Some(root) = storage_root {
198        let dir = root.join("audio_hear");
199        std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
200        let p = dir.join(format!("sonify_{:016x}.wav", r.media_hash));
201        std::fs::write(&p, &wav).map_err(|e| e.to_string())?;
202        path_out = Some(p.display().to_string());
203    }
204    Ok(serde_json::json!({
205        "wav_data_url": format!("data:audio/wav;base64,{}", B64.encode(&wav)),
206        "path": path_out,
207        "n_events": r.n_events,
208        "frames": r.frames,
209        "sample_rate": r.sample_rate,
210        "note": "Parametric sonification of event intervals (U3-style navigate/hear). Not original PCM playback."
211    }))
212}
213
214/// Greedy phone-decode demo over seed speech weights (not full ASR).
215pub fn speech_demo(supported: bool) -> Result<serde_json::Value, String> {
216    let (n, model) = speech_phone_demo(supported)?;
217    Ok(serde_json::json!({
218        "tokens": n,
219        "model_hash": format!("0x{:016x}", model),
220        "language_supported": supported,
221        "note": if supported {
222            "Greedy phone decode over seed speech weights (not full ASR)."
223        } else {
224            "Unknown language: empty transcript (no silent map)."
225        }
226    }))
227}
228
229/// Capture policy demo: intent required before live ring accepts PCM.
230pub fn capture_policy_demo() -> Result<serde_json::Value, String> {
231    let mut s = CaptureSession::new(CapturePurpose::Analysis, 16000, 1);
232    let denied = s.start().is_err();
233    s.grant_intent();
234    s.start().map_err(|e| format!("{e:?}"))?;
235    let pushed = s.push_mono(&[0.1, 0.2, 0.3, 0.0]);
236    let mut out = [0.0f32; 8];
237    let pulled = s.pull_mono(&mut out);
238    Ok(serde_json::json!({
239        "denied_without_intent": denied,
240        "pushed": pushed,
241        "pulled": pulled,
242        "note": "Shell must call grant_intent before device/file stream. Hardware mic via shell push_mono."
243    }))
244}
245
246/// Seed and persist AED weights under storage_root/models/aed_seed.qaed
247pub fn ensure_aed_weights(storage_root: &std::path::Path) -> Result<String, String> {
248    let path = storage_root.join("models").join("aed_seed.qaed");
249    if path.is_file() {
250        let b = qualia_audio::AedWeightBundle::load_path(&path)?;
251        return Ok(format!(
252            "loaded AED weights hash=0x{:016x} path={}",
253            b.model_hash,
254            path.display()
255        ));
256    }
257    let b = qualia_audio::AedWeightBundle::from_seed(0xAED1);
258    b.save_path(&path)?;
259    Ok(format!(
260        "wrote AED seed weights hash=0x{:016x} path={}",
261        b.model_hash,
262        path.display()
263    ))
264}
265
266/// Ensures speech-weights.
267pub fn ensure_speech_weights(storage_root: &std::path::Path) -> Result<String, String> {
268    let path = storage_root.join("models").join("speech_seed.qspk");
269    if path.is_file() {
270        let w = qualia_audio::SpeechEncoderWeights::load_path(&path)?;
271        return Ok(format!(
272            "loaded speech weights hash=0x{:016x} path={}",
273            w.model_hash,
274            path.display()
275        ));
276    }
277    let w = qualia_audio::SpeechEncoderWeights::from_seed(7, 16);
278    w.save_path(&path)?;
279    Ok(format!(
280        "wrote speech seed weights hash=0x{:016x} path={}",
281        w.model_hash,
282        path.display()
283    ))
284}
285
286/// DAW history demo.
287pub fn daw_history_demo() -> Result<serde_json::Value, String> {
288    use qualia_audio::{OpKind, ProcessPlan, SessionHistory, SessionOp, TrackState};
289    let mut plan = ProcessPlan::new(48000, 64);
290    plan.add_track(TrackState::default());
291    let mut hist = SessionHistory::new();
292    hist.apply_and_record(
293        &mut plan,
294        SessionOp {
295            kind: OpKind::SetGain,
296            track: 0,
297            value_f32: 0.4,
298            value_bool: false,
299            prev_f32: 1.0,
300            prev_bool: false,
301        },
302    );
303    let g1 = plan.tracks[0].gain;
304    hist.undo(&mut plan);
305    let g0 = plan.tracks[0].gain;
306    hist.redo(&mut plan);
307    let g2 = plan.tracks[0].gain;
308    let mut lane = qualia_audio::AutomationLane::new(0);
309    lane.add(0, 0.0);
310    lane.add(1000, 1.0);
311    let mid = lane.value_at(500);
312    Ok(serde_json::json!({
313        "gain_after_set": g1,
314        "gain_after_undo": g0,
315        "gain_after_redo": g2,
316        "automation_mid": mid,
317        "note": "SessionHistory undo/redo + AutomationLane interp (cold path)."
318    }))
319}
320
321/// Run weighted AED on mono PCM (from mic pull or file).
322pub fn analyze_mono_pcm(
323    mono: &[f32],
324    sample_rate: u32,
325    storage_root: Option<&std::path::Path>,
326) -> Result<EarsDemoDto, String> {
327    use qualia_audio::types::{AuditoryEvent, AuditoryModel, TranscriptToken};
328    use qualia_audio::wav::encode_wav_i16_mono;
329    use qualia_audio::{AedWeightBundle, WeightedAedModel};
330
331    if mono.is_empty() {
332        return Err("no PCM (arm mic or import WAV first)".into());
333    }
334    let mut i16s = vec![0i16; mono.len()];
335    for (i, &s) in mono.iter().enumerate() {
336        i16s[i] = (s.clamp(-1.0, 1.0) * 32767.0) as i16;
337    }
338    let mut wav = vec![0u8; 44 + mono.len() * 2];
339    let wn = encode_wav_i16_mono(&i16s, sample_rate.max(8000), &mut wav)
340        .map_err(|e| format!("{e:?}"))?;
341    let decoded = qualia_audio::decode_wav(&wav[..wn]).map_err(|e| format!("{e:?}"))?;
342
343    let bundle = if let Some(root) = storage_root {
344        let path = root.join("models").join("aed_seed.qaed");
345        if path.is_file() {
346            AedWeightBundle::load_path(&path)?
347        } else {
348            let b = AedWeightBundle::from_seed(0xAED1);
349            let _ = b.save_path(&path);
350            b
351        }
352    } else {
353        AedWeightBundle::from_seed(0xAED1)
354    };
355
356    let mut model = WeightedAedModel::from_bundle(bundle);
357
358    let mut events = [AuditoryEvent::empty(); 32];
359    let mut tokens = [TranscriptToken::empty(); 8];
360    let mut emb = [0.0f32; 4];
361    let mut ws = [0u8; 8];
362    let counts = model
363        .infer_chunk(decoded.view(), &mut events, &mut tokens, &mut emb, &mut ws)
364        .map_err(|e| format!("{e:?}"))?;
365
366    let media_hash = qualia_audio::media_digest(&wav[..wn]).hash;
367    let mut quins = [qualia_audio::AudioQuin::with_parity(0, 0, 0, 0, 0); 64];
368    let n_q = qualia_audio::compile_auditory_quins(
369        qualia_audio::MediaDigest {
370            hash: media_hash,
371            byte_len: wn as u64,
372        },
373        &events[..counts.events],
374        model.model_hash(),
375        &mut quins,
376    );
377
378    Ok(EarsDemoDto {
379        sample_rate: decoded.sample_rate,
380        frames: mono.len() as u32,
381        n_events: counts.events,
382        n_quins: n_q,
383        model_hash: format!("0x{:016x}", model.model_hash()),
384        media_hash: format!("0x{:016x}", media_hash),
385        is_reference: false,
386        mel_frames: 0,
387        cqt_peak: 0.0,
388        event_instance_hashes: events[..counts.events]
389            .iter()
390            .map(|e| format!("0x{:016x}", e.source_hash ^ e.start_frame))
391            .collect(),
392        note: "Live/imported mono analyzed with disk AED weights (seed-shaped, not certified foundation)."
393            .into(),
394    })
395}
396
397/// One mixer track strip (UI ↔ process plan).
398#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
399pub struct MixerTrackDto {
400    pub name: String,
401    pub gain: f32,
402    pub pan: f32,
403    pub mute: bool,
404    pub solo: bool,
405    pub lowpass: f32,
406    pub eq_gain_db: f32,
407    pub eq_freq_hz: f32,
408    pub comp_threshold: f32,
409    pub comp_ratio: f32,
410    pub delay_samples: u32,
411    pub delay_mix: f32,
412}
413
414impl Default for MixerTrackDto {
415    fn default() -> Self {
416        Self {
417            name: "Track".into(),
418            gain: 0.85,
419            pan: 0.0,
420            mute: false,
421            solo: false,
422            lowpass: 0.0,
423            eq_gain_db: 0.0,
424            eq_freq_hz: 1000.0,
425            comp_threshold: 1.0,
426            comp_ratio: 1.0,
427            delay_samples: 0,
428            delay_mix: 0.0,
429        }
430    }
431}
432
433/// Default 3-strip mixer session for Listen UI.
434pub fn mixer_default_session() -> serde_json::Value {
435    let tracks = vec![
436        MixerTrackDto {
437            name: "Tone A".into(),
438            pan: -0.4,
439            ..Default::default()
440        },
441        MixerTrackDto {
442            name: "Tone B".into(),
443            pan: 0.4,
444            gain: 0.7,
445            ..Default::default()
446        },
447        MixerTrackDto {
448            name: "Pad".into(),
449            gain: 0.5,
450            lowpass: 0.25,
451            ..Default::default()
452        },
453    ];
454    serde_json::json!({
455        "sample_rate": 48000,
456        "block_frames": 64,
457        "tracks": tracks,
458        "note": "Reference mixer — not a commercial DAW. EQ/comp/delay are deterministic primitives."
459    })
460}
461
462/// Bounce a mixer session (synthetic tones per track) through ProcessPlan FX chain.
463pub fn mixer_bounce(tracks: &[MixerTrackDto]) -> Result<serde_json::Value, String> {
464    use qualia_audio::{ProcessPlan, TrackState};
465    let n = tracks.len().min(16).max(1);
466    let frames = 2048usize;
467    let sr = 48000u32;
468    let mut plan = ProcessPlan::new(sr, 64);
469    let mut mono_bufs: Vec<Vec<f32>> = Vec::with_capacity(n);
470    for (i, t) in tracks.iter().take(n).enumerate() {
471        plan.add_track(TrackState {
472            gain: t.gain.clamp(0.0, 2.0),
473            pan: t.pan.clamp(-1.0, 1.0),
474            mute: t.mute,
475            solo: t.solo,
476            lowpass: t.lowpass.clamp(0.0, 0.99),
477            eq_gain_db: t.eq_gain_db,
478            eq_freq_hz: t.eq_freq_hz.max(20.0),
479            comp_threshold: t.comp_threshold.clamp(0.05, 1.0),
480            comp_ratio: t.comp_ratio.max(1.0),
481            delay_samples: t.delay_samples.min(512),
482            delay_mix: t.delay_mix.clamp(0.0, 1.0),
483        });
484        let freq = 220.0 * (i as f32 + 1.0);
485        let mut buf = vec![0.0f32; frames];
486        for (s, sample) in buf.iter_mut().enumerate() {
487            *sample = (2.0 * core::f32::consts::PI * freq * s as f32 / sr as f32).sin() * 0.4;
488        }
489        mono_bufs.push(buf);
490    }
491    let refs: Vec<&[f32]> = mono_bufs.iter().map(|b| b.as_slice()).collect();
492    let mut out = vec![0.0f32; frames * 2];
493    let written = plan
494        .bounce_interleaved(&refs, &mut out)
495        .map_err(|e| e.to_string())?;
496    let peak = out
497        .iter()
498        .take(written * 2)
499        .map(|x| x.abs())
500        .fold(0.0f32, f32::max);
501    let energy: f32 = out.iter().take(written * 2).map(|x| x * x).sum();
502    Ok(serde_json::json!({
503        "frames_written": written,
504        "peak": peak,
505        "energy": energy,
506        "n_tracks": n,
507        "note": "Offline bounce of synthetic tones through mixer FX — reference quality only."
508    }))
509}
510
511/// Music analysis demo: onsets + tempo + structure (reference quality).
512pub fn music_analysis_demo() -> Result<serde_json::Value, String> {
513    use qualia_audio::{
514        detect_onsets, estimate_tempo_from_onsets, propose_structure_segments, MusicAssumptions,
515        OnsetEvent, StructureSegment,
516    };
517    let sr = 16000u32;
518    let mut mono = vec![0.0f32; 8000];
519    // Impulses ~2 Hz → ~120 BPM when folded
520    for k in 0..8 {
521        let i = (k * 4000 / 2) as usize;
522        if i < mono.len() {
523            mono[i] = 1.0;
524        }
525    }
526    let mut onsets = [OnsetEvent {
527        frame: 0,
528        strength: 0.0,
529    }; 32];
530    let n_on = detect_onsets(&mono, 256, 128, 0.01, &mut onsets);
531    let tempo = estimate_tempo_from_onsets(
532        &onsets[..n_on],
533        sr,
534        MusicAssumptions {
535            assumes_4_4: true,
536            assumes_12tet: false,
537            tuning_a4_hz: 440.0,
538        },
539    );
540    let mut segs = [StructureSegment {
541        start_frame: 0,
542        end_frame: 0,
543        mean_energy: 0.0,
544        label_hash: 0,
545    }; 8];
546    let n_seg = propose_structure_segments(&mono, 256, 128, &mut segs);
547    Ok(serde_json::json!({
548        "n_onsets": n_on,
549        "bpm": tempo.bpm,
550        "tempo_confidence": tempo.confidence,
551        "n_segments": n_seg,
552        "note": "Reference music analysis — not a production beat tracker."
553    }))
554}
555
556/// DAW FX chain demo (EQ + comp + delay on mono).
557pub fn daw_fx_demo() -> Result<serde_json::Value, String> {
558    use qualia_audio::{ProcessPlan, TrackState};
559    let mut tr = TrackState::default();
560    tr.eq_gain_db = 3.0;
561    tr.comp_threshold = 0.3;
562    tr.comp_ratio = 3.0;
563    tr.delay_samples = 48;
564    tr.delay_mix = 0.2;
565    let mono: Vec<f32> = (0..512)
566        .map(|i| (2.0 * core::f32::consts::PI * 440.0 * i as f32 / 48000.0).sin() * 0.5)
567        .collect();
568    let mut out = vec![0.0f32; 512];
569    let n = ProcessPlan::process_mono_fx(&tr, 48000, &mono, &mut out);
570    let energy: f32 = out.iter().map(|x| x * x).sum();
571    Ok(serde_json::json!({
572        "frames": n,
573        "energy": energy,
574        "note": "EQ+comp+delay offline path (deterministic FX primitives)."
575    }))
576}
577
578/// TTS consent + stem separation demo.
579pub fn gen_audio_demo() -> Result<serde_json::Value, String> {
580    use qualia_audio::{separate_two_stems_reference, synthesize_reference_tone, VoiceConsent};
581    let mut consent = VoiceConsent::synthesis_only("demo-voice");
582    let mut tone = [0.0f32; 512];
583    let rec = synthesize_reference_tone(consent, 440.0, 16000, 512, 7, &mut tone)
584        .map_err(|e| format!("{e:?}"))?;
585    consent.revoke(1);
586    let denied = synthesize_reference_tone(consent, 440.0, 16000, 512, 7, &mut tone).is_err();
587    let mut body = [0.0f32; 512];
588    let mut detail = [0.0f32; 512];
589    let (s0, s1) = separate_two_stems_reference(&tone, 0x00E0_D1A0_u64, &mut body, &mut detail)
590        .map_err(|e| format!("{e:?}"))?;
591    Ok(serde_json::json!({
592        "synth_frames": rec.frames,
593        "is_reference_synth": rec.is_reference_synth,
594        "revoke_denies": denied,
595        "stem_body": format!("0x{:016x}", s0.stem_class),
596        "stem_detail": format!("0x{:016x}", s1.stem_class),
597        "note": "Reference synth + sep; licensed TTS/demucs COMPLETE-WITH-GATE."
598    }))
599}
600
601/// Shared media clock + joint window demo.
602pub fn shared_clock_demo() -> Result<serde_json::Value, String> {
603    use qualia_audio::{events_overlapping_window, SharedMediaClock, TimeIntervalMs};
604    let clock = SharedMediaClock::new(0xC10C, 16000, 25.0);
605    let v_ms = clock.video_frame_to_ms(25);
606    let a_ms = clock.audio_frame_to_ms(16000);
607    let intervals = [
608        TimeIntervalMs {
609            start_ms: 0,
610            end_ms: 500,
611            instance: 1,
612        },
613        TimeIntervalMs {
614            start_ms: 800,
615            end_ms: 1200,
616            instance: 2,
617        },
618    ];
619    let win = TimeIntervalMs {
620        start_ms: 400,
621        end_ms: 900,
622        instance: 0,
623    };
624    let mut out = [0u64; 4];
625    let n = events_overlapping_window(&intervals, win, &mut out);
626    Ok(serde_json::json!({
627        "video_25_frames_ms": v_ms,
628        "audio_1s_ms": a_ms,
629        "drift_at_1s": clock.drift_ms(25, 16000),
630        "window_hits": n,
631        "asserts_causality": false,
632        "note": "Shared clock + joint interval query; overlap ≠ causality."
633    }))
634}
635
636/// Speech using disk weights if present.
637pub fn speech_from_disk(
638    storage_root: &std::path::Path,
639    supported: bool,
640) -> Result<serde_json::Value, String> {
641    use qualia_audio::{decode_for_language, SpeechEncoderWeights, TranscriptToken};
642    let path = storage_root.join("models").join("speech_seed.qspk");
643    let w = if path.is_file() {
644        SpeechEncoderWeights::load_path(&path)?
645    } else {
646        let w = SpeechEncoderWeights::from_seed(7, 16);
647        w.save_path(&path)?;
648        w
649    };
650    let mut mono = vec![0.0f32; 4096];
651    for i in 0..mono.len() {
652        mono[i] = (2.0 * core::f32::consts::PI * 180.0 * i as f32 / 16000.0).sin() * 0.25;
653    }
654    let mut tok = [TranscriptToken::empty(); 32];
655    let n =
656        decode_for_language(&w, &mono, 16000, supported, &mut tok).map_err(|e| format!("{e:?}"))?;
657    Ok(serde_json::json!({
658        "tokens": n,
659        "model_hash": format!("0x{:016x}", w.model_hash),
660        "weights_path": path.display().to_string(),
661        "language_supported": supported,
662        "note": "Speech weights loaded from disk when present."
663    }))
664}
665
666#[cfg(test)]
667mod tests {
668    use super::*;
669
670    #[test]
671    fn ears_dto() {
672        let d = ears_demo(None).unwrap();
673        assert!(d.n_events >= 1);
674        assert!(d.is_reference);
675        assert!(d.cqt_peak > 0.0);
676    }
677
678    #[test]
679    fn cross_modal_not_causal() {
680        let d = cross_modal_demo();
681        assert!(!d.asserts_causality_any);
682    }
683
684    #[test]
685    fn section18_ok() {
686        assert!(section18_smoke_dto().unwrap().contains("OK"));
687    }
688
689    #[test]
690    fn analyze_mono_pcm_seed() {
691        let mut mono = vec![0.0f32; 4096];
692        for i in 0..mono.len() {
693            mono[i] = (2.0 * core::f32::consts::PI * 440.0 * i as f32 / 16000.0).sin() * 0.3;
694        }
695        let d = analyze_mono_pcm(&mono, 16000, None).unwrap();
696        assert_eq!(d.sample_rate, 16000);
697        assert!(d.frames > 0);
698        assert!(!d.model_hash.is_empty());
699    }
700}