Mozart KV516 f Musical Dice Game

Here as promised is the code to perform the musical dice game to generate Waltzes and Contredanses in the style of Mozart. This version of the game was published first in 1788. I am indebted to Martin Tarenskeen who published python scripts to simulate the game as far as the Waltzes are concerned, utilising the music engraver program Lilypond and the midi playing program Timidity.You can download his version here.

I have modified his original script and added the code to generate the Contredanses, which are included in my dice game version produced by Philips entitled Musikalisches Wurfelspiel KV516 f, their product nunber being 422 500-0

I have also altered the scripts to use version 2.14.2 of Lilypond which is the version currently available on the Raspberry Pi.

Bascially the dice game throws two dice and uses the sum of the numbers thrown to select in turn the 16 bars needed to compose the Waltz or the Contredanse. These are printed on 176 cards (1 set for the Waltz and a separate set for the Contredanse). A separate booklet holds a lookup table, which selects the bar required for each throw. There are 11 possibilities for each bar matching the sums 2 to 12 for each pair of dice throws.

For example if the first throw give a 2 and a 5, this gives a total of 7 and the lookup table shows that for the first bar of the waltz, choice 7 give bar number 104 of the 176 available.

If you are composing the contredanse, then 7 chooses bar 138 of the 176 contredanse bars.

The python script holds an array containing the 176 bars, and each location contains the note pitches and durations for that bar. The left and right hand staves are held separately in the array. the program simulates the throwing of the dice, and adds the appropriate bar notes to produce a source lilypond file containing the notes. This is then compiled by lilypond to produce a printed score (in this case as a png file, although it could be a pdf or jpg) and a midifile.

I have included a simple bash script appropriate to the Raspberry Pi to run the python script twice, first generating a waltz and then a contredanse and then to display them one after the other in the Midori browser whilst playing them using Timidity to the audio output on the Pi, selectable to HDMI or 3.5mm audio jack.

I will not go into the scripts in detail. The main script is This parses the various input parameters, and generates the dice throws and the bar choices, using procedures contained in the separate, which contains the lookup tables for the two types of music.

Dicewalz then chooses the appropriate output file, in this case a lilypond source file, which is generated by calling either or These scripts contain the arrays with the 176 bars of note data. The relevant bars are assembled into  a file, which also contains details of time signature, title, tempo etc. If you use the bash script the files generated are called for the waltz, and for the contredanse. It is worth inspecting these to get an idea of how lilypond works.

The lilypond compiler is then called for each turn, using parameters which generate a png file for the output score. Thus playme.png and playme2.png are produced, together with the midi files playme.midi and playme2.midi.

The script carries out one further task. It uses the convert progam which is part of imagemagick to crop the png files so that they display nicely in Midori. Otherwise they are A4 size which is rather large.

So in order to use these files on the Raspberry PI you need the following.

An up to date installation of raspian Wheezy.

on this you should perform

sudo apt-get update

sudo apt-get install lilypond timidity

sudo apt-get install imagemagick
(this is only needed for the convert program)

You can then download a zip file of the programs here

There is a README.txt file which gives further information about using the files, together with the originalREADME.txt file which accompanied Martin Tarenskeen’s program.

Sample printouts are shown below, and you can hear two pieces here



One thought on “Mozart KV516 f Musical Dice Game

  1. Pingback: Peterborough Raspberry Jam | rbnrpi

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s