Skip to content

Fix the audio pop on game start / exit and Genesis save-state sound - #137

Merged
finger563 merged 3 commits into
mainfrom
fix/audio-pops-and-genesis-saves
Nov 13, 2026
Merged

finger563 merged 3 commits into
mainfrom
fix/audio-pops-and-genesis-saves

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Audio pop when a game starts or exits

Every core sets its own sample rate on start and restores 48 kHz on exit. Each switch stops the I2S clocks and reprograms the ES8311's clock dividers under a live DAC, which the speaker played as a pop.

BoxEmu::audio_sample_rate() now:

  • skips a switch to the rate already set;
  • for a real switch, holds the speaker amplifier off, clears the queued audio, changes the rate, waits 50 ms for the DAC to settle on silence, then re-enables the amplifier.

Genesis: silent Sonic 2 after resuming a save, and heap corruption

Bumps components/genesis/gwenesis to esp-cpp/gwenesis#1. A save state from another firmware build restored stale YM2612 pointers, so the FM output was silent (even after an emulator reset) and stray writes corrupted the heap. This was the intermittent TLSF assert / MMU fault seen while testing #133 and the follow-ups.

The submodule points at the gwenesis PR branch commit (d984bd5). If that PR is squash-merged, re-pin it to the merge commit on esp32 before merging this one.

Testing (ESP32-S3-BOX-3)

  • Sonic 2 resumed from an old save: the sound is correct immediately, and there was no crash in a minute of play.
  • No pop when starting or leaving the emulators (both confirmed by ear).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

- BoxEmu::audio_sample_rate(): skip a switch to the current rate, and hold
  the speaker amplifier off across a real switch (the I2S clocks stop and the
  ES8311 dividers are reprogrammed, which popped every time a core set its
  rate and again when it restored 48 kHz)
- gwenesis: rebuild the YM2612 operator routing / detune pointers and keep
  this build's audio rates after loading a save state; a save from another
  build left the FM output silent and wrote to stray memory

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
Copilot AI balanced review requested due to automatic review settings November 13, 2026 17:44
@github-actions

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The audio changes call methods unavailable in the declared espp/esp-box 1.3.8 dependency, causing compilation to fail with that supported release.

1 open issue
What changed in this PR

The reviewed changes address audio pops when emulators start or exit by centralizing sample-rate switching in BoxEmu.

Changes:

  • Skip invalid rates and switches to the current rate.
  • Disable the speaker amplifier, clear queued audio, and wait 50 ms after switching before re-enabling it.
  • Move the implementation out of the header and document its behavior.
File Description
components/​box-emu/​src/​box-emu.cpp Implements guarded sample-rate switching with amplifier control and a settling delay.
components/​box-emu/​include/​box-emu.hpp Replaces the inline setter with a documented declaration.

Review effort: Balanced

Comment on lines +592 to +596
bsp.enable_audio(false);
bsp.clear_audio();
bsp.audio_sample_rate(rate);
std::this_thread::sleep_for(settle_time);
bsp.enable_audio(true);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the requirement: enable_audio() / clear_audio() came with the espp::Audio move (espp #889, first in v1.4.1), so f63fc69 raises it to espp/esp-box >=1.4.1 instead of switching back to the deprecated enable_sound().

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

ESP-IDF Size Report for 'Esp Box Emu'

Metric Base PR Delta
FLASH 4,215,556 bytes (67.00%) 4,215,856 bytes (67.01%) 🔺 +300 bytes (+0.01%)
DRAM 170,565 bytes (49.91%) 170,565 bytes (49.91%) 0 bytes (0.00%)
IRAM 0 bytes 0 bytes 0 bytes
RAM (DRAM+IRAM) 170,565 bytes 170,565 bytes 0 bytes (0.00%)

FLASH uses app .bin size or json2 flash sum. RAM sums DRAM+IRAM via idf_size. Percentages shown when totals are available.
DRAM/IRAM usage does not include memory used by the heap allocator at runtime.
This report was generated by esp-idf-size-delta.

finger563 and others added 2 commits November 13, 2026 16:00
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
@finger563
finger563 merged commit 81b196c into main Nov 13, 2026
3 checks passed
@finger563
finger563 deleted the fix/audio-pops-and-genesis-saves branch November 13, 2026 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants