# Introduction to Sound Waves

Atrevida Game Programming Tutorial #21
Copyright 1998, Kevin Matz, All Rights Reserved.

Prerequisites:

• None!

Before we begin sound card programming, it will be helpful to learn a little bit about sound and its properties. (Fortunately, this will be a short article.)

### Basic Sound Theory

Sound is the movement of waves of energy through air (or another medium, such as water). Any source of sound, whether a musical instrument, a human's vocal chords, a pair of speakers, or even something like a hammer hitting a nail, all cause vibrations that cause sound waves to travel through the air. When these sound waves strike our eardrums, causing our eardrums to vibrate, we perceive sound.

To learn more about sound waves, let's examine how a speaker works. A speaker contains a "speaker cone", which can move forward and back. The movement of the speaker cone is controlled by electrical signals. The cone contains a magnet, and the wire carrying the electricity is wrapped into a coil; this way, electromagnetic properties can be used to move the speaker cone: by increasing and decreasing the amount of electrical current, the coil of wire produces a magnetic field of varying strength, and this moves the magnet and the speaker cone forward and backward.

When the speaker cone moves forward, it hits the particles of air that are in front of it. The air particles get squished together.

When the speaker cone moves back, it briefly creates a small space in front of it when there are few air particles.

The speaker cone will move in and out quite rapidly -- often several thousand times a second or more. So the cone will produce many of these regions of "high density" of air, or high pressure (when the air particles are squished together), and regions of "low density" of air, or low pressure (when the number of air particles in the region is lower than average).

These regions will travel away from the speaker, basically in all directions. These regions moving in sequence make up a "sound wave":

```         COMPRESSION                   COMPRESSION              COMPRESSION
o     o   o o  o     o           o   o  o  o oo    o      o     o o  o  o
o  o oo   o         o       o  o oo o  o     o              o oo
o    o  o oo  o      o        o     o   o oo   o        o    o  o  o oo
o oo o o    o                o   o o oo     o               oooo
o      o   o o o  o           o     o   o  o oo   o           o     o o oo
o    o  o oo o      o        o    o   oo o  o      o           o  oo o
o      o  o oo o  o  o     o       o    o  o o   o       o      o   o  o
EXPANSION                 EXPANSION                    EXPANSION

(An "o" represents an air particle.)
```

The important thing to note is this: the air particles don't move away from the speaker along with the sound wave. It's the regions of high pressure and low pressure that travel. The air particles bump into each other and transmit the "energy" of the sound wave.

Sound waves are rather similar to waves in the ocean: the water molecules don't move along with the wave. If you put an object (that floats) in the water, perhaps a small boat, or a duck, the object will bob up and down as waves pass, but it generally won't be pushed to shore, unless there is some current (movement of the water).

Sound waves also tend to lose energy as they travel farther and farther away from the source. That's why music sounds quieter and quieter as you walk further and further away from a stereo. The regions of high and low pressure are still traveling along, but the high-pressure regions gradually contain less and less pressure, and the low-pressure regions gradually gain more and more pressure. (When the high- and low-pressure regions both equal the average pressure in the air, then the sound wave has completely lost all of its energy, so it has stopped.)

Sound waves in the air travel at "the speed of sound", right? That's true, but the speed of sound varies according to the density of the medium. So, at sea level, where the air is the most dense (i.e. the highest pressure), sound waves travel faster than at, say, one kilometer above sea level, where the air pressure is lower. Water is much denser than air, so sound waves travel faster in water.

### Graphical representation of sound waves

You have probably seen graphic representations of sound waves before. They are generally drawn as graphs with "time" on the x (horizontal) axis, and "amplitude" on the y (vertical) axis, like this:

```    /|\    |   **            **            **            **
|     |  *  *          *  *          *  *          *  *
|     | *    *        *    *        *    *        *    *
| *    *        *    *        *    *        *    *
Amplitude |*      *      *      *      *      *      *      *
(y)    |*------*------*------*------*------*------*------*--------------
|       *      *      *      *      *      *      *    Time (x)
|     |        *    *        *    *        *    *        *   -------->
|     |        *    *        *    *        *    *        *
\|/    |         *  *          *  *          *  *          *  *
|          **            **            **            **
```

The above illustration is meant to represent a sine wave, which would be produced if the speaker cone went backwards and forwards in a sine-wave pattern. The easiest type of sound wave for a computer to produce is called a square wave:

