@@ -67,26 +67,28 @@ def note_to_name(note):
67
67
def name_to_note (name ):
68
68
"""Translates a note name to a MIDI sequential note value. Note names are
69
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.
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.
74
74
75
75
:param str name: The note name input in SPN format. No default value.
76
76
"""
77
77
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 :
79
86
# 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
90
92
91
93
92
94
def note_to_frequency (note ):
0 commit comments