Skip to content

Instantly share code, notes, and snippets.

@jsundram
Created November 27, 2013 01:31
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 jsundram/7669296 to your computer and use it in GitHub Desktop.
Save jsundram/7669296 to your computer and use it in GitHub Desktop.
Calculate the range between two musical notes
def location(pitch):
shift = {"C":0, "D":2, "E":4, "F":5, "G":7, "A":9, "B":11, "#":1, "-":-1}
location = 12 * int(pitch[-1])
for c in pitch[:-1]:
location += shift[c]
return location
def count(low, hi):
return location(high) - location(low)
def main():
# would be good to have some tests here
print "location of C4 is %d" % location('C4')
print "interval (C4, C5) is %d" % count('C4', 'C5')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment