Getting Started with Functional Music Programming

One of my 2019 goals is to learn Haskell. To do this, I picked up Haskell School of Music, which teaches Haskell by showing how it can be used to create music. Here is how to get started (on a Mac), as I had to look around a bunch of places to get going.

  • Install the Haskell Platform.
  • Install FluidSynth via brew install fluid-synth.
  • Find samples for the synth to use by searching for FluidR3GM.sf2 and download them.
  • Probably set up an alias like alias synth='fluidsynth ~/Lib/FluidR3GM.sf2'.
  • Install Euterpea and HSoM:
cabal update
cabal install Euterpea
cabal install HSoM

Now, let's play something! Start your synth (perhaps with an alias like the one above: synth). Start ghci and then type:

import Euterpea
play $ c 4 qn

Here, c 4 is the C note from the 4th octave. qn is a quarter note (this follows the American convention... I barely know the 'British' one, but apparently, the American one is more standard for algorithmic music). You can play notes simultaneously with the :=: operator and in sequence with :+:.

play $ c 4 qn :+: e 4 qn :=: e 3 qn

It is possible to create a chord in a simple way:

cEx =  chord [c 4 qn, e 4 qn, g 4 qn]
play cEx

And you can operate on them, e.g., transposing or building melodies from lists. In just a little code, you seem to be able to build very complex melodies.