Skip to content

Add Scientific Pitch Notation converter helper to synthio #8406

@CedarGroveStudios

Description

@CedarGroveStudios

In addition to the MIDI note value to frequency converter helper already in synthio, it would be very handy to include a converter for a note's Scientific Pitch Notation (SPN) string to a note frequency.

Here's an example of a helper in CedarGroveStudios/CircuitPython_MIDI_Tools (also in the community bundle) that converts SPN to a MIDI note value.

NOTE_BASE = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]

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' 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 "-" 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)
        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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions