Musikalisches Würfelspiel – Hacking music composition

I’ve been playing music instruments for a long time in my life, never seriously though. Lately I’ve been taking piano lessons and also decided to get a little further in music theory and composition.

So I started reading whatever books and lessons I could find. The book Harmony by Walter Piston is very easy to read if you are in a beginning level of music theory and want to learn about harmony. Something caught my attention in this book. At the beginning of chapter III (Mayor mode harmonic progressions) there’s a table that says:

The following table is based in the observation of common uses of the compositors.

  • The I degree is followed by the IV or V, sometimes the VI, and less frequently the I or III.
  • The II degree is followed by the V, sometimes the IV or the VI, and less frequently the I or III.
  • The III degree is followed by the VI, sometimes the IV, and less frequently the I, the II or the V.
  • ….

A chord progression is a series of musical chords based on a root chord (I) establishing a tonality. The degrees of the progression represent the distance to the root chord in the scale. i.e. If the root chord is C the IV degree is F and the V is G. So this table sets a pattern for building chord progressions based on common uses by the compositors.

Being a software developer is hard not to see this table as a set of business rules. So when I read this I wondered: What if I could program this almost accurate business rules and randomly generate mayor chord progressions? And what if, based on the generated chord progression, I could randomly generate a melody over it? What if I don’t? This would be fun to try anyway, I would be learning music and programming at the same time, so no loss.

Before going to the computer I decided to prove the concept in a simpler way, I picked my guitar and a six-sided dice and wrote on a paper the following table:

1, 2, 3 use general rule
4, 5 use “sometimes” rule
6 use “less frecuently” rule

And when I had to choose between various options I threw the dice again. The first random generated progression I got was: C – F – G – F. Not bad at all. Actually it shouldn’t be a surprise that the progression sounded nice to the ear since what this rules tend to do is to have a C – F – G – C progression as ideal. You will find thousands of hits that made it with this (I-IV-V) progression, Twist and shout by The Beatles, Beverly Hills by Weezer, Wild Thing  by The Troggs, Auto rojo by Vilma Palma, etc.

I decided to use an array of arrays for the progression rules, having the current chord as an index the elements inside that position will give me the candidates for the next chord. For the frecuency I repeated the most frequent elements.

C_SCALE = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
F_PROG = [[5, 5, 5, 4, 4, 4, 6, 6, 1, 3],
    [5, 5, 5, 4, 4, 6, 6, 1, 3],
    [6, 6, 6, 4, 4, 1, 2, 5],
    [5, 5, 5, 1, 1, 2, 2, 3, 6],
    [1, 1, 1, 4, 4, 6, 6, 2, 3],
    [2, 2, 2, 5, 5, 3, 3, 4, 4, 1],
    [1, 1, 1, 3, 3, 3, 6, 6, 2, 4]]

def get_next_chord(chord):

    index = C_SCALE.index(chord)
    candidates = F_PROG[index]
    return C_SCALE[random.choice(candidates)-1]

So now that I had a working concept I started looking for python libraries to make sounds with the computer. However I decided to go with midi libraries since using the speaker would make my songs sound like a golden axe weapon.
The library I used is PMIDI no particular reason but that it works and its easy.

For the melody I decided to use my basic knowledge about improvisation.  The rules are, use any note of the scale of the root chord, but it’s always beter to use notes of the chord. Also, we dont want to “jump” too much between notes, say from C to A and then back to D, etc. So I added a rule to make it unlikely for this to happen and try to stay in a 3-note range.

You can listen here, what you’ve been waiting for, an example of the results. I hope you like it! And if you do you can see the source code in here.

So what does Musikalisches Würfelspiel mean? Actually when I did a little research I found this concept is not new, this name was given to a music dice game to generate random minuets in the 18th century. Also Wolfram made a very interesting online version of this kind of program which is called Wolfram Tones so you can check out the not-so-interesting-as-my songs in their site.

Tags: ,

One Response to “Musikalisches Würfelspiel – Hacking music composition”

  1. Rene Dudfield Says:

    Sweet thanks for this article. A good idea, and very simple in python! bookmarked for later.

    For a cross platform midi library consider pygame.midi with pygame 1.9.x +.
    python -m pygame.examples.midi –output


Leave a Reply

Get Adobe Flash player