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.
- Fix frame counter running at 2× speed: clock_frame_counter now skips
odd CPU cycles (APU cycle = CPU/2), so envelope, sweep, and length
counters tick at the correct rate. Fixes sweep-driven whistle in Megaman II.
- Switch audio sampling to per-CPU-cycle granularity in
run_until_frame_complete_with_audio to eliminate square-wave harmonic
aliasing caused by sampling only once per instruction.
- Add IIR one-pole low-pass filter (~14 kHz) to AudioMixer to smooth
abrupt level transitions (crackling) introduced by correct envelope timing.
- Mute triangle channel when timer_period < 2 (≥27 kHz), which aliases
into the audible range at 48 kHz. Real NES RC circuit removes these
ultrasonics; emulator must suppress them explicitly.
- Update all APU bus tests to use correct (doubled) CPU cycle counts.