XSharp

Original SNES audio extraction

AudioExtractor / XSharp.Tools.Audio is an independent .NET 9 console/API. It renders the original X1/X2/X3 SPC700/DSP driver with an external Snes9x libretro core. It needs no XSharp engine, window, GPU or playback device. Supported headerless SHA1 identities: X1 USA 1.0 / Rev 1, X2 USA and X3 USA. Source ROM files are never modified.

Setup and cmd examples

powershell -NoProfile -File tools\Get-AudioExtractorCore.ps1
powershell -NoProfile -File tools\Get-AudioEncoder.ps1

The encoder helper downloads an external FFmpeg build from gyan.dev, linked by the official FFmpeg download page. Both native tools stay in %LOCALAPPDATA%\XSharp\AudioExtractor, with their own licenses, outside Git. Supply --core and --ffmpeg explicitly to use other compatible installations.

dotnet run --project AudioExtractor -- --rom "C:\ROMs\Megaman X (USA).sfc" --core "%LOCALAPPDATA%\XSharp\AudioExtractor\core\snes9x_libretro.dll" --output "D:\XSharpAudio\X1\Music" --music --format flac --ffmpeg "C:\Tools\ffmpeg.exe"

Formats: wav (default; PCM16 stereo), flac (lossless compressed PCM16) and mp3 (LAME VBR, --mp3-quality 0..9, default 2). MP3 exports at 44100 Hz and rescales loop coordinates; WAV/FLAC retain the core’s rate (32040 Hz in the validated core). MP3 decoder delay/padding handling depends on the player; FLAC is preferred for the native lossless asset collection.

Omit --music for effects. --ids 4,17,19 selects decimal command IDs, while filenames/manifests also retain hexadecimal IDs. --banks 66,71 selects static resource overlays. --catalog prints native song presets, sequence aliases and ordered effect contexts without starting emulation.

The music default is the introduction plus one combined native sequence cycle, or a finite track through its natural ending/release. --seconds is a safety cap (music default 1800, SFX default 10); reaching it is explicitly marked duration-limit. --no-loop-trim --seconds 30 requests a duration recording. The importer uses a 15-second SFX cap.

Native loops and aliases

MusicSequence reads the ROM’s eight big-endian voice pointers, note lengths, dotted/triplet flags, four repeat counters, conditional repeat exits, jumps, ends and tempo changes. A repeated control-flow/rhythm state establishes a voice cycle. Independent voice periods are combined by LCM: a longer compound cycle can legitimately contain several melodic repeats while other voices have not yet returned. Finite tracks are not treated as loops just because they contain a refrain.

Schema-3 manifests include sequence (native tick/tempo/voice analysis) and loop (start frame and exclusive end frame in decoded output PCM). Audio coordinates use the original sequencer’s 512-native-sample update grid, aligned to the first audible onset. This is approximately 16 ms at 32040 Hz, not a sample-exact recurrence of DSP echo, noise or envelopes. The precisionFrames and method fields make that limit explicit; source tick positions are retained for future refinement. A duration-truncated track is not given an out-of-file loop. Looping files end at the exported loop endpoint and contain no subsequent cycles. WAV additionally writes smpl loop data (inclusive end in that format); FLAC/MP3 receive LOOPSTART / LOOPEND tags.

Both effects and music start from a shared quiet CPU/SPC/DSP state. Static ROM banks are overlaid into validated serialized SPC resource RAM. Music uses the original $FB restart command after loading its sequence; native bank-upload duration therefore cannot create fake phase variants. The same canonical PCM and loop definition share a file even when requested by different presets. Genuine resource/program changes remain separate. No perceptual merging, amplitude normalization or approximate synthesis is used.

Regenerable local assets

powershell -NoProfile -File tools\Import-NativeAudio.ps1 -X1Rom "C:\ROMs\Megaman X (USA).sfc" -X2Rom "C:\ROMs\Megaman X2 (USA).sfc" -X3Rom "C:\ROMs\Megaman X3 (USA).sfc"
powershell -NoProfile -File tools\Test-NativeAudioAssets.ps1

