Mozart Musical Dice Game

Kevin Wayne

Overview.  A Musikalisches Würfelspiel is a process for generating music by concatenating a random sequence of precomposed musical fragments. In this exercise, you will “compose” a Viennese waltz in the style of Mozart by playing a sequence of 32 WAV files that are chosen at random (from a set of 272) according to the results of throwing dice, as described below.

The waltz is composed of two parts—the minuet, followed by the trio.

Minuet.  The minuet consists of a sequence of 16 musical phrases (numbered 0 to 15), each chosen at random. For the purposes of this assignment, a musical phrase is short musical fragment, approximately two seconds in length. To determine which WAV file to play for phrase i, roll two fair dice.


If the sum is s, then play minueti-s.wav. For example, if you roll an 8 for phrase 3, then play minuet3-8.wav. Note that each roll has 11 possible outcomes (2 through 12) but some outcomes (such as 7) are much more likely than others (such as 2 or 12).

Trio.  The trio consists of a sequence of 16 phrases (numbered 0 to 15), each chosen at random. To determine which WAV file to play for phrase i, roll one fair die.

If the result is s, then play trioi-s.wav. For example, if you roll a 6 for phrase 15, then play trio15-6.wav.

Data files.  The WAV file are available for download as After unzipping, the piano directory contains the 272 WAV files (\(11 \times 16 = 176\) for the minuet phrases and \(6 \times 16 = 96\) for the trio phrases), using the naming conventions described above. Other directories (clarinet, flute-harp, and mbira) contain the same 272 musical phrases, but generated using different musical instruments.

Playing a WAV file.  To play a WAV file, call with the name of the WAV file as an arugment. For example, the following two statements will play the two specified WAV files, one after the other:"piano/minuet3-8.wav");"piano/trio5-6.wav");

To access StdAudio, you must either add stdlib.jar to your Java classpath or put in your working directory.

Your program.  Write a program that plays a waltz according to the rules above. Your program should take one string as a command-line argument. That string specifies the name of a directory containing the WAV files, such as piano.

Here are some sample executions:

~/Desktop/mozart> javac

~/Desktop/mozart> java MusicalDiceGame piano

~/Desktop/mozart> java MusicalDiceGame piano

~/Desktop/mozart> java MusicalDiceGame clarinet

~/Desktop/mozart> java MusicalDiceGame flute-harp

~/Desktop/mozart> java MusicalDiceGame mbira

Context.  Each time you run the program you will compose a piece of music that has never been heard before! The precomposed musical phrases obey a rigid harmonic structure so that each waltz reflects Mozart’s distinct style. However, due to this rigid structure, the process never results in anything truly extraordinary.

Credits.  The 272 phrases were published in 1792 by Mozart's publisher Nikolaus Simrock and attributed to Mozart. The WAV files were generated using MuseScore and the open-source MuseScore_General.sf3 soundfont; they were post-processed using ffmpeg. The images of the dice are minor modifications to those created by Steaphan Greene and are released under a CC BY-SA 3.0 license.