```    /|\    |
|     |******     *******     *******     *******
|     |     *     *     *     *     *     *     *
|     *     *     *     *     *     *     *
Amplitude |     *     *     *     *     *     *     *
(y)    |-----*-----*-----*-----*-----*-----*-----*----------------------
|     *     *     *     *     *     *     *            Time (x)
|     |     *     *     *     *     *     *     *            -------->
|     |     *     *     *     *     *     *     *
\|/    |     *******     *******     *******     ******
|
```

Square waves are easy for the computer to generate -- it just has to push the speaker cone out, wait for a while (a tiny fraction of a section, though), pull the speaker cone in, wait for the same duration of time, then push the speaker cone out, and so on.

Of course, sound waves in the real world aren't nearly as regular as sine waves and square waves. The sound waves produced by musical instruments, or the human voice, are much more complex, and often have wildly varying amplitudes and frequencies.

### Amplitude

What does "amplitude" mean? The amplitude of a sound wave indicates how high and low the air pressure is in the high-pressure and low-pressure regions. The following diagram may help explain this better:

```              Highest Amplitude        Lowest         Highest Amplitude
Amplitude
/|\    |        *****                                   *****
|     |     ***  .  ***                             ***  .  ***
|     |   **     .     **                         **     .     **
|  *       .       *                       *       .       *
Amplitude | *        .        *                     *        .        *
(y)    |*-------------------*-------------------*-------------------*-
|          .          *                 *          .      Time
|     |          .           *               *           .     ----->
|     |          .            **           **            .       (x)
\|/    |          .              ***     ***              .
|          .                 *****                 .
.                   .                   .
.                   .                   .
|  o   o  oo o  o o    o     o      o      o  o  o ooo o  o   o
|o   o   oooo  o  o  o                 o     o oo ooo o o   o
|    o o  oooo o   o      o               o o o  ooooo o  o
|o    o  o oo o  o   o          o        o  o   o ooo oo o  o
|  o   oo oooo o  o     o           o      o  o oooo o o oo   o

Highest              Lowest             Highest
Density             Density             Density
(Pressure)          (Pressure)          (Pressure)
```

When someone refers to the "amplitude of a wave", they normally mean the difference between the highest amplitudes (the peaks) on the graph and the lowest amplitudes (the valleys) on the graph.

For example, in the diagrams below, wave "A" has a higher amplitude than wave "B":

```WAVE "A":
/|\    |        *****----------                         *****
|     |     ***     ***    /|\ Amplitude            ***     ***
|     |   **           **   |                     **           **
|  *               *  |                    *               *
Amplitude | *                 * |                   *                 *
(y)    |*-------------------*|------------------*-------------------*-
|                     |                 *                 Time
|     |                     |*               *                 ----->
|     |                     | **           **                    (x)
\|/    |                    \|/  ***     ***
|                    --------*****
```

```WAVE "B":
/|\    |
|     |
|     |      *********--------                       *********
|   ***         ***  /|\ Amplitude          ***         ***
Amplitude | **               ** |                   **               **
(y)    |*-------------------*|------------------*---------------------
|                     |*               **                 Time
|     |                    \|/***         ***                  ----->
|     |                    ------*********                       (x)
\|/    |
|
```

Volume (loudness) is directly linked to amplitude. A high-amplitude wave is louder than a wave with a lower amplitude. That's because the high-amplitude wave has denser high-pressure regions and sparser low-pressure regions, and the difference between the high and low pressures makes your eardrum vibrate more vigorously (and the brain interprets this as higher volume).

### Frequency

What does "frequency" mean? Frequency refers to how often (or "how frequently") something repetitive occurs. More specifically, "frequency" refers to the number of times something happens in one second. With waves (at least "regular" waves like sine waves and square waves), the frequency is the number of cycles that occur within one second.

A cycle is hard to describe in words, so I'll show diagrams to illustrate the concept. Here is one cycle of a sine wave:

```    /|\    |        *****
|     |     ***     ***
|     |   **           **
|  *               *
Amplitude | *                 *
(y)    |*-------------------*-------------------*----------------------
|                     *                 *              Time (x)
|     |                      *               *               -------->
|     |                       **           **
\|/    |                         ***     ***
|                            *****
```

And here is one cycle of a square wave:

