Skip to content

Instantly share code, notes, and snippets.

@Preinfarction
Created October 11, 2022 22:35
Show Gist options
  • Save Preinfarction/39fd5a78e71ad1f154a21233f814d236 to your computer and use it in GitHub Desktop.
Save Preinfarction/39fd5a78e71ad1f154a21233f814d236 to your computer and use it in GitHub Desktop.
# Give name_chord() a chord, in the form of a list of strings of interval names, with one interval name for each of the odd scale degrees ^1, ^3, ^5, ^7, ^9, ^11, ^13, and the program will tell you what kind of chord you have. Some example chords are provided. Empty strings must be listed in the place of absent scale degrees.
def get_chord_prefix(chord):
# A chord's prefix specifies the intervals on the third, fifth, and seventh scale degrees, more or less. Chord prefixes include minor, major, diminished, augmented, minor-major, diminished-major, and the empty string for dominant chords. I might also include sus chords here at some point.
if chord[2] == "P5":
if chord[1] == "m3" and chord[3] == "m7":
chord_prefix = "m"
elif chord[1] == "m3" and chord[3] == "M7":
chord_prefix = "m-maj"
elif chord[1] == "M3" and chord[3] == "m7":
chord_prefix = ""
elif chord[1] == "M3" and chord[3] == "M7":
chord_prefix = "maj"
elif chord[1] == "m3":
chord_prefix = "m"
elif chord[1] == "M3":
chord_prefix = "maj"
elif chord[2] == "d5":
if chord[1] == "m3" and chord[3] == "m7":
chord_prefix = "m"
elif chord[1] == "m3" and chord[3] == "d7":
chord_prefix = "dim"
elif chord[1] == "m3" and chord[3] == "M7":
chord_prefix = "dim-maj"
elif chord[1] == "M3" and chord[3] == "m7":
chord_prefix = ""
elif chord[1] == "M3" and chord[3] == "M7":
chord_prefix = "maj"
elif chord[1] == "m3":
chord_prefix = "dim"
elif chord[1] == "M3":
chord_prefix = "maj"
elif chord[2] == "A5":
if chord[1] == "m3" and chord[3] == "m7":
chord_prefix = "m"
elif chord[1] == "m3" and chord[3] == "M7":
chord_prefix = "m-maj"
elif chord[1] == "M3" and chord[3] == "m7":
chord_prefix = "aug"
elif chord[1] == "M3" and chord[3] == "M7":
chord_prefix = "aug-maj"
elif chord[1] == "m3":
chord_prefix = "m"
elif chord[1] == "M3":
chord_prefix = "aug"
return chord_prefix
def get_chord_type(chord):
# In the chord "A.maj7#9", the "7" is the chord type. It's a seventh chord. A chord's type is the largest odd scale degree which has a natural interval in the chord (i.e. perfect or major) and for which all lower degrees among (5, 7, 9, 11, 13) are also present, except that the 7th scale degrees doesn't have to be natural for chords of type 7 and the 5th scale degree doesn't have to be natural for chords of type 5. For example, a chord with "P1 M3 P5 m7" is some kind of 7th chord, and "P1 M3 P5 M7 M9 M13" is a 9th chord, since the 11th scale degree is missing.
if chord[-1] == "M13" and chord[-2] != "" and chord[-3] != "" and chord[-4] != "":
chord_type = 13
elif chord[-2] == "P11" and chord[-3] != "" and chord[-4] != "":
chord_type = 11
elif chord[-3] == "M9" and chord[-4] != "":
chord_type = 9
elif chord[-4] == "m7" or chord[-4] == "M7" or chord[-4] == "d7":
chord_type = 7
else:
chord_type = 5
return chord_type
def get_chord_suffix(chord, chord_prefix, chord_type):
# A chord suffix mostly specifies added upper chord tones, including their alterations. For example, in "A.maj7#9", the "#9" is the suffix.
chord_suffix = ""
if chord[2] == "d5":
if chord_prefix != "dim" and chord_prefix != "dim-maj":
chord_suffix += "b5"
elif chord[2] == "A5":
if chord_prefix != "aug" and chord_prefix != "aug-maj":
chord_suffix += "#5"
if chord[-3] == "m9":
chord_suffix += "b9"
if chord[-3] == "A9":
chord_suffix += "#9"
if chord[-3] == "M9" and chord_type < 9:
chord_suffix += "(add9)"
if chord[-2] == "A11":
chord_suffix += "#11"
if chord[-2] == "P11" and chord_type < 11:
chord_suffix += "(add11)"
if chord[-1] == "m13":
chord_suffix += "b13"
if chord[-1] == "M13" and chord_type < 13:
chord_suffix += "(add13)"
return chord_suffix
def name_chord(chord):
chord_prefix = get_chord_prefix(chord)
chord_type = get_chord_type(chord)
chord_suffix = get_chord_suffix(chord, chord_prefix, chord_type)
if chord_type == 5:
chord_type_string = ""
else:
chord_type_string = str(chord_type)
name = "." + chord_prefix +chord_type_string + chord_suffix
return name
chords = [
['P1', 'M3', 'A5', '', '', '', ''],
['P1', 'M3', 'A5', 'M7', '', '', ''],
['P1', 'M3', 'A5', 'm7', '', '', ''],
['P1', 'M3', 'P5', '', '', '', ''],
['P1', 'M3', 'P5', 'M7', '', '', ''],
['P1', 'M3', 'P5', 'm7', '', '', ''],
['P1', 'M3', 'd5', '', '', '', ''],
['P1', 'M3', 'd5', 'M7', '', '', ''],
['P1', 'M3', 'd5', 'd7', '', '', ''],
['P1', 'M3', 'd5', 'm7', '', '', ''],
['P1', 'm3', 'A5', '', '', '', ''],
['P1', 'm3', 'A5', 'M7', '', '', ''],
['P1', 'm3', 'A5', 'm7', '', '', ''],
['P1', 'm3', 'P5', '', '', '', ''],
['P1', 'm3', 'P5', 'M7', '', '', ''],
['P1', 'm3', 'P5', 'm7', '', '', ''],
['P1', 'm3', 'd5', '', '', '', ''],
['P1', 'm3', 'd5', 'M7', '', '', ''],
['P1', 'm3', 'd5', 'd7', '', '', ''],
['P1', 'm3', 'd5', 'm7', '', '', ''],
['P1', 'M3', 'A5', 'm7', 'M9', 'A11', 'M13'],
['P1', 'M3', 'd5', '', 'A9', 'A11', ''],
['P1', 'M3', 'd5', 'M7', 'A9', 'P11', 'm13'],
['P1', 'M3', 'd5', 'd7', 'M9', '', 'm13'],
['P1', 'm3', 'd5', 'm7', 'm9', 'A11', 'M13'],
]
for chord in chords:
name = name_chord(chord)
print("[" + " ".join(i for i in chord if i != "") + "]", "#", name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment