music-core
Pitches, notes, chords and keys in Rust
Start here
music-core is a small Rust library for the things music theory is built out
of: the twelve pitches, the distances between them, notes that know which octave
they are in, and chords.
It has no dependencies, works without the standard library, and never allocates.
Every type is Copy and small enough to pass around without thinking about it.
use music_core::Pitch;
let c = Pitch::C;
let e = Pitch::E;
let g = Pitch::G;
Two Levels
A Pitch is a note name with the octave thrown away, so every C is the same C.
A Note is that same pitch given a register.
use music_core::Interval;
use music_core::Pitch;
let pitch = Pitch::C;
let note = Pitch::C.at(4);
let wraps = pitch + Interval::OCTAVE; // C, unchanged
let climbs = (note + Interval::OCTAVE).midi(); // 72, an octave higher
A pitch wraps around like a clock, because the octave is exactly what it throws away. A note keeps climbing. Knowing which one you want is most of the library.
Adding It to a Project
[dependencies]
music-core = { git = "https://codeberg.org/graebe/neo-riemann" }
Everything is exported from the crate root, so one use line is enough.
use music_core::{Chord, Interval, Note, Pitch, PitchSet};
Where to Start
| Page | Covers |
|---|---|
| The twelve pitches | Pitch, Interval, arithmetic, measuring gaps |
| Giving a pitch a register | Note, MIDI numbers, frequency |
| Sets of pitches | PitchSet, set algebra |
| Chords | Chord, building, voicing |
| Identifying a pitch set | Working out the chord from the notes |
| Keys and modes | Key, Mode, signatures, spelling, roman numerals |
The three examples this tutorial follows are worth running as you read.
cargo run -p music-core --example pitch
cargo run -p music-core --example note
cargo run -p music-core --example chordThe twelve pitches
A Pitch is a note name without the octave. E.g. every C is the same C. Let’s
initialize some examples.
use music_core::Pitch;
let c = Pitch::C;
let f_sharp = Pitch::F_SHARP;
We can also initialize pitches by number, using Pitch::new.
use music_core::Pitch;
let c = Pitch::new(0);
let f_sharp = Pitch::new(6);
Or by string parsing.
use music_core::Pitch;
let c = "C".parse::<Pitch>().unwrap();
let f_sharp = "F#".parse::<Pitch>().unwrap();
let d_flat = "Db".parse::<Pitch>().unwrap();
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Sharp | C | C# | D | D# | E | F | F# | G | G# | A | A# | B |
| Flat | C | Db | D | Eb | E | F | Gb | G | Ab | A | Bb | B |
Both rows are the same twelve values. Printing always uses sharps, so ask for flats by name when they read better.
use music_core::Pitch;
use music_core::Spelling;
let e_flat = Pitch::E_FLAT;
println!("{}", e_flat); // D#
println!("{}", e_flat.name(Spelling::Flats)); // Eb
Overflow handling is treated by modulo twelve.
use music_core::Pitch;
let b = Pitch::new(-1); // modulo 12 yields 11 (B)
let c_sharp = Pitch::new(13); // modulo 12 yields 1 (C#)
Pitch Arithmetic
We can manipulate pitches arithmetically, adding or subtracting intervals to move around the twelve-tone circle.
use music_core::Pitch;
use music_core::Interval;
let c = Pitch::C;
let d = c + Interval::MAJOR_SECOND;
let b = c - Interval::MINOR_SECOND;
| Semitones | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Constant | UNISON | MINOR_SECOND | MAJOR_SECOND | MINOR_THIRD | MAJOR_THIRD | PERFECT_FOURTH | TRITONE | PERFECT_FIFTH | MINOR_SIXTH | MAJOR_SIXTH | MINOR_SEVENTH | MAJOR_SEVENTH | OCTAVE |
The pitch wraps, just like for instantiation. The interval itself does not: it is a plain semitone count, so it can be negative or larger than an octave.
use music_core::Interval;
let down_a_semitone = Interval::new(-1);
let two_octaves_and_a_fifth = Interval::new(31);
Measuring the Gap
There are three ways to ask how far apart two pitches are, and they give different answers on purpose.
use music_core::Pitch;
let c = Pitch::C;
let g = Pitch::G;
let up = c.interval_to(g).semitones(); // 7, going up from C to G
let back = g.interval_to(c).semitones(); // 5, going up from G to C
let shortest = c.distance_to(g).value(); // 5, in either direction
let signed = (g - c).semitones(); // -5, the shortest path with a sign
| Method | Answer | Range |
|---|---|---|
interval_to | How far up? Direction matters, so it is not symmetric. | 0 to 11 |
distance_to | How far must a voice move? Direction is ignored. | 0 to 6 |
a - b | The shortest path, with a direction attached. | -5 to 6 |
The tritone is the one exception. It sits exactly half an octave away, so both
directions are equally short and there is nothing to be negative about. The tie
resolves upward and both answers come out +6.
use music_core::Pitch;
let up = (Pitch::F_SHARP - Pitch::C).semitones(); // 6
let down = (Pitch::C - Pitch::F_SHARP).semitones(); // 6
An IntervalClass is the folded form used by distance_to: direction and
octaves are both discarded, so a fifth up and a fourth down are the same class.
use music_core::IntervalClass;
let fifth_up = IntervalClass::new(7).value(); // 5
let fourth_down = IntervalClass::new(-5).value(); // 5
Comparing Pitches
Two pitches are equal when they are the same note, whatever you called it when you built them.
use music_core::Pitch;
let c = Pitch::C;
let also_c = Pitch::new(0);
let same = c == also_c; // true
let enharmonic = Pitch::C_SHARP == Pitch::D_FLAT; // true
let different = c == Pitch::D; // false
Voicing a Pitch Gives a Note
A Pitch by itself does not have an octave. To get a full Note, you need to add an octave.
use music_core::Pitch;
use music_core::Note;
let c = Pitch::C;
let c_in_fourth_octave = c.at(4);
See the Notes tutorial for more information on working with Notes.
Giving a pitch a register
A note is a voiced pitch (a pitch with an octave). We can initialize it in different ways
use music_core::Pitch;
use music_core::Note;
let c3 = Pitch::C.at(3);
let c4 = Note::from_midi(60);
let eb3 = "Eb3".parse::<Note>().unwrap();
Converting a Pitch to a Note
We can turn a Pitch into a Note by giving it an octave.
use music_core::Pitch;
let c = Pitch::C;
let c_in_fourth_octave = c.at(4);
The data type of a Note is Voiced<Pitch>. Every note now corresponds to a specific frequency.
use music_core::Pitch;
let c = Pitch::C;
let c_in_fourth_octave = c.at(4);
let frequency = c_in_fourth_octave.frequency_hz();
This table shows some example frequencies for the 4th and 5th octaves.
| Note | C4 | D4 | E4 | F4 | G4 | A4 | B4 | C5 |
|---|---|---|---|---|---|---|---|---|
| Frequency (Hz) | 261.63 | 293.66 | 329.63 | 349.23 | 392.00 | 440.00 | 493.88 | 523.25 |
| Midi Number | 60 | 62 | 64 | 65 | 67 | 69 | 71 | 72 |
music-core gives you the number, not the sound. It has no audio output of its
own, so feed .midi() to a synth or a MIDI library when you want to hear it.
use music_core::Pitch;
let midi = Pitch::C.at(4).midi(); // 60
Arithmetic
Notes support basic arithmetic operations like addition and subtraction with intervals.
use music_core::Pitch;
use music_core::Interval;
let c4 = Pitch::C.at(4);
let major_third = Interval::MAJOR_THIRD;
let e4 = c4 + major_third;
assert_eq!(e4, Pitch::E.at(4));
We can also calculate the difference between two notes using subtraction.
use music_core::Pitch;
use music_core::Interval;
let c4 = Pitch::C.at(4);
let e4 = Pitch::E.at(4);
let major_third = e4 - c4;
assert_eq!(major_third, Interval::MAJOR_THIRD);
Comparison
Two notes are equal only when the pitch and the octave both match.
use music_core::Pitch;
let c4 = Pitch::C.at(4);
let c5 = Pitch::C.at(5);
let same = c4 == Pitch::C.at(4); // true
let octave_apart = c4 == c5; // false, the octave differs
let enharmonic = Pitch::D_SHARP.at(4) == Pitch::E_FLAT.at(4); // true
Other Concert Pitches
The table above assumes A4 sounds at 440 Hz. Orchestras have not always tuned there, so you can ask for another reference and every note shifts with it.
use music_core::Pitch;
let modern = Pitch::A.at(4).frequency_hz_at(440.0); // 440.00
let older = Pitch::A.at(4).frequency_hz_at(432.0); // 432.00
let baroque = Pitch::C.at(4).frequency_hz_at(415.0); // 246.76
These two methods are the only part of music-core that needs the standard
library, because core has no floating-point maths. Everything else on this
page works in a no_std build.
A List of Notes
Notes holds up to sixteen notes in the order you add them. Unlike a set it
keeps that order, and it can hold the same note twice.
use music_core::Pitch;
use music_core::Notes;
let mut chord = Notes::EMPTY;
let _ = chord.push(Pitch::C.at(3));
let _ = chord.push(Pitch::G.at(3));
let _ = chord.push(Pitch::E.at(4));
let count = chord.len(); // 3
let lowest = chord.bass(); // Some(C3)
let printed = chord.to_string(); // "[C3 G3 E4]"
Drop the octaves and you are left with the pitches alone.
use music_core::Pitch;
use music_core::Notes;
let chord = Notes::from_slice(&[Pitch::C.at(3), Pitch::E.at(4), Pitch::C.at(5)]).unwrap();
let pitches = chord.pitch_set(); // {C, E}
let unique = chord.pitch_set().len(); // 2, the C appears twice above
Printing Notes
A note can be printed using the Debug or Display traits.
use music_core::Pitch;
use music_core::Note;
let c4 = Pitch::C.at(4);
let c4_note = Note::from_midi(60);
println!("{:?}", c4_note); // Debug format
println!("{}", c4_note); // Display format
See the Pitch sets tutorial for what happens when you throw the octaves away entirely.
Sets of pitches
A PitchSet holds any group of pitches, like a handful of piano keys held down
at once. It has no order and no repeats.
use music_core::Pitch;
use music_core::PitchSet;
let c_major = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G]);
let size = c_major.len(); // 3
let has_e = c_major.contains(Pitch::E); // true
let printed = c_major.to_string(); // "{C, E, G}"
The whole set is twelve bits, one per pitch, so it takes two bytes whether it holds one pitch or all twelve. C sits at bit 0.
use music_core::Pitch;
use music_core::PitchSet;
let c_major = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G]);
let bits = c_major.bits(); // 0b1001_0001
let empty = PitchSet::EMPTY; // {}
let all = PitchSet::CHROMATIC; // all twelve
Adding and Removing
insert and remove return a new set rather than changing the one you have.
Every type here is Copy, so building a changed version costs nothing.
use music_core::Pitch;
use music_core::PitchSet;
let c_major = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G]);
let with_seventh = c_major.insert(Pitch::B); // {C, E, G, B}
let without_third = c_major.remove(Pitch::E); // {C, G}
let still_three = c_major.len(); // 3, the original is unchanged
let twice = c_major.insert(Pitch::E); // {C, E, G}, no repeats
let absent = c_major.remove(Pitch::D); // {C, E, G}, D was never there
Set Algebra
The usual operators work, and the questions they answer are musical ones.
use music_core::Pitch;
use music_core::PitchSet;
let c_major = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G]);
let a_minor = PitchSet::from_pitches(&[Pitch::A, Pitch::C, Pitch::E]);
let shared = c_major & a_minor; // {C, E}
let only_in_c = c_major - a_minor; // {G}
let together = c_major | a_minor; // {C, E, G, A}
let count = c_major.common_tones(a_minor); // 2
| Operator | Method | Asks |
|---|---|---|
& | intersection | What do these two chords share? |
| | union | What notes do they use between them? |
- | difference | What does the first have that the second lacks? |
! | complement | Everything else in the octave. |
C major and A minor share two of their three notes, which is why the move between them sounds smooth.
Iterating
A set yields its pitches ascending from C, never in the order you inserted them.
use music_core::Pitch;
use music_core::PitchSet;
let spread = PitchSet::from_pitches(&[Pitch::G, Pitch::C, Pitch::E]);
for pitch in spread {
println!("{}", pitch); // C, then E, then G
}
Moving and Mirroring
Transposing rotates every bit at once. Inverting reflects the whole set.
use music_core::Interval;
use music_core::Pitch;
use music_core::PitchSet;
let c_major = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G]);
let up_a_fifth = c_major.transpose(Interval::PERFECT_FIFTH); // {D, G, B}
let mirrored = c_major.invert(0); // {C, F, G#}
The Shape of a Chord
The interval vector counts how many pairs sit at each distance, 1 through 6. It ignores transposition and inversion, so it describes the shape of a chord rather than its position.
use music_core::Pitch;
use music_core::PitchSet;
let c_major = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G]);
let a_minor = PitchSet::from_pitches(&[Pitch::A, Pitch::C, Pitch::E]);
let major = c_major.interval_vector(); // [0, 0, 1, 1, 1, 0]
let minor = a_minor.interval_vector(); // [0, 0, 1, 1, 1, 0]
Both give the same answer: one minor third, one major third, one fourth. Major and minor are mirror images of each other.
See the Chords tutorial for what happens when you give a set a root.
Chords
A Chord is two things together: a root, and a set of pitches. The usual way to
build one is to name the kind you want.
use music_core::Chord;
use music_core::Pitch;
let c = Chord::major(Pitch::C);
let dm7 = Chord::min7(Pitch::D);
let g7 = Chord::dom7(Pitch::G);
let root = c.root(); // C
let pitches = c.pitches(); // {C, E, G}
let printed = dm7.to_string(); // "Dm7"
There are thirty-one named kinds, from a bare fifth up to thirteenths. None of them can fail, so there is nothing to unwrap.
| Kind | major | minor | dom7 | maj7 | min7 | dim7 | sixth | six_nine |
|---|---|---|---|---|---|---|---|---|
| Prints as | C | Cm | C7 | Cmaj7 | Cm7 | Cdim7 | C6 | C6/9 |
You can also build a chord from a root and any set of pitches. If you leave the root out of the set it is put back in, because a chord contains its own root.
use music_core::Chord;
use music_core::Pitch;
use music_core::PitchSet;
let without_root = PitchSet::from_pitches(&[Pitch::E, Pitch::G]);
let c = Chord::new(Pitch::C, without_root);
let pitches = c.pitches(); // {C, E, G}
The Root Is Part of the Chord
The same four pitches with two different roots are two different chords.
use music_core::Chord;
use music_core::Pitch;
use music_core::PitchSet;
let notes = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G, Pitch::A]);
let on_c = Chord::new(Pitch::C, notes); // C6
let on_a = Chord::new(Pitch::A, notes); // Am7
let same_notes = on_c.pitches() == on_a.pitches(); // true
let same_chord = on_c == on_a; // false
Once the root is known, the kind of chord is not a matter of opinion.
use music_core::Chord;
use music_core::ChordQuality;
use music_core::Pitch;
let c = Chord::major(Pitch::C);
let quality = c.quality(); // Some(ChordQuality::Major)
Inversions and Slash Chords
A chord carries a bass alongside its root. It equals the root until you say
otherwise, and over is how you say otherwise.
use music_core::Chord;
use music_core::Pitch;
let c = Chord::major(Pitch::C);
let first_inversion = c.over(Pitch::E);
let printed = first_inversion.to_string(); // "C/E"
let root = first_inversion.root(); // C, unchanged
let bass = first_inversion.bass(); // E
let inverted = first_inversion.is_inverted(); // true
The root still decides what kind of chord it is. The bass only decides which note is underneath.
inversion counts upward from the root through the chord’s own notes, so it
works for chords of any size.
use music_core::Chord;
use music_core::Pitch;
let c = Chord::maj7(Pitch::C); // C E G B
let root_position = c.inversion(); // 0
let first = c.over(Pitch::E).inversion(); // 1
let second = c.over(Pitch::G).inversion(); // 2
let third = c.over(Pitch::B).inversion(); // 3
| Bass | C | E | G | B |
|---|---|---|---|---|
| Prints as | Cmaj7 | Cmaj7/E | Cmaj7/G | Cmaj7/B |
inversion() | 0 | 1 | 2 | 3 |
Slash chords print and parse. The split happens at the last slash, so C6/9
stays a six-nine chord and only C6/9/E puts one underneath it.
use music_core::Chord;
let six_nine = "C6/9".parse::<Chord>().unwrap(); // C6/9, no bass given
let over_e = "C6/9/E".parse::<Chord>().unwrap(); // C6/9/E
let slash = "C/E".parse::<Chord>().unwrap(); // C/E
in_root_position puts the root back underneath. It does not undo over,
because a bass that was not already one of the pitches stays in the chord — it
sounded.
use music_core::Chord;
use music_core::Pitch;
let c = Chord::major(Pitch::C);
let back = c.over(Pitch::E).in_root_position(); // C
Adding and Removing Notes
Adding a note can turn the chord into a different kind of chord.
use music_core::Chord;
use music_core::Interval;
use music_core::Pitch;
let c = Chord::major(Pitch::C);
let c_maj7 = c.with(Pitch::B); // Cmaj7
let back = c_maj7.without(Pitch::B); // C
let no_change = c.without(Pitch::C); // C, the root cannot be removed
let g = c.transpose(Interval::PERFECT_FIFTH); // G
Chords With No Name
Most possible chords have no name, and that is fine. Printing falls back to the root followed by the semitones above it, and that form reads back in.
use music_core::Chord;
use music_core::Pitch;
use music_core::PitchSet;
let odd = Chord::new(
Pitch::C,
PitchSet::from_pitches(&[Pitch::C, Pitch::C_SHARP, Pitch::E, Pitch::F_SHARP]),
);
let quality = odd.quality(); // None
let printed = odd.to_string(); // "C[0,1,4,6]"
Reading a Chord From Text
Chord symbols written the usual way can be read in.
use music_core::Chord;
let dm7 = "Dm7".parse::<Chord>().unwrap();
let maj7 = "F#maj7".parse::<Chord>().unwrap();
let refused = "Cwobble".parse::<Chord>(); // Err(InvalidQuality)
Prefer the constructors in your own code. They cannot fail, so there is nothing to handle. Reading text is for input that came from a person.
Playing a Chord
A chord has no octave, the same way a pitch has none. Give it one and you get back real notes stacked upward from the root.
use music_core::Chord;
use music_core::Pitch;
let c = Chord::major(Pitch::C);
let low = c.voice(3).to_string(); // "[C3 E3 G3]"
let mid = c.voice(4).to_string(); // "[C4 E4 G4]"
let seventh = Chord::maj7(Pitch::C).voice(4).to_string(); // "[C4 E4 G4 B4]"
voice stacks everything inside one octave. voice_as lets you ask for
something else.
use music_core::Chord;
use music_core::Pitch;
use music_core::Voicing;
let c = Chord::maj7(Pitch::C);
let close = c.voice_as(3, Voicing::Close); // [C3 E3 G3 B3]
let drop2 = c.voice_as(3, Voicing::Drop2); // [G2 C3 E3 B3]
let open = c.voice_as(3, Voicing::Open); // [C3 G3 E4 B4]
let rootless = c.voice_as(3, Voicing::Rootless); // [E3 G3 B3]
| Voicing | Puts the notes |
|---|---|
Close | All inside one octave. What voice does on its own. |
Stacked | As written, with extensions above the octave. |
Drop2 | Stacked, with the second note from the top dropped an octave. |
Open | Every second note lifted, so the chord reaches past an octave. |
Rootless | Without the root, as a pianist would play under a bass. |
However a voicing was built, spread reports how far it actually reaches and
is_close says whether it fits inside an octave.
use music_core::Chord;
use music_core::Pitch;
use music_core::Voicing;
let c = Chord::maj7(Pitch::C);
let close = c.voice_as(3, Voicing::Close);
let open = c.voice_as(3, Voicing::Open);
let narrow = close.spread().unwrap().semitones(); // 11
let wide = open.spread().unwrap().semitones(); // 23
let fits = close.is_close(); // true
let does_not = open.is_close(); // false
See the Identification tutorial for going the other way: working out the chord when all you have is the notes.
Identifying a pitch set
A PitchSet has no root, so it does not know what chord it is. Working that out
means trying each pitch as a root and seeing which ones name something.
use music_core::Pitch;
use music_core::PitchSet;
let notes = PitchSet::from_pitches(&[Pitch::D, Pitch::F_SHARP, Pitch::A]);
let named = notes.identify(); // Some(D)
A set that spells nothing gets no answer.
use music_core::Pitch;
use music_core::PitchSet;
let nothing = PitchSet::from_pitches(&[Pitch::C, Pitch::C_SHARP]);
let named = nothing.identify(); // None
Every Reading
Often a set can be read more than one way. interpretations yields all
of them, one per pitch that works as a root.
use music_core::Pitch;
use music_core::PitchSet;
let notes = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G, Pitch::A]);
for chord in notes.interpretations() {
println!("{}", chord); // C6, then Am7
}
let count = notes.interpretations().count(); // 2
Both readings are correct. The same four notes really are a C sixth and an A minor seventh.
How identify Chooses
identify is a convenience over interpretations for when you want one answer. It prefers the lowest rank, and ties go to the
lower root.
| Rank | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| Kind | major, minor | diminished, augmented | suspended | sevenths | sixths | added ninths | ninths | elevenths, thirteenths | altered |
So for C, E, G, A the seventh beats the sixth.
use music_core::ChordQuality;
use music_core::Pitch;
use music_core::PitchSet;
let notes = PitchSet::from_pitches(&[Pitch::C, Pitch::E, Pitch::G, Pitch::A]);
let best = notes.identify().unwrap();
let root = best.root(); // A
let quality = best.quality(); // Some(ChordQuality::Minor7)
When the answer matters, read interpretations and decide with the context you
have.
Chords With No Single Root
A few chords divide the octave into equal steps, so every one of their notes works as a root.
use music_core::Chord;
use music_core::Pitch;
let dim7 = Chord::dim7(Pitch::C).pitches();
let aug = Chord::aug(Pitch::C).pitches();
let four = dim7.interpretations().count(); // 4: Cdim7, D#dim7, F#dim7, Adim7
let three = aug.interpretations().count(); // 3: Caug, Eaug, G#aug
This is not a flaw. It is a real property of those chords, and it is why a diminished seventh can pivot to so many places in a piece of music.
Naming a Voicing
Everything above works on pitch classes, which have no octave and so no lowest note. But the bass is exactly what decides whether three pitches are a C major triad or a C over E.
Notes holds real notes, so it has a bass, and it can identify itself.
use music_core::Notes;
use music_core::Pitch;
let rooted_on_a = Notes::from_slice(&[
Pitch::A.at(3),
Pitch::C.at(4),
Pitch::E.at(4),
Pitch::G.at(4),
]).unwrap();
let rooted_on_c = Notes::from_slice(&[
Pitch::C.at(3),
Pitch::E.at(3),
Pitch::G.at(3),
Pitch::A.at(4),
]).unwrap();
let first = rooted_on_a.identify(); // Some(Am7)
let second = rooted_on_c.identify(); // Some(C6)
The same four pitch classes, read two different ways, decided by what is underneath. Throw the octaves away and that evidence is gone.
| Voicing | Notes::identify | pitch_set().identify() |
|---|---|---|
[A3 C4 E4 G4] | Am7 | Am7 |
[C3 E3 G3 A4] | C6 | Am7 |
[E3 G3 C4] | C/E | C |
That last row is the other half of it. When the bass will not serve as a root, the reading falls back to the pitch classes and records the bass as a slash.
use music_core::Notes;
use music_core::Pitch;
let over_e = Notes::from_slice(&[
Pitch::E.at(3),
Pitch::G.at(3),
Pitch::C.at(4),
]).unwrap();
let named = over_e.identify(); // Some(C/E)
Prefer this over pitch_set().identify() whenever you have real notes. It is
strictly better informed, because it keeps the one piece of evidence that tells
an inversion from a chord in root position.
Inversions cannot round-trip exactly, and that is a fact about music rather than a gap in the library. A C6 over E and an A minor 7 over E are the same sounding notes; only the surrounding music decides which one was meant. What does survive is the pitch set and the bass.
What Could This Be Part Of
interpretations asks what these notes are. completions asks what they could
be part of, which is a different question — real voicings leave notes out, and
the root goes first.
use music_core::Pitch;
use music_core::PitchSet;
// A C minor 9 as it would actually be played, with no C in it.
let played = PitchSet::from_pitches(&[
Pitch::E_FLAT,
Pitch::G,
Pitch::B_FLAT,
Pitch::D,
]);
let read_as_is = played.identify(); // Some(D#maj7)
for chord in played.completions() {
println!("{}", chord); // Cm9, Cm11, D#maj7, D#maj9, D#maj13
}
Reading it as it stands gives an E flat major 7, and that is not wrong. But it is also four fifths of a C minor 9, which nothing else would tell you — and the root of that chord is not among the notes at all.
Printing uses sharps, so the E flat reads back as D#. The value is the same
either way.
Every interpretation is also a completion, since a chord contains itself. The empty set is contained in everything, so it yields all 372 named chords.
See the Chords tutorial for building a chord when you already know its root.
See Keys and modes for printing it with flats, and for what a chord is called inside a key.
Keys and modes
A Key is a tonic and a mode. It says which seven pitches belong, how to spell
the black keys, and what a chord is called inside it.
use music_core::Key;
use music_core::Mode;
use music_core::Pitch;
let c_major = Key::new(Pitch::C, Mode::Ionian);
let a_minor = Key::new(Pitch::A, Mode::Aeolian);
let d_dorian = Key::new(Pitch::D, Mode::Dorian);
The Seven Modes
A mode is the major scale started from another of its notes. Ionian is the major scale itself, and Aeolian is the natural minor.
| Mode | Steps above the tonic | Starts on, in C major |
|---|---|---|
| Ionian | 0 2 4 5 7 9 11 | C |
| Dorian | 0 2 3 5 7 9 10 | D |
| Phrygian | 0 1 3 5 7 8 10 | E |
| Lydian | 0 2 4 6 7 9 11 | F |
| Mixolydian | 0 2 4 5 7 9 10 | G |
| Aeolian | 0 2 3 5 7 8 10 | A |
| Locrian | 0 1 3 5 6 8 10 | B |
use music_core::Mode;
let steps = Mode::Dorian.steps(); // [0, 2, 3, 5, 7, 9, 10]
let name = Mode::Dorian.to_string(); // "Dorian"
let all = Mode::ALL.len(); // 7
The Notes Of A Key
pitch_set gives the seven pitches. parent gives the major scale they come
from.
use music_core::Key;
use music_core::Mode;
use music_core::Pitch;
let d_dorian = Key::new(Pitch::D, Mode::Dorian);
let notes = d_dorian.pitch_set().to_string(); // "{C, D, E, F, G, A, B}"
let parent = d_dorian.parent(); // C
let has_b = d_dorian.pitch_set().contains(Pitch::B); // true
D Dorian uses the white keys, just like C major. Only the starting note differs.
The Signature
signature counts the sharps or flats: positive for sharps, negative for flats.
It is the parent major’s, so it is also the key’s place on the circle of fifths.
use music_core::Key;
use music_core::Mode;
use music_core::Pitch;
let g = Key::new(Pitch::G, Mode::Ionian).signature(); // 1
let f = Key::new(Pitch::F, Mode::Ionian).signature(); // -1
let c_minor = Key::new(Pitch::C, Mode::Aeolian).signature(); // -3
let a_minor = Key::new(Pitch::A, Mode::Aeolian).signature(); // 0
The range is -5 to 6. Six is always six sharps: a pitch class cannot say whether F sharp or G flat was meant.
Each pitch also knows its own place on the circle, counted in fifths above C.
use music_core::Pitch;
let g = Pitch::G.fifths(); // 1
let d = Pitch::D.fifths(); // 2
let f = Pitch::F.fifths(); // 11, one fifth below C
Spelling
Printing always uses sharps, so one value has one text. A key that has flats
asks for them with spelling. Hand that to spelled on a chord or a note.
use music_core::Chord;
use music_core::Key;
use music_core::Mode;
use music_core::Pitch;
let key = Key::new(Pitch::C, Mode::Aeolian);
let spelling = key.spelling(); // Flats
let chord = Chord::major(Pitch::A_FLAT);
let plain = chord.to_string(); // "G#"
let in_key = chord.spelled(spelling).to_string(); // "Ab"
let note = Pitch::E_FLAT.at(4);
let printed = note.spelled(spelling).to_string(); // "Eb4"
The value does not change. Only its text does.
How A Key Writes Its Notes
Sharps or flats throughout is not how a score is written. C major writes its
lowered seventh as B flat but its raised fourth as F sharp, and F sharp major
writes its seventh as E sharp, not F. name_of names a pitch the way the key
does: its own seven notes take the scale’s seven letters, and every other note
takes the letter of the step its roman numeral is on.
use music_core::Key;
use music_core::Mode;
use music_core::Pitch;
let c_major = Key::new(Pitch::C, Mode::Ionian);
let seventh = c_major.name_of(Pitch::B_FLAT).to_string(); // "Bb"
let fourth = c_major.name_of(Pitch::F_SHARP).to_string(); // "F#"
let f_sharp_major = Key::new(Pitch::F_SHARP, Mode::Ionian);
let leading = f_sharp_major.name_of(Pitch::F).to_string(); // "E#"
A NoteName is a letter and its accidentals. The letter is what a staff
needs: it decides which line or space the note sits on.
use music_core::Letter;
use music_core::NoteName;
use music_core::Pitch;
let c_flat = NoteName::new(Letter::C, -1);
let sounds = c_flat.pitch(); // B
let octave = c_flat.octave_of(Pitch::B.at(3)); // 4, as written: Cb4
Roman Numerals
degree_of names a chord by where its root sits in the key. The case of the
numeral follows the chord’s third: upper for major, lower for minor.
use music_core::Chord;
use music_core::Key;
use music_core::Mode;
use music_core::Pitch;
let c_major = Key::new(Pitch::C, Mode::Ionian);
let one = c_major.degree_of(Chord::major(Pitch::C)).to_string(); // "I"
let two = c_major.degree_of(Chord::min7(Pitch::D)).to_string(); // "ii7"
let five = c_major.degree_of(Chord::dom7(Pitch::G)).to_string(); // "V7"
let seven = c_major.degree_of(Chord::dim(Pitch::B)).to_string(); // "vii°"
A root outside the key still gets a numeral. It is marked flat or sharp against the key’s own step.
use music_core::Chord;
use music_core::Key;
use music_core::Mode;
use music_core::Pitch;
let c_major = Key::new(Pitch::C, Mode::Ionian);
let borrowed = c_major.degree_of(Chord::major(Pitch::B_FLAT)).to_string(); // "bVII"
let a_minor = Key::new(Pitch::A, Mode::Aeolian);
let leading = a_minor.degree_of(Chord::dim7(Pitch::G_SHARP)).to_string(); // "#vii°7"
| Root above the tonic | Written as, when outside the key |
|---|---|
| 1 semitone | bII |
| 3 semitones | bIII |
| 6 semitones | #IV |
| 8 semitones | bVI |
| 10 semitones | bVII |
In a minor or modal key the same rule raises a note instead, when the major
scale has it higher: the leading tone of A minor is #vii.
The bass does not change a numeral. Am7/C in C major is still vi7.
Naming Two Notes
Chords need three notes, or a fifth. Two other notes are an interval, and
name says which.
use music_core::Interval;
let third = Interval::MAJOR_THIRD.name(); // "major 3rd"
let tenth = Interval::new(16).name(); // "major 3rd"
let more = Interval::new(16).compound_octaves(); // 1
A tenth is a third and an octave more, so its name is the third’s.
See Where to go next for the crate that builds on all of this.
Where to go next
You have seen the whole of music-core: pitches, intervals, notes, sets of
pitches, chords, and keys.
The Transformations
neo-riemann is the crate built on top of this one. It adds consonant triads —
major and minor only — and the PLR transformation group: the moves that change
one note by one step and hold the other two still.
| Move | Name | C major becomes |
|---|---|---|
P | parallel | C minor |
L | leading-tone exchange | E minor |
R | relative | A minor |
use neo_riemann::prelude::*;
use neo_riemann::Pitch;
use neo_riemann::Triad;
let c = Triad::major(Pitch::C);
let parallel = c.p().to_string(); // "Cm"
let leading = c.l().to_string(); // "Em"
let relative = c.r().to_string(); // "Am"
let shared = c.common_tones(c.p()); // 2
It re-exports everything from music-core, so depending on it alone is enough.
A Triad is the narrower type, and the two convert both ways.
use neo_riemann::Chord;
use neo_riemann::Pitch;
use neo_riemann::Triad;
let triad = Triad::major(Pitch::C);
let as_chord = Chord::from(triad); // C
let back = Triad::try_from(Chord::major(Pitch::C)); // Ok
let refused = Triad::try_from(Chord::dom7(Pitch::G)); // Err, not a triad
So a triad is always a chord, but only some chords are triads.
What Is Not Here Yet
Analysis passes have not arrived in music-core, nor file or MIDI input and
output, nor key detection. Seventh-chord transformations are missing from
neo-riemann.
Anything that is not specifically neo-Riemannian belongs in music-core. The
split exists so a project wanting a Pitch does not have to take a
transformation group with it.
Reference
music-core lives in this repository, at
engines/shared/crates/music-core,
with its test suite and the three examples this tutorial follows. NI
Chord-Detector names chords with it. neo-riemann is at
codeberg.org/graebe/neo-riemann and
depends on this repository for music-core.