ForMIDI, a small Fortran MIDI sequencer for composing music 🎵

ForMIDI v0.1 “Formidable” has been released under GNU GPLv3 license:

It is a translation of a C project used for teaching. You can build it using fpm or the script.

It includes several demos with comments:

  • demo1: five notes that could be useful to communicate with an E.T. intelligence…
  • demo2: a simple canon based on the first measures of Pachelbel’s Canon. Listen to the ogg file.
  • demo3: a stochastic blues, including a percussion track.

Some difficulties concerning the C->Fortran translation were discussed here:

Coding the basics of the MIDI protocole was not very complicated, except concerning the fact that some 32 bits data are encoded using the Variable-Length Quantity code to use only the minimum number of bytes. It’s because MIDI (Musical Instrument Digital Interface) was launched in 1983 and its speed is only 31250 baud!


Two interesting articles about an early experiment: “Push Button Bertha” (1956). In the second link, there is some newspapers articles published in 1956-57:

Probably not yet in Fortran, our baby was just being born…

:musical_keyboard: ForMIDI v0.2 “Forever Young” has been released with these new features:

  • src/music.f90: a module containing some music theory elements (scales, circle of fifths, chords), and the subroutine write_chord() and the function get_note_name().
  • src/MIDI_control_changes.f90: a module with all the MIDI Control Changes parameters.
  • src/GM_instruments.f90: contains the list of 128 General MIDI instruments and 47 percussive instruments (channel 9).
  • src/demos.f90: a new demo4 plays a random walk on the circle of fifths. You can listen to it here :musical_note:. It’s a pleasant music, although just a random walk: the probabilities are 1/3 to move one chord clockwise on the circle of fifths, 1/3 to move one chord counterclockwise, 1/3 to switch between the major and minor circles. The Pythagoreans already knew the fifth was very important in music, with its simple 2/3 string length ratio!

The music theory elements are stored in arrays, for example chords can be defined like this and passed to the write_chord() subroutine:

integer, parameter :: MAJOR_CHORD(1:3) = [ 0, 4, 7 ]
integer, parameter :: MINOR_CHORD(1:3) = [ 0, 3, 7 ]
integer, parameter :: DOMINANT_7TH_CHORD(1:4) = [ 0, 4, 7, 10 ]

A scale can be defined like this:

character(2), dimension(1:6),  parameter :: WHOLE_TONE_SCALE = &
                                          & ['C ','D ','E ','F#','G#','A#']

(if you want to sound like Debussy :ocean:).


Another long term hobby project, ForMIDI 0.3 “Forbidden Planet” was just released. It was heavily refactored with a MIDI_file class and the API was considerably simplified (see the

A new example la_folia.f90 was added. On a first track, strings are playing the chords of that classical theme. On the second track, the same chords are arpeggiated in several ways. Listen to the OGG file.

But for your first encounter with ForMIDI, start by the simplest example third_kind.f90, which is just playing five notes on a single track:

$ fpm run --example third_kind

More comments were added in the examples, as a documentation for beginners.

Work in progress…


ForMIDI 0.4 “Forbidden Love” was just released, with especially:

  • A FORD documentation:
  • A time_signature optional argument in the new() method (4/4 is the default).
  • The MIDI event pitch_bend() (corresponding to the left wheel on synthesizers).
  • Most useful notes values, alongside quarter_note, are now defined as Fortran parameters: whole_note, half_note, eighth_note, sixteenth_note and thirty_second_note, expressed in MIDI ticks. There is also a new dotted() function returning the value of a dotted note.
  • Common note levels are defined as Fortran parameters from pppp_level to ffff_level.
  • The method play_broken_chord() can now either accept a value argument with the whole duration or a values array argument containing the values for each note.

A new example/motifs.f90 was added: the original idea was to play with Dmitri Shostakovich’s DSCH motif and BACH motif (in German notation). As there are four musical tracks, it was also used to explore ways of coding synchronized tracks. Indeed, all MIDI tracks are played simultaneously but are coded sequentially in the Fortran code, unlike a musical score. Listen to the Ogg file.