```    /|\    |
|     |***********
|     |*         *
|*         *
Amplitude |*         *
(y)    |*---------*----------------------------------------------------
|          *         *                                 Time (x)
|     |          *         *                                 -------->
|     |          *         *
\|/    |          ***********
|
```

A longer wave is composed of many cycles, one after the other. So to find the frequency of a wave, you count the number of cycles that occur during one second. For example:

```    /|\    |
|     |
|     |
|   **            **            **            **
Amplitude | *    *        *    *        *    *        *    *
(y)    |*------*------*------*------*------*------*------*-------------
|        *    *        *    *        *    *|           Time (x)
|     |          **            **            **  |           -------->
|     |                                          |
\|/    |                                       1 second
|
```

In the above diagram, a scale has been placed on the x axis. Between the origin of the graph and the 1 second mark, there are three cycles. This means that the above wave has a frequency of 3 cycles per second.

Frequencies can be fractional, such as in the following case:

```    /|\    |
|     |
|     |
|   **            **            **            **
Amplitude | *    *        *    *        *    *        *    *
(y)    |*------*------*------*------*------*------*------*-------------
|        *    *       |*    *        *    *|           Time (x)
|     |          **         |  **            **  |           -------->
|     |                     |                    |
\|/    |                  1 second            2 seconds
|
```

In this case, only one and one-half cycles occur within one second, so the frequency is 1.5 cycles per second.

There is a metric unit for frequency: Hz (hertz). 1 Hz (one hertz) is exactly the same as one cycle per second. (Then you can use the standard metric prefixes with the Hz unit, so 22 kHz means 22 kilohertz, or 22,000 Hz, and 33 MHz means 33 megahertz, or 33,000,000 Hz.)

What does frequency mean to us? Well, if we hear a low-frequency tone, and then we hear a high-frequency tone, we perceive the high-frequency tone to be higher in pitch. (What is pitch? Here's one way of putting it: the sound produced by each key on the piano keyboard has a higher pitch than the sound produced by the key to the left of it. Or, a "soprano" has a higher pitch than a "tenor".)

The higher the frequency, the higher the pitch. Is the relationship between frequency and pitch a linear relationship? Actually, no. The following chart shows the frequencies of several octaves of musical notes.

```    Musical Note Frequencies
------------------------------------------------------------

Note    Frequency (Hz)                Note    Frequency (Hz)
----    --------------                ----    --------------
A       110                           A       440
A#      117                           A#      466
B       123                           B       494
C       131                           C       523 (Middle C)
C#      139                           C#      554
D       147                           D       587
D#      156                           D#      622
E       165                           E       659
F       175                           F       698
F#      185                           F#      740
G       196                           G       784
G#      208                           G#      831
A       220                           A       880

A       220                           A       880
A#      233                           A#      932
B       247                           B       988
C       262                           C      1047
C#      277                           C#     1109
D       294                           D      1175
D#      311                           D#     1245
E       330                           E      1319
F       349                           F      1397
F#      370                           F#     1480
G       392                           G      1568
G#      415                           G#     1661
A       440                           A      1760

NOTE: Frequencies are rounded off to the nearest decimal.

Source: TI-99/4A User's Reference Guide.
------------------------------------------------------------
```

(This means that, for example, the middle "C" note on the piano keyboard produces a sound that has a wave that "goes up and down" 523 times per second!)

Musicians tell us that there is a constant difference in pitch between two adjacent semitones (either black or white keys on the piano keyboard). In other words, the "difference in pitch" that exists between the two notes C and C# is exactly the same as the difference of pitch that exists between the notes C# and D.

If there was a linear relationship between pitch and frequency, then the difference between the frequencies between any two adjecent notes should be the same. But these differences vary! For example, the difference between the first C note on the table and the C# note immediately following it is 139 Hz - 131 Hz = 8 Hz. The difference between middle C and the C# note immediately following it is 554 Hz - 523 Hz = 31 Hz. So the relationship between pitch and frequency is not linear.

However, if you look closely at the table, you may notice this convenient relationship: the frequencies for two notes that are separated by exactly one octave differ by a factor of 2. The frequency for "A below middle C" is 440 Hz, and the frequency for the note one octave higher, "A above middle C", is 880 Hz -- double the first frequency!

So the relationship is actually exponential. You can verify this by checking that there is a multiplicative factor between two adjacent notes: it's approximately 1.059. Take one frequency in the table, such as 698 Hz (for F above middle C), and divide it by the frequency just before it -- in this case, 659 Hz (for E above middle C). The result is roughly 1.059.

This is handy to know in case you ever leave your trusty frequency table at home and you suddenly need to determine the frequency for some particular musical note. Take one of the easy-to-memorize frequencies, like 440 Hz (for A below middle C), and multiply it by the factor 1.059. The result is 466 Hz, and yes, that's the frequency for (A# below middle C). Then multiply 466 Hz by 1.059 to get 493 Hz -- okay, this is a little bit off, but these frequencies are all rounded off and 1.059 is an approximation (and most people probably won't detect a 1 Hz difference anyway!). In fact, if the error starts getting bad, you can remember that the frequency for your first note (in this case, 440 Hz, for A below middle C), can be doubled to get the frequency for the note one octave higher (which would be 880 Hz, for A above middle C).

