qualia_core_db/wgsl_forge/audio/
mel.rs1use crate::wgsl_forge::ForgeError;
3pub use etalon_wgsl_forge::numerical::audio::mel::{mel_apply_cpu, mel_apply_into, MelShape, PreparedMel, MEL_APPLY_ENTRY, MEL_APPLY_WGSL};
4
5pub fn mel_apply_forge(spectrum: &[f32], mel_fb: &[f32], n_frames: usize,
6 n_bins: usize, n_mel: usize) -> Result<Vec<f32>, ForgeError> {
7 use crate::wgsl_forge::execute::WgpuComputeContext;
8 use etalon_wgsl_forge::numerical::audio::mel::{mel_apply_with_context, mel_gpu_capacity};
9 let capacity = mel_gpu_capacity(spectrum, mel_fb, MelShape { n_frames, n_bins, n_mel })?;
10 let shared = crate::gpu_context::device_registry::try_auxiliary_gpu().ok_or_else(|| {
11 ForgeError::GpuUnavailable(
12 "mel_apply_forge: no GPU circuit available (auxiliary→primary both absent)".to_string(),
13 )
14 })?;
15 let mut ctx = WgpuComputeContext::from_device(
16 shared.device.clone(), shared.queue.clone(), &shared.adapter_caps, capacity,
17 )?;
18 mel_apply_with_context(&mut ctx, spectrum, mel_fb, n_frames, n_bins, n_mel)
19}
20
21pub fn mel_apply(
28 spectrum: &[f32],
29 mel_fb: &[f32],
30 n_frames: usize,
31 n_bins: usize,
32 n_mel: usize,
33) -> Vec<f32> {
34 if crate::wgsl_forge::dispatch::caps().wgpu {
35 if let Ok(out) = mel_apply_forge(spectrum, mel_fb, n_frames, n_bins, n_mel) {
36 return out;
37 }
38 }
41 mel_apply_cpu(spectrum, mel_fb, n_frames, n_bins, n_mel)
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47 #[test]
50 fn mel_apply_public_matches_cpu() {
51 let (n_frames, n_bins, n_mel) = (3usize, 5usize, 4usize);
52 let spectrum: Vec<f32> = (0..n_frames * n_bins)
53 .map(|k| (k as f32) * 0.5 - 3.0)
54 .collect();
55 let mut mel_fb = vec![0.0f32; n_mel * n_bins];
57 for m in 0..n_mel {
58 for b in 0..n_bins {
59 mel_fb[m * n_bins + b] = ((m + b) as f32 % 3.0) * 0.25;
60 }
61 }
62 let public = mel_apply(&spectrum, &mel_fb, n_frames, n_bins, n_mel);
63 let oracle = mel_apply_cpu(&spectrum, &mel_fb, n_frames, n_bins, n_mel);
64 assert_eq!(public.len(), oracle.len());
65 for (p, o) in public.iter().zip(oracle.iter()) {
66 let tol = 1e-3 * o.abs().max(1.0);
67 assert!((p - o).abs() <= tol, "public/CPU mismatch: {p} vs {o}");
68 }
69 }
70
71 #[test]
74 #[serial_test::serial(gpu)]
75 fn mel_gpu_matches_oracle() {
76 if !crate::wgsl_forge::test_gpu_available() {
77 return;
78 }
79 if let Some(shared) = crate::gpu_context::device_registry::try_auxiliary_gpu() {
82 let caps = &shared.adapter_caps;
83 eprintln!(
84 "mel_gpu_matches_oracle: forge on adapter '{}' ({:?}, {:?})",
85 caps.name, caps.device_type, caps.backend
86 );
87 }
88 let (n_frames, n_bins, n_mel) = (17usize, 33usize, 12usize);
89 let spectrum: Vec<f32> = (0..n_frames * n_bins)
90 .map(|k| ((k as f32) * 0.017).sin().abs() + 0.001)
91 .collect();
92 let mut mel_fb = vec![0.0f32; n_mel * n_bins];
94 for m in 0..n_mel {
95 let centre = (m as f32 + 1.0) * (n_bins as f32) / (n_mel as f32 + 1.0);
96 for b in 0..n_bins {
97 let w = 1.0 - ((b as f32 - centre).abs() / 3.0);
98 mel_fb[m * n_bins + b] = w.max(0.0);
99 }
100 }
101 let expected = mel_apply_cpu(&spectrum, &mel_fb, n_frames, n_bins, n_mel);
102 let gpu = mel_apply_forge(&spectrum, &mel_fb, n_frames, n_bins, n_mel)
103 .expect("mel_apply_forge on an available device");
104 assert_eq!(gpu.len(), expected.len());
105 for (g, e) in gpu.iter().zip(expected.iter()) {
106 let tol = 1e-3 * e.abs().max(1.0);
107 assert!((g - e).abs() <= tol, "GPU/CPU mismatch: {g} vs {e}");
108 }
109 }
110}