Routing controller messages in Pure Data
/I’m quite proud of the above Pure Data patch. What’s Pure Data? I hear you ask. What’s a patch? you add. Why don’t you get out more? Well, that’s just not a very nice question.
Anyhoo, Pure Data is programming environment that you can use to process audio data. It is graphical (see above). A Pure Data program is called a “patch” and is comprised of objects that are linked together with “patch cords” which are drawn as lines. And, I do get out. Quite a bit as it happens.
One of the things that you might want to do in a Pure Data is get controller values and use them to control stuff in your patches. A controller is something with a control on it. This could be a knob you can twist or a slider you could slide. For example, you could use a knob to control the volume of a signal. You can buy controllers or you can make your own. I’ve just made one using a PICO device.
Controllers are connected to your computer using a protocol called MIDI (Musical Instrument Digital Interface). This specifies an electrical standard and a message standard. You can buy devices with MIDI sockets and connect them using MIDI cables. However, MIDI also works over USB connections. In other words you can buy a MIDI controller, plug it into your computer and it will be recognised as a MIDI device. Then you just need something that understands MIDI messages. The Pure Data environment can both talk and listen to MIDI.
You then tell Pure Data about your MIDI device and you can start to receive control messages in your music patches and use them to manage the behaviour of your digital instrument. But how does this work, and what is the best way to do it?
Pure Data provides an object called ctlin which accepts MIDI control messages and makes them available to a patch. Each time the user changes the value by moving a control, the ctlin object sends out data values as messages. To use ctlin you don’t need to do anything more than just plonk a ctlin object in your patch and start using the output values that it sends.
The ctlin object produces three outputs, the patch above uses two, the value that represents the position of the controller, and the number of the controller generating that value. Perhaps I might want to use the value from controller 21 to control the attack value of a sound, the value from 22 to control decay, and so on.
That’s what the patch above does. The left hand path through the patch takes the control value and uses it to assemble a send message to send out the value to any patches that wish to receive it. The right hand path uses the controller number to index a list of message destination names so that controller number 21 is sent to attack, 22 to decay and so on.
Then, anywhere that I want to receive the latest decay value, I just have to use a receive object:
The tiny bit of Pure Data above receives the decay value and then multiplies it by 8 before sending it on to another component.
An interesting thing about Pure Data is that send and receive are effectively broadcasts. Any patch can use the decay value. Probably not something you’d be keen on if you worry about global variables in conventional programs, but great if you have lots of components that all have to react to a particular value.
The great thing about my little controller patch is that you can add new messages for additional controller numbers just by changing the contents of the list.
If this is the first time that you’ve seen Pure Data, I’d strongly advise you to take a proper look at it. It really is great fun to play with, very immediate and a great way to introduce people to programming without having to actually write anything. And you can make some interesting sounds. You can find out more here.