MIDI_Note Function

public function MIDI_Note(note)

Returns the MIDI note number, from 12 (C0) to 127 (G9). The note name is composed of two or three characters, for example "A4", "A#4", "Ab4", where the final character is the octave.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: note

Return Value integer


Called by

proc~~midi_note~~CalledByGraph proc~midi_note MIDI_Note program~blues blues program~blues->proc~midi_note program~canon canon program~canon->proc~midi_note program~circle_of_fifths circle_of_fifths program~circle_of_fifths->proc~midi_note program~la_folia la_folia program~la_folia->proc~midi_note program~motifs motifs program~motifs->proc~midi_note program~third_kind third_kind program~third_kind->proc~midi_note

Source Code

    integer function MIDI_Note(note)
        character(*), intent(in) :: note
        ! 0 <= octave <=9
        integer :: octave
        ! Gap relative to A4 (note 69) in semitones:
        integer :: gap
        ! ASCII code of the 0 character:
        integer, parameter :: zero = iachar('0')

        select case (note(1:1))
            case ('C')
                gap = -9
            case ('D')
                gap = -7
            case ('E')
                gap = -5
            case ('F')
                gap = -4
            case ('G')
                gap = -2
            case ('A') 
                gap = 0
            case ('B')
                gap = +2
            case default
                write(error_unit, *) "ERROR 4: unknown note name!"
                error stop 4
        end select

        ! Treating accidentals (sharp, flat) and computing the octave:
        select case (note(2:2))
            case ('b')
                gap = gap - 1
                octave = iachar(note(3:3)) - zero
            case ('#')
                gap = gap + 1
                octave = iachar(note(3:3)) - zero
            case default
                octave = iachar(note(2:2)) - zero
        end select

        if ((octave >= 0) .and. (octave <= 9)) then
            gap = gap + (octave - 4) * 12
        else
            write(error_unit, *) "ERROR 5: octave out of bounds [0; 9]"
            error stop 5
        end if

        ! Computing and returning the MIDI note number (A4 is 69):
        MIDI_Note = 69 + gap
    end function MIDI_Note