MusicTheory

MusicTheory

Music theory for .NET that spells every note correctly, from a single pitch to an analysed progression.

dotnet add package MusicTheory

I

The note

A note is a letter, an accidental and an octave. Middle C is C4. Two notes are equal when all three match, so C♯4 and D♭4 are different notes that happen to sound the same.

Note.Parse("A4").Frequency
440

Concert pitch is a parameter, not a constant. Orchestras tune higher, baroque ensembles lower.

Note.Parse("A4").GetFrequency(415)
415
Note.Parse("C#4").IsEnharmonicWith(Note.Parse("Db4"))
True
II

The interval

Six semitones can be an augmented fourth or a diminished fifth. The letters decide, and the library counts letters. C up to F𝄪 is not a tritone: it is a doubly augmented fourth.

Interval.Between(Note.Parse("C4"), Note.Parse("F##4"))
AA4
Interval.Between(Note.Parse("C4"), Note.Parse("Gb4"))
d5

Intervals have a direction. E down to C is a descending major third, written the way music21 writes it.

Interval.Between(Note.Parse("E4"), Note.Parse("C4"))
M-3

Every one of the 3,669 spelled intervals between two octaves is checked against music21 in the test suite.

III

The scale

A scale is a set of intervals above a tonic. Built that way, F♯ major cannot come out with an F natural: its seventh degree is E♯.

new Scale(Note.Parse("F#4"), ScaleType.Major).GetNotes()
F#4 G#4 A#4 B4 C#5 D#5 E#5 F#5
new Scale(Note.Parse("Gb4"), ScaleType.Major).GetNotes()
Gb4 Ab4 Bb4 Cb5 Db5 Eb5 F5 Gb5
IV

The chord

Read a lead sheet the way a pianist does. The symbol is parsed into a type and its tensions, then every tone is spelled from the root.

ChordSymbol.Parse("C7b9#11").ToChord().GetNotes()
C4 E4 G4 Bb4 Db5 F#5
ChordSymbol.Parse("E7#9").Voice(3)
E3 G#3 B3 D4 F##4

Every key is a place on one circle.

V

The key

The circle of fifths

C major relative Am

Key signature on a treble staff 𝄞

no sharps or flats

C D E F G A B

The fifteen major keys from seven flats to seven sharps, with their relative minors, as the library computes them:

MajorMinorSignatureMajor scale
C♭A♭m7 flatsC♭ D♭ E♭ F♭ G♭ A♭ B♭
G♭E♭m6 flatsG♭ A♭ B♭ C♭ D♭ E♭ F
D♭B♭m5 flatsD♭ E♭ F G♭ A♭ B♭ C
A♭Fm4 flatsA♭ B♭ C D♭ E♭ F G
E♭Cm3 flatsE♭ F G A♭ B♭ C D
B♭Gm2 flatsB♭ C D E♭ F G A
FDm1 flatF G A B♭ C D E
CAmno sharps or flatsC D E F G A B
GEm1 sharpG A B C D E F♯
DBm2 sharpsD E F♯ G A B C♯
AF♯m3 sharpsA B C♯ D E F♯ G♯
EC♯m4 sharpsE F♯ G♯ A B C♯ D♯
BG♯m5 sharpsB C♯ D♯ E F♯ G♯ A♯
F♯D♯m6 sharpsF♯ G♯ A♯ B C♯ D♯ E♯
C♯A♯m7 sharpsC♯ D♯ E♯ F♯ G♯ A♯ B♯
VI

Harmony

Put chords in a key and read them back as Roman numerals. The analysis works by pitch class, so a careless spelling on the lead sheet still lands on the right degree.

HarmonicAnalyzer.Analyze(new[] { "Dm7", "G7", "Cmaj7" }.Select(ChordSymbol.Parse), KeySignature.Parse("C"))
ii7 V7 Imaj7

Or ask which key a progression is in. Am F C G starts and ends near A minor.

KeyDetector.Infer(new[] { "Am", "F", "C", "G" }.Select(ChordSymbol.Parse))[0].Key
Am