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). Metrical and valid SMPTE divisions are retained losslessly.
§Examples
Round-tripping a minimal single-track file:
use sim_lib_midi_smf::{
read_smf, write_smf, SmfDivision, SmfFile, SmfFormat, SmfTrack,
};
use sim_lib_midi_core::{
MetaEvent, MidiEvent, MidiPayload, TickTime, synthetic_origin,
};
let file = SmfFile {
format: SmfFormat::SingleTrack,
division: SmfDivision::metrical(480).unwrap(),
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.division, SmfDivision::metrical(480).unwrap());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, time division, and tracks.
- SmfMerge
Cursor - An iterator that merges all tracks of an
SmfFileinto a single time-ordered, track-tagged stream. - SmfRead
Limits - Defensive resource bounds applied while parsing an SMF byte slice.
- SmfTrack
- One track: an ordered list of timestamped events.
- SmfWrite
Options - Options controlling SMF serialisation.
Enums§
- SmfDivision
- The lossless time-division field from an SMF header.
- SmfError
- Errors raised while reading or writing a Standard MIDI File.
- SmfFormat
- The SMF header format field: how the file’s tracks relate.
- SmfLimit
Kind - Defensive parser resources that can be bounded with
SmfReadLimits. - SmfTempo
Maps - Tempo-map topology implied by an SMF file’s format.
- SmfTime
Semantics - How track-local event times relate in an SMF format.
- Smpte
Rate - A valid SMPTE frame rate encoded by the signed high byte of an SMF division.
Statics§
- RECIPES
- Cookbook recipes for this lib, embedded at build time.
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
bytesusing defensive default resource bounds. - read_
smf_ with_ limits - Parses a complete Standard MIDI File from
bytesunder explicit resource bounds. - write_
smf - Serialises
fileto SMF bytes using default options. - write_
smf_ with_ options - Serialises
fileto SMF bytes under the givenSmfWriteOptions.