Computers offer us a source of untamed possibilities in the form of a random number generator. This chapter focuses on ways to tame this source of possibilities to serve our aesthetic purposes. More information is provided in the reference textbook.

Creating Mozart’s “Musikalisches Würfelspiel”

In 1787, Wolfgang Amadeus Mozart wrote “Musikalisches Würfelspiel”, a musical process for generating a 16-measure waltz through randomness – he rolled dice. In this process, each measure is selected from a set of 11 precomposed chunks of music.

This code sample (Ch. 6, p. 157) demonstrates how to implement a simplified version of Mozart’s musical game.

Creating Pierre Cage’s “Structures pour deux Chances”

An interesting way of applying randomness in music is in a style referred to as chance music. Chance music, also known as aleatoric music, is a compositional technique that introduces elements of randomness into the compositional process. John Cage, among other composers, is well known for his aleatoric compositions. On the other hand, serialism involves using deterministic rules to control choices within the compositional process. Pierre Boulez is well known for his serial compositions.

Aleatoric and serial techniques are compositional opposites of each other. Surprisingly, though, the musical outcome can appear to be very similar. This can be observed in these two pieces — the first aleatoric, the second serialist:

This code sample (Ch. 6, p. 161) capitalizes on this similarity to create a program where is impossible to determine, simply by listening to it, if the compositional approach was aleatoric or a serialist. This piece is attributed to Pierre Cage. Pierre Cage is a fictitious composer (a remix of the names, Pierre Boulez and John Cage).

Creating Iannis Xenakis’ stochastic piece,“Concret PH”

Stochastic music is a compositional method employed by Iannis Xenakis, as a reaction to the abstractness and complexity of music from the Serialist movement. Xenakis proposed that the mathematics of probability could be the basis of a more general and manageable compositional technique (Xenakis 1971).

“Concret PH” is a very influential piece of stochastic music. It was created by Xenakis to be played inside the Philips Pavilion in the 1958 World’s Fair in Brussels. This building was designed by architect Le Corbusier, who employed Xenakis as an architect and mathematician at the time.

The following program (Ch. 6, p. 167) demonstrates how to generate a stochastic piece of music. In the original piece, Xenakis used spliced tape of sounds made by burning charcoal. Here, we mimic the sound using the MIDI instrument BREATHNOISE, which at short “bursts” (notes with short duration) sounds much like Xenakis’ original sound elements.

# ConcretPH_Xenakis.py
#
# A short example which generates a random cloud texture
# inspired by Iannis Xenakis's 'Concret PH' composition
#
# see http://en.wikipedia.org/wiki/Concret_PH
from music import *
from random import *
# constants for controlling musical parameters
cloudWidth = 64 # length of piece (in quarter notes)
cloudDensity = 23.44 # how dense the cloud may be
particleDuration = 0.2 # how long each sound particle may be
numParticles = int(cloudDensity * cloudWidth) # how many particles
part = Part(BREATHNOISE)
# make particles (notes) and add them to cloud (part)
for i in range(numParticles):
# create note with random attributes
pitch = randint(0, 127) # pick from 0 to 127
duration = random() * particleDuration # 0 to particleDuration
dynamic = randint(0, 127) # pick from silent to loud
panning = random() # pick from left to right
note = Note(pitch, duration, dynamic, panning) # create note
# now, place it somewhere in the cloud (time continuum)
startTime = random() * cloudWidth # pick from 0 to end of piece
phrase = Phrase(startTime) # create phrase with this start time
phrase.addNote(note) # add the above note
part.addPhrase(phrase) # and add both to the part
# now, all notes have been created
# add some elegance to the end
Mod.fadeOut(part, 20)
View.show(part)
Play.midi(part)
Write.midi(part, "ConcretPh.mid")

Since randomness is involved, it will generate output similar (but not identical) to this:

Harnessing (or sieving) randomness – wind chimes

As mentioned above, a way to generate artifacts that are aesthetically pleasing, starting with pure randomness, is to filter a random process through a sieve. For example, wind chimes capture random movements of air and force them onto a narrow set of aesthetic possibilities. The following program (Ch. 6, p. 169) demonstrates how to create wind chimes out of randomness.

Music from Brownian motion

Brownian motion is a very correlated, yet unpredictable (random) process observed commonly in nature. The following Python program demonstrates how we can we harness randomness to generate music that is more correlated, “natural” sounding.

Let the drums come alive

In the following program (Ch. 6, p. 179), every now and then (randomly) we interject an open hi-hat sound to the sequence of closed hi-hat sounds. It also randomly varies the loudness (dynamic level) of the notes.

Creating generative music

Thee following program (Ch. 6, p. 187) demonstrates how to develop more intricate algorithmic processes for setting up probabilities of musical events (e.g., pitches and durations) and mapping them into interesting musical artifacts.