MusicTheory
Music theory for .NET that spells every note correctly, from a single pitch to an analysed progression.
dotnet add package MusicTheory
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").FrequencyConcert pitch is a parameter, not a constant. Orchestras tune higher, baroque ensembles lower.
Note.Parse("A4").GetFrequency(415)Note.Parse("C#4").IsEnharmonicWith(Note.Parse("Db4"))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"))Interval.Between(Note.Parse("C4"), Note.Parse("Gb4"))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"))Every one of the 3,669 spelled intervals between two octaves is checked against music21 in the test suite.
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()new Scale(Note.Parse("Gb4"), ScaleType.Major).GetNotes()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()ChordSymbol.Parse("E7#9").Voice(3)Every key is a place on one circle.
The key
C major relative Am
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:
| Major | Minor | Signature | Major scale |
|---|---|---|---|
| C♭ | A♭m | 7 flats | C♭ D♭ E♭ F♭ G♭ A♭ B♭ |
| G♭ | E♭m | 6 flats | G♭ A♭ B♭ C♭ D♭ E♭ F |
| D♭ | B♭m | 5 flats | D♭ E♭ F G♭ A♭ B♭ C |
| A♭ | Fm | 4 flats | A♭ B♭ C D♭ E♭ F G |
| E♭ | Cm | 3 flats | E♭ F G A♭ B♭ C D |
| B♭ | Gm | 2 flats | B♭ C D E♭ F G A |
| F | Dm | 1 flat | F G A B♭ C D E |
| C | Am | no sharps or flats | C D E F G A B |
| G | Em | 1 sharp | G A B C D E F♯ |
| D | Bm | 2 sharps | D E F♯ G A B C♯ |
| A | F♯m | 3 sharps | A B C♯ D E F♯ G♯ |
| E | C♯m | 4 sharps | E F♯ G♯ A B C♯ D♯ |
| B | G♯m | 5 sharps | B C♯ D♯ E F♯ G♯ A♯ |
| F♯ | D♯m | 6 sharps | F♯ G♯ A♯ B C♯ D♯ E♯ |
| C♯ | A♯m | 7 sharps | C♯ D♯ E♯ F♯ G♯ A♯ B♯ |
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"))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