Created
April 4, 2022 14:37
-
-
Save johentsch/1999aac21f95908c8f94915bad972771 to your computer and use it in GitHub Desktop.
Given an absolute chord (such as 'Ab') and an absolute key (such as 'C') return the chord's scale degree ('bVI').
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ms3 import fifths2rn, name2fifths, rel2abs_key | |
def absolute_chord2rn(abs_chord, abs_key): | |
"""Given an absolute chord (such as 'Ab') and an absolute key (such as 'C') return the chord's scale degree ('bVI').""" | |
is_minor_chord, is_minor_key = abs_chord.islower(), abs_key.islower() | |
chord_fifths, key_fifths = name2fifths(abs_chord), name2fifths(abs_key) | |
interval = chord_fifths - key_fifths | |
result = fifths2rn(interval, is_minor_key) | |
if is_minor_chord: | |
result = result.lower() | |
return result | |
tests = { | |
('G', 'G'): 'I', | |
('g', 'G'): 'i', | |
('G', 'g'): 'I', | |
('g', 'g'): 'i', | |
('Bb', 'G'): 'bIII', | |
('b', 'G'): 'iii', | |
('Bb', 'g'): 'III', | |
('b', 'g'): '#iii', | |
('d', 'G'): 'v', | |
('D', 'g'): 'V', | |
('f', 'A'): 'bvi', | |
('F#', 'A'): 'VI', | |
('F', 'a'): 'VI', | |
('f#', 'a'): '#vi', | |
} | |
for params, true_result in tests.items(): | |
result = absolute_chord2rn(*params) | |
test_result = 'OK' if result == true_result else f"FAIL (expected '{true_result}')" | |
print(f"{test_result} {params} => {result}") | |
### OUTPUT: | |
# | |
# OK ('G', 'G') => I | |
# OK ('g', 'G') => i | |
# OK ('G', 'g') => I | |
# OK ('g', 'g') => i | |
# OK ('Bb', 'G') => bIII | |
# OK ('b', 'G') => iii | |
# OK ('Bb', 'g') => III | |
# OK ('b', 'g') => #iii | |
# OK ('d', 'G') => v | |
# OK ('D', 'g') => V | |
# OK ('f', 'A') => bvi | |
# OK ('F#', 'A') => VI | |
# OK ('F', 'a') => VI | |
# OK ('f#', 'a') => #vi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment