diff --git a/cedargrove_midi_tools.py b/cedargrove_midi_tools.py index 3cc0343..de1694f 100755 --- a/cedargrove_midi_tools.py +++ b/cedargrove_midi_tools.py @@ -67,26 +67,28 @@ def note_to_name(note): def name_to_note(name): """Translates a note name to a MIDI sequential note value. Note names are character strings expressed in Scienfic Pitch Notation (NoteOctave) format - such as 'C4' or 'G#7'. Note names range from 'C-1' (note value 0) to - 'F#9' (note value 127). Note values are of integer type in the range of - 0 to 127 (inclusive). If the input value is outside that range, the value - of `None` is returned. + such as 'C4' or 'G#7' with middle C defined as 'C4'. Note names range from + 'C-1' (note value 0) to 'G9' (note value 127). Note values are of integer + type in the range of 0 to 127 (inclusive). If the input value is outside + that range, the value of `None` is returned. :param str name: The note name input in SPN format. No default value. """ name = name.upper() # Convert lower to uppercase - if name[:-1] in NOTE_BASE: + if "-" in name: + octave = int(name[-2:]) + name = name[:-2] + else: + octave = int(name[-1:]) + name = name[:-1] + + if name in NOTE_BASE: # Note name is valid - note = NOTE_BASE.index(name[:-1]) - if "-1" in name: - # Special case for octave -1 - note = NOTE_BASE.index(name[:-2]) - octave = -1 - else: - note = NOTE_BASE.index(name[:-1]) - octave = int(name[-1]) - return min(max(0, note + (12 * (octave + 1))), 127) # MIDI note value - return None # Input string is not in NOTE_BASE + note = NOTE_BASE.index(name) + midi_note = note + (12 * (octave + 1)) # MIDI note value + if 0 <= midi_note <= 127: + return midi_note + return None # Name is invalid or outside MIDI value range def note_to_frequency(note):