Thursday, 9 May 2013

Algorithms of the Street - Part 1: Frequency Modulation

So far in my blog, it's all been sine waves. Mostly because this is easy to put down in a hurry, useful for live coding and allows me to focus on a different aspect of the patch I'm creating. All my tutorials so far have been recipes, and like all recipes you have to understand how it works before you start to change things, add extra ingredients, cook it at a different temperature, use a blender instead of a vegetable knife, and so on. (It seems I've started enacting this principal in by blog by adding bad cooking metaphors).

Anyway, I think it's high-time I started talking a little about how to make slightly more complex tones than just sine waves, so here's the first in a series of synthesis algorithms you can use in place of osc~ which will work in the same way, but sound different.

For the slightly less maths orientated amongst us (like me), an algorithm is a repeatable method of reaching a solution, or at least producing the same pattern. It's a mathematical method which will respond to different input and produce results accordingly. So in PureData terms, it's useful to learn some algorithms which you can then use time and again to produce similar, but different, results.

Step 1: The problem...



So this is a random melody patch, similar to one we made in an earlier post. It chooses random numbers between 60 and 84, converts those to frequencies then makes the osc~ produce that frequency and sends it out to the sound card (I suggest you build this patch before proceeding).

The problem is that a sine wave is a very simple waveform, with only one frequency, and it can become grating pretty quickly, especially if you're not hiding this effect by changing the pitch regularly. A patch like this:


will drive you nuts. You're in luck, however, because an oscillator isn't only controllable with numbers. You can also control it directly with anther audio signal.

Step 2: Modulate the frequency



So what's going on here? Well, the osc~ at the top of the chain is at a very low frequency, half of one herz, meaning it takes 2 seconds to complete it's cycle. Incidentally if you ever see a piece of technology with the term LFO written on it, this is all it is, a Low Frequency Oscillator.

Now, as I have explained before, an osc~ creates it's tone from an arbitrary 1 to -1 range. Meaning if we multiply it (with *~) by 20, this will move up to 20 and down to -20. If we then add (+~) 440, the result will range from 420 to 460. The result is that you can hear the slow waveform of the first oscillator changing the pitch of the second.

We call this higher oscillator the Modulator or Modulating Oscillator, because it is modulating (which simply means 'changing') the Frequency of the tone. Meaning we have achieved Frequency Modulation, or FM.

The practical result of this oscillator working at low speed and changing a very audible aspect of the sound is that we can actually hear the shape of it's wave or it's waveform, which looks like this.


So you'll hear that wavy, siren sound in the frequency of the resulting sound. And this is the exact same sound which, sped up, is the tone you're hearing it produced with. Just like the clicks in Pure Data and BACON (Part 1) the wave has a different effect when it happens less than 30 times a second, allowing you to hear individual events. 

It follows that by speeding up the modulator, we can hear a very different flavour of sound.

Step 3: Modulating at speed




All we've done here is change some numbers, but the modulator is running at a speed which you would be able to hear as a tone, which is also half of the pitch of the second oscillator. I've used half simply because it's quite a nice ratio to use. I've also changed the amount by which it's changing the frequency of the second oscillator, what we call the Modulation Depth. Because it changes how deeply the modulator modulates the tone. You're probably starting to get that these terms do actually say what they mean, once you understand the words being used.

The result is something like a kind of bell sound. It's still more or less at 440 herz, because it's revolving around that frequency, but it's more of an interesting sound. You can't hear the individual waves of your modulator any more, but they're changing or 'colouring' the sound of that first oscillator, which is still just producing a sine wave, it's just changing it's frequency at high speed.

So if that plus is the figure we're revolving around, does this mean we can feed that melody generator into the plus and hear a melody in our new bell tone?

Step 4: Bell + Melody = ?


If you had to predict what the above patch does, you'd probably assume that the melody generator feeding the centre frequency of your frequency modulation would play that melody with our FM tone.

But your ears won't be telling you quite that story, the effect  we're actually achieving is that each note sounds like it's being made with a different tone and they sound more or less out of tune. This is because the 'flavour' of an FM tone comes from the proportional relationship between the modulator frequency and the carrier (the carrier being the oscillator which isn't the modulator).

