Created
December 8, 2008 00:59
-
-
Save vilterp/33293 to your computer and use it in GitHub Desktop.
scale name >> key signature
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
octave = ['C',('C#','Db'),'D',('D#','Eb'),'E','F',('F#','Gb'),'G',('G#','Ab'),'A',('A#','Bb'),'B'] | |
def is_valid_scale_name(scalename): | |
if len(scalename) > 2: return False | |
if len(scalename) == 2 and scalename[1] != 'b': return False | |
if scalename[0].upper() not in 'ABCDEFG': return False | |
return True | |
def find_note(name): | |
for i in range(len(octave)): | |
note = octave[i] | |
if isinstance(note,str): | |
if note == name: | |
return i | |
elif((name == note[0]) or (name == note[1])): | |
if name in note: | |
return i | |
def get_note(index,is_flat_scale): | |
note = octave[index] | |
if isinstance(note,str): | |
return note | |
else: | |
if is_flat_scale: | |
return note[1] | |
else: | |
return note[0] | |
def is_flat_scale(tonenumber): | |
if(tonenumber<6): | |
return tonenumber%2==1 | |
else: | |
return tonenumber%2==0 | |
def name_to_key_sig(scalename): | |
pos = find_note(scalename) | |
if is_flat_scale(pos): | |
return next_flat(pos, []) | |
else: | |
return next_sharp(pos,[]) | |
def next_sharp(pos, signature): | |
if pos == 0: | |
return signature | |
else: | |
signature = next_sharp((pos-7)%12,signature) | |
signature.append(get_note(pos-1,False)) | |
return signature | |
def next_flat(pos, signature): | |
if pos == 0: | |
return signature | |
else: | |
signature = next_flat((pos-5)%12, signature) | |
signature.append(get_note((pos+5)%12, True)) | |
return signature | |
if __name__ == '__main__': | |
try: | |
while 1: | |
scalename = raw_input('scale name: ') | |
if is_valid_scale_name(scalename): | |
print name_to_key_sig(scalename) | |
else: | |
print 'Enter a valid scale name. Ex: A, Bb, C' | |
except KeyboardInterrupt: | |
import sys | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment