Friday, 26 April 2013

How to make a musical instrument in 10 minutes.

First things first, an update.

My talk at this years BACON has been released for the whole world to see. Needless to say I'm pretty excited about the whole thing. If you've been enjoying the Pure Data tutorials I'd recommend you take a little time to check out the video.

The really excited part is that on the strength of that, I wound up showing off one of my old Pd party pieces at this months Music Tech Meetup at the London offices of Mozilla. Great location and fantastic crowd of techies and musos alike, it's definitely my kind of evening. Including talks on a new music aggregation service and an almost literally viral marketing campaign. 

Then there was myself, once again it seems techies like to see music software come together in short order. So I did one of my old favourites, How to make a musical instrument in 10 minutes. I know it's a big claim, but I've managed it a few times now. It's a simple system to control a synth with your mouse position, start and stop the tone (with a little volume envelope) and a simple effect for good measure. I thought it was time I popped it online so folks can try it themselves at a more leisurely pace. So here it is:

How to make a musical instrument in 10 minutes. 


Groundwork here, you've seen it in earlier posts, and if not, I'd start a little earlier than this and get some basics. It's an oscillator, with a multiply to reduce the volume. Actually, it's probably time for a little maths refresher to help us out with volume controls in pure data. 

Multiply anything by 0, the result is 0.

Multiply anything by 1, the result is what you started with.

Now, digital audio is actually a high speed stream of numbers, between 1 and -1. The further this stream of numbers gets from 0, the louder your sound will be. Simple enough? The raw output from an oscillator ranges from 1 to -1, meaning it uses all of the digital audio stream, and potentially produces quite a loud sound. Also, when and if we add anything else, it can't output numbers higher than 1, we lose some of the definition of the sound we're producing. So we multiply by less than one to give ourselves some head room. 

We can also multiply by 1 or 0 to simply turn the sound on and off. But if we do that all at once, we tend to hear a click as this stream of numbers moves between where it happens to be, and 0 all at once. So, to avoid this unwanted click we use something like this...

Step 1: Add a volume envelope


The new object here, line~, simply creates a line, or envelope, which takes a given amount of time to move from the number it's on, to another number. The tilde(~) denotes that it's an audio object, so it draws this line, but outputs it as a digital audio signal. 

The boxes above the line~, are messages (Put > Message or ctrl + 2), for the time being they just output their content when clicked. There are other things you can do with them, but more on that a little later.

The messages being passed to it consist of a target figure, and how long it will take to get there. So if you click the message on the left, the volume will take a tenth of a second to get up to volume, and if you click the one on the right it will take a whole second to fade out. Simple enough, now all we need to do is make life a little easier...

Step 2: Turn two messages into one. 


As a tiny refresher, the toggle box (Puts > Toggle, or ctrl + shift + t) expresses booleans as 0 and 1, and switches between the two. Also, I've replaced the number 1 with $1, which is simply substituted by the first thing passed in to the message. So when we click the toggle, it will ramp up to 1, or down to 0 in a tenth of a second. The result is a short fade in or out of that tone. 

Step 3: Take direct control


Okay, I've done a few things here. Firstly I've used the object MouseState, there's no particular concept for that to illustrate, it just feeds some data from your mouse into Pd. Although first you've got to pass it the message 'poll'. For more on that, like almost everything else in Pd, you can right click on MouseState and see a working help patch which demonstrates exactly what it does. So I don't have to talk much about it, except that once it's had the 'poll' message, that left hand inlet tells us if the mouse button is pressed or not. 

It's then being passed to a mysterious object called s. This is short for send (you could also type the word send), and a name I've given it. As it's telling me about my mouse button I've called it btn. Then, if something's sent, it re-appears on the patch where you see r, for receive, and the same name. So when you click your mouse, it will send a toggle into the send called btn and the receive called btn will get it and send it to the volume envelope. In short, when you click your mouse, that note will be heard. 

But it's just one note...

Step 4: Take control of the pitch, too.


Well, I've just added a send to the second outlet of MouseState (which the help patch will tell you is the X, or horisontal position of your mouse) to a send called x. You may have to click poll again to make sure this number's changing but when you do, you'll find that moving your mouse left or right will change it, this depends on the size and resolution of your screen, but it will be 0 on the far left hand side and something higher than 1000 at the far right. 

Above the oscillator, we're receiving the send called x and putting it in to something that looks a lot like the volume envelope we made earlier. It's actually identical, do make this easier drag select the message box and the line~ and press ctrl + d, which simply duplicates the selected objects and you can drag them up there. This will take the harsh edges off the numbers we're getting from the mouse position, which then controls the oscillator. 

However, it's controlling the pitch of our oscillator, not it's volume, so we'd call that a volume envelope. 

And you're done!

No, really. It's a musical instrument. Clicking your mouse starts a note, releasing it stops the note and moving your mosue pointer around changes the pitch. It's not very precise or easy to control, but it's essentially a musical instrument. It only listens to clicks made on a Pure Data window, so you might want to full screen the window for more playing space. 

Try a simple rendition of the Dr Who theme, it'll sound horrible. 

But you don't want to leave it there...

Step 5: Cause a little delay


Another pair of corresponding objects here, delwrite~ and delread~, these work a little like send but, oddly enough, with a delay. So delwrite~ builds up a buffer with a given name, in this case d1, and a length in milliseconds, we're using 1000 milliseconds, or a second. 

delread~ is reading d1 and outputting it 1000 milliseconds later, to another dac~ which also outputs the sound to your sound card. 

It doesn't take a genius to realise that what you'll hear now when you click your mouse is the same sound you heard earlier, but repeated a second later. It's a kind of echo, only a really strange echo because its exactly the same volume as the initial sound and it only echoes once. We can fix both of these problems like so:

Step 6: Feed it back


More new objects! 

These ones are pretty straight forwards, tho. s~ and r~ are audio sends and receives, they work in the same way but they move audio around on a named bus, instead of control signals. So after your sound is delayed by a second it is then fed back into the delay machine. 

And I've added a *~ to reduce the volume of this echo by half every time it feeds back. So sounds you make will repeat every second at half of their volume, until they can't be heard any more. It's a real, big, spacey echo and if you use it a lot you'll quickly start to hear a fairly busy soundscape. It build up pretty quickly, tho. I'd suggest you use it quite sparsely.

One last thing to do to this patch...

Step 7: Take control of your feedback


Again, a handful of things have happened here, I've used the third outlet of MouseState to get the vertical position of my mouse and sent it to a send called y. But between those is something a little scary with 4 arguments. 

