fix(audio): fix DMC loop byte skip, add DC blocker, lazy cpal stream
Some checks failed
CI / rust (push) Has been cancelled

Three audio bugs fixed:

1. DMC loop mode skipped the last byte of each sample iteration.
   provide_dmc_dma_byte() was immediately setting dmc_dma_request on
   loop restart while the sample buffer was still full, causing the
   while-loop in clock_cpu_cycles to service a second DMA immediately
   and overwrite the valid buffer. Per NES hardware spec, the reader
   only fills an empty buffer — the request is now left to clock_dmc
   when the output unit actually empties the buffer into the shift
   register. Fixes intermittent clicking/crackling in games that use
   looped DMC samples (BGM, SFX).

2. Missing DC blocker (high-pass filter) in AudioMixer. The NES APU
   has a capacitor-coupled output stage that blocks DC bias. Without
   it, abrupt channel state changes (length counter expiry, sweep
   mute, triangle period < 2) produce DC steps that manifest as
   audible clicks. Added a one-pole IIR high-pass filter at ~5 Hz
   applied after the existing low-pass filter.

3. cpal stream was opened at application startup with
   BufferSize::Fixed(256), forcing PipeWire/PulseAudio to run the
   entire audio graph at a 5.3 ms quantum. This disrupted other audio
   applications (browsers, media players) even when no ROM was loaded.
   Fixed by: (a) creating the stream lazily on the first push_samples
   call so no device is touched until a ROM is running, and (b)
   switching to BufferSize::Default so the audio server chooses the
   quantum instead of the emulator imposing one. Ring buffer capacity
   increased from 1536 to 4096 samples to absorb larger server quanta.
This commit is contained in:
2026-03-15 10:41:19 +03:00
committed by Se.Cherkasov
parent d8f41bc2c9
commit d94fbb894b
3 changed files with 59 additions and 20 deletions

View File

@@ -11,12 +11,21 @@ pub struct AudioMixer {
// Coefficient: a = exp(-2π * fc / fs). At fc=14000, fs=48000: a ≈ 0.160
lp_coeff: f32,
lp_state: f32,
// One-pole IIR high-pass filter (DC blocker). Removes the DC bias that
// accumulates when APU channels switch state, preventing audible clicks and
// pops. Approximates the NES capacitor-coupled output stage (~5 Hz cutoff).
// Formula: y[n] = hp_coeff * y[n-1] + x[n] - x[n-1]
// Coefficient: a = exp(-2π * fc / fs). At fc=5, fs=48000: a ≈ 0.99935.
hp_coeff: f32,
hp_prev_x: f32,
hp_prev_y: f32,
}
impl AudioMixer {
pub fn new(sample_rate: u32, mode: VideoMode) -> Self {
let cpu_hz = mode.cpu_hz();
let lp_coeff = (-2.0 * std::f64::consts::PI * 14_000.0 / sample_rate as f64).exp() as f32;
let hp_coeff = (-2.0 * std::f64::consts::PI * 5.0 / sample_rate as f64).exp() as f32;
Self {
sample_rate,
samples_per_cpu_cycle: sample_rate as f64 / cpu_hz,
@@ -24,6 +33,9 @@ impl AudioMixer {
last_output_sample: 0.0,
lp_coeff,
lp_state: 0.0,
hp_coeff,
hp_prev_x: 0.0,
hp_prev_y: 0.0,
}
}
@@ -35,6 +47,8 @@ impl AudioMixer {
self.sample_accumulator = 0.0;
self.last_output_sample = 0.0;
self.lp_state = 0.0;
self.hp_prev_x = 0.0;
self.hp_prev_y = 0.0;
}
pub fn push_cycles(&mut self, cpu_cycles: u32, channels: ChannelOutputs, out: &mut Vec<f32>) {
@@ -56,17 +70,23 @@ impl AudioMixer {
let a = self.lp_coeff;
let b = 1.0 - a;
if samples == 1 {
let s = a * self.lp_state + b * sample;
self.lp_state = s;
out.push(s);
let lp = a * self.lp_state + b * sample;
self.lp_state = lp;
let hp = self.hp_coeff * self.hp_prev_y + lp - self.hp_prev_x;
self.hp_prev_x = lp;
self.hp_prev_y = hp;
out.push(hp);
} else {
let denom = samples as f32;
for idx in 0..samples {
let t = (idx + 1) as f32 / denom;
let interp = start + (sample - start) * t;
let s = a * self.lp_state + b * interp;
self.lp_state = s;
out.push(s);
let lp = a * self.lp_state + b * interp;
self.lp_state = lp;
let hp = self.hp_coeff * self.hp_prev_y + lp - self.hp_prev_x;
self.hp_prev_x = lp;
self.hp_prev_y = hp;
out.push(hp);
}
}
self.last_output_sample = sample;