A Guide To Creating Oscillators Using Recursive Functions

Preparation

Within the moments that follow this one, you will get to know about the basic principles of building sine and saw wave oscillators.

You can use the code component for various tasks and calculations. One application of its use is causing oscillation as a consequence of the execution of a recursive function.

First, you may want to layout the inputs and outputs of the module (in this case: an oscillator). So, we begin with that:

(Put into the blue code component.) Code Module

streamin frequency;
streamin phase;
streamin volume;
streamout out;

Header

The above statement is the header, the first part of our new module. You may want to do a different design, or assign different names to the parameters you are assigning, so regard this and the following code as a suggestion only ^_^.

The next part is to declare variables (in here floats, that means unrounded number values e.g. 1.25 , 2.35 , 3.74 , 4.12 , 5.89 , 6.44 , etc. - in contrast to integers which are values somehow, often down, rounded-to-the-next-whole-number number values e.g. 1 , 2 , 3 , 4 , 5 , 6 , etc.) that you are going to need for the application of your ideas:

float a,b;

You may have realized that at each end-of-line, there is a semi-colon. That is done to tell “Synthmaker” that you have finished that line. If you leave it out, all code inside your module will be ignored. In your module, you will see a kind of syntax highlighting. If you make a mistake in writing (a typing error), the code down from the mistake remains black, that is an aid to finding typing errors (but not thinking errors!).

So, now we may go into the actual coding procedure!

First Example

a = a + frequency;
a = a - (a > 1) & 2;
b = sin1(a + phase);
out = volume*b;

Now, you may ask: “How does it work?" Well, at first, you might think that a fixed equation could never produce sound (like a = 1 would stay the same). But, as I said before, you would be going into recursive functions, now you are! Look at that a = a + 1 : At first sight, you don’t know about a, but you know that, basically, a is by 1 bigger than itself. That is, in itself, a paradoxon. (Like 1 ≠ 2.) But don’t think that a computer can be logical (in basic operations)! Imagine what a computer does (!?): Calculating, the whole day long! So it does with a = a + 1. At first, the computer thinks: “I don’t know a, so I assume it to be zero (0).". Then, in the next step (calculating cycle = times having looked at an “equation”), it looks like this: a = (0) + 1 (and now you might begin to comprehend the meaning of “recursive function” - it is a mathematical function that is being looked at, and calculated with several times in a sequence, replaceing the recursive variable by the result of it after the previous calculating cycle!). So it would go on: a = (1) + 1 , a = (2) + 1 , etc. , if you don’t tell the computer to change its behaviour!

Now you have (hopefully) understood the way in which a recursive function works! Your computer natively calculates the code you’ve written into the code module at samplingrate!

Now let’s look at the code again! Here we have the code put together we have written so far:

streamin frequency;
streamin phase;
streamin volume;
streamout out;
float a,b;
a = a + frequency;
a = a - (a > 1) & 2;
b = sin1(a + phase);
out = volume*b;

Sine

A frequency input value is being added in value to itself inside a recursive function (a + etc.). Then, it is being brought down by the value of 2 each time it reaches a value higher than 1 (as the recursive function used in here is natively a continuously rising function), so that a (theoretically) infinite loop (cycle) is being achieved. This, in itself would then be a saw (more exactly: an “upward sawtooth”) wave function.

Simple Saw OSC

So, you could rewrite the code as simple as this in case you only need a common upsaw wave:

streamin frequency;
streamin volume;
streamout out;
float a;
a = a + frequency;
a = a - (a > 1) & 2;
out = volume*a;

Saw

Here, we have the saw wave fed directly into the output (out), with a volume modification applied by the external volume source to the output signal.

Sine OSC

If we want to create a sine wave, then we need the first complete code example:

Sine

streamin frequency;
streamin phase;
streamin volume;
streamout out;
float a,b;
a = a + frequency;
a = a - (a > 1) & 2;
b = sin1(a + phase);
out = volume*b;

This function looks very similar to the example above it, but here, the sawtooth is being fed through a sine function which has a value added to it before conversion into a sine (this alters the phase of the output signal, so the sine wave does not begin at zero (0) like it would normally). Then, again, there is a volume multiplication (loudness scaling) performed before letting the signal exit the code module.

Congratulations! You have created your first Oscillator in SynthMaker!

tutorials/oscillators/simple_sine_and_saw_wave_oscillators.txt · Last modified: 2008/05/20 23:11 (external edit)