Relatively simple in CircuitPython: ```python
list of valid note names used by note_lexo, note_name, and name_note helpers
note_base = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
def note_to_name(note):
""" Translates a MIDI sequential note value to a note name. Note values are
of integer type in the range of 0 to 127 (inclusive). Note names are
character strings expressed in the format NoteOctave, such as
'C4' or 'G#7'. Note names can range from 'C-1' (note value 0) to
'F#9' (note value 127). If the input value is outside of that range,
the value of None is returned.
"""
if 0 <= note <= 127: # check for valid note value range
return note_base[note % 12] + str((note // 12)-1) # assemble name
else: return None # note value outside valid range
def name_to_note(name):
""" Translates a note name to a MIDI sequential note value. Note names are
character strings expressed in the format NoteOctave, such as
'C4' or 'G#7'. Note names can 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 of that range,
the value of None is returned.
"""
name = name.upper() # convert lower to uppercase
if (name[:1] or name[:2]) in note_base: # check for valid note name
if '#' in name: # find name and extract octave
note = note_base.index(name[:2])
octave = int(name[2:])
else: # find name and extract octave
note = note_base.index(name[0])
octave = int(name[1:])
return note + (12 * (octave + 1)) # calculate note value
else: return None # string not in note_base