akka

Steve Reich’s Clapping Music with Scala and Akka

Using functions to express musical ideas is nothing new: Harmony, time signatures, the relations between notes in a scale and musical form all have their roots in mathematics, and composers have used mathematical abstractions for millennia (see Pythagorean Tuning for a 2500-year-old example).

These abstractions, however, are not always obvious from they way that music is written down, leading to a potential disconnect between composer and performer. In this post, we will use Steve Reich’s 1972 Clapping Music to show how functional programming can be used to capture an underlying musical idea.

Clapping music is an example of phasing, a technique in which an identical musical phrase is played by two instruments that gradually shift out of unison. A visual description of this is given in the The Scrolling Score Project‘s video below:

We can begin to capture this musical idea in code, beginning with the fundamental element of the piece: a beat. Every beat in Clapping Music consists of either a clap or a rest, and we can model this with the following algebraic data type:

The principal phrase on which the phase effect is applied can then be described as a sequence of Beat. We add a constructor to the Phrase class so that simple graphical scores (passed in as Strings) can be used to build these patterns of beats and rests:

Now that we have a simple representation of musical patterns, we can define a function that computes the nth phase for a particular pattern. We outline the expected behaviour of this function in a ScalaTest specification:

Using the drop and take functions on the sequence of beats within a Phrase object, we arrive at the following implementation of the getPhase function. In generating the nth phase, this function removes the first n beats from the phrase, and adds them to the end.

To handle longer pieces of music (which can be modelled as sequences of phrases), we will use scala’s Stream object. This supports lazy evaluation–eliminating the need to construct an entire piece on instantiation–and provides an elegant means to generate repetitions of a particular phrase via the continually function. Since Clapping Music requires two performers, we will use a different  Stream object for each part. The first part simply repeats the original pattern; the number of repeats is given by the length of the pattern plus one, since part two will cycle through each distinct phase of the pattern. We define these two parts as follows:

To capture both of the parts as a single expression, we use the higher-order function zip to create pairs of patterns corresponding to the two parts. We wrap this in the composeTwoPartPhaseMusic function which completes our definition of the Composer object:

We can try out what we have written so far on the REPL. The initial pattern for Clapping Music can be represented graphically as “XXX_XX_X_XX_”, so we will use this to check that the correct variations are generated for each phase:

Now that we have a program that can be used to help compose phase music, we can use scala to play this, with the aid of a MIDI synthesizer. Concurrency is a central aspect of music, and is well supported by functional programming languages like scala. So that two parts can be played in unison, we will use Akka actors to represent different musicians in our program. In the below implementation, each Musician receives a series of messages corresponding to the Beat objects in the piece, which it then uses to create MIDI events:

Ping messages serve as a mechanism for the actors to reply to clients when a series of messages have been processed; in our case these are sent after the last Beat message.

We can finally bring everything together, and write an App object that sets up the MIDI channel, generates the streams of beats in our piece, and sends the messages in order to the two Musician actors. The Future companion object is used to wait until both actors have processed all the messages before the system is stopped.

You can listen to how this sounds here:

 

All the code for this example is available on GitHub, and there are a number of ways in which the ideas outlined here could be developed further. For example, we could extend the Beat type to support musical notes (comprising distinct pitches and durations), and further variations could be generated by reversing the Phrase objects. For the time being though, this has been a useful exercise in learning more about Scala and Akka. Any feedback appreciated in the comments!

Discussion

No comments yet.

Leave a Reply

Email Updates

Subscribe to this blog and receive notifications of new posts by email.

LinkedIn

Richard Ashworth
Head of Engineering at Goodlord
Experienced software engineer and technical lead. Passionate about functional programming, agile methods and domain-driven design. Check out my blog at richashworth.com

Twitter


Read this blog in your favourite news reader:
Subscribe
%d bloggers like this: