Jump to audio example

Not too many years ago, musicians with more passion than money thought nothing of building their own hardware. In fact, you can still do it if you want. (Check out PAiA for some time-tested DIY music hardware kits.) But now that the world is inundated with massively engineered gadgets such as cell phones and shirt-pocket computers, fewer musicians are satisfied with the limitations of home-brew hardware.

So how about home-brew software? Creating your own music software is easier than you might think, and more affordable, too. In fact, assuming you have a halfway decent computer, you can do it for free.

Music software for DIY aficionados comes in many flavors. You can write your own sequencer or synth using a general-purpose programming language such as C, but that's a massive undertaking. For people who are new to programming, there are easier options. One of my favorites is Csound. But that's a subject for a different article. Also noteworthy is SuperCollider. In this article, however, we're going to take a quick look at one of the more powerful and (relatively) user-friendly graphical programming environments for music: Miller Puckette's Pd.

You don't need a degree in computer programming to use Pd, but calling it point-and-click would be wildly optimistic. Some patience and the ability to think logically will be essential--patience both to wade through some fairly dense documentation, and logic to figure out why your Pd patch isn't doing what you expected (which it almost never will until it has been thoroughly tested and debugged).

Given that so many wonderful music software tools are available with the wave of a credit card, why would you want to roll your own? For one thing, you may want to do esoteric things that no commercial software will do. With the aid of a program like Pd, you can generate and process complex streams of MIDI and audio data for a live performance, or build your own software-based synthesizer and effects.

Beyond that, solving technical problems in software design can be a fascinating intellectual challenge. You get a sense of accomplishment from creating something that works exactly the way you envisioned. Then there's the money factor. Your personal Pd software synth may not sound quite as massive as Native Instruments Absynth or as smooth and rich as AAS Ultra Analog, but if you've just spent your last dime on a fast computer and a decent soundcard, Pd is an unbelievable deal.

In this introductory tutorial, I'll tell you how to get Pd and provide a few insights into what it's like to work with it. Trying to explain all of the details of the program would take a prohibitive amount of space, and reading a full explanation without actually installing Pd would put almost anyone to sleep. There's no substitute for learning by doing. Close to 100 tutorial examples (actual working patches) are included with the Pd distribution, and the best way to learn Pd is to go through these files all one at a time. After a while, you'll find yourself tinkering with a patch. Save the results under a new filename, and congratulate yourself: you're a Pd programmer.

Pd isn't just for audio and MIDI. This video was processed with a free Pd extension called Framestein.

To give you some ideas about what you might want to do with Pd, I've created a couple of MIDI processing utilities, an unusual step sequencer, and a very simple FM synthesizer; see the sidebar We Have Patches for a download link. Once you've installed Pd, you can load and use them. And once you learn a bit about how Pd works, you'll be able to customize them to meet your needs.

What Is Pd?

Pd is a freeware graphical programming environment for realtime MIDI and audio manipulation. (Extensions are also available for video processing.) It's available for Windows, Linux, Mac OS X, and more.

Figure 1: Typical Pd Patch Figure 1. This diagram shows what one portion of a typical Pd patch looks like. (See the full window with comments.) It contains inlets at the top through which data is received, an outlet at the bottom through which the results are transmitted, division (/) and multiplication (*) operators, comments (at the bottom of the window), and so on. This particular subpatch, which is part of the envelope generators in the Simple Synth patch that accompanies this article, processes MIDI velocities (in the range 1-127) based on a velocity response amount (in the range ±100). If you aren't familiar with mathematical symbols like > (greater than) and functions like abs (which takes the absolute value of a number), Pd is not for you.

To create a program in Pd, you connect little boxes to one another with the mouse using graphic "patch cords." Figure 1 shows a not-very-complicated Pd subpatch, which will give you a taste of what Pd programming looks like. Once you've created or loaded a patch, you can interact with it by sending MIDI messages to your computer from a hardware controller and by using onscreen sliders and buttons.

