MusicTheory Docs API Toolbox GitHub NuGet
Contents

API / MusicTheory

KeySignature record

A key: a tonic and a mode, with its signature computed on the line of fifths. Theoretical keys are supported up to two accidentals per note (G# major has F##); equality ignores the tonic's octave.

Constructors

KeySignature(Note tonic, KeyMode mode)
Creates a key.

Throws ArgumentNullException tonic is null.

Throws ArgumentOutOfRangeException The mode is undefined, or the key needs triple accidentals (more than 14 sharps or flats, e.g. Cbb minor).

Properties

Note Tonic { get; }
The tonic.
KeyMode Mode { get; }
Major or minor.
int AccidentalCount { get; }
Accidentals in the signature: sharps positive, flats negative. A double sharp counts twice, so G# major is 8.
IReadOnlyList<NoteName> AlteredNotes { get; }
The letters the signature alters, in the order they enter it (F C G… for sharps).

Methods

bool Equals(KeySignature? other)
Alteration GetAlteration(NoteName noteName)
The accidental the signature puts on noteName (F## in G# major).
KeySignature GetDominant()
Gets the dominant key (fifth above).

Returns The dominant key signature.

IEnumerable<KeySignature> GetEnharmonicEquivalents()
The enharmonic keys with at most seven accidentals: B ↔ Cb, F# ↔ Gb, C# ↔ Db, and in minor G#m ↔ Abm, D#m ↔ Ebm, A#m ↔ Bbm.
int GetHashCode()
KeySignature GetParallel()
The parallel key: same tonic, other mode.
KeySignature GetRelative()
The relative key: the major's 6th degree, the minor's 3rd (C ↔ Am, C# ↔ A#m).
KeySignature GetSubdominant()
Gets the subdominant key (fifth below).

Returns The subdominant key signature.

KeySignature NextInCircle()
The key a fifth above, same mode (C → G, C# → Ab).
static KeySignature Parse(string text)
Parses a key name: a tonic (any case, ASCII or Unicode accidentals) optionally followed by m/min/minor or maj/major/M, e.g. Eb, E♭ minor, F#m, C major. Theoretical keys such as G# (eight sharps) parse, up to fourteen accidentals.

Throws FormatException The text is not a key.

KeySignature PreviousInCircle()
The key a fifth below, same mode (C → F, Cb → E).
Note Spell(int pitchClass)
Spells a pitch class (0 = C) in this key: scale notes (and, in minor, the raised 6th and 7th) use the key's spelling; other black keys are sharps in sharp keys and flats in flat keys; in C major / A minor they are Db Eb F# Ab Bb. The returned note is in octave 4.
string ToString(AccidentalStyle style)
The key name, e.g. Ab, F#m or (Unicode) A♭.
string ToString()
The key name in ASCII, e.g. Ab or F#m.
KeySignature TransposeBySemitones(int semitones)
The key semitones above (or below) this one, same mode, spelled with the fewest accidentals: G +1 is Ab major, not G# major. When both spellings have six accidentals (F#/Gb major, D#/Eb minor) the side of this key wins, and flats win from a key with none.
static bool TryParse(string? text, out KeySignature? key)
Tries to parse a key name; see KeySignature.Parse.