Skip to main content

Crate sim_lib_midi_smf

Crate sim_lib_midi_smf 

Source
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.
SmfMergeCursor
An iterator that merges all tracks of an SmfFile into a single time-ordered, track-tagged stream.
SmfTrack
One track: an ordered list of timestamped events.
SmfWriteOptions
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 value as 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 bytes into an SmfFile.
write_smf
Serialises file to SMF bytes using default options.
write_smf_with_options
Serialises file to SMF bytes under the given SmfWriteOptions.