Expand description
Standard MIDI File (SMF) reading and writing for the SIM music stack.
This crate parses the on-disk .mid/.smf byte format into the in-memory
SmfFile model and serialises it back, reusing the event types from
sim_lib_midi_core. It covers the three SMF formats (SmfFormat), the
variable-length quantity encoding (encode_vlq/decode_vlq), running
status, and track canonicalisation/merging. Reading is read_smf;
writing is write_smf (or write_smf_with_options for running-status
control). Only metric (ticks-per-quarter) division is supported; SMPTE
division is rejected.
§Examples
Round-tripping a minimal single-track file:
use sim_lib_midi_smf::{read_smf, write_smf, SmfFile, SmfFormat, SmfTrack};
use sim_lib_midi_core::{
MetaEvent, MidiEvent, MidiPayload, TickTime, synthetic_origin,
};
let file = SmfFile {
format: SmfFormat::SingleTrack,
tpq: 480,
tracks: vec![SmfTrack {
events: vec![MidiEvent {
time: TickTime::new(0, 480).unwrap(),
origin: synthetic_origin(),
payload: MidiPayload::Meta(MetaEvent::EndOfTrack),
}],
}],
};
let bytes = write_smf(&file).unwrap();
let parsed = read_smf(&bytes).unwrap();
assert_eq!(parsed.format, SmfFormat::SingleTrack);
assert_eq!(parsed.tpq, 480);The variable-length quantity codec is reversible:
use std::io::Cursor;
use sim_lib_midi_smf::{decode_vlq, encode_vlq};
let bytes = encode_vlq(0x4000);
assert_eq!(decode_vlq(&mut Cursor::new(bytes)).unwrap(), 0x4000);Structs§
- SmfFile
- A parsed Standard MIDI File: its format, resolution, and tracks.
- SmfMerge
Cursor - An iterator that merges all tracks of an
SmfFileinto a single time-ordered, track-tagged stream. - SmfTrack
- One track: an ordered list of timestamped events.
- SmfWrite
Options - Options controlling SMF serialisation.
Enums§
- SmfError
- Errors raised while reading or writing a Standard MIDI File.
- SmfFormat
- The SMF header format field: how the file’s tracks relate.
Functions§
- decode_
vlq - Decodes a MIDI variable-length quantity from
reader. - encode_
vlq - Encodes
valueas a MIDI variable-length quantity (big-endian, 7 bits per byte with the high bit marking continuation). - read_
smf - Parses a complete Standard MIDI File from
bytesinto anSmfFile. - write_
smf - Serialises
fileto SMF bytes using default options. - write_
smf_ with_ options - Serialises
fileto SMF bytes under the givenSmfWriteOptions.