dark mode light mode Search Menu

Sequence Melodies with PureData

Salvation Army USA West on Flickr

 Welcome back to another Pure Data tutorial!

Last time, we learned how to make an on-screen keyboard for controlling the pitch of our oscillator with message boxes. Today, we’ll learn how to program a “counter” that will allow us to automatically play the keys of our keyboard in sequence.

A counter does what its name suggests – it counts up from a lower number (conventionally 0 or 1) to a higher one over time. What’s essential to the counter is the introduction of a measurement of time. The easiest way to introduce time into our PD project is by utilizing the “metro” object. “Metro” is short for metronome – a device that we use in the real world as a means of keeping ourselves in time, often when playing an instrument live. Usually, metronomes produce “clicks”, or short bursts of sound, at consistent intervals. In the same way that using your mouse to click on the keyboard triggers changes in pitch with our oscillator, the metro object generates triggers at regular intervals that we can send to whatever PD boxes we’d like.

Let’s introduce a “metro” object into our PD project by going to the “Put” dropdown menu at the top left corner of the screen. Alternatively, we can use the shortcut “cmd + 1” (Mac), or “ctrl + 1” (Windows). Once we place the object on the canvas, we type in “metro”, and then add a space. After the space, we need to enter an argument for the object – this defines the interval (in milliseconds) of each click. I’ll set the interval to 300ms by typing in “metro 300”

It should look just like this.

Now, the metro object shouldn’t be doing anything yet, since it’s not yet been set to run. To toggle the metronome on or off, we need to connect a “toggle” box. Do so by going back to the “Put” menu and selecting “Toggle”. Alternatively, we can use the shortcut “cmd + SHIFT + T” (Mac), or “ctrl + SHIFT + T” (Windows). The toggle box does as its name suggests – it toggles things on or off.

Once placed on the PD canvas, it should look like this:

Now, we’ll connect the toggle box to the metro object by clicking on the outlet of the metro object, and dragging down to the left inlet of the metro object. Remember, outlets and inlets are represented by the black boxes on the edges of PD boxes, where the black boxes on the top edges of PD boxes are the inlets and those on the bottom are the outlets. It should look like this:

To see our metro object work, let’s connect a “Bang” PD box to its outlet. The “bang” PD box sends out a trigger when it’s clicked, just like your mouse does. It might seem like a simple or even redundant PD box, but from a single click on a bang, we can trigger multiple events at once by routing its output to multiple destinations. Plus, it gives us useful visual feedback that events are being triggered. Let’s add a “bang” to our PD project by going once again to the “Put” menu and selecting “Bang”. Alternatively, we can use the shortcut “cmd + SHIFT + B” (Mac), or “ctrl + SHIFT + B” (Windows).

It should look just like this:

Make sure to get out of edit mode by pressing “cmd + E” (Mac), or “ctrl + E” (Windows), and click on the “toggle” object to toggle the “metro” object. You should see the “bang” flash at regular intervals. This is what we’d call a simple clock. Clocks are essential for what we call sequencing, where we define a set of parameters to change in sequence. A counter produces a sequence of numbers that steadily go up in value.

Now, we can start building our counter. We begin by introducing a “float” object. The “float” is an object that stores numbers that pass through its inlet, and lets them out at its outlet. We’ll do this by introducing a new object to the canvas via the “Put” menu or by pressing “cmd + 1” (Mac), or “ctrl + 1” (Windows). In the box, we’ll type “float”. Alternatively, you can abbreviate “float” with “f”. Hit enter, and connect the output of our bang to its inlet. When you’re done, it should look like this:

Next, let’s introduce a “+” object. This does as its name suggests – it adds things. Do so by, once again, adding a new object to your canvas. In it, key in “+ 1”. Note the space between “+” and “1”. Connect the outlet of your float to the “+ 1” object’s inlet. It should look just like this:

The trigger sent into the float object by our bang now passes through to the inlet of the “+” object. This means that every time the float receives a bang, the “+” object, with an argument of “1”, adds 1 to whatever number is stored in the float object. The third object we need to introduce now is the “modulus”, or “mod” object. This object performs a division on its argument with its input and outputs the remainder.

So, if my argument for “mod” is 5 and my input is 1, then we see that 5 ÷ 1 = 0, with a remainder of 1. If my input is 2, then we see that 5 ÷ 2 = 0, with a remainder of 2. If my input is 5, though, then 5 ÷ 5 = 1, we’re back to the remainder of 0. If we keep adding 1 to the input, the “mod” outputs will cycle between 0 and 4. Let’s introduce the mod object now with an argument of “5”, by going to the familiar “Put” menu, or using our shortcut for placing objects: “cmd + 1” (Mac), or “ctrl + 1” (Windows). When you’re done, it should look like this:

