Skip to content

Commit 59f5644

Browse files
update name_to_note() helper
1 parent 70aa65f commit 59f5644

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

cedargrove_midi_tools.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,26 @@ def note_to_name(note):
6666

6767
def name_to_note(name):
6868
"""Translates a note name to a MIDI sequential note value. Note names are
69-
character strings expressed in the NoteOctave format such as 'C4' or 'G#7'.
70-
Note names range from 'C-1' (note value 0) to 'F#9' (note value 127). Note
71-
values are of integer type in the range of 0 to 127 (inclusive). If the
72-
input value is outside that range, the value of `None` is returned.
69+
character strings expressed in Scienfic Pitch Notation (NoteOctave) format
70+
such as 'C4' or 'G#7'. Note names range from 'C-1' (note value 0) to
71+
'F#9' (note value 127). Note values are of integer type in the range of
72+
0 to 127 (inclusive). If the input value is outside that range, the value
73+
of `None` is returned.
7374
74-
:param str name: The note name input in NoteOctave format. No default value.
75+
:param str name: The note name input in SPN format. No default value.
7576
"""
7677
name = name.upper() # Convert lower to uppercase
7778
if name[:-1] in NOTE_BASE:
7879
# Note name is valid
7980
note = NOTE_BASE.index(name[:-1])
80-
octave = int(name[-1])
81-
return note + (12 * (octave + 1)) # Calculated note value
81+
if "-1" in name:
82+
# Special case for octave -1
83+
note = NOTE_BASE.index(name[:-2])
84+
octave = -1
85+
else:
86+
note = NOTE_BASE.index(name[:-1])
87+
octave = int(name[-1])
88+
return min(max(0, note + (12 * (octave + 1))), 127) # MIDI note value
8289
return None # Input string is not in NOTE_BASE
8390

8491

0 commit comments

Comments
 (0)