@@ -67,26 +67,28 @@ def note_to_name(note):
6767def name_to_note (name ):
6868 """Translates a note name to a MIDI sequential note value. Note names are
6969 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.
70+ such as 'C4' or 'G#7' with middle C defined as 'C4'. Note names range from
71+ 'C-1 ' (note value 0) to 'G9' (note value 127). Note values are of integer
72+ type in the range of 0 to 127 (inclusive). If the input value is outside
73+ that range, the value of `None` is returned.
7474
7575 :param str name: The note name input in SPN format. No default value.
7676 """
7777 name = name .upper () # Convert lower to uppercase
78- if name [:- 1 ] in NOTE_BASE :
78+ if "-" in name :
79+ octave = int (name [- 2 :])
80+ name = name [:- 2 ]
81+ else :
82+ octave = int (name [- 1 :])
83+ name = name [:- 1 ]
84+
85+ if name in NOTE_BASE :
7986 # Note name is valid
80- note = NOTE_BASE .index (name [:- 1 ])
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
89- return None # Input string is not in NOTE_BASE
87+ note = NOTE_BASE .index (name )
88+ midi_note = note + (12 * (octave + 1 )) # MIDI note value
89+ if 0 <= midi_note <= 127 :
90+ return midi_note
91+ return None # Name is invalid or outside MIDI value range
9092
9193
9294def note_to_frequency (note ):
0 commit comments