mkmidilibrary
A comprehensive Rust library for music notation, MIDI file I/O, and real-time MIDI communication.
Overview
mkmidilibrary is a Rust translation and unification of three popular music libraries:
- music21 (Python) → Music notation and analysis
- midifile (C++) → MIDI file reading/writing
- rtmidi (C++) → Real-time MIDI I/O
Features
- Core Music Primitives: Pitch, Duration, Note, Rest, Chord, Interval, Scale, Unpitched
- Stream Hierarchy: Score, Part, Measure, Voice containers, with a full
music21-style operation set (flatten/recurse,chordify/implode,explode/voicesToParts,makeMeasures/makeTies/makeBeams/makeAccidentals/makeNotation,quantize/sliceBy*, whole-containertranspose/augmentOrDiminish,expandRepeats, and more) - MIDI File I/O: Read and write Standard MIDI Files (SMF), including Format 0/1 track join/split, tick-state conversion, and tempo-aware time mapping
- Real-time MIDI: MIDI input/output on macOS (CoreMIDI); Linux/Windows backends are stubs (see below)
- Music Notation: Clefs, key signatures, meter (including additive/summed time signatures and beat-hierarchy/beaming), dynamics (including Spanner-based hairpins), articulations, and ornament realization (trills/turns/mordents)
- Score Rendering: Graphical rendering via mkgraphic (optional)
- Music Analysis: Chord identification and Forte set-class labels, roman numeral analysis (parsing and pitch realization, both directions), 5 key-finding algorithms, floating-key/ modulation tracking, chord reduction, and melodic analysis (ambitus, interval diversity)
Installation
Add to your Cargo.toml:
[]
= "0.2.0"
Feature Flags
realtime- Real-time MIDI I/O (enabled by default)graphics- Score rendering with mkgraphic (enabled by default)
To disable optional features:
[]
= { = "0.2.0", = false }
Quick Start
Creating Notes and Chords
use *;
// Create a pitch (Middle C)
let pitch = from_parts;
// Create notes with different durations
let quarter_note = quarter;
let half_note = half;
let dotted_quarter = new;
// Create a C major chord
let c_major = major_triad;
Building a Score
use *;
// Create a new score
let mut score = new;
score.set_title;
// Create a part
let mut part = with_name;
// Create a measure with time signature
let mut measure = new;
measure.set_time_signature;
// Add notes
measure.append;
measure.append;
measure.append;
measure.append;
part.add_measure;
score.add_part;
MIDI File I/O
use ;
// Read a MIDI file
let midi = read?;
println!;
println!;
// Create a new MIDI file
let mut midi = new;
midi.set_ticks_per_quarter;
let track = midi.add_track;
track.add_note; // C4 quarter note
track.add_note; // E4 quarter note
midi.write?;
Real-time MIDI
use ;
// List available ports
let input = new?;
for port in input.ports
let mut output = new?;
for port in output.ports
// Open a port and send a note
output.open_port?;
output.send_message?; // Note On
output.send_message?; // Note Off
Score Rendering
use ;
// Create a renderer
let renderer = new;
// Render to PNG
let config = default;
if let Some = render_score_to_image
Module Structure
mkmidilibrary/
├── core/ # Music primitives (Pitch, Duration, Note, etc.)
├── stream/ # Container hierarchy (Score, Part, Measure, Voice)
├── midi/ # MIDI file I/O and translation
├── realtime/ # Real-time MIDI I/O (platform-specific)
├── notation/ # Musical notation elements
├── render/ # Graphical rendering (requires "graphics" feature)
└── analysis/ # Music analysis tools
Platform Support
| Platform | Real-time MIDI Backend |
|---|---|
| macOS | CoreMIDI (fully implemented and tested) |
| Linux | ALSA (stub — port enumeration/open/send are no-ops; not yet implemented) |
| Windows | Windows MM (stub — port enumeration/open/send are no-ops; not yet implemented) |
Known Limitations / Out of Scope
A few upstream (music21/midifile/rtmidi) capabilities are intentionally not implemented, rather than silently missing:
- Binasc (midifile's ASCII↔binary MIDI text format) is not implemented.
- MTS tuning SysEx (MIDI Tuning Standard bulk/single-note tuning dump builders) is not implemented.
- ALSA/Windows MM backends (
realtime::alsa_impl/realtime::winmm_impl) are stubs — real-time MIDI I/O is only functional on macOS today. Extending these needs a Linux/Windows environment to verify against real hardware/drivers. - Forte set-class labels (
Chord::forte_class) only cover trichords and tetrachords (Forte 3-1..3-12, 4-1..4-Z29) — pentachords and larger returnNonerather than a hand-transcribed (and unverifiable in this environment) larger table. - Several analysis/notation routines (
Score::chordify,analysis::discrete's key-finding profiles,Part::best_time_signature, etc.) are scoped, documented subsets of their music21 namesakes — see each function's doc comment for the specific simplification.
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.