The reason the sound we're producing sounds so 'interesting' (which can be thoroughly euphemistic when talking about music), is because the modulation frequency isn't changing, but the carrier frequency is. So 220 hertz is a different proportion of 480 than it is of 440, or any other number. Each pitch has a different 'flavour' of FM, meaning your ear can't settle to hearing the centre frequency as the pitch, it keeps moving back towards the modulator frequency.

This isn't a problem when we're using it as an LFO, but when the modulation frequency would be audible as it's own tone it can really throw you off hearing this as music.

To combat this problem, we simply have to write our patch so that the modulation frequency is a set proportion of the carrier frequency...

Step 5: Proportional tones


Now, this patch hasn't changed very much, but the effect of it has. We've just added an extra multiply and a link to that from the same source as the carrier frequency. Meaning the modulation frequency is always half that of the carrier. The effect you get from your ears now is that each note produces the same kind of tone, it's like a kind of organ tone.

A little experimentation will show that if this multiply is set to any figure between 0 and 1 you get some interesting sounds (0.2 is a favourite proportion for me, it produces a nice thick tone). Armed with this information, you can start to take some more direct control. Time for some GUI elements

Step 6: Take control of your modulation


You'll notice something new here, a slider (Put > Vslider or Ctrl + Shift + V). It's easy enough to understand, it outputs a number based on you sliding the line through it up or down. To get a decent effect you'll have to change it's range by right clicking on it and selecting Properties.


This opens up a dialogue with properties of the slider. You just need to set it's output range from 0 to 1, like so:


Now when you move your slider up and down (when you're out of edit mode), it will change the proportion of your modulation frequency and give you a different kind of tone as you do. The melody is out of your control, but the tone is right within it.

There's only one other part of the algorithm you may want to change, the depth of the modulation. So, just for the fun of it, we're going to use a new GUI (Graphic User Interface) object called a grid...

Step 7: Two dimensional control with a grid



You may notice one big change on this patch, namely a great big blue grid to the right hand side of it. Create this grid by creating an object as ususal and typing the word 'grid', when you click off this object it will initialise to a grid just like the one above. Just like the slider you can right click it and select Properties, then I recommend setting the properties of your grid like this:


So in short, width and height decide how much space the grid will take on screen, the X min, X max and X step change the numbers that it will output when you move left to right on your grid and Y min, Y max and Y step do the same for movements up and down. The two outlets from the grid then output this information which we're sending (via named sends) to the two multiplies in our FM algorithm.

The result is that when you leave edit mode and click on your grid, you can drag the pointer around and change both the frequency proportion and the depth of your fm synthesizer. Towards the top you will hear deeper or more severe modulation, and lower down the change will be more subtle.

Towards the left you'll hear thicker sounds, and right at the left hand side you'll hear some slow wobbles, until the modulation reaches 0 and you hear no sound at all. As you move right on your grid, you'll hear richer, bell-like tones.

Now you've made this synth, explore the sounds you can make, you may as well. See if you can produce a tone resembling a synth you've heard on a record, or in some classic sci-fi, the tones are there if you look hard enough.


The fact that this is an algorithm means it's re-usable in other contexts, anywhere you use an oscillator to produce a frequency, you could use two oscillators coupled to produce frequency modulation.

The inquisitive mind will now be thinking, "I'm seeing two oscillators here... does that mean I could replace them with FM within my FM?". The answer is yes! You can make multi-layered frequency modulation, use another oscillator as an LFO to change the frequency proportion or depth, the possibilities are literally endless. You now know the recipe, so why not play around with it?

I'd be interested to see what creative variations on FM you can come up with, and help you through any problems you run into in the process. As ever you can get hold of me any time on @MarmiteJunction.

Until next time, with more synthesis algorithms, good bye, and God bless.


Appendix: Doing it right

Reading this back two days after it's publication, I've realised that I made two mistakes in the final version of the patch, namely:


  • Crossing wires in Pure Data is bad form, while it's usually possible to tell where they're going because they're all straight lines, it gets very messy very quickly and that's where untidy, indecipherable Pd patches come from.
  • The modulation depth was a fixed figure, where we can make the sound more consistent again by making the depth a proportion of the pitch as well...
So, with both axes of the grid set to range from 0 to 1 (with small increments) an extra send for the pitch and a multiply for the pitch by the depth added, here's my improved version...



2 comments:

  1. Very clear, concise and useful tutorial.

    ReplyDelete
    Replies
    1. Thanks, it can be a tricky balance between talking too much and not saying enough.

      Delete