We Have Patches

Here are four Pd patches you can download (24K .zip file) and try out.

Real-Time CC Processor. This real-time processor for MIDI Control Change (CC) data allows you to scale and range-limit both the input and output values. For instance, you could tell it to respond to CC 2 values between 12 and 90 and map them to CC 17 with values between 127 and 72, thus inverting the output as well as scaling it.

Sidestep Sequencer. This is a sort of "sideways" step sequencer. Instead of having a fixed row of steps and letting you define distinct data output values for steps 1, 2, and so forth, it has an independent bank of eight "step" values, each of which can be triggered on any three rhythm steps in the cycle. That makes the sequencer polyphonic and lets you create interesting rhythms more easily. Here's what it sounds like when playing Native Instruments Battery 2:

This is not a finished piece of music, but simply an improvised sketch (which I produced by playing Pd's MIDI output into Cubase SX3). It could be turned into an interesting rhythm track by doing some editing.

Note Transposer/Delay. This is a four-stage MIDI note transposer, rechannelizer, and delay line. When the delay values of all four stages are zero and more than one stage is switched on, the output will be an interval or chord of some variety. When the delay values are non-zero, the algorithm becomes a one-shot arpeggiator: each input note will trigger an entire four-note arpeggio. The rhythm clock is internal, and won't sync to an external sequencer.

Simple Synth. While not a fully functional synthesizer in any sense, this patch can be played from a MIDI keyboard and will produce a variety of timbres. It illustrates two-operator FM synthesis, and makes use of two multi-stage time/level envelopes.

If you've ever seen or used Cycling '74's Max, Pd will look eerily familiar. To a considerable extent, Pd is Max. Puckette created Max at IRCAM, the French research facility, in the mid-1980s. At present, Max is a commercial application, and Puckette has no direct involvement with it. However, he's actively developing Pd.

One of the main differences between Pd and Max is that Pd's functionality is not quite as far-reaching as Max's. Its user interface, although adequate, is quite bare-bones compared to the relatively sexy Max UI. A third variety of the same software, jMax, is also available. It's a free download.

The fact that Pd is free means that there's no tech support. There are, however, active mailing lists to which you can subscribe. Technical questions posted to the lists are usually answered within a day or two by more experienced Pd users. For lots of useful miscellany on Pd (including downloadable goodies designed by various people), the place to go is the PureData website. Puckette, who now teaches at the University of California at San Diego, is not affiliated with PureData, but he does monitor the mailing lists.

Getting and Running Pd

All of the versions of Pd can be downloaded from this page. No special installation procedures are needed (though some Linux users, who tend to be computer experts, prefer to download the Pd source code, which is also available, and compile it themselves). The Windows version, which is what I use, arrives as a .zip file. After it's unzipped, you should place the folder named pd in the root directory of your C drive. Mac users will find the latest info on what to download at PureData.org.

When Pd is first launched in Windows, the Windows XP Firewall may warn you that Pd is attempting a security breach, and ask whether it should unblock. This happens because (according to the manual) Pd is actually two programs that talk to one another using a network-type connection. So you have to allow the unblocking.

In Windows, Pd can be run the old-fashioned way, from a command-line interface. Until recently, in fact, this was the only way to run it. But the latest version (0.38) can be run by double-clicking on the program icon, just as with any other Windows program. The first time you run Pd, you may want to open the Startup window (see Figure 2) and set some startup flags. The syntax for these is the same as the syntax for command-line arguments (which are documented in the manual). The most important flags are the ones that tell Pd what MIDI and audio hardware to use. My soundcard uses an ASIO driver, so I've set my startup flags like this:

-asio -midiindev 1 -midioutdev 3 -audiodev 3

This tells Pd to use the ASIO driver, the first MIDI input device listed in the Windows control panel, the third MIDI output device, and the third audio device. You can make these same settings from a Windows-style dialog box accessed through the Media menu, but then your settings won't be saved for next time.

The other reason you used to have to keep the MS-DOS window open while Pd was running was to display the program's message outputs. For diagnostic purposes, Pd includes a handy print object whose output appears as text. This used to be in the MS-DOS window, but is now in the main Pd window--hooray!

Figure 2: Startup Window Figure 2. Pd's Startup window allows you to enter command-line-style flags (in the bottom area).

Even with the text output, Pd's main window barely qualifies as a window at all. It does have audio clipping indicators and a couple of checkboxes (see Figure 3). When you open an existing file or load a new one, you'll be working in a separate patch window that looks more like Figure 1. New objects can be added to a patch from a menu, or you can type Control-key commands, which is faster.

Figure 3: Main Window Figure 3: The main window in Pd doesn't do much, other than display messages from the program. You can send your own messages to this window from a patch (which is often useful during debugging) using the print object. By adding a text argument to a print object, you can see at a glance where the printout is coming from. Here, I'm looking at the data reaching a print object called modenvseg ("modulation envelope segment").

When MIDI I/O is activated in Windows, the Pd window reports that "midi input is dangerous in Microsoft Windows\; see Pd manual." Sounds ominous, but I haven't yet encountered any difficulties.

Using Pd with Other Music Software

I'm not aware of any way to send audio between Pd and other applications running on the same computer, but if it hasn't been built yet, it's a good bet someone in the community of Pd developers is working on it. MIDI communications are a snap, though, at least for Windows users: I downloaded and installed MIDI Yoke NT from MIDI-Ox. Installing MIDI Yoke in Windows XP is a little tricky, but there's a step-by-step guide on the MIDI-Ox site that shows how to do it.

Programming in Pd

Handling audio signals in Pd requires some care, so it's best to start learning Pd by building and debugging a few MIDI patches. Pd provides a number of objects for handling MIDI, the two most basic being notein and noteout. To use them, insert a new object box from the Put menu, and type "notein" (without the quotation marks) in the box. Follow the same procedure to make a noteout, and connect them as shown on the left side of Figure 4.

Figure 4: Pd Thru Figure 4. A MIDI Thru patch in Pd is pretty darn simple: just connect the notein to the noteout as shown.

The little black indicators at the top and bottom of each box are inlets and outlets. All objects in Pd receive signals at inlets (on the top of the box) and transmit signals from their outlets (on the bottom). You make a connection by clicking on an outlet and dragging with the mouse to the inlet of another box. Each type of object does something different, so each may have a different number of inlets and outlets. The inlets expect to receive certain types of signals, and if an inlet receives a signal that it doesn't know how to handle, you'll see an error message in the Pd window.

The connection between notein and noteout in Figure 4 does nothing except pass on to the output all of the MIDI Note On messages received at the input. It's a MIDI Thru connector for notes. The connection between bendin and bendout is a little more interesting (though perhaps not a great deal more interesting). This patch subtracts 8,191 from the data coming out of the left outlet of bendin, and then passes the result to the corresponding inlet of bendout.

Why do that? Because this simple patch fixes a bug in Pd. The bendin object is sending the MIDI channel of the incoming pitchbend message from its right outlet, and the value of the pitchbend message from its left outlet. MIDI pitchbend is a 14-bit message, which means the data can have any of 16,384 possible values. But as it happens, bendin transmits this data in the range between 0 and 16,383, while bendout expects to receive it in the range between –8,192 and +8,191. By subtracting 8,191, this patch puts the pitchbend data in the range that bendout needs. (Like most computer programs, Pd expects large numbers to be entered with no commas.)

Just about everything you do in programming Pd will be more or less like that: figuring out what range of values the receiving object needs to see, and then shaping the data stream so that it looks right when it reaches its destination. In addition to simple arithmetic operations, you'll use objects like clip (which limits data to the range you specify), spigot (which opens to allow data through and shuts to cut data off), moses (which "parts a data stream," sending values above a threshold that you set to one outlet and values below the threshold to another outlet), and value (which stores a numeric value for later use).

If you're new to the whole idea of graphic programming, you should definitely spend a few days working your way through Pd's included tutorial files. Pd also has interactive help files for most of its objects. Right-clicking on an object brings up a context menu from which you can select help. That opens a new Pd window with information and an actual functioning Pd patch that demonstrates how the object works. Some of the discussions are a bit sketchy, but even so, the help files are a great resource. You can also read the HTML manual in your web browser.


Paradiddle allows you to create Mac OS X applications that control Pd patches "without writing a single line of code."

Clean Cups, Move Down!

The key to Pd programming is understanding the order in which the messages are transmitted. The process is extremely fast, but if two output "cables" are attached to a given object, a message will be sent first down one cable, then down the other. A Pd algorithm can work in an unexpected way, or fail to work at all, if you make a bad assumption about the order in which the messages are being sent.

Objects in Pd send nominally "simultaneous" messages from their outlets in a seemingly counterintuitive way, servicing the outlets in right-to-left order. Why do that? Because it lets you "pre-load" an object by sending inputs to its right inlet(s), and then trigger the object's action by sending a message to the left inlet.

"In Max, you can 'break' a patch if you tidy up the screen. In Pd, that situation is turned on its head."

But what happens when several patch cords are attached to a single outlet? In Max, the objects attached to a single outlet receive their messages in right-to-left order based on the screen position of the receiving object. That has a good side and a bad side. The good side is that you can always see the order in which messages are passed by looking at a patch on the screen. The bad side is that you can "break" a patch if you tidy up the screen and in the process change the right-to-left orientation of two objects that are attached to the same outlet.

In Pd, that situation is turned on its head. When several patch cords are attached to a single outlet, the Pd object sends its messages in the order in which the patch cords were created. As a result, you can safely shuffle objects around on the screen (you'll be doing that a lot) without breaking the patch. But if a patch is malfunctioning, you can't necessarily tell what's wrong by looking at it, because there's no visual indication of the order in which the messages are being transmitted. Deleting the patch cords and reattaching them may be the only way to be sure of what's going on. In fact, I've found that when subpatches are copied and pasted in Pd, the order of the connections can change. After copying, I sometimes have to delete patch cords and reattach them.

Audio in Pd

When it comes to synthesis and signal processing, Pd is very good at some things and not quite so good at others. It will do sample playback and FM synthesis, and the waveshaping tools let you easily produce a wide variety of tone colors. Pd's filters, however, don't compare with what you can get in commercial software. A basic ADSR envelope generator is part of the software library that comes with Pd, but creating a multi-segment envelope with velocity modulation inputs is a project that can easily take an evening if you're new to Pd programming.

To save you the trouble, I've created an extremely simple two-operator FM synthesizer with envelope generators that you can copy and use in your own projects (see the We Have Patches sidebar).

Audio objects in Pd (more than two dozen are provided) have tildes (the ~ character) at the end of their names. The osc~ object, for example, is an oscillator. The mtof~ object (the letters are an abbreviation for "MIDI to frequency") translates MIDI note numbers into frequency values suitable for playing equal-tempered scales. The readsf~ object plays a sound file from the computer's hard drive, the dac~ object sends audio to your soundcard, and so on.

Pd Links

Audio data in Pd normally has a range from -1 to +1. If you accidentally multiply an audio data stream by a large value, Pd will not protect your speakers or your ears from the results. Nor does it have a DSP usage meter; in my experiments, however, Pd seems fairly efficient (though the screen redraws are slow).

Pure DIY

Currently at version 0.38, Pd is a work in progress, not a finished program. But while it may not do everything you might envision (or at least, not until you put in some hard work), it has enough tools to keep a do-it-yourselfer busy for a long, long time. Best of all, you don't have to worry about burning yourself with a soldering iron.

[Note: A shorter version of this article appeared in the April 2004 issue of Electronic Musician magazine.]

Return to digitalmedia.oreilly.com