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.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | note |
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