Music Theory for Rust
A comprehensive music theory library and CLI for Rust
This library covers music-theoretic fundamentals through graduate-level theory: notes, intervals, chords, scales, harmony analysis, voice leading, neo-Riemannian transformations, pitch-class set theory, counterpoint, and figured bass.
Every music theory fact in the library has been verified against 4,315 concept cards from 29 authoritative textbooks.
Note that this project started as a fork of Ozan Kaşıkçı's excellent library.
Quick Start
As a Library
[]
= "0.4"
use ;
use ;
use ;
use Interval;
// Notes and chords
let chord = new;
assert_eq!;
// Correct enharmonic spelling everywhere
let gm = new;
let notes = gm.notes; // G, Bb, D — not G, A#, D
// Identify chords from notes
let matches = identify;
// Finds: C major, first inversion
// Calculate intervals between pitches (letter-aware)
let interval = between.unwrap;
// Augmented 4th (not diminished 5th — letters matter)
// Find scales containing a set of notes
let scales = identify;
// Matches: C Lydian, G Ionian, ...
As a CLI
After building with make build, the binary is at ./bin/mt:
Modules
Fundamentals
| Module | Description |
|---|---|
note |
Pitch, Note, NoteLetter, PitchSymbol, KeySignature, enharmonic equivalence, transposition |
interval |
Simple and compound intervals (0-24 semitones), quality/number classification, letter-aware between(), inversion |
chord |
22+ chord types, letter-based spelling, identification with inversion detection, regex parsing |
scale |
8 scale types, 14 modes, identification from notes, ascending/descending support |
Harmony & Analysis
| Module | Description |
|---|---|
harmony |
Diatonic triads/sevenths, common tones, chord-scale compatibility, pivot chords |
analysis |
Roman numeral labeling (I, vi, V7, vii°, etc.), secondary dominant detection (V/x) |
voice_leading |
Optimal voice assignment minimizing total semitone movement |
Advanced Theory
| Module | Description |
|---|---|
neo_riemannian |
P, R, L operations on triads with chaining |
set_class |
PitchClassSet with normal/prime form, T_n, I_n, interval vector, Forte numbers |
counterpoint |
First-species rule checking (parallel 5ths/8ves, consonance, voice crossing) |
figured_bass |
Realize figured bass symbols into chord voicings |
Feature Flags
| Flag | Description |
|---|---|
midi |
Enables Note::midi_pitch() for MIDI pitch number conversion |
serde |
Derives Serialize/Deserialize on all public types |
= { = "0.4", = ["serde", "midi"] }
Examples
Harmony Analysis
use harmony;
use analysis;
use ;
use ;
use Mode;
// Diatonic chords in C major
let chords = diatonic_triads;
// I=C maj, ii=D min, iii=E min, IV=F maj, V=G maj, vi=A min, vii°=B dim
// Roman numeral analysis
let g7 = new;
let rn = roman_numeral.unwrap;
assert_eq!;
// Secondary dominant detection
let d_major = new;
let sd = secondary_dominant.unwrap;
assert_eq!;
// Pivot chords between C major and G major
let pivots = pivot_chords;
// G major is V in C, I in G; C major is I in C, IV in G; etc.
Neo-Riemannian Transformations
use ;
use ;
use ;
let c_major = new;
// P (Parallel): C major → C minor
let c_minor = transform.unwrap;
// R (Relative): C major → A minor
let a_minor = transform.unwrap;
// Chain operations: C major → P → R → L
let path = transform_chain.unwrap;
Pitch-Class Set Theory
use PitchClassSet;
let major_triad = new;
assert_eq!;
assert_eq!;
assert_eq!;
// Transpose and invert
let transposed = major_triad.transpose; // T_5
let inverted = major_triad.invert; // I_0
Building From Source
&&
Project Structure
Cargo.toml workspace root
mt/ library crate (music-comp-mt)
src/
lib.rs
note/, interval/, chord/, scale/
harmony/, analysis/, voice_leading/
neo_riemannian/, set_class/, counterpoint/, figured_bass/
tests/
mt-cli/ binary crate (music-comp-mt-cli)
src/
main.rs, cli.rs
tests/
License
MIT