A music theory guide written in Rust.
A library and executable that provides programmatic implementation of the basis of the music theory.
Rust Music Theory is used to procedurally utilize music theory notions like Note, Chord, Scale,
Interval and more. The main purpose of this library is to let music theory be used in other programs and produce music/audio in a programmatic way.
Add rust-music-theory as a dependency in your Cargo.toml.
[dependencies]
rust-music-theory = "0.5"After installing the dependencies, you can use the library as follows.
…This is the simplest form of the usage. For detailed examples, please see the tests folder.
Version 0.5 adds a normalized lead-sheet chord model and a compact parser. Parsing preserves generic note spelling, resolves modifiers in musical order, and formats every accepted alias as portable ASCII.
use rust_music_theory::chord::Chord;
use rust_music_theory::note::Notes;
let chord = Chord::parse("Cmaj9#11/G")?;
assert_eq!(chord.canonical_symbol(), "Cmaj9#11/G");
assert_eq!(
chord.notes().iter().map(|note| note.pitch.to_string()).collect::>(),
["G", "B", "D", "F#", "C", "E"]
);
// FromStr and Display use the same normalized grammar.
let half_diminished: Chord = "Cø7".parse()?;
assert_eq!(half_diminished.to_string(), "Cm7b5");
# Ok::(())Common supported forms include:
C, Cm, Cdim, Caug, C5, C6, Cm6, C6/9, Cm6/9C7, Cmaj7, Cm7, CmMaj7, Cm7b5, Cdim7, Caug7, CaugMaj79, 11, and 13C7sus4, Cm7sus4, Csus2, Cadd2, Cadd4, Cadd6, Cadd9, Cadd11, Cadd13C7b5, C7#9, Cmaj9#11, C7no5, C13omit11C7alt, C/E, C/F#, C/1CΔ, CΔ9, CmΔ, C^7, CM7, Cma7, C-7, Cmi7, Cm/M7, C°7, Cø7, Ch7, C+7, C69, C6add9, C7(b9,#11)ASCII and Unicode single/double accidentals are accepted on roots and slash basses. Generated note
octaves follow written scientific pitch, so Cb4 plays as MIDI 59 and B#4 as MIDI 72.
Chord::builder(root) exposes the same validated model for programmatic construction. See
the chord-symbol reference for grammar, canonicalization, builder usage,
errors, and the 0.4 to 0.5 migration.
All seven rotations of the harmonic-minor and melodic-minor families are available through the
same Scale, ScaleType, and Mode APIs:
use rust_music_theory::note::{NoteLetter, Notes, Pitch};
use rust_music_theory::scale::{Direction, Mode, Scale, ScaleType};
let scale = Scale::new(
ScaleType::HarmonicMinor,
Pitch::new(NoteLetter::C, 0),
4,
Some(Mode::PhrygianDominant),
Direction::Ascending,
)?;
assert_eq!(
scale.notes().iter().map(|note| note.pitch.to_string()).collect::>(),
["C", "Db", "E", "F", "G", "Ab", "Bb", "C"]
);
# Ok::(())Harmonic Minor, Locrian natural 6, Ionian #5, Dorian #4,
Phrygian Dominant, Lydian #2, and Ultralocrian.Melodic Minor, Dorian b2, Lydian Augmented, Lydian Dominant,
Mixolydian b6, Locrian #2, and Altered.Names are case-insensitive and accept ASCII or Unicode accidentals, words such as flat and
sharp, snake-case API identifiers, and established aliases such as Spanish, Overtone,
Hindu, Super Locrian, and Diminished Whole Tone. The definitions follow the
Tonal scale registry.
The existing base melodic-minor scale retains classical behavior: descending it lowers the sixth
and seventh. Derived melodic-minor modes use the ascending/jazz pitch collection in both
directions. Altered mode retains rotation-derived diatonic spelling, so C altered is written
C Db Eb Fb Gb Ab Bb rather than using enharmonic chord-tension names.
The library supports MIDI file export and real-time MIDI playback to hardware/software synthesizers.
Enable the midi feature to export chords and scales to MIDI files:
[dependencies]
rust-music-theory = { version = "0.5", features = ["midi"] }…Enable the midi-playback feature to play notes on connected MIDI devices (hardware synths, DAWs like Ableton):
[dependencies]
rust-music-theory = { version = "0.5", features = ["midi-playback"] }…For MIDI clock sync, enable "Ext" sync in Ableton's transport.
cargo install --git https://github.com/ozankasikci/rust-music-theory
This lets cargo install the library as an executable called rustmt. Some usage examples;
rustmt scale D Locrian
Notes:
1: D
2: D#
3: F
4: G
5: G#
6: A#
7: C
8: Drustmt chord C# Dominant Eleventh
Notes:
1: C#
2: E#
3: G#
4: B
5: D#
6: F#Compact symbols and canonical normalization are also available:
$ rustmt chord C7sus4
Notes:
1: C
2: F
3: G
4: Bb
$ rustmt chord normalize 'C7(b9,#11)'
C7b9#11rustmt scale list
Available Scales:
- Ionian
- Dorian
- Phrygian
- Lydian
- Mixolydian
- Aeolian
- Locrian
- Harmonic Minor
- Locrian natural 6
- Ionian #5
- Dorian #4
- Phrygian Dominant
- Lydian #2
- Ultralocrian
- Melodic Minor
- Dorian b2
- Lydian Augmented
- Lydian Dominant
- Mixolydian b6
- Locrian #2
- Altered
- Pentatonic Major
- Pentatonic Minor
- Blues
- Chromatic
- Whole Tonerustmt chord list
Supported chord syntax:
- Major Triad: C
- Sevenths: C7, Cmaj7, Cm7, CmMaj7, Cm7b5, Cdim7, Caug7, CaugMaj7
- Altered dominant: C7alt
- Slash basses and inversions: C/E, C/F#, C/1
...Try the library in your browser with the interactive WASM playground:
https://ozankasikci.github.io/rust-music-theory/
The binary returns the notes of the requested scale or chord. Chords use the normalized Unicode-aware tokenizer; the older regex-named chord entry point is retained only as a deprecated compatibility wrapper. To quickly build and run the executable locally;
git clone http://github.com/ozankasikci/rust-music-theory && cd rust-music-theory
Then you can directly compile using cargo. An example;
cargo run scale D Locrian
Notes:
1: D
2: D#
3: F
4: G
5: G#
6: A#
7: C
8: DNo open issues yet, or sync has not completed.