Wednesday, 17 April 2013

Pure Data and BACON (part 2)

Well, you may well remember that this Monday I got half-way through tutorialising my live code from the fantastic conference, BACON. Well, It's time we finished the job!

You'll probably remember that I got to this point...


And if you don't, you probably want to start reading at part 1.

Well, the first few boxes (metro, random and tabread), combined with the array, length, make up a kind of rhythm generator, which makes the metro in the middle sound a bit more interesting than just a straight-up rhythm which doesn't change at all. Well, we're going to shelve that for now and concentrate on the other audio 'Hello, world!' of pure data, the oscillator.

Step 1: Add oscillator, cut the volume, hear the tone


Important note: That audio multiply (*~) is important! The oscillator produces a tone which uses all of your audio bus (literally, the way audio gets off your computer, it gets on the bus, and goes to your speakers). As a very short course in digital audio, the digital audio bus is a stream of numbers, typically 44100 every second between 1 and -1, you could think of it as instructions for the position of your speakers. It can only push the speaker membrane as far as 1 and only pull it as far as -1. So if it's not reduced, one oscillator will use all of this and it's always best to leave yourself room to build, or Headroom.

We multiply it by a number that's less than 1 because then lowering that number is quieter, and raising it... louder.

An osc~ is a sine-wave oscillator, which produces a sound with only one frequency, and we can set that frequency after it's name, which is how many times it will make this wave in a second (Herz, or Hz). In this case I've used 440 Hz, which is the note orchestras tune to, it's used for dial tones and for testing audio equipment, so you may recognise the sound of this tone. It also gets grating very fast, so let's change it...

Step 2: Take control


All I've done is add a number box here (Put > Number, or ctrl + 3), then changed it. If you're out of edit mode you can drag the number up and down (but be careful, in edit mode you'll drag the number box around your screen instead. You can also set it to a specific number by clicking the number box, typing the number then pressing enter.

As you play with this number you will notice (and you can't miss it) the pitch of your tone changing. You might want to play around for a while, but there is more to do here.

Step 3: Tired of control? Automate!


So what've we done here? Simply added the familiar pattern of random (up to 1000Hz, which won't blow your ears off), metro and toggle. Resulting in a random choice of frequency every quarter of a second (for more on random, metro, toggle and bangs, go back to part 1). It's a kind of kind-of melody, although you might find it puts your teeth on edge a little. It doesn't fit the rules of music you've heard before and it doesn't necessarily play notes you'd find on a musical keyboard.

There is a way to remedy this, however, with the magic of MIDI!

Step 4: muzzle your randomisation, limit it's options


So... I've reduced the random to 24, and added 60 (I'm assuming that you'll be familiar with the plus(+) operation if you're reading this), so the result will range from 60 to 84. And then fed it into a mysterious object called mtof (I pronounce it 'em-toff' mostly to annoy people but it should be ' m to f'). M to F is MIDI to Frequency, I've already explained frequency, but MIDI...

MIDI is a synthesizer control standard which has been around since the 1980's, it expresses everything a musical instrument does in numbers of 0 - 127 (notes, tone, how hard the note is hit, down to where the musician stands and which instrument he picks up). So 60 plus something from 0 - 24 puts us somewhere in the middle of that range, and mtof changes that into the right frequency for the oscillator.

It's sounding a little more palatable, but it needs a bit more...

 Step 5: Muzzled your sound? now to refine it...


All I've done here is add an array, with a size of 8 and a range of 0 - 12, and populated it with a sequence of numbers (don't remember how? look back at part 1, step 6). Then I've reduced the options from random again, down to 8, and fed it into a tabread before continuing.

As a quick recap, this will select one of the 8 numbers in my array, then add 60 and feed that into mtof. You're probably getting the hang of reading Pd patches by now, it's just a matter of knowing what each part does.

It's pretty musical now, using a major scale, changing note every quarter of a second. But now's the time to use you're existing knowledge of pure data to give it a bit more rhythm...

Step 6: Make it awesome, create rhythm...


(The array 'length' has a size of 4 and a vertical range of 1000)

Now, we had a random melody, controlled by a metro. In part 1 we had a rhythm generator, which controlled a metro, making it decide how many times to output it's message each second. All we've done here is plug the end result of part 1 into the patch we had for making a melody.

Note that they're now both started by the same toggle, meaning they will start at the same time if you click this toggle on. There's only one last thing to do here...


Step 7: The coup de grĂ¢ce? Generative duet.


Relax! You don't have to write the whole patch out again. This is actually pretty simple to do, fast. You just drag-select from the first random box down to dac~ and press crtl + d to duplicate it. Be carefull not to lose control of this before you've dragged it clear of the starting chain. Make sure your next click after duplicating a chain is on the duplicated objects (which will already be highlighted in blue).

You then just need to connect that top metro to the random at the top of each chain, and the toggle to all three metro's. Then when you turn that toggle on (or off, then on again) it will start both chains creating random rhythms at the same time and those rhythms will be used to make a random melody from the same scale. In short, a duet.

For added separation, I've put the left chain in the left speaker and the right one in the right speaker, which is why there's two inlets to dac~

So what's it missing?

Well, this is a pretty passable first try at generative music, it has rhythm, melody, and a pretty acceptable scale. It produces harmonies between the two chains. Plus you've learned the basics of Pure Data, well done.

It's lacking Structure, which is pretty important in music. If you've ever heard a band 'just jamming' the same handful of riffs for over an hour, you'll realise this. It also doesn't leave any gaps.

Technologically speaking, there's no volume envolopes, meaning each note is at the same volume all the way through. And the synthesizer (one single sine wave oscillator) in each chain is pretty darned basic. This can all be solved with a bit more pure data, more on that later.

Still, it's a pretty good demo to start off with. Well done you for making it!

What the hey! Let's take it too far!

Armed with the content of this demo, the knowledge that adding or removing 12 from our MIDI note keeps the note the same, but higher/lower, and hiding the arrays away in a sub-patch (more on that later). Here's a couple of variations...


Three identical chains... The last one has no rhythm of it's own, it's controlled directly by the 'master' metro and only picks from the first 5 notes and it's 12 semitones lower (that's 12 midi numbers, too. It's basically when you get low enough that it's the same note again, but lower.



In this example, the third chain only does the 'deciding what the note will be' part once, but makes the sound 3 times. One is 12 semitones higher (again, it's high enough that it's the same note again) and the other 24 semitones higher (high enough that it's the same note again, again). The effect of these three together is a bit like an organ.

I'm sure you can think of more ways to vary this, play around, be creative, I'd love to hear what you've come up with.

As ever, feel free to contact me for any help or questions you have about this stuff. I'm constantly up for helping you out at @MarmiteJunction on twitter.

No comments:

Post a Comment