//! The board's sound at 96 kHz: the WSG mix plus the 54xx noise through a //! model of the analog network in MAME's `galaga_a.cpp` (`docs/sound-hardware.md`, //! "Filter network" and "What MAME's 54xx does for Galaga"): the resistor //! DAC, two of the three multiple-feedback band-passes (167 Hz Q 2.47 and //! 450 Hz Q 2.12), the 0.1 µF output coupling (15.9 Hz high-pass). //! //! The band gains are *fitted to MAME's recordings* of the power-on bang and //! a player death (2026-09-11), not derived: by MAME's source the MCU's //! noise nibble reaches only the 450 Hz channel, and simulating that chain //! on the MCU's actual output reproduces neither the level nor the spectrum //! of what MAME records, two thirds of whose energy lies at 100-300 Hz with //! no linear relation to the noise bits. What the ear gets is a 100-600 Hz //! boom with the measured envelope; this reproduces its band spectrum //! (L1 distance 0.19 over nine bands), RMS and peak within 10 %. use super::n54xx::N54xx; use super::roms::RomSet; use super::wsg::{self, Wsg}; pub const SAMPLE_RATE: u32 = wsg::SAMPLE_RATE; pub const SAMPLES_PER_FRAME: usize = wsg::SAMPLES_PER_FRAME; /// DAC output per channel-1 nibble: 4 V x 2,635 Ω x sum(bit_i / R_i), /// R = 47k, 22k, 10k, 4.7k for bits 0-3. fn dac_volts(nibble: u8) -> f32 { const WEIGHTS: [f32; 4] = [0.224, 0.479, 1.054, 2.243]; (0..4) .filter(|b| nibble & (1 << b) != 0) .map(|b| WEIGHTS[b]) .sum() } /// A biquad band-pass, constant 0 dB peak gain (RBJ), at the sample rate. struct BandPass { b0: f32, a1: f32, a2: f32, x1: f32, x2: f32, y1: f32, y2: f32, } impl BandPass { fn new(fc: f32, q: f32, fs: f32) -> BandPass { let w0 = 2.0 * std::f32::consts::PI * fc / fs; let alpha = w0.sin() / (2.0 * q); let a0 = 1.0 + alpha; BandPass { b0: alpha / a0, a1: -2.0 * w0.cos() / a0, a2: (1.0 - alpha) / a0, x1: 0.0, x2: 0.0, y1: 0.0, y2: 0.0, } } #[inline] fn run(&mut self, x: f32) -> f32 { let y = self.b0 * (x - self.x2) - self.a1 * self.y1 - self.a2 * self.y2; self.x2 = self.x1; self.x1 = x; self.y2 = self.y1; self.y1 = y; y } } pub struct Sound { pub wsg: Wsg, pub n54: N54xx, band_low: BandPass, band_high: BandPass, hp_x1: f32, hp_y1: f32, hp_a: f32, /// A multiplier on the fitted noise level, 1.0 by default. pub noise_gain: f32, } /// The two band-passes and their fitted gains, full-scale output per volt /// of DAC swing at each filter's peak (fit of 2026-09-11, see the module note). pub const BAND_LOW_FC: f32 = 167.0; pub const BAND_LOW_Q: f32 = 2.47; pub const BAND_LOW_GAIN: f32 = 0.379; pub const BAND_HIGH_FC: f32 = 450.0; pub const BAND_HIGH_Q: f32 = 2.12; pub const BAND_HIGH_GAIN: f32 = 0.126; impl Sound { pub fn new(roms: &RomSet) -> Sound { let fs = SAMPLE_RATE as f32; let rc = 0.1e-6 * 100_000.0; let dt = 1.0 / fs; Sound { wsg: Wsg::new(roms), n54: N54xx::new(), band_low: BandPass::new(BAND_LOW_FC, BAND_LOW_Q, fs), band_high: BandPass::new(BAND_HIGH_FC, BAND_HIGH_Q, fs), hp_x1: 0.0, hp_y1: 0.0, hp_a: rc / (rc + dt), noise_gain: 1.0, } } /// One frame of samples appended to `out`, applying the frame's timed /// WSG register writes and 54xx bytes at the samples they fall in. pub fn render_frame( &mut self, frame: u64, wsg_writes: &[(u64, u8, u8)], n54_writes: &[(u64, u8)], out: &mut Vec, ) { let base = frame * (SAMPLES_PER_FRAME as u64) * wsg::T_STATES_PER_SAMPLE; let mut next_w = 0; let mut next_n = 0; for i in 0..SAMPLES_PER_FRAME as u64 { let t = base + i * wsg::T_STATES_PER_SAMPLE; while next_w < wsg_writes.len() && wsg_writes[next_w].0 <= t { self.wsg.write(wsg_writes[next_w].1, wsg_writes[next_w].2); next_w += 1; } while next_n < n54_writes.len() && n54_writes[next_n].0 <= t { self.n54.write(n54_writes[next_n].1, n54_writes[next_n].0); next_n += 1; } self.n54.run_to(t); let dac = dac_volts(self.n54.output); let noise = (self.band_low.run(dac) * BAND_LOW_GAIN + self.band_high.run(dac) * BAND_HIGH_GAIN) * self.noise_gain; let x = self.wsg.sample() * wsg::ROUTE_GAIN + noise; // 15.9 Hz high-pass: the output coupling capacitor. let y = self.hp_a * (self.hp_y1 + x - self.hp_x1); self.hp_x1 = x; self.hp_y1 = y; out.push(y.clamp(-1.0, 1.0)); } for w in &wsg_writes[next_w..] { self.wsg.write(w.1, w.2); } for w in &n54_writes[next_n..] { self.n54.write(w.1, w.0); } } }