soundlog
soundlog — builder, parser and stream-processor for retro sound-chip register-write logs
soundlog is a small crate for building and parsing register-write
logs for retro sound chips. It currently supports the VGM
(Video Game Music) file format.
Key features:
- Builder API to construct VGM documents programmatically.
- Parser support to read VGM data into a structured
VgmDocument. - Type-safe APIs: chip specifications and VGM commands are modeled as Rust types to help prevent invalid register writes at compile time.
- Stream processing:
VgmStreamprovides a low-memory, iterator-based processor that can accept either chunked binary input (viapush_chunk) or a pre-parsedVgmDocument(viafrom_document) and yields parsedVgmCommandvalues as they become available. - Callback-based processing:
VgmCallbackStreamwrapsVgmStreamto provide callback registration for chip register writes with automatic state tracking and event detection (KeyOn, KeyOff, ToneChange). - Memory limits: Configurable limits for data block accumulation (default 32 MiB) and parsing buffer size (default 64 MiB) prevent unbounded memory growth from untrusted input.
- Chip state tracking: Monitor register writes to track key on/off events and extract tone information (frequency, pitch) from sound chip registers in real-time.
VgmStream overview
VgmStream is designed for streaming/real-time consumption of VGM data:
- It yields
VgmCommandvalues wrapped in stream results as it parses input and as it generates writes from DAC streams. - It understands DAC stream control commands (e.g.
SetupStreamControl,SetStreamData,SetStreamFrequency,StartStream,StartStreamFastCall,StopStream) and will expand stream-generated writes into the output timeline at the correct sample positions. - It also supports YM2612 direct DAC writes and expands them into corresponding
Ym2612Port0Address2AWriteAndWaitNcommands on the stream timeline. - During
Waitcommands, the internal scheduler finds upcoming stream- generated writes and splits waits as necessary so that generated chip writes are interleaved with parsed commands. This avoids emitting large bursts and preserves per-sample timing when multiple DAC streams are active concurrently. - DataBlock compression (e.g. bit-packed and DPCM streams) is automatically decompressed and expanded by the crate so compressed streams and their associated decompression tables are applied transparently.
- Memory limits are enforced to protect against malicious or malformed files:
- Data block size limit (default 32 MiB, configurable via
set_max_data_block_size()) - Parsing buffer size limit (default 64 MiB, configurable via
set_max_buffer_size())
- Data block size limit (default 32 MiB, configurable via
Examples
VgmBuilder as builder
use ;
use ;
use ;
use Gd3;
let mut builder = new;
// Register the chip's master clock in the VGM header (in Hz)
builder.register_chip;
// Append chip register writes using a chip-specific spec
builder.add_chip_write;
// Append a VGM command (example: wait)
builder.add_vgm_command;
// ... add more commands
// Set GD3 metadata for the document
builder.set_gd3;
// Finalize the document
let document: VgmDocument = builder.finalize;
// `into()` converts the finalized `VgmDocument` into VGM-format binary bytes
let bytes: = document.into;
VgmDocument as parser
use ;
use ;
// Read VGM bytes from somewhere
let bytes: = /* read a .vgm file */ Vecnew;
// For this example we construct a VGM byte sequence using the builder
// and then parse it back.
let mut b = new;
b.add_vgm_command;
b.add_vgm_command;
let doc = b.finalize;
let bytes: = .into;
// Parse the bytes into a `VgmDocument`
let document: VgmDocument =
.try_into
.expect;
// Example: map commands to their sample counts and sum them.
let total_wait: u32 = document
.iter
.map
.sum;
assert_eq!;
VgmStream::from_document
The from_document constructor is convenient when you already have a
parsed VgmDocument (for example: constructed programmatically via the
VgmBuilder). The stream will expand DAC-stream-generated writes into
the emitted command sequence and split waits so emitted writes are
interleaved at the correct sample positions. All wait commands
(WaitSamples, WaitNSample, Wait735Samples, Wait882Samples) are converted
to WaitSamples for consistent processing.
use ;
use StreamResult;
use ;
use Ym2612Spec;
use ;
// Build a minimal document that contains a data block and stream control
// commands. (Builder helpers for data blocks / stream setup exist on the
// `VgmBuilder` type; see the vgm module docs for details.)
let mut b = new;
// Example: append a YM2612 chip register write using the chip-specific spec
b.add_chip_write;
// (pseudo-code) append data block, configure stream and start it
// b.add_data_block(...);
// b.add_vgm_command(SetupStreamControl { /* ... */ });
// b.add_vgm_command(StartStream { /* ... */ });
b.add_vgm_command;
b.add_vgm_command; // 6 samples (5+1)
b.add_vgm_command; // 735 samples
b.add_vgm_command; // 882 samples
let doc: VgmDocument = b.finalize;
// Create a stream from the parsed document. The iterator will yield
// parsed commands as well as any stream-generated writes expanded into
// the timeline.
let mut stream = from_document;
stream.set_loop_count; // Prevent infinite loops
while let Some = stream.next
VgmStream — feeding raw byte chunks
Note: apart from providing input via push_chunk, handling the stream is the same as the from_document example above — iterate over the stream and handle StreamResult variants (Command, NeedsMoreData, EndOfStream, Err) in the same way.
Important: Always set a loop count limit for untrusted input to prevent infinite loops.
use VgmStream;
use StreamResult;
let mut parser = new;
parser.set_loop_count; // Prevent infinite loops
let chunks = vec!;
for chunk in chunks
VgmCallbackStream overview (WIP)
Note: This feature is still under testing.
VgmCallbackStream wraps VgmStream to provide automatic chip state tracking
and event-driven callbacks for real-time VGM processing:
- Automatic State Tracking: Enables per-chip state management for 35+ supported sound chips, automatically detecting register writes and maintaining internal state.
- Event Detection: Emits
StateEventnotifications for key musical events:KeyOn: Channel starts playing with tone/frequency informationKeyOff: Channel stops playingToneChange: Frequency changes while channel is active
- Flexible Callbacks: Register chip-specific callbacks using type-safe spec types
(e.g.,
Ym2612Spec,Sn76489Spec) to handle register writes with sample timing and associated events. - Real-time Processing: Low-overhead design suitable for streaming playback, with callbacks invoked automatically as commands are processed.
- Comprehensive Chip Support: Works with all major sound chips including FM synthesizers (YM2612, YM2151, OPL series), PSG chips (SN76489, AY-8910), PCM chips, and more.
This enables building advanced VGM analysis tools, real-time visualizers, and custom playback engines with minimal boilerplate code.
use ;
use Instance;
use ;
// Build a simple VGM document with YM2612 commands
let mut b = new;
b.register_chip;
// YM2612 initialization: LFO off
b.add_chip_write;
// Key on channel 1
b.add_chip_write;
b.add_vgm_command;
b.add_vgm_command;
let doc = b.finalize;
let stream = from_document;
let mut callback_stream = new;
// Prevent infinite loops in documentation
callback_stream.set_loop_count;
// Enable state tracking for YM2612 at NTSC Genesis clock
callback_stream.;
// Register callback for YM2612 writes
callback_stream.on_write;
// Process stream - callbacks fire automatically
for result in callback_stream
Chip State Tracking (WIP)
Note: This feature is still under testing.
The chip::state module provides real-time state tracking for sound chips,
detecting key on/off events and extracting tone information from register writes.
Implemented Chips
| Chip | Channels | Key On/Off | Tone Extract | Status | Test |
|---|---|---|---|---|---|
| SN76489 (PSG) | 3 tone + 1 noise | ✅ | ✅ | Master System, Game Gear | ⬜ |
| YM2413 (OPLL) | 9 FM | ✅ | ✅ | MSX, SMS FM Unit | ⬜ |
| YM2612 (OPN2) | 6 FM | ✅ | ✅ | Sega Genesis/Mega Drive | ⬜ |
| YM2151 (OPM) | 8 FM | ✅ | ✅ | Arcade systems | ⬜ |
| SegaPcm | N/A | N/A | N/A | Sega PCM chip | ⬜ |
| Rf5c68 | N/A | N/A | N/A | RF5C68 PCM chip | ⬜ |
| Ym2203 (OPN) | 3 FM + 3 PSG | ✅ | ✅ | NEC PC-8801, etc. | ⬜ |
| Ym2608 (OPNA) | 6 FM + 3 PSG | ✅ | ✅ | NEC PC-8801, etc. | ⬜ |
| Ym2610b (OPNB) | 6 FM + 3 PSG + ADPCM | ✅ | ✅ | Neo Geo, etc. | ⬜ |
| YM3812 (OPL2) | 9 FM | ✅ | ✅ | AdLib, Sound Blaster | ⬜ |
| Ym3526 (OPL) | 9 FM | ✅ | ✅ | C64 Sound Expander, etc. | ⬜ |
| Y8950 | 9 FM + ADPCM | ✅ | ✅ | MSX | ⬜ |
| Ymf262 (OPL3) | 18 FM | ✅ | ✅ | Sound Blaster 16, etc. | ⬜ |
| Ymf278b (OPL4) | 18 FM + PCM | ✅ | ✅ | YMF278B | ⬜ |
| Ymf271 (OPX) | 12 FM + PCM | ✅ | ✅ | YMF271 | ⬜ |
| Scc1 | 5 | ✅ | ✅ | Konami SCC (same as K051649) | ⬜ |
| Ymz280b | N/A | N/A | N/A | YMZ280B PCM | ⬜ |
| Rf5c164 | N/A | N/A | N/A | RF5C164 PCM | ⬜ |
| Pwm | N/A | N/A | N/A | Sega PWM | ⬜ |
| Ay8910 | 3 tone + noise | ✅ | ✅ | ZX Spectrum, MSX, etc. | ⬜ |
| GbDmg | 4 | ✅ | ✅ | Game Boy | ⬜ |
| NesApu | 5 | ✅ | ✅ | NES | ⬜ |
| MultiPcm | N/A | N/A | N/A | Sega MultiPCM | ⬜ |
| Upd7759 | N/A | N/A | N/A | uPD7759 ADPCM | ⬜ |
| Okim6258 | N/A | N/A | N/A | OKIM6258 ADPCM | ⬜ |
| Okim6295 | N/A | N/A | N/A | OKIM6295 ADPCM | ⬜ |
| K051649 | 5 | ✅ | ✅ | Konami SCC | ⬜ |
| K054539 | N/A | N/A | N/A | Konami K054539 PCM | ⬜ |
| Huc6280 | 6 | ✅ | ✅ | PC Engine/TurboGrafx-16 | ⬜ |
| C140 | N/A | N/A | N/A | Namco C140 PCM | ⬜ |
| K053260 | N/A | N/A | N/A | Konami K053260 PCM | ⬜ |
| Pokey | 4 | ✅ | ✅ | Atari 8-bit computers | ⬜ |
| Qsound | N/A | N/A | N/A | Capcom QSound | ⬜ |
| Scsp | N/A | N/A | N/A | Sega Saturn SCSP | ⬜ |
| WonderSwan | 4 | ✅ | ✅ | WonderSwan APU | ⬜ |
| Vsu | 6 | ✅ | ✅ | Virtual Boy VSU | ⬜ |
| Saa1099 | 6 | ✅ | ✅ | SAM Coupé, etc. | ⬜ |
| Es5503 | N/A | N/A | N/A | Ensoniq ES5503 | ⬜ |
| Es5506U8 | N/A | N/A | N/A | Ensoniq ES5506 (8-bit) | ⬜ |
| Es5506U16 | N/A | N/A | N/A | Ensoniq ES5506 (16-bit) | ⬜ |
| X1010 | N/A | N/A | N/A | Setia X1-010 | ⬜ |
| C352 | N/A | N/A | N/A | Namco C352 | ⬜ |
| Ga20 | N/A | N/A | N/A | Irem GA20 | ⬜ |
| Mikey | 4 | ✅ | ✅ | Atari Lynx | ⬜ |
| GameGearPsg | 3 tone + 1 noise | ✅ | ✅ | Game Gear PSG (same as SN76489) | ⬜ |
License
MIT License