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.
- 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.
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.
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.
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
.
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
You can use GarageBand.
You can simply play your MIDI files with the Windows Media Player.
You can convert your MIDI files to several audio formats using online tools such as:
With some of them, you can even choose the soundfont.
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:
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
You can of course import your .mid
file into any sequencer like LMMS (Linux, Windows, macOS) or Rosegarden.
- 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.
- https://en.wikipedia.org/wiki/MIDI
- A simple introduction to MIDI files: Oliveira, H. M. de, and R. C. de Oliveira, "Understanding MIDI: A Painless Tutorial on Midi Format", arXiv, 15th May 2017, http://arxiv.org/abs/1705.05322.
- Somascape, a complete MIDI 1.0 documentation.
- MIDI Association:
- Standard MIDI-File Format Spec. 1.1, updated
- (fr) La norme MIDI et les fichiers MIDI
- Codage Variable Length Quantity
- General MIDI instruments
- MIDI notes
- (fr) Introduction au MIDI : les control change
- CrisisGeneralMIDI 3.01 (1.57 Gb)
- Virtual Piano Keyboard (ONLINE PIANIST).
- Jean-Claude Risset, Computer music: why ?, 2003.
- Dave Benson, Music - A Mathematical Offering, 2008.
- Illiac Suite (>Wikipedia).
- My History of electronic music.
- Vincent Magnin, "Avec MIDI, lancez-vous dans la musique assistée par ordinateur !", Linux Pratique, HS n°29, février 2014.
- Vincent Magnin, "Format MIDI : composez en C !", GNU/Linux Magazine, n°196, sept. 2016.
- Vincent Magnin, "Format MIDI et musique algorithmique", GNU/Linux Magazine, n°198, nov. 2016.
- Vincent Magnin, "Lancez-vous dans la « dance music » avec Linux MultiMedia Studio !", Linux Pratique, n°82, mars 2014.
- Vincent Magnin, "Composez librement avec le séquenceur Rosegarden", Linux Pratique, HS n°29, février 2014.
- Moreno Andreatta, "Musique algorithmique", 2009.
- Laurent de Wilde, Les fous du son - D'Edison à nos jours, Éditions Grasset et Fasquelle, 560 pages, 2016, ISBN 9782246859277.
- Laurent Fichet, Les théories scientifiques de la musique aux XIXe et XXe siècles, Vrin, 1996, ISBN 978-2-7116-4284-7.
- Guillaume Kosmicki , Musiques électroniques - Des avant-gardes aux dance floors, Éditions Le mot et le reste, 2nd edition, 2016, 416 p., ISBN 9782360541928.
- Bibliothèque Tangente n°11, Mathématiques et musique - des destinées parallèles, Paris : Éditions POLE, septembre 2022, ISBN 9782848842462.