Monday, 12 August 2013

MIDI controllers in Pure Data

Well, it's been ages since I wrote any blogs on Pure Data, and I thought it was time we really got to grips with phyiscal, tactile controllers. For this, we're going to look at a very common way to control music software, MIDI.

MIDI stands for Musical Instrument Digital Interface is a standard for controlling synthesisers and other sound equipment which was invented in the 80's and has been used since with most music technology, in one form or another. Simply put, MIDI is a set of digital signals which are the same coming from any device, and which any synthesizer responds to in the same way. So any MIDI controller can work with any MIDI synth.

Now, MIDI has mostly worked through MIDI cables, but in the last decade or so it's been possible to get USB midi controllers, which use the same system of signals but can be plugged in to a computer, to control music software. Here's an example:


As you an see, it's a keyboard, plugged into a laptop, it's very simple. And now that we have our controller hooked up, it's time to look at the signals we're getting in pure data. Now, different operating systems do this differently, but under the Media menu in Pd, you'll find some midi settings, (in Linux this is either 'default-midi' or 'alsa-midi'. Select this and you'll see a window like this.



Make sure you select a MIDI device as the input device. If you don't see anything here, it might be because you opened Pd before plugging in your midi device, try restarting Pd.

Now, let's see what kind of signals we're getting out of this. First, we're going to try a MIDI object, notein, which, unsurprisingly, takes the signal from a note into your computer. Now, just so we can get a look at it, lets just hook it up with some number boxes.


Simple enough so far, lets see what happens when we press a note on the keyboard.


Incidentally, this is my first attempt at using an animated visual aid.

So, when I press a key, the first number changes depending which key I press. Now, I have explained earlier that midi notes range from 0 to 127 and are one semitone apart. So when I move from a C to an E, the numbers are 4 semitones apart (from 60 to 64).

The second number is what's known as velocity, or how fast the key is pressed. That's a little harder to show with a GIF, so you'll have to take my word for it, or try it yourself. This, too ranges from 0 to 127. And you'll notice that when you take your finger off the key, the velocity is 0, the end of that note.

The third is a little more difficult to explain. It's what's called the midi channel, which is used for when a keyboard or sequencer are sending out notes for more than one instrument, these will be sent on different channels. There are 16 of these. Usually your keyboard will always send signals on channel 1, so for now we're going to ignore this.

Now, besides notes, midi also includes control signals, to get these into Pd we shorten 'control' to 'ctl' and use the object, ctlin.


Now, almost any control which isn't a note will come in via this object. My keyboard has a few rotary controls (otherwise known as knobs), so lets see what happens when we move some of these:


Now, this is simple enough, turning the knob to the right increases the first number from ctlin, turning it to the left reduces this number.

The last number is once again the midi channel, once again we're only using channel 1.

The middle number is very simple, it's which control is the source of the number, there's up to 128 of these (from 0 to 127, sound familiar). And I'll tell you now that there's no real logic to how these are arranged, you just have to move one of the controllers and see which number it is. So the next challenge is to get just the number from one control. Lets try that right hand knob, labelled B4 and on MIDI control number 73.

We're going to use the object called spigot, which has two inlets. If the right hand one is one or higher, whatever goes into the left inlet is passed through, and when the right inlet gets 0, nothing passes through.

We're also going to use an operator, borrowed from the world of mathematics, ==, or double equals. So when the left inlet is equal to it's argument it outputs a 1, for true, and otherwise, it's a 0. So when we connect one to the other...


So, when the control number is 73, the resulting number gets through to the number below it. So that's just the result of that one knob will get to the lowest number, others will not.

Now, we can do the same with our note numbers. So lets replace double equals with more than (>), this follows the same rules. so we can filter just pressing the keys (the 'note on' signal) by filtering it with more than 0.


So this is just a simplified version of midi notes, the number which gets through is just the last key which was pressed.

So, that's about it, getting midi controls into Pure Data as numbers, and as I've said before, any number you can get into pure data can be used to control pretty much anything. So let's try applying it to an earlier pure data post. In Pure Data and Bacon (Part 2), I made this simple synthesiser which uses a random number to make some simple music.


So, I'm just going to build the same chain, only replacing the random number generator with our simple key controller. And I'm going to set the volume (with *~) from that knob.


So, this is a very simple keyboard synthesizer. There are problems with it, firstly the note won't dissipate when you take your finger off the key, you'll have to turn your notes on and off with the midi control. But the important fact here is that you've made it from scratch, and by playing with a physical object (the keyboard) you can make music directly.

This is only to demonstrate how we can use this simple method of getting input to take direct control of just about anything we've done so far in Pure Data. Also, next I'll tell you how to use a keyboard to make boring sounds into interesting sounds with a patch that's similar to the one which saw me looking like this...


Although that's rather a long story. Feel free to get in touch on @MarmiteJunction and ask me about it.

Or for any other reason. So enjoy playing with Pure Data and MIDI, and stay tuned for some audio manipulation.

No comments:

Post a Comment