Skip to content

Instantly share code, notes, and snippets.

@HandyAndyShortStack
Created November 23, 2011 14:13
Show Gist options
  • Save HandyAndyShortStack/1388756 to your computer and use it in GitHub Desktop.
Save HandyAndyShortStack/1388756 to your computer and use it in GitHub Desktop.
Note translator, Engish to lilypond
def parseNote(note):
"""translates English notes to (default) lilypond notes"""
"""takes a string consisting of a note name 'a', 'b', 'c', 'd', 'f', or 'g'
(note names can be either upper or lower case)
optionally followed by a '#' or 'b' to signify sharps and flats, followed by
an octave number corresponding to the note's piano octave.
returns a default lilypond representation of the note in absolute octave
notation. Spaces are ignored. returns None if the input is invalid."""
try:
note = note.replace(' ', '')
piano_octave = eval(''.join([ i for i in note if i.isdigit() ]))
ly_octave = piano_octave - 3
if ly_octave > 0:
octave_marker = "'" * ly_octave
elif ly_octave < 0:
octave_marker = ',' * ly_octave
else:
octave_marker = ''
note_name = note[:(-1 * len(str(piano_octave)))]
if len(note_name) > 2:
raise KeyError
if note_name[0].lower() not in 'abcdefg':
raise KeyError
if len(note_name) == 2:
if note_name[1] == 'b':
accidental = 'es'
elif note_name[1] == '#':
accidental = 'is'
else:
raise KeyError
if len(note_name) == 1:
accidental = ''
note_letter = note_name.lower()[0]
except Exception:
print '\nnot a valid note input'
return None
return ''.join([note_letter, accidental, octave_marker])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment