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



I’m still off topic…and then on it again!

Not added a post for a few days..but I’ve been very busy. Still got the music bug, and I’ve been investigating the musical dice games in the Mozart KV 516f composition(s). Basically you throw two dice and choose bars from a stock of 176 bars using a lookup table. The result, a passable waltz in Mozartian style. Others have been there before, and I started with a python script by Martin Tarenskeen This worked very well, but I had a physical copy of the cards given to me in a present a few years ago, and that also had cards to generate a 2/4 time Contredanse. I decided to add this to the original program. It was quite an organisational task to type in all 176 bars of this new version and check them, and then transfer them to a python array for use in the program.

So I got diverted back on to my Mac where I spent a considerable time getting the Frescobaldi GUI front end for lilypond installed and working. Eventually I got this working and it is a great productivity tool. I typed in the bars (left and right hands are stored separately) and checked them, and then transferred the lilypond source for each bar into a spreadsheet and also a filemaker database, where I worked out duplicates, and generated allocation statements for the python array. I then transferred these back to a copy of the original python script, replacing the original array, typed in the new lookup table (supplied with the card game), modified the script for a different time signature, and eventually got it working.

Finally I integrated the two scripts together, with an input argument to enable the user to choose which one to output, the waltz or the contredanse.

At this stage I went back on topic, and transferred the script to my Pi. I had to amend it again slightly, because the original was written for lilypond 2.16.2 (which I have on my Mac) whereas the installed version on the Pi is 2.14.2 Luckily, apart from the version number there was only one further change to make {to the repositioning of the /tempo command, which wouldn’t compile} and it worked on the PI.

Finally, I used some of the techniques employed in the Sounding-Off project to set up a small bash script to compile and run a pair of waltz and contredanse and display them in Netsurf whilst playing the tunes with timidity.

The end result is perhaps a little more sonorous than the 12tone project that preceded it!
The whole operation is also a great example of how to use different software and indeed different platforms to pass around the data you are working on to achieve your end result.

TextWrangler, lilypond, frescobaldi, fluidsynth, qsynth, Excel, FilemakerPro on the Mac, followed by lilypond, convert, timidity, nano, netsurf on the Pi

A sample Contredanse produced by the script on the Pi


And a sample Waltz

I will post the code for the scripts next week, when it is finalised

Off Topic – A Mac diversion

Having just completed the Sounding-Off project, I was intrigued by the lilypond music engraver program. I wanted to explore it more fully, but found that the version on the Pi was not the latest, and that my early model Pi with only 256Mb of RAM was prone to running out of memory when compiling longer music pieces. I decided to install lilypond on my Mac as well. This was easy using the installer here and there were instructions on the installer page on adding one line script wrappers to enable it to be called from the command line as well as via the built in GUI for running scripts.
I then thought about running the sounding-off scripts from my Mac. This threw up some problems. FIrst I would need to also install Timidity, lame and ImageMagick. Also shuf which is not native on the Mac. A more fundamental problem is that the Mac is based on BSD Unix and the syntax for commands like sed and readlink is different from that used by a gnu based linux.

Unlike a Pi where you don’t mind trashing your OS because it is so easy and quick to flash your SD card again, with a computer like a Mac you are always concerned not to trash it by installing items which are non standard, and you want to be sure that you can easily remove them again if need be. There are several systems to import extra non-Mac packages to the machine including macports, fink and my favourite brew. This project enables you to install packages from the command line using a simple command brew install xxx. Packages are installed to /usr/local/Cellar and symbolic links are placed in usr/local/bin to the binaries. So they are easy to remove either with brew or manually. Brew is installed from here.

With brew installed I added timidity lame coreutils and gnu-sed all using commands like
brew install nnnnnn
Timidity needs a configuration file to be added, and also sound fonts. For tge sound font I used the freepats package which is bundled with the timidity package on the Pi. I downloaded the tar file here and moved the extracted folder to the
/usr/local/Cellar/timidity/2.14.0/share/timidity/ folder.
I created a timidity.cfg file in the same folder with the single line content

source /usr/local/Cellar/timidity/2.14.0/share/timidity/freepats/freepats.cfg

and I also edited the file freepats.cfg inside the freepats folder and added the line

dir /usr/local/Cellar/timidity/2.14.0/share/timidity/freepats immediately after the first two comment lines.

The final package needed was ImageMagick. This is not in the brew setup but can be installed from here. This is in a different location, but uninstall details are provided on the installer page should you wish to remove it later. Once installed, I added it to my PATH by typing /usr/libexec/path_helper
and then copying and pasting the output supplied to execute it. If you then type
which convert you see /opt/ImageMagick/convert showing that it is installed and recognised ok.

The only remaining thing to do is to update the sounding-off scripts by replacing all sed commands with gsed, readlink with greadlink, shuf with gshuf. You also need to remove any sections to display the file in a browser in the Pi.
I modified two of the scripts, the and scripts to create two Mac versions, and which together create web pages of the long versions of the 12tone tune, but a lot faster than on the Pi, because of the more substantial resources available.

This was a slightly tricky exercise overall, but well worth the final results.

I am now investigating the Mozart Musilakisches Wurfelspiel, KV516f pieces. Dnases can be created with the throw of two dice, to select bars which slot together to give reasonable tunes in Mozartian style. A good starting point is the python version, which outputs to lilypond, by Martin Tarenskeen available here.

Sounding-Off… The final Part

Phew! The final part of the Sounding-Off project is now complete and published here

It shows you how to generate the long tunes  and also how to generate tunes on the Pi which are then played automatically on a Mac computer under the control of the Pi.

It also illustrates the use of cron to turn any of the tune generation or playing programs into an alarm clock.

The Endtonerow.previewOf the Sounding-Off Projecttone-2013-08-10-20-27-10


Sounding Off – Still Gestating!

Well I had hope to have this project published by now, but it has developed into something much bigger than I at first envisaged. I have had the programs running for a week now, but have been doing copious testing and revising to try and get them as robust as possible.

The topic covers a wide range. Getting to grips with lilypond, the music engraver program.

Looking at the 12 tone tune and seeing how to add randomly generated rhythms to it, whilst still retaining the 3/4 structure. Dealing with some of the sound issues on the Pi. Looking at how GUI programs can be launched remotely via an ssh terminal.

Then extending the project so that the music files generated on the Pi can be transferred to a Mac, where, under the control of the Pi they are launched and played.

There remain a couple of rough edges I would like to smooth out, and tgeh write up looks like having to be rather large, so I may publish it in installments, but it is gestating well!