Skip to content

vmagnin/formidi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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...)
  • Includes the list of the General MIDI instruments and percussive instruments, and the list of the MIDI Control Changes.
  • 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 (there are 16 MIDI channels, from 0 to 15. The channel 9 is only for percussion). Listen to the ogg file.
    • demo4: a random walk on the circle of fifths. Listen to the ogg file.
  • 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/demo?.out executable files.

Running the examples

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

$ fpm run --example demo1

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

$ file demo1.mid
demo1.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 demo1.mid
00000000  4d 54 68 64 00 00 00 06  00 01 00 02 00 80 4d 54  |MThd..........MT|
00000010  72 6b 00 00 00 45 00 ff  51 03 07 a1 20 00 ff 01  |rk...E..Q... ...|
00000020  36 54 68 69 73 20 66 69  6c 65 20 77 61 73 20 63  |6This file was c|
00000030  72 65 61 74 65 64 20 77  69 74 68 20 74 68 65 20  |reated with the |
00000040  46 6f 72 4d 49 44 49 20  46 6f 72 74 72 61 6e 20  |ForMIDI Fortran |
00000050  70 72 6f 6a 65 63 74 00  ff 2f 00 4d 54 72 6b 00  |project../.MTrk.|
00000060  00 00 34 00 c0 5d 00 90  43 40 81 00 80 43 00 00  |..4..]..C@...C..|
00000070  90 45 40 81 00 80 45 00  00 90 41 40 81 00 80 41  |.E@...E...A@...A|
00000080  00 00 90 35 40 81 00 80  35 00 00 90 3c 40 82 00  |...5@...5...<@..|
00000090  80 3c 00 00 ff 2f 00                              |.<.../.|
00000097

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

You can use TiMidity++:

$ timidity demo1.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 demo1.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 demo1.mid

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:

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

Playing your MIDI file with your synthesizer

You can connect your musical keyboard or synthesizer to your computer using a USB / MIDI interface. First price is around 15 € or $.

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 demo1.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, follow the stdlib conventions.
  • When ready, make a Pull Request.

MIDI technical information

Bibliography

English

French

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published