1.059 is more accurately represented as the "twelfth root of two", because there are twelve divisions in an octave (C, C#, D, D#, E, F, F#, G, G#, A, A#, and B), and the multiplicative factor between two notes one octave apart is 2:

```          __________
12     /
----\  /     2           =  1.05946309436...
\/
```

Semi-interesting fact: The average human can hear sounds that are within the range of about 20 Hz to about 15,000 Hz (although many people can hear much higher than that).

### Quiz

Okay, want to test your knowledge? Try this very short quiz!

1. Which of the two waves shown below would sound louder? (Assume the amplitude scales are equal for the two graphs.)

```WAVE "A":
/|\    |   **            **            **            **
|     |  *  *          *  *          *  *          *  *
|     | *    *        *    *        *    *        *    *
| *    *        *    *        *    *        *    *
Amplitude |*      *      *      *      *      *      *      *
(y)    |*------*------*------*------*------*------*------*----------
|       *      *      *      *      *      *      *  Time (x)
|     |        *    *        *    *        *    *        * ------->
|     |        *    *        *    *        *    *        *
\|/    |         *  *          *  *          *  *          *  *
|          **            **            **            **
```

- or -

```WAVE "B":
/|\    |
|     |
|     |
|   **            **            **            **
Amplitude | *    *        *    *        *    *        *    *
(y)    |*------*------*------*------*------*------*------*----------
|        *    *        *    *        *    *         Time (x)
|     |          **            **            **           -------->
|     |
\|/    |
|
```

If you picked the first wave, wave "A", as being louder, then you're correct! It has a higher amplitude.

2. Which of the two waves shown below would have the higher pitch?

```WAVE "A":
/|\    |   **            **            **            **
|     |  *  *          *  *          *  *          *  *
|     | *    *        *    *        *    *        *    *
| *    *        *    *        *    *        *    *
Amplitude |*      *      *      *      *      *      *      *
(y)    |*------*------*------*------*------*------*------*----------
|       *      *      *      *      *      *      | Time (x)
|     |        *    *        *    *        *    *       | -------->
|     |        *    *        *    *        *    *       |
\|/    |         *  *          *  *          *  *       0.01 seconds
|          **            **            **
```

- or -

```WAVE "B":
/|\    |  *       *       *       *       *       *
|     | * *     * *     * *     * *     * *     * *
|     | * *     * *     * *     * *     * *     * *   0.01 seconds
|*   *   *   *   *   *   *   *   *   *   *   *   |
Amplitude |*   *   *   *   *   *   *   *   *   *   *   *   |
(y)    |*---*---*---*---*---*---*---*---*---*---*---*---*-----------
|    *   *   *   *   *   *   *   *   *   *   *   *  Time (x)
|     |    *   *   *   *   *   *   *   *   *   *   *   *  -------->
|     |     * *     * *     * *     * *     * *     * *
\|/    |     * *     * *     * *     * *     * *     * *
|      *       *       *       *       *       *
```

If you picked the second wave, wave "B", as having the higher pitch, then you're correct! It has a higher frequency.

### Summary

In this chapter, we've learned about sound waves, and two of the most important properties of sound waves -- amplitude and frequency.

In the future, this article may contain a section on sampling -- this will be important when playing and mixing digital sounds.

Copyright 1998, Kevin Matz, All Rights Reserved. Last revision date: Fri, Oct. 23, 1998. A project