A small Fortran MIDI sequencer for composing music, exploring algorithmic music and music theory.

Find us on…

GitHub

ForMIDI

A small Fortran MIDI sequencer for composing music, exploring algorithmic music and music theory. It can also be used to teach Fortran programming in a fun way.

MIDI notes are numbers. You can therefore make music with prime numbers, mathematical suites (Fibonacci, Syracuse...), the decimals of Pi, etc. Like Bach and Shostakovich, you can also use letters from your name to create your own musical cryptogram, like BACH and DEsCH.

Features

  • Creates multi-tracks .mid files (MIDI 1.0).
  • Includes some music theory elements (scales, circle of fifths, chords, notes values and levels...)
  • Includes routines for easily playing chords and broken chords.
  • Includes the list of the General MIDI instruments and percussive instruments, and the list of the MIDI Control Changes.
  • The API is documented with FORD: https://vmagnin.github.io/formidi/
  • Includes various examples with comments.
  • You just need a modern Fortran compiler and a MIDI media player, whatever your OS.
  • GPL-3.0-or-later license.

If you are interested by sound synthesis, you may also be interested by its twin project ForSynth.

Compilation and execution

You can easily build the project using the Fortran Package Manager fpm at the root of the project directory:

$ fpm build

Or you can use the build.sh script and run the build/*.out executable files.

Running the examples

The examples can be found in the example/ directory. For example, the canon.f90 example can be run with the command:

$ fpm run --example canon

The demos files are generated in the root directory of the project:

$ file canon.mid
canon.mid: Standard MIDI data (format 1) using 2 tracks at 1/128

MIDI files are similar to music scores. They don't contain any sound but just binary coded commands for the instruments, and are therefore very light files:

$ hexdump -C canon.mid
00000000  4d 54 68 64 00 00 00 06  00 01 00 05 00 80 4d 54  |MThd..........MT|
00000010  72 6b 00 00 00 1c 00 ff  02 0d 50 75 62 6c 69 63  |rk........Public|
00000020  20 64 6f 6d 61 69 6e 00  ff 51 03 0f 42 40 00 ff  | domain..Q..B@..|
00000030  2f 00 4d 54 72 6b 00 00  08 8a 00 ff 03 0b 67 72  |/.MTrk........gr|
00000040  6f 75 6e 64 20 62 61 73  73 00 b0 5b 40 00 c0 30  |ound bass..[@..0|
00000050  00 90 32 40 81 00 80 32  00 00 90 2d 40 81 00 80  |..2@...2...-@...|
[...]
00002329

The "MThd" string begins the header of the MIDI file. Each track begins with a header beginning by "MTrk". The first track is always a metadata track, containing for example the tempo of the music.

Playing your MIDI file with a media player

Linux

TiMidity++

You can use TiMidity++:

$ timidity canon.mid

The quality of the output depends essentially on the quality of the soundfont. By default, timidity uses the freepats soundfont. A better soundfont is FluidR3_GM.sf2 (fluid-soundfont-gm package in Ubuntu):

$ timidity canon.mid -x "soundfont /usr/share/sounds/sf2/FluidR3_GM.sf2"

You can save the music as a WAV file with the -Ow option, and a OGG with -Ov.

FluidSynth

Another software synthesizer is FluidSynth:

$ fluidsynth -a alsa -m alsa_seq -l -i /usr/share/sounds/sf2/FluidR3_GM.sf2 canon.mid

Add the option -F canon.wav to generate a WAV file, instead of listening directly.

Soundfonts can have several sound banks. For example, the FluidR3_GM.sf2 has more instruments in the bank 8 and the bank 128 (drums). You can see the lists of instruments in those banks with those (Linux) commands:

$ fluidsynth /usr/share/sounds/sf2/FluidR3_GM.sf2
> fonts
ID  Name
 1  /usr/share/sounds/sf2/FluidR3_GM.sf2
> inst 1

macOS

You can use GarageBand.

Windows

You can simply play your MIDI files with the Windows Media Player.

Online tools

You can convert your MIDI files to several audio formats using online tools such as:

  • https://audio.online-convert.com/convert/midi-to-ogg
  • https://www.conversion-tool.com/midi/

With some of them, you can even choose the soundfont.

Playing your MIDI file with your synthesizer

You can easily connect your musical keyboard or synthesizer to your computer using a USB / MIDI interface (first price around 15 €), for example an E-MU Xmidi 2x2:

A MIDI/USB interface

Linux

This ALSA command will print the list of the connected MIDI devices:

$ aplaymidi -l
 Port    Client name                      Port name
 14:0    Midi Through                     Midi Through Port-0
 24:0    E-MU Xmidi 2x2                   E-MU Xmidi 2x2 MIDI 1
 24:1    E-MU Xmidi 2x2                   E-MU Xmidi 2x2 MIDI 2

If the synthesizer is connected to the port 24:0, this command will play the MIDI file:

$ aplaymidi -p 24:0 canon.mid

Importing your MIDI file in other softwares

You can of course import your .mid file into any sequencer like LMMS (Linux, Windows, macOS) or Rosegarden.

Contributing

  • Post a message in the GitHub Issues tab to discuss the function you want to work on.
  • Concerning coding conventions, stay consistent with the style of the project.
  • When ready, make a Pull Request.

MIDI technical information

Other ressources

Bibliography

English

French

Developer Info

Vincent Magnin