Here comes the tricky bit: we’ve got to connect our “float”, “+”, and “mod” objects together in a way that keeps adding 1 to the float object. First, we connect the “+” object’s outlet to the “mod” object’s inlet. It should look like this:

Then, we connect the outlet of the “mod” object to the right inlet of the “float” object. This changes the argument for “float” to whatever the remainder of the “mod” object is. The float object’s argument – which we could have defined by adding a space + some numerical value when creating it – defines the “default” value that’s stored in the float. When you’re done wiring things up, it should look like this:

What we’ve done is made a loop between these three objects, pushed along by the “metro” object’s regular triggers every 300ms. Since we didn’t specify an argument for “float”, it defaults to 0.

So, when the “float” object receives a trigger from the “bang” object, its default value of 0 is pushed over to the “+” object. In the “+” object, the value stored in “float” is added to the “+” object’s argument. In simpler terms, we’ve made it perform the following function: 0 +1= 1. Then, the output of the “+” object (now 1) gets fed into the “mod” object. The “mod” object performs its function and outputs the remainder of 1. In simpler terms, we get the following function: 5 ÷ 1 = 0, with a remainder of 1.

Finally, to close the loop, our remainder gets fed back into the float object and, on the next trigger from our metro object, gets fed back into our “+” object. Now, we get 1 + 1 = 2, and 5 ÷ 2 = 0, with a remainder of 2. This gets fed back into the float and the cycle continues till we get a remainder of 0 and start all over again.

Watch your counter do its thing by introducing a number box. Do so by clicking on the “Put” menu and selecting “Number”, or by using the shortcut “cmd + 3” (Mac), or “ctrl + 3” (Windows). Connect the inlet of the number box either to the “float” object’s outlet, or the “+” object’s outlet. Doing the former would give you a sequence of numbers from 0 to 4, while the latter gives you a counter that goes from 1 to 5. I will connect the number box to the “+” object’s outlet. It should look just like this:

If you click on the toggle object (out of edit mode!), you should see the numbers count up from 1 in the number box. Now, we’re finally able to do some proper sequencing.

Let’s bring back up our last PD project. If you recall, we introduced several message boxes that overrode the argument for an oscillator object’s frequency. Here’s what we did:

If you started this project in a new PD project, fret not – you don’t have to do this all over again. One of the great things about PD is that we can copy and paste between PD projects. Either copy the whole keyboard-controlled oscillator patch by dragging over it in edit mode and pressing “cmd + c” (Mac), or “ctrl + c” (Windows), or copy your counter in the same way. Then go to whichever PD project you’d like to use for this, and paste what you copied over with “cmd + v” (Mac), or “ctrl + v” (Windows) We should then see them together in the same project like this:

Now, to make our counter useful, let’s introduce a new object: The “Select” object. Do so by going to the “Put” menu and selecting “object”, or use the shortcut “cmd + 1” (Mac), or “ctrl + 1” (windows). Now, type “select”, or “sel”, into the object box. Before you hit “enter”, we need to define a set of arguments for the “select” object. If the “select” object receives a numerical value in its inlet that matches one of its arguments, it will output a trigger from a corresponding outlet. Since our counter goes up to 5, let’s define 5 arguments for our “select” object by typing “select 1 2 3 4 5”. Remember to always put a space between arguments. It should look like this:

If you look at the bottom edge of the “select” object, you should see 6 outlets. The first 5 outlets (from left to right) correspond to the arguments we’ve specified for the select object. The last one passes through any numerical value that doesn’t correspond to any of the “select” object’s arguments. Since our “+ 1” object will only output numbers from 1 to 5, we shouldn’t need to worry about the 6th outlet on the “select” object. Now, let’s connect our number box’s outlet to the “select” object’s inlet, and the first 5 of its outlets to the message boxes that override the oscillator’s argument (which defines the frequency of the oscillator). I’ll do a bit of rearranging to make things neater. It should look like this:

Now, when we toggle the “metro” object and turn up the volume on our output, we should hear each of our message boxes being triggered in sequence.

Congratulations, you’ve programmed your first sequencer. I encourage you to tinker with the values of the “metro” object, the “mod” object, and the message boxes too. Or, duplicate the whole thing, have each duplicate share the same metro/toggle (remember, you’re not limited to one connection per inlet/outlet!), and have two (or more) melodies play at the same time. Experiment, and have some fun.