Playing Sound Files Once

This post is part of a series of notes and exercises for a summer camp on making musical instruments with Arduino and Pure Data.

Certainly the most intellectually stimulating means of producing sound is to synthesize it. But sometimes we just want to play something that’s been prerecorded. In this exercise, we’ll create an abstraction to open and play an existing sound file.

As users or clients of this abstraction, we want to be able to specify the path to the sound file as either a creation argument or as a message. Also, we want the sound file to be played when we send a path message. If we send a bang message, we want to play whatever sound file was last played. A patch calling our play_sound_file abstraction might look like this:

The user appears to want to play foo.wav by default, but be able to request difficult.wav later on.

To read a sound file and play it, we use the readsf~ object. It expects a compound message of the form open PATH, start, but we need to replace PATH with either the creation parameter or the path sent in via a message. Normally, we can refer back to creation parameters using $1, $2, $3, and so on. We could, for example, say print $1 to print the creation argument path. But these variable names work differently inside messages. In a message, $1 refers not to a creation argument, but to the first element of whatever comes into the message’s inlet.

The symbol object can help us here. If we say symbol $1, the box will capture the path we send as a creation argument. But we can also hook symbol up to the abstraction’s inlet to let the path be overridden by a path message. Another nice feature of symbol is that when it receives a bang message, it just fires off what string was previously stored.

All told, our abstraction looks like this:

This abstraction reads the file in each time it is played. This reading will cause a delay, which might not be what we want. We’ll consider a mechanism for pre-reading the sound file in another exercise.


After you get your patch working, adjust your pentatouch to play various sound files instead of MIDI notes.


Leave a Reply

Your email address will not be published. Required fields are marked *