# ForMIDI, a small Fortran MIDI sequencer for composing music 🎵

**URL:** https://fortran-lang.discourse.group/t/formidi-a-small-fortran-midi-sequencer-for-composing-music/773
**Category:** Announcements
**Created:** [March 2, 2021, 8:19pm UTC](https://fortran-lang.discourse.group/t/formidi-a-small-fortran-midi-sequencer-for-composing-music/773 "2021-03-02T20:19:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [March 2, 2021, 8:19pm UTC](https://fortran-lang.discourse.group/t/formidi-a-small-fortran-midi-sequencer-for-composing-music/773/1 "2021-03-02T20:19:16Z")

</div>

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

> **[GitHub - vmagnin/formidi: A small Fortran MIDI sequencer for composing music,...](https://github.com/vmagnin/formidi)**
>
> A small Fortran MIDI sequencer for composing music, exploring algorithmic music and music theory. - GitHub - vmagnin/formidi: A small Fortran MIDI sequencer for composing music, exploring algorithm...

It is a translation of a C project used for teaching. You can build it using fpm or the build.sh 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](http://magnin.plil.net/IMG/ogg/canon.ogg).
- demo3: a stochastic blues, including a percussion track.

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

- [How to write bytes in a binary file?](https://fortran-lang.discourse.group/t/how-to-write-bytes-in-a-binary-file/763/)
- [How to replace C ftell() and fseek() functions in Fortran?](https://fortran-lang.discourse.group/t/how-to-replace-c-ftell-and-fseek-functions-in-fortran/761/)

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](https://en.wikipedia.org/wiki/Variable-length_quantity) 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!

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [March 8, 2021, 8:02am UTC](https://fortran-lang.discourse.group/t/formidi-a-small-fortran-midi-sequencer-for-composing-music/773/2 "2021-03-08T08:02:24Z")

</div>

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

> **[“Automation Divine”: Early Computer Music and the Selling of the Cold War](https://nmbx.newmusicusa.org/automation-divine-early-computer-music-and-the-selling-of-the-cold-war/)**
>
> Matthew Guerrieri dives deep into something particular about the early days of computer music in the United States. It got its start, quite literally, in the off-hour downtime of the military-industrial complex.

> **[Push Button Bertha – Musica Informatica](https://www.musicainformatica.org/topics/push-button-bertha.php)**

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

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [December 15, 2022, 8:43pm UTC](https://fortran-lang.discourse.group/t/formidi-a-small-fortran-midi-sequencer-for-composing-music/773/3 "2022-12-15T20:43:06Z")

</div>

🎹 [ForMIDI v0.2](https://github.com/vmagnin/formidi) [“Forever Young”](https://tabs.ultimate-guitar.com/tab/alphaville/forever-young-chords-1131576) 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 🎵](http://magnin.plil.net/IMG/ogg/demo4.ogg). 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](https://en.wikipedia.org/wiki/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:

```fortran
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:

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

```

(if you want to sound like Debussy 🌊).

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [June 15, 2024, 1:55pm UTC](https://fortran-lang.discourse.group/t/formidi-a-small-fortran-midi-sequencer-for-composing-music/773/4 "2024-06-15T13:55:20Z")

</div>

Another long term hobby project, [ForMIDI](https://github.com/vmagnin/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 `CHANGELOG.md`).

A new example [`la_folia.f90`](https://github.com/vmagnin/formidi/blob/main/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](http://magnin.plil.net/IMG/ForMIDI/la_folia.ogg).

But for your first encounter with ForMIDI, start by the simplest example [`third_kind.f90`](https://github.com/vmagnin/formidi/blob/main/example/third_kind.f90), which is just playing five notes on a single track:

```bash
$ fpm run --example third_kind

```

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

Work in progress…

---

<div class="post-metadata">

### Author: ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)
#### Post date: [July 4, 2024, 7:29pm UTC](https://fortran-lang.discourse.group/t/formidi-a-small-fortran-midi-sequencer-for-composing-music/773/5 "2024-07-04T19:29:05Z")

</div>

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

- A FORD documentation: [https://vmagnin.github.io/formidi/](https://vmagnin.github.io/formidi/)
- 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`](https://github.com/vmagnin/formidi/blob/main/example/motifs.f90) was added: the original idea was to play with Dmitri Shostakovich’s [DSCH motif](https://en.wikipedia.org/wiki/DSCH_motif) and [BACH motif](https://en.wikipedia.org/wiki/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](http://magnin.plil.net/IMG/ForMIDI/motifs.ogg).