The importer defaults to FLAC under XSharp/Assets/Sounds/Native/{X1,X2,X3}. These three generated directories, including per-game manifests and clip-store metadata, are ignored by Git. Only tool source, this documentation and validation summaries are versioned. Executable hosts import build/NativeAudioAssets.targets to copy locally present native assets to their own build/publish output. Libraries and tests do not receive transitive copies of the collection. No ROM or extraction core is required at playback. A checkout without these optional files can still build using the existing non-native assets.

-Format wav|flac|mp3, -Ffmpeg, -MusicSeconds, -EffectSeconds, -OnlyGame and -Resume configure imports. Resume checks schema/profile, ROM/core hashes, format, caps, banks, IDs and completed files. Incompatible older captures must be regenerated. --clip-store shares encoded SFX directly across contexts, avoiding thousands of redundant encoder invocations. Its .clip.json records retain PCM identity, encoder-specific key, file hash, sample count and format.

The verifier decodes each unique file with FFmpeg, checks frame counts and lossless PCM hashes, validates every catalog mapping, canonical state hash, loop bounds, duplicate and orphan file. MP3 is checked as lossy decoded PCM.

Research and tests

The original recompile-host and canonical-state investigations are recorded in docs/native-audio-deduplication.md. Format/control-flow research was checked against original ROM driver code and VGMTrans CapcomSnes at revision 00aa7fff8c0307a4d6d3f5611b25e6d6656091e4 (zlib license). No MIDI synthesis or recompilation-host source is embedded in the extractor. See docs/native-audio-loops.md for timing limits, references and Git cleanup.

dotnet test AudioExtractor.Tests -c Release covers native boot/queue/resource boundaries, PCM, canonical state, sequence flow/tempo and WAV sampler chunks. Set XSHARP_TEST_FFMPEG to enable the codec round-trip test. Full real-ROM validation is recorded in docs/native-audio-assets-validation.json.

Engine playback is documented in docs/native-audio-playback.md. Register a manifest entry with PrecacheSoundFromManifest, then use PlayOST(name) or PlaySound(channel, name) to apply its frame-based loop automatically. Explicit time overrides remain supported for authored recordings. The playback backend has no dependency on AudioExtractor or FFmpeg.

Identified effect names

dotnet run --project AudioExtractor -c Release -- --name-assets XSharp/Assets/Sounds/Native renames confirmed effects using the embedded Data/native-sound-names.json catalog. Each mapping is scoped to a game, SPC context and native command and retains its ROM call-site evidence. The manifest gains displayName and nameEvidence; IDs, waveform bytes, hashes and loop coordinates do not change. All aliases to a shared clip are updated together. Individual .clip.json manifests follow the audio filenames; a small .clip-index.json preserves lookup by the original encoding key, so resumed extraction reuses both files. Unknown commands keep their existing filenames. tools/Import-NativeAudio.ps1 applies naming after import. Run --verify-assets after manual naming, and clean/rebuild an already-built executable host to remove obsolete copied filenames.

Use --name-map <JSON> with --name-assets to write original/new filename documentation in JSON and Markdown (audio and individual manifests). By default it is written to <native-root>/sound-renames.json. The tracked filename map documents the installed collection.

Repeated native SFX research

--probe-sfx compares a single command, a repeat train and another single command from the same quiet native SPC/DSP state. It uses the original ROM driver through the headless libretro core; it does not generate or rename assets. Choose a canonical context listed by --catalog (equal bank sets have one canonical name).

AudioExtractor --rom "C:\ROMs\Megaman X2 (USA).sfc" --core snes9x_libretro.dll --probe-sfx 112 --context Song-15 --repeat-every 8 --repeat-count 8 --probe-frames 180 --report repeat70.json

IDs and repeat intervals are decimal. The JSON records ROM/core/state hashes, exact bank IDs, PCM hashes, the first differing stereo frame and the single cue’s audible bounds. A failed single/control comparison invalidates conclusions about repeats. A stable difference establishes that the repeated command affects that fixture; it does not identify restart versus overlap, priority, music interactions or panning. See the revision-pinned observation.

Equal effect-bank presets retain their original names in the catalog’s aliases list. Imports write a flat Sfx/context-aliases.json without duplicate media. tools/Import-NativeAudio.ps1 -MetadataOnly refreshes these two metadata files for matching existing captures without a core, encoder or audio writes. Runtime rules and validation are documented in native context aliases. The repeat probe also accepts aliases and reports their canonical context.