Sunday, April 29, 2012

MIDI Basics

So basic MIDI capability. This is a picture of roughly the simplest polyphonic MIDI patch that will work and play multiple notes, plus a little side demo that shows off how this also can be used with "ordinary" generated notes.

The nice thing about this is that this gives you general polyphony for your projects-- not just if you happen to plug in a MIDI keyboard. (By the way, I'll be using the Korg nanoKey, which for the money is about the best damn keyboard I can imagine getting. It's utterly miniscule but has two octaves worth of velocity sensitive(!) keys and a pair of shifter buttons to allow 10 octaves total from one little keyboard, and that's before you do anything with PD.

So, quick breakdown of functionality.  First we have a notein object, which is how you get MIDI messages into PD.  The 1 in the argument list is a channel select I think-- so far I've just left that filled in.  From there, into the poly object, which does the tricky work of polyphonic splitting.  In Pure Data this is handled in a pretty straightforward manner: the poly object just passes the message along, adding an integer indicating which of the N voices it's directed to, where N is the first argument.  (Here I used 3 to save visual space, but I usually use six so I can play two simultaneous chords.)

Everything up to this point has been ordinary, single-element messages, but as of the pack block, we now have a single wire sending ordered triplets, whose pattern is: [voice, note, velocity].  The route block uses the first element in the message (the "voice" element) to select to which of the three voices the note message will be sent.

In this way, if I hit three notes simultaneously on my keyboard, they'll show up at the notein block in (very) rapid succession, where the poly block assigns them a voice, and then the route block sends each of the three messages to its own unique player below.

The players are identical.  Each consists of an unpack statement to split apart the message back into note and velocity, a square wave generator fed by the note message, and an ead~ envelope generator set to trigger only on non-zero velocities.  (This is because zero velocity is MIDI for "shut off the note" and is triggered by releasing the key.)

The results are summed at the output and you get polyphonic square waves.  This configuration isn't hard to feed with your own data either-- just make sure you provide a nonzero velocity message simultaneously with your note, or as shown earlier, nothing will play.

Download the patch

Saturday, April 14, 2012

MIDI: Polyphony Is Hard, Deal With It

This part's just fundamental, if you do polyphonic MIDI in PD, it's gonna look like this:



And that's if you don't have arguments on that oscillator. That's why I'm learning how to use route, select, etc for message handling, and so far, it ain't pretty...