Friday, 30 April 2010

Random Walks in Max and PureData

It's been a while since we've looked at PureData and Max as algorithmic composition tools, today's algorithmic composition tutorial looks at Random Walks.

We've looked before in this Random Walks OpenMusic tutorial but this is the first time we've used in random walks in Max and PureData.

Random Walks
We'll start with a simple example:

1. Given a starting pitch of middle C
2. We will make a random choice of two possibilities, up or down 1 semitone from our current pitch
3. We now have a new pitch, from this new pitch go back to step 2 and repeat.


The easiest way to use random walks in Max and PureData is to use the drunk object. This object allows you to set a start value, maximum step size and a bound. Drunk then performs a random walk between 0 and the bound value. Here's an example of a drunk random walk in PureData:


And in Max:

As you can see the patches are identical in PureData and Max. Here we've set a bound of 24, so drunk will perform a random walk between 0 and 24 using the default step size, we've then assuming that this will be mapped to pitch added 48 to the value to lift the pitch range into a useful range for MIDI pitches.

Random walks can be mapped to any musical parameter, e.g. onsets, durations, dynamics, pitch, timbre etc. As the random walk moves around the range of values, it can sound more musical than using a standard random function.

In future posts, we'll look at incorporating random walks into algorithmic music, mapping it to rhythm, pitch, dynamics and other musical parameters. It can be useful and possibly more flexible to create our own random walk object. The rest of this post looks at creating a simple random walk from scratch in Max and PureData, then outputting this random walk to MIDI notes to create a simple algorithmic composition.

To create this algorithmic composition example in PureData:
1. From the put menu add a toggle [or SHIFT CMD T on Mac, SHIFT CTRL T on PC]
2. Create a new metro object [CMD 1 on Mac, CTRL 1 on PC] and connect the toggle to this. In this example the metronome is set to pulse at 100ms intervals.
3. To create our random choice, add a new object and type random 2 into this object.
4. To see the result of the random object you can connect a number box to the random object's output.


5. Add a new object type t b f into it. The t object is a trigger and it helps sequence messages so they get where they need to be in the right order. In Max and PureData B stands for bang and used to send a message to other objects: 'whatever you do, do it now!' f stands for float. Both Max and Puredata use a right-to-left order so the float will be output first, then the bang.
6. Add another new object, type select 0 1 into this object. This select object matches numbers: if the number coming in matches the numbers we've listed [0 and 1 in this case], it will output a bang.
7. Connect two bang buttons from the put menu [or SHIFT CMD B on Mac, SHIFT CTRL B on PC], create two new messages from the put menu [or CMD 2 on Mac, CTRL 2 on PC]. Type -1 into 1 message and 1 into the other.

8. Create a new f object, f stands for float: a floating point number.
9. Create a new + object and connect your objects up as shown in this screenshot:

10. So far our patch will output a bang every 100ms using a metro object. This triggers a random object. The sel object adds -1 to our number if 0 is chosen by the random object and 1 to our number if the random object chooses 1.
11. Add a new loadbang object, connect this to a new message, type 60 into this message. This will be the start pitch for our random walk. Add a new number box to the output of the f object.

12. Now we need to create our MIDI output, create 3 new objects: a makenote object, a flush object and a noteout object.

13. Connect up these final elements, here's a screenshot of the completed patch:


To create this algorithmic composition example in Max:
1. Add a toggle [press t]

2. Create a new metro object [create a new object by pressing n] and connect the toggle to this
3. To create our random choice, add a new object and type random 2 into this object.
4. To see the result of the random object you can connect a number box [press i to create an integer box] to the random object output.


5. Press n to add a new object type t b f into this object. The t object is a trigger and it helps sequence messages so they get where they need to be in the right order. In Max and PureData B stands for bang and used to send a message to other objects: 'whatever you do, do it now!' f stands for float. Both Max and Puredata use a right-to-left order so the float will be output first, then the bang.
6. Press n to add another new object, type select 0 1 into this object. This select object matches numbers: if the number coming in matches the numbers we've listed [0 and 1 in this case], it will output a bang.
7. Connect two bang buttons from the put menu [press b], create two new messages from the put menu [press m]. Type -1 into 1 message and 1 into the other.

8. Create a new float object [press n then type f into this object].
9. Create a new + object and connect your objects up as shown in this screenshot:


10. Our metro object outputs a bang every 100ms, this triggers a random object that randomly outputs a 0 or 1. The sel object adds -1 to our number if 0 is chosen by the random object and 1 to our number if the random object chooses 1.
11. Add a new loadbang object, connect this to a new message, type 60 into this message. This will be the start pitch for our random walk. Add a new number box to the output of the f object.


12. Now we need to create our MIDI output, create 3 new objects: a makenote object, a flush object and a noteout object.

13. Connect up these final elements, here's a screenshot of the completed patch:


Random walks are useful tools for creating algorithmic music. In this algorithmic composition tutorial we've mapped the random walk to pitch, but these can be mapped to any musical parameter.

Subscribe to the atom or rss feed below, or bookmark this page {CTRL D on PC's, CMD D on Macs}, and check back soon for more algorithmic composition tutorials.

1 comment:

Jack said...

This is weird because even though its a tutorial for random walks, the pitch just goes down notes chromatically every time you play it. Done in a round about way to include random walks.

Post a comment