This used to be called range, but someone involved in the development of Pure Data decided it would be better named maxlib/scale. It simply transforms a range of numbers, the first two arguments (it will be 0 at the top, and your screen height at the bottom, mine is 767 pixels so that's what it is) into another range, the second two arguments. So here we're making the height of our screen change the send y between 0 and 1. 

This is received just above our delay line, fed into the multiply via a line~ (notice that I've taken out the argument, and this means the right inlet is a heavy audio inlet, instead of an empty signal inlet) and that's the feedback level of our delay generator. 

Now if you click your mouse to make a sound and hold your mouse pointer at the bottom of the screen that sound will not decrease in volume and will continue indefinitely. If you move your mouse to the very top, there will be no delay at all. 

I generally hover somewhere three quarters of the way down.



Anyway, that's it. As ever you can follow me @MarmiteJunction and feel free to ask any questions about this tutorial, let me know how you found it. Does it make sense? What would you like to see next? 


Thursday, 18 April 2013

A BACON retrospective

So, as briefly mentioned earlier, I was speaking at the fantastic
BACON
last weekend. It was a fantastic conference! Simply on the topic of stuff that those of us who program computers day in, day out think are awesome! It could have been made for me.

There's a lot of techies around the many and various interwebs who're raving about it right now, so I thought I'd add my voice to the many and give you

My best of BACON



Firstly, beyond the conference itself, it spawned two amazing after parties. A few hundred creative, switched-on and overall inspired techies in one place. Simply chatting, meeting like-minded people and spawning some crazy ideas. It goes a little like this:

Developer A: I wish I could make a machine to start cooking bacon when I'm 5 minutes from home.

Developer B (thoughtfully): you know, with RFID, an arduino and a gas stove...

It's just great to be in that kind of environment when ideas are flowing, and many of those niggling techy "I wonder how to do that" issues get fixed simply by sharing some experience.

The conference itself was great, with talks on subjects as wide as the Chinese game Go, creating images with Lego and bacon flavoured salt.

Personally I was most engaged by those who were just talking about what they love.

Joseph Wilk showed us the game of Go, both as a logical puzzle and a way of life.

Joel Scotkin showed us some real dream chasing, from being one of the first Linux-nerds to designing and prototyping rocket control systems for NASA.

Then the fantastic Vanessa Hurst drew a great many parallels between programmers and super heroes. Suggesting that perhaps we should be using our powers for good after all.

The effect of the whole weekend is pretty immersive, two very full days with a great community and some exceptional speakers. And the true highlight, when the community rise to take their turn. The ten-minute lightening talks covered subjects as wide as flying drones, how to be friendly in London and tea.

As an overview, I'm still tired, and still inspired!




P.S. I'm still posting on my phone from the train. So I'm rediculously sorry it the HTML on this post renders as plain text.

Top Techie Tip #1

I've noticed something unusual, a piece of useful trivia which every tech-minded person should know. And why do I know this? Well...

Over a year ago now, I received a back-pack as a gift which had the added appeal of being a laptop case. Which is a very beneficial situation.

To cut a long story short, this bag was stolen a few months later and eventually I was repaid from insurance for a replacement, and recently got my ducks in a row and actually went out to buy one.

My mother told me she'd bought it at a popular supermarket chain, so I set off back there for a replacement. The staff there had never heard of such a thing and actually told me they never sold back packs. Well, that left me high and dry for a while, until I found out that THE place to get hold of a laptop case is...

Mountain Warehouse

And almost certainly other outdoor retailers. Because what you really need on your next camping trip is a laptop. And don't forget the BS52 nature-national grid adapter so you can charge it from the nearest tree.

Rant over, I would recommend this style of laptop bag to all computer-carriers for a couple of reasons:

* you can be certain it will comfortably fit on your back.
* you can use it for most other things, at the same time, if need be.
* it has the added bonus of not looking like a laptop case. With the obligatory bullseye logo and "mug me, it's a laptop" slogan.

And you can get one of these snazzy bags at your nearest outdoor supplies stockist!

P.S. This is also my first attempt at posting from the Blogger iPhone app. I'm finding the very basic functionality quite limiting. Oh well, perhaps I'll replace those asterisk's with real bullet points later, and I have literally no idea where that picture will display.

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.

Monday, 15 April 2013

Pure Data and BACON (Part 1)

Well, it's been a busy weekend, week, month and year. I've not been able to blog in ages. I've been rushed off my feet for the last couple of months with work and preparing to speak at the most awesome conference (per head of population) in the world, BACON. It happened, I survived two early (and I mean EARLY) starts and late nights and, heck, my talk went down incredibly well. Text to music is going to get a burst of activity, when I get a moment to myself to sort through the pull requests. What's more, a handful of people actually told me I had their favourite talk! Overall not a bad first experience of public speaking. Even got a mention in this Tech City review of the event.

But enough about me, down to some actual content. The inimitable @jongold asked for a demo of my generative patch from the talk, but I flatly refuse to hand code out to a potential Pd coder, so here's something a tiny bit better, how to make it yourself. (Also, a it leads on quite well from my last post, back in the day).

Step 1: Install Pure Data, yadda, yadda, yadda

Step 2: Open Pd, Start a patch, Place some objects, join them up. Click the button:


Create objects with Put > Object or ctrl + 1 then just type in their name. Success results in a solid border.

That button is a 'bang' which sends an activation message, create it with Put > Bang or ctrl + shift + b

You may have to switch in and out of edit mode with a ctrl + e

The result is a little clicking sound. Not very impressive, I know, your mouse probably clicks whenever you click your mouse, but it's an important staging post on the way to step 3.

If you're not hearing it, make sure your volume is up, and the DSP checkbox is checked on your Pd terminal, you may also have to select your sound card under the Media menu (Audio Options on Windows or Mac, on Linux select an audio driver and experiment until that button makes a click)

Step 3: Assume you want to hear that click, save your fingers and automate it.


That empty box is a toggle, if you click it when you're not in edit mode it sends an 'on' message, or, of course, an 'off' message.

metro outputs a bang (the same message as the button) at a given interval, once you've turned it on. In this case it's 1000 milliseconds. So, if you click the toggle, you guessed it, you'll hear a click every second. It's like a clock.

But that's a bit dull...

Step 4: Take control of your metronome.


Okay, nothing much has changed here. I've added a number box and connected it to the right-hand inlet of the metro. This will override the number in the metro and change it's speed dynamically. Drag your number up and down (when you're not in edit mode, so it changes the number) and you'll hear the regularity of your metronome change.

Psycho-acoustics alert! The human ear can only interpret around 30 separate sounds a second. So if we divide 1000 by 30 we'll get a figure at which we'll stop hearing separate clicks, and start to hear something continuous. As a rule of thumb, I just use 30 milliseconds or lower. Feel free to demonstrate this to yourself.

Well, there's some interesting sounds, when that click gets really fast, so let's save ourselves the effort of  choosing:

Step 5: Randomize AND automate it


So, random accepts our old friend, the bang, and outputs, you guessed it, a random number. This one will be somewhere between 0 and 99 (don't be confused, this is 100 options, just that none of them are the number 100).

You'll also notice that I've skipped a step, and added a metro above the random, so once both toggles are checked, you'll see the number change every quarter of a second, and hear a difference in the sound, too. So this is a flat rhythm of changes to a harsh, imprecise kind of sound. Let's refine it...

Step 6:  Not just a beat, rhythm...


Okay, it may be time to take a step back. That box labelled 'length' is an array. If you're not much of a techy, you'll need to know that it's basically a list of numbers, and this one has 4 numbers in it. Creating it is a tiny bit less simple, so here's a step-by-step:


  1. Put > Array
  2. Name it length, and set it's size to 4
  3. Right click, choose properties and set it's Y range from 1000 to -1 
  4. Put > Message
  5. fill the message in as you see above ("; length 0 1000 500 250 125"), that semicolon sends it to an object by name, so you don't need to connect it to anything.
  6. Read it with tabread (which reads a table, predictably enough) 
  7. hook up random and metro above it, which you're already familiar with.
Now, when all the metronomes are started, it'll decide every second whether to change the regularity of the sound once, twice, four or eight times every second. This is actually a fairly common way of dividing time in music, we've taken a common beat (1 second) and divided it by 2 repeatedly. 

I realise that the rhythm here is a bit obscured by the nasty click-tones, so lets simplify...

Step 7: Delete most of it, listen to the result


All we've done here is cut out the middle bit of the chain and driven a click directly from that second metronome. Instead of deciding each second how many times to change the regularity of the click, it's just going to decide how many times to click. The result is something like a rhythm exercise, which most musicians will have done some form of, in which you change between note lengths whilst keeping a steady rhythm. 


Phew! That's it! 

This has become something of a whopper of a blog post, I guess it's time for a cliff-hanger. As ever, if you have some difficulty, feel free to bother me about it by contacting @MarmiteJunction 

In the next exciting episode! We'll start to move from clicks to tones, some recognisable pitches, and a generative duet.