Music Compojure Tutorial

This is a quick tutorial on how to create music with Music Compojure. Music Compojure is an algorithmic music framework for the Clojure programming language. The music compojure source is hosted on github

Namespaces

The primary namespace is music-compojure. Symbol names for midi notes are defined in the namespace music-compojure.notes. You might typically begin a program with:

  (ns example
    (:use music-compojure)
    (:use music-compojure.notes))

Note Sequences

Monophonic sequences of notes are defined using Clojure vectors. By default, the elements of the vector are alternating midi-note number, note spacing. For example:

  [a4 4, c4 4]

This defines a sequence of two quarter notes, a4 and c4. Note that commas in Clojure are treated as white space. They are included only to improve readability.

The symbol names a4 and c4 evaluate to 69 and 60, the corresponding MIDI note number. The MIDI note numbers could have been used directly to produce exactly the same result:

  [69 4, 60 4]

Dealing with numbers directly is useful in algorithmic generation scenarios.

Creating a MIDI file

A midi file can be created with the create-midi-file function. Here is a complete Clojure program that creates the midi file music.mid:

 (ns example
   (:use music-compojure)
   (:use music-compojure.notes))
   
 (def music [a4 4, c4 4])
 
 (def copyright (str (char 0xA9) " 2010 Author"))
 (def outfile "/tmp/music.mid")
 (create-midi-file music copyright outfile)

Note that the character 0xA9 is the © symbol

Note Sequence Modifiers

Note sequence modifiers are used to specify aspects of how the notes should be played, or how the elements of the note sequence should be interpreted. They can be specified at the beginning of a sequence or at a boundary between any two notes in a sequence. Note sequence modifiers are specified using Clojure maps. For example, to set the tempo to 240 bpm (beats per minute):

  [{:tempo 240} a4 4, c4 4]

To play the first note loudly (high velocity) and the second note quietly (low velocity):

  [{:velocity 127} a4 4, {:velocity 10} c4 4]

An alternate way of doing this is to redefine the format of the note sequence using the :format modifier as follows:

  [{:format [:note :spacing :velocity]} a4 4 127, c4 4 10]

For longer sequences of notes where velocity information is required for each note, this is a much more efficient representation.

Drums

Here is another example that demonstrates the flexibility of note sequence modifiers. This one is tailored to creating a drum beat:

  (def o 0)
  (def x 64)
  (def X 127)
  
  (def settings {:channel 10
                 :note a4
                 :spacing 8     
                 :format [:velocity] })
                 
  (def music [settings o o o x o o o X o x o X o x x X])

Getting Polyphonic

So far, only monophonic sequences of notes have been considered. Polyphonic voicing is supported via Clojure lists. For example:

  [(list a4 a3) 4, c4 4]

The list function must be used rather than simply writing (a4 a3), otherwise Clojure would try to interpret a4 as a function, which it is not. A literal list construct '(a4 a3) can not be used either because the a4 and a3 symbols must be evaluated to produce the corresponding midi note numbers. Because this scenario (as well as more complex evaluations) is very commonly required in music clojure, the underscore symbol _ has been defined as a synonym for the list function, so the above would generally written more tersely as:

  [(_ a4 a3) 4, c4 4]

in progress...

More information is coming here soon. For now, visit Music Compojure on Github


back to main page