Skip to content

MIDI import and export

load_midi() accepts a path or a mido.MidiFile and always returns MidiLoadResult. Inspect its report before using material imported with the repair policy.

Import policies

Mode Repairs malformed input Permits ignored unsupported messages
repair Yes, with diagnostics Yes, with diagnostics
strict No Yes, with diagnostics
lossless No No

Rejected imports raise MidiImportError; its report attribute contains the complete diagnostic report. Diagnostics use stable machine-readable code values and human-readable details.

"""Executable MIDI-diagnostics example used by the documentation."""

import mido

from scoda import MidiImportError, load_midi


def main() -> None:
    midi_file = mido.MidiFile(ticks_per_beat=24)
    midi_file.tracks.append(
        mido.MidiTrack(
            [
                mido.Message("note_off", note=61, channel=0, time=0),
                mido.Message("note_on", note=60, velocity=90, channel=0, time=3),
                mido.MetaMessage("end_of_track", time=9),
            ]
        )
    )

    repaired = load_midi(midi_file, mode="repair")
    assert {diagnostic.code for diagnostic in repaired.report.errors} == {
        "unclosed_note",
        "unmatched_note_off",
    }

    try:
        load_midi(midi_file, mode="strict")
    except MidiImportError as error:
        assert error.report == repaired.report
    else:  # pragma: no cover - executable documentation assertion
        raise AssertionError("strict import unexpectedly accepted malformed MIDI")


if __name__ == "__main__":
    main()

Deterministic repairs

Repair mode closes notes left active at a track endpoint, discards unmatched note-off messages, resolves same-key retriggers into adjacent intervals, and canonicalises representable same-tick ordering. Every such change is reported. PPQN scaling uses exact rational arithmetic before integer rounding.

For corpora that must not learn fabricated long sustains, use load_midi(source, mode="repair", unclosed_note_policy="drop"). Notes with no authoritative note-off are discarded instead of being closed at the source track endpoint. Diagnostics still report unclosed_note (or zero_length_unclosed_note), so callers can count drops against original audible onsets and reject excessively damaged pieces. This setting does not bypass strict or lossless validation. The default unclosed_note_policy="close_at_track_end" retains the normal repair behavior.

Lossless limits

Independent timelines in MIDI format 2 cannot be represented by one synchronised sequence collection and are rejected. Lossless mode also rejects non-integral PPQN resampling and ignored messages. Note-off velocity and all standard time-signature bytes are preserved.

Export

to_mido() constructs a deterministic mido.MidiFile; save_midi() writes the same representation. Tracks must share one PPQN. Overlapping notes with the same channel and pitch are rejected because ordinary MIDI note-off messages cannot identify which overlapping interval ends.