Skip to content

Commit

Permalink
update name_to_note() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarGroveStudios committed Sep 16, 2023
1 parent 59f5644 commit 706597d
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions cedargrove_midi_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 706597d

Please sign in to comment.