Simple sound waves in Supercollider

By Martin McBride, 2017-07-08
Tags: supercollider sound synthesis
Categories: supercollider sound synthesis

In computer music, we often generate sound waves using mathematical functions. The simplest sound functions generate pure tones - a single, unvarying musical note. Here we will look at some of the functions used, and how to create them in SuperCollider.

Sine waves

A sine wave is the purest musical sound - it contains a single frequency with no extra harmonics. Here is the graph of a sine wave:

sine wave

It is called a sine wave because it is based on the mathematical sine function (the same function used to calculate the ratio of sides in a triangle).

Experiment - playing a sine wave

Start up SuperCollider as described earlier.

Don't forget to start the server - the Language|Boot server menu option, or use Ctrl-B if you prefer. Wait a few seconds until the messages appear to show that the server is running.

Take care if you are using headphones, the sound from SuperCoillider can sometimes be louder than expected. Keep the volume low at first.

Type the following into the code window:

{ SinOsc.ar() }.play;

To make the sound play, position the cursor anywhere on the line, and press Ctrl-Enter (hold down the Control key and press the Enter key). You should hear a sound.

What does this do? SinOsc is a type of object that creates a sine wave. ar is a function that creates an audio signal (as opposed to a control signal which we will cover later). play tells SuperCollider to play the sound.

Whenever you want to stop the sound, press Ctrl-Period (the Control key plus the period, or full stop, key .). All sounds will cease.

Next we will look at the three main characteristics of a simple wave - the amplitude, the frequency (or pitch), and the wave shape.

Amplitude

The signal in the graph above has a maximum value of 1, and a minimum value of -1, so we say it has an amplitude of 1 (the furthest is swings above or below zero is one unit either way).

The signal below has an amplitude of 0.5:

sine wave

The amplitude affects the volume of the signal (the is, how loud it sounds). However, the scale is relative, because the actual volume of the sound depends on what type of speakers or headphones are connected, and the volume setting on your computer. We normally take +/- 1 as the maximum amplitude the sound hardware can create, and so this will be the loudest sound you can get from a particular set of speakers on a particular volume setting.

When the amplitude of the signal is set to 0.5, you might expect the sound to be half the volume. This is more or less correct, but in fact you ears don't work in an exactly linear way. And if you think about it, if you listen to two different sounds, would you even be able to tell that one was half as loud as the other?

Experiment - changing the amplitude

In SuperCollider, type the following:

{ SinOsc.ar( mul: 1 ) }.play;

This code is almost the same as before, but we have added a parameter called mul which multiplies the amplitude of the signal by the value 1. As you might expect, multiplying the amplitude by 1 doesn't change anything - it should sound exactly the same as before.

But now try this:

{ SinOsc.ar( mul: 0.5 ) }.play;

This time we are multiplying the amplitude by 0.5, so the sound will have half the amplitude of before. Try it, it will sound quieter.

HINT - rather than typing the line in again, you can just edit the previous line, and use Ctrl-Enter to re-run it. Don't forget to use Ctrl-Period to turn the old sound off before starting the new sound.

Frequency

If you look at the sine wave, you will see that the shape repeats over and over:

sine wave

Each repeat is called a cycle. The pitch, or frequency, of a tone is measured in cycles per second. The more scientific term for this is Hertz (or Hz for short). These terms are often used interchangeably.

The scale of x axis in the graph is measured in milliseconds (ms). One millisecond is a thousandth of a second. The length of one cycle in the graph above is 2 ms, or 0.002 seconds. The frequency is given by

frequency = 1 / cycle_time

Since the cycle time is 0.002 seconds, the frequency is 500 Hz.

If we decrease the cycle time, we get more cycles per second, in other words the frequency is higher. This gives a higher pitched tone. In this graph, the cycle time is 1ms, half what it was before. You can see that the graph has twice as many peaks as before (and the scale of the time axis is the same as before), so the frequency has doubled to 1000 Hz.

sine wave

In a similar way, f we increase the cycle time, we get fewer cycles per second, in other words the frequency is lower. This gives a lower pitched tone. In this graph, the cycle time is 4ms, twice what it was in the original case. You can see that the graph has half as many peaks,so the frequency has halved to 250 Hz.

sine wave

Experiment - changing the frequency

In SuperCollider, type the following:

{ SinOsc.ar( freq: 500, mul: 1 ) }.play;

Here we have added an extra parameter, freq, that controls the frequency. It is set to 500Hz.

NOTE the parameters are separated by a comma.

This sound should be fairly similar to the previous sound. That is because, if you don't supply a value for freq, the SinOsc function will use a default value of 440Hz. The value we set, 500Hz, is a slightly higher pitch.

Now try increasing the frequency to 1000Hz:

{ SinOsc.ar( freq: 1000, mul: 1 ) }.play;

This should sound quite a lot higher in pitch. Even if you don't have a particularly musical ear, you should notice the difference.

Try changing the frequency to 250Hz - you should know how to do this by now. It will sound much lower in pitch.

The audible range

A human with good hearing can hear sound frequencies between about 20Hz and 20,000Hz.

1 kilohertz (KHz) is equal to 1000Hz, so 20,000Hz would normally be written 20KHz.

The ear is most sensitive to sounds between about 1KHz and 4KHz. Above 4KHz, as the frequency increases the sound becomes more and more difficult to hear, until it becomes completely inaudible at around 20KHz. In fact, as you get older, your ability to hear high pitched sounds tends to decrease, so the sound might disappear before it gets to 20KHz.

Sounds below 1KHz become less audible as the frequency decreases, down to about 20Hz.

Many animals can hear higher frequencies than humans. You may have heard of a dog whistle, a whistle which is too high pitched to be heard by humans, but can still be heard by dogs. Dogs can hear sounds up to about 40KHz, but some types of dolphin can hear sound of up to 200KHz!

The wave shape

So far we have looked at sine waves, but there are many different ave shapes. For example, this is a triangle wave:

triangle wave

It looks a little like a sine wave. It has the same amplitude as our original sine wave. It has the same frqeuecny - 500Hz. But instead of being smooth, the wave form has points.

So what difference does this make to the sound? Try it with this code:

{ LFTri.ar( freq: 500, mul: 1 ) }.play;

The sound clearly has the same frequency and similar volume, but is sounds different - it has a different timbre, it is like listening to the same note played on a different instrument. Some people might say it sound more brassy.

Changing the waveform is one of the basic tools we have when we want to create different sounds, whether we are trying to emulate existing instruments, or creating brand new ones.

See also

Sign up to the Creative Coding Newletter

Join my newsletter to receive occasional emails when new content is added, using the form below:

Popular tags

555 timer abstract data type abstraction addition algorithm and gate array ascii ascii85 base32 base64 battery binary binary encoding binary search bit block cipher block padding byte canvas colour coming soon computer music condition cryptographic attacks cryptography decomposition decryption deduplication dictionary attack encryption file server flash memory hard drive hashing hexadecimal hmac html image insertion sort ip address key derivation lamp linear search list mac mac address mesh network message authentication code music nand gate network storage none nor gate not gate op-amp or gate pixel private key python quantisation queue raid ram relational operator resources rgb rom search sort sound synthesis ssd star network supercollider svg switch symmetric encryption truth table turtle graphics yenc