use std::f32::consts::PI; pub const EQ_FREQS: [f32; 10] = [ 32.0, 64.0, 125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0, 16000.0, ]; pub const EQ_LABELS: [&str; 10] = [ "32", "64", "125", "250", "500", "1k", "2k", "4k", "8k", "16k", ]; pub struct EqPreset { pub name: &'static str, pub gains: [f32; 10], } pub const EQ_PRESETS: &[EqPreset] = &[ EqPreset { name: "Плоский", gains: [0.0; 10] }, EqPreset { name: "Бас+", gains: [6.0, 5.5, 4.0, 2.0, 0.0, 0.0, 0.5, 1.0, 1.5, 1.5] }, EqPreset { name: "Поп", gains: [-1.0, 1.5, 3.5, 4.0, 2.0, -0.5, -1.0, 1.0, 2.5, 3.0] }, EqPreset { name: "Рок", gains: [4.5, 3.0, 1.5, -1.0, -1.5, 1.0, 3.5, 4.5, 5.0, 4.0] }, EqPreset { name: "Электроника", gains: [5.0, 4.0, 1.5, -1.5, -1.0, 1.5, 3.0, 4.0, 4.5, 5.0] }, EqPreset { name: "Вокал", gains: [-2.0, -1.5, 0.0, 2.5, 4.5, 4.5, 3.0, 1.0, -0.5, -1.0] }, EqPreset { name: "Джаз", gains: [3.0, 2.0, 1.0, 2.0, -1.0, -1.0, 0.0, 1.5, 2.5, 3.0] }, ]; #[derive(Clone, Copy)] struct Biquad { b0: f32, b1: f32, b2: f32, a1: f32, a2: f32, z1_l: f32, z2_l: f32, z1_r: f32, z2_r: f32, } impl Biquad { fn identity() -> Self { Self { b0: 1.0, b1: 0.0, b2: 0.0, a1: 0.0, a2: 0.0, z1_l: 0.0, z2_l: 0.0, z1_r: 0.0, z2_r: 0.0, } } fn set_peaking(&mut self, sample_rate: f32, freq: f32, q: f32, gain_db: f32) { if gain_db.abs() < 0.05 || freq >= sample_rate * 0.48 { self.b0 = 1.0; self.b1 = 0.0; self.b2 = 0.0; self.a1 = 0.0; self.a2 = 0.0; return; } let a = 10.0f32.powf(gain_db / 40.0); let w0 = 2.0 * PI * freq / sample_rate; let cos_w0 = w0.cos(); let alpha = w0.sin() / (2.0 * q); let b0 = 1.0 + alpha * a; let b1 = -2.0 * cos_w0; let b2 = 1.0 - alpha * a; let a0 = 1.0 + alpha / a; let a1 = -2.0 * cos_w0; let a2 = 1.0 - alpha / a; self.b0 = b0 / a0; self.b1 = b1 / a0; self.b2 = b2 / a0; self.a1 = a1 / a0; self.a2 = a2 / a0; } #[inline(always)] fn process_stereo(&mut self, l: f32, r: f32) -> (f32, f32) { let out_l = self.b0 * l + self.z1_l; self.z1_l = self.b1 * l - self.a1 * out_l + self.z2_l; self.z2_l = self.b2 * l - self.a2 * out_l; let out_r = self.b0 * r + self.z1_r; self.z1_r = self.b1 * r - self.a1 * out_r + self.z2_r; self.z2_r = self.b2 * r - self.a2 * out_r; (out_l, out_r) } } pub struct Equalizer10Band { bands: [Biquad; 10], enabled: bool, sample_rate: f32, gains_db: [f32; 10], } impl Equalizer10Band { pub fn new(sample_rate: f32) -> Self { Self { bands: [Biquad::identity(); 10], enabled: false, sample_rate, gains_db: [0.0; 10], } } pub fn update(&mut self, enabled: bool, gains_db: [f32; 10]) { self.enabled = enabled; if self.gains_db != gains_db { self.gains_db = gains_db; for i in 0..10 { self.bands[i].set_peaking(self.sample_rate, EQ_FREQS[i], 1.41, gains_db[i]); } } } #[inline(always)] pub fn process(&mut self, mut l: f32, mut r: f32) -> (f32, f32) { if !self.enabled { return (l, r); } for band in &mut self.bands { (l, r) = band.process_stereo(l, r); } (l.clamp(-1.0, 1.0), r.clamp(-1.0, 1.0)) } } /// Real-time zero-allocation audio energy & bass analyzer feeding the Vulkan My Vibe shader. pub struct AudioEnergyAnalyzer { lp_state: f32, energy_smooth: f32, bass_smooth: f32, } impl AudioEnergyAnalyzer { pub fn new() -> Self { Self { lp_state: 0.0, energy_smooth: 0.0, bass_smooth: 0.0, } } #[inline(always)] pub fn push_sample(&mut self, mono: f32) { // 1-pole low-pass (~150 Hz at 48 kHz) to isolate kick/bass punch self.lp_state += 0.019 * (mono - self.lp_state); let abs_full = mono.abs(); let abs_bass = self.lp_state.abs(); // Fast punchy attack + musical decay for expressive My Vibe orb pulsation let atk = 0.012; let rel = 0.0007; self.energy_smooth += if abs_full > self.energy_smooth { atk * (abs_full - self.energy_smooth) } else { rel * (abs_full - self.energy_smooth) }; self.bass_smooth += if abs_bass > self.bass_smooth { (atk * 1.8) * (abs_bass - self.bass_smooth) } else { (rel * 1.6) * (abs_bass - self.bass_smooth) }; } pub fn energy(&self) -> f32 { (self.energy_smooth * 3.8).clamp(0.0, 1.0) } pub fn bass(&self) -> f32 { (self.bass_smooth * 5.2).clamp(0.0, 1.0) } }