dark mode light mode Search Menu

Create a Keyboard with PureData

oatsy40 on Flickr

Welcome back to another PureData (PD) music tutorial. Last time, we made a simple oscillator with frequency/pitch control to make sounds. You can find that article at https://kidscodecs.com/pure-data-create-music-1/

Today, we’ll make a simple on-screen keyboard. Along the way, we’ll learn what MIDI is: a simple language used to connect instruments and computers in electronic music production.

In the last article, we made an Oscillator object named “osc~ 440” tuned to 440 Hz to make sounds. We connected that oscillator object to our output object named “output~” and used its volume slider to make our oscillator louder or softer. To change the frequency of our oscillator object, we connected a slider to its top left inlet. Moving the slider would correspondingly change the “440” argument in our osc~ object. It looked like this:

To make our keyboard, let’s introduce a Message box. To do so, press cmd+2 (mac), or ctrl+2 (windows). Alternatively, you could click on the Put menu link at the top left of your screen and then select Message.

Once you’ve placed the Message box, your PD canvas should look like this with two stacked boxes, one narrow message box above a wide slider box:

If you’re using the PD file from last time, first delete the slider and replace it with the Message box. To do so, press cmd+E (mac), or ctrl+E (windows) to enter Edit mode. Then, click and drag over the “slider” box. You should see a selection window following your mouse, like so:

Now, once clicked, the slider should turn blue, like this:

Hit the delete or backspace key, and the slider should disappear. Remember, when you select PD-boxes, you also can copy them with cmd+C (mac), or ctrl+C (windows) and then paste them elsewhere on the canvas with cmd+V (mac) or ctrl+V (windows).

Now that nothing’s connected to our oscillator object, it will produce a sine wave at 440Hz, just as the “440” argument specifies. Let’s connect our Message box to the oscillator now. In the same way you connected the slider to the oscillator, connect the outlet of the Message box to the left inlet of the oscillator, like so:

The Message box specifies the argument for whatever object it’s connected to. If we clicked on the message (in edit mode) and specified some argument – let’s say 560 – the oscillator object would produce a frequency at 560Hz, like so:

Now, the argument in the “osc~” object will only change when we click on the Message box. This is the beginning of our simple keyboard. It’s quite a simple one at the moment since it only has one key. That’s not very interesting, is it? Let’s change that.

Doing this is as easy as adding more message boxes. I’m going to add 3 more message boxes here, but you can go ahead and add as many as you like. Connect all message boxes to the same inlet on the oscillator, and specify the arguments for each message box. It should look something like this:

Click on any of these message boxes – with the volume on your output turned up – and you should hear the pitch of your oscillator change with each box. If you like, you could have 12 message boxes that correspond to the chromatic scale, the pitches produced by both the black and white keys on a piano, or 8 that correspond to the major scale which, in the key of C, is all the white notes on a piano. Or whatever you like.

This table has the various frequency values that correspond to note values.

Fill out a couple of message boxes with frequencies from this table and see how they sound. Try a set of message boxes with only “sharp” notes, the ones with “#” which have a pitch one step higher, or one without any sharps at all. Experiment with these numbers to find out what sounds good to you.

Feel free, as well, to go online and search for musical scales from around the world. Some of them might not have notes that correspond to the standard frequencies in Western music, for example, the scales used in Indonesian gamelan music. If you can find out what their frequency values are, you’d be able to play in all sorts of musical scales.

That’s all we have for this lesson.

Learn More

Learn music theory in half an hour


Music Scales From Around the World


Introduction to the MIDI Standard by John Gibson


Keyboard and MIDI Control in PureData


Keyboard in PureData


Making a Virtual QWERTY MIdI Keyboard


What is MIDI?


How MIDI Works and Why It’s Useful


MIDI Explained for Beginners


Chromatic Scale