Skip to content

Instantly share code, notes, and snippets.

@pianomanfrazier
Last active May 3, 2017 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pianomanfrazier/cff8a13e50bfab0ce55b9e71e8d3b9e5 to your computer and use it in GitHub Desktop.
Save pianomanfrazier/cff8a13e50bfab0ce55b9e71e8d3b9e5 to your computer and use it in GitHub Desktop.
Help function for lilypond. get_interval will return an interval from a given note.
#! python3
CHR_SCALE_S = ('a', 'ais', 'b', 'c', 'cis', 'd', 'dis', 'e', 'f', 'fis', 'g', 'gis')
CHR_SCALE_F = ('a', 'bes', 'b', 'c', 'des', 'd', 'ees', 'e', 'f', 'ges', 'g', 'aes')
CHR_SCALES = (CHR_SCALE_S, CHR_SCALE_F)
SCALE = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
INTERVAL = {'m2':1, 'M2':2, 'm3':3, 'M3':4, 'P4':5, '4+':6, '5-':6, 'P5': 7, 'm6':8, 'M6':9, 'm7':10, 'M7':11}
def get_interval(note, interval, up=True):
'''get a note at interval from given note'''
#get correct letter name, then modify sharp or flat
half_steps = INTERVAL[interval]
for scale in CHR_SCALES:
if note in scale:
if up:
return scale[(scale.index(note) + half_steps) % len(scale)]
else:
return scale[(scale.index(note) - half_steps) % len(scale)]
if __name__ == "__main__":
assert get_interval('aes', 'm2', up=True) == 'a'
assert get_interval('a', 'M6', up=False) == 'c'
assert get_interval('g', '4+', up=False) == 'cis'
assert get_interval('fis', 'm3') == 'a'
assert get_interval('f', 'm3') == 'aes'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment