Skip to Content
Under the HoodEmulator InternalsHow a computer makes sound

How a Computer Makes Sound

A loudspeaker can do exactly one thing: push its cone out and pull it back in. That’s it. Every sound you have ever heard from a computer, a beep, a chord, a voice, is nothing more than a list of positions for that cone. So how can a list of numbers sound like a voice?

Sound is how a program talks to you without taking your eyes off the screen. Music sets the mood of a game, an effect tells you that you were hit, a beep says a key was pressed. On a home computer of the ZX Spectrum’s era it did one more job: programs were saved to and loaded from cassette tape as sound.

You do not need to read anything first, and you do not need to know any electronics. Nothing here is about one machine: these are the basics that the later chapters, which explain how particular machines make their sound, can then skip.

The road ahead

We follow a sound on its way out of the computer. First comes what a sound is, and what a speaker does to make one. Then we turn a sound into a list of numbers, one sample at a time, and back into a voltage with a DAC, which delivers a staircase rather than a smooth curve. The filter that smooths that staircase is the key idea, so give it your full attention: it is the step that makes the result analogue. After that we see how many samples a second are enough, and what goes wrong with too few. Then two surprises: how a single bit, only ever on or off, can make music, and how several sounds become one by simple adding. We end with two speakers, stereo and a sense of space, and with where the numbers come from in the first place.

Every waveform here is computed, not sketched, so the figures are drawn to scale.

Sound is air pushed back and forth

Sound is a change in air pressure that travels. A loudspeaker’s cone moves forward and squeezes the air in front of it a little; it moves back and leaves the air a little thinner. These squeezed and thinned bands spread out from the speaker and reach your ear, where they push your eardrum in and out in the same rhythm.

Draw the pressure at one spot over time and you get a curve, the sound’s waveform. Two things about it decide most of what you hear:

  • How far the cone moves is the amplitude, which you hear as loudness. A tall waveform is a loud sound.
  • How often the cone moves back and forth is the frequency, which you hear as pitch. It is counted in hertz (Hz), cycles a second. The A that orchestras tune to is 440 Hz; human hearing reaches from about 20 Hz to about 20,000 Hz.

The shape of the waveform within each cycle is the sound’s timbre. A flute and a violin playing the same 440 Hz sound different because their waveforms are different.

A speaker moves its cone in proportion to the voltage on its wires: more voltage, further out. So making a sound means making a voltage that changes in the shape of the waveform. The whole job of a computer’s sound hardware is to produce that voltage, and it does it in a chain of steps:

NumbersDACFilterAmplifierSpeakerAir128177218245255245218177⋮cone movespressuredigital: a list of numbersanalogue: a voltage that changes smoothly
From numbers to sound. Everything up to the DAC is digital, a list of numbers; from the DAC on, it is a voltage, and finally a movement of the air.

We follow this chain from left to right, one box at a time.

A sample is one number for one moment

A computer cannot store a smooth curve. It can store only numbers, so it records the waveform’s height at regular moments, one number each time. Each number is a sample, and the list of them is the sound in digital form.

Two settings decide how good the list is:

  • The sample rate is how many samples are taken each second. More samples follow the curve more closely.
  • The resolution is how many different values one sample can have. It is usually given in bits: an 8-bit sample has 256 possible values, a 16-bit sample 65,536. More values mean that each sample can be closer to the real height of the curve.

Here is the same 1,000 Hz wave sampled at two rates:

16,000 samples a second5,000 samples a second0 ms1 ms2 ms
The same 1,000 Hz wave sampled at two rates. Each orange dot is one sample: the wave’s height at that moment, stored as a number.

At 16,000 samples a second, 16 samples fall in each cycle of the wave and the dots trace it closely. At 5,000 samples a second, only 5 fall in each cycle, and the outline is much coarser. Coarse is not yet wrong, though; you will see where “coarse” turns into “wrong” in Fast enough, or it sounds wrong.

A music CD is the everyday example: it stores 44,100 samples a second for each of its two sides (left and right), each sample 16 bits. That is enough for every pitch a human can hear.

From a number to a voltage: the DAC

To play the list back, the computer needs a voltage for each number. The chip that makes it is a digital-to-analogue converter, a DAC. You give it a number, and its output becomes the voltage that belongs to that number: 0 gives the lowest voltage, the largest number the highest, and every number in between a voltage in between.

Between two samples, a DAC does nothing at all. It holds its voltage until the next number arrives, then jumps to the new one. So its output is not a smooth curve but a staircase: flat steps, each one sample long, with a jump at every sample.

The steps are easiest to see when the resolution is low. In the next figure, each sample has only 16 possible values (4 bits), and the wave is played back at 16,000 samples a second, the faster of the two rates above. The orange staircase is what the DAC delivers:

0 ms1 ms2 ms150number
The DAC’s output (orange): every number held as a flat step until the next one, on only 16 possible heights. The same output after a low-pass filter (green): a smooth wave.

Leave the green curve for a moment; the next section is all about it. The staircase alone already sounds like the wave, but not quite. The sharp corners of the steps add a faint hiss or buzz of their own, much higher than the note.

From a staircase to a smooth wave: the filter

Here is the idea I would ask you to keep if you keep only one. Look at the speeds in the figure. The note goes up and down once every millisecond, but the DAC jumps 16 times in that millisecond. The steps change far faster than anything in the wanted sound, and that difference in speed is what makes it easy to get rid of them.

A low-pass filter lets slow changes through and holds back fast ones. In its simplest form it is just a resistor and a capacitor. The capacitor has to be charged through the resistor, which takes time, so the voltage across it cannot follow a sudden jump; it moves towards the new level gradually. A slow change, like the 1,000 Hz note, has time to come through almost whole. The sudden jumps of the steps are rounded off.

That is the green curve in the figure above: the staircase after a filter of two resistor-and-capacitor stages. The steps are gone, and what remains is a smooth wave. It comes out a little smaller and about a tenth of a millisecond behind the staircase, because the filter takes time to follow. Neither matters to the ear.

This is the step that makes the result analogue. Before the filter, the voltage can only stand on a fixed set of steps, at fixed moments. After it, the voltage changes continuously, like the air pressure it will become. The speaker helps, too: its cone has weight and cannot follow very fast changes, so it smooths what the filter leaves over. And our ears do not hear very high frequencies at all, which finishes the job.

Fast enough, or it sounds wrong

How many samples a second are enough? The rule is simple: the sample rate must be more than twice the highest frequency in the sound. A wave that goes up and down needs at least one sample on its way up and one on its way down in every cycle, or the samples cannot tell what it did.

Break the rule, and you do not merely get a rough copy of the high note. You get a different, lower note that was never in the original. This is called aliasing: the samples of the fast wave happen to lie exactly on a slow one, and the DAC and the filter faithfully play the slow one. The machine is not wrong, mind you. It plays exactly what the numbers say.

Take a telephone line, which carries 8,000 samples a second. Twice the highest frequency must stay below 8,000, so the line can carry sounds up to 4,000 Hz. Here is what happens to a 7,000 Hz tone sent through it:

7,000 Hz: what goes in8,000 samples a second1,000 Hz: what comes out00.5 ms1 ms
Aliasing. Sampled too slowly, a 7,000 Hz tone leaves samples that describe a 1,000 Hz tone, and that is what is played back.

The fast blue wave goes through seven cycles in the millisecond, but the samples catch it only eight times. Each sample lands seven eighths of a cycle after the one before, which gives the same height as landing an eighth of a cycle before it, so the samples slowly walk backwards through the wave. The dots therefore describe one slow cycle of 1,000 Hz, the difference between the sample rate and the tone (8,000 − 7,000), and that is what comes out of the speaker.

Think about what that means: no filter after the DAC can undo it. The samples themselves are a correct record of a 1,000 Hz wave. So a sound must be free of frequencies above half the sample rate before it is sampled. The CD’s rate of 44,100 samples a second was chosen with this in mind: half of it, 22,050 Hz, is just above the highest pitch anyone hears.

Sound from a single bit

The simplest sound hardware has no DAC at all. It has just one bit, which the computer switches on or off, and which drives the speaker fully out or fully in. The beeper of the ZX Spectrum works like this. On or off, nothing in between. How on earth can that make music?

A note from switching. Switch the bit on and off at a steady pace, and the cone goes out and in at that pace: a square wave. The pace is the pitch. To play the 440 Hz A, the program switches the bit 880 times a second, on and off once in each cycle of 2.27 milliseconds. It sounds harsher than a smooth wave, because the square shape has a strong timbre of its own, but it is clearly a note. The program has to time every switch itself, so while it plays, it usually does little else.

Any level from switching fast. Well, one bit can do more than square waves. Switch it much faster than any note, faster than the speaker and the ear can follow, and vary how long it stays on in each round. Then the filter from earlier, and the speaker’s own sluggishness, average the pulses out: a bit that is on a quarter of the time gives a quarter of the full level, half of the time gives half.

on 25 % of the timeon 50 % of the timeon 75 % of the timeonofftime: 24 pulse periods
Pulse-width modulation. A bit that is only on or off (blue), filtered (green), gives the level of its average on-time; the dashed lines mark a quarter, a half and three quarters.

The green curve is the blue pulse train after a filter, the same kind of smoothing the DAC’s staircase went through. Each time the on-time changes, it moves over to the new average and settles at about a quarter, a half and three quarters of the full level, wobbling slightly with every pulse. With pulses fast enough, the wobble is far above hearing.

This is called pulse-width modulation: the width of each pulse carries the level. Change the widths in the shape of a waveform, and the averaged output follows that waveform, so one bit can play any sound a DAC could. I find that one of the nicest tricks in all of electronics.

Many modern converters are built on the same idea. A delta-sigma converter switches its single output bit millions of times a second, each time checking whether the output so far has been above or below the wanted level and switching to make up the difference. The filter after it turns the stream of bits into a smooth voltage. Notice the pattern? The filter is what makes either kind work.

Several sounds at once are added together

Music has many voices at once, and a game plays its effects over its music. But the air in front of a speaker can be in only one state at a time, so several sounds can only reach it as one waveform. That waveform is simply their sum: at every moment, the levels of all the sounds are added together. In a computer, mixing is adding the numbers (or, in the electronics, adding the voltages).

1,000 Hz3,000 Hz+=top of the rangebottom of the rangethe sum0 ms1 ms2 ms
Mixing is adding. The dashed curve is the true sum; the green curve is what a converter with a limited range delivers, flattened where the sum goes past the range (red).

Adding has one catch. A DAC, an amplifier or a speaker can deliver only a certain range of levels. When the sum goes past the top or the bottom of the range, the output stays at the limit and the tip of the wave is cut off flat. This is clipping, marked in red in the figure, and it sounds like a harsh crackle. A designer avoids it by leaving headroom: making each source quiet enough that the loudest possible sum still fits.

There is a second, quieter consequence. Many sound sources never go below zero: a level of 0 is “off”, and the wave moves above it. Their sum then always contains a constant extra level, an offset, around which the sound moves. A speaker has no use for a constant level; it would only push the cone out and keep it there. So a capacitor in the path removes it. A capacitor lets changes through but not a level that stays the same, and what reaches the speaker is the sound moving around zero.

Two speakers: stereo and space

Your two ears hear every sound slightly differently. A sound from the left arrives at the left ear a moment earlier and a little louder than at the right one, and your brain uses those differences to tell where the sound comes from.

Stereo uses this. It keeps two separate sounds, two channels, one for a left speaker and one for a right speaker, each going through its own DAC, filter and amplifier. Mixing then happens twice, once for each side. A mono sound is the opposite case: one channel, or the same sound on both sides.

The simplest way to place a sound is panning: mix it into the two channels at different levels.

left onlyLRleftrightequal: centreLRleftrightmore on the rightLRleftrightBars: the level in each channel. The green dot: where the sound seems to come from.
Panning. The same sound at different levels in the left and right channels seems to come from different places between the speakers.
  • Equal levels on both sides put the sound in the centre, between the speakers.
  • More on one side moves it towards that speaker.
  • A sound on one side only seems to come from that speaker alone. In headphones this can feel odd, because in real life a sound always reaches both ears.

Panning is how a computer gives each voice its own place: the melody in the centre, an accompaniment to the left, a drum to the right. Each voice stays easier to follow when they do not all come from the same spot, a bit like a dinner party where not everybody talks from the same chair.

Other effects use the timing difference, still with the same two channels:

  • A copy of a sound added a few milliseconds later on one side pulls it towards the other side, or makes it sound wider than the speakers.
  • Echo and reverberation add quieter, delayed copies of the sound, the way the walls of a room or a valley would send it back. They tell the ear how large the space is.

In a real room, a sound reaches you first along the straight path, and then again and again after bouncing off the walls, each time later and quieter:

a room seen from above (12 × 8 m)speakerlistenerwhat reaches the ear0 ms20 ms40 ms60 msleveldirectone bouncetwo bounces (fainter)
Echoes in a room, drawn to scale. Each bounce makes the path longer, so the sound arrives later, and the wall swallows some of it, so it arrives quieter. A program imitates a room by adding such delayed, quieter copies to the sound.

None of this needs new hardware beyond the second channel. It is all done by choosing what to add into each side, and when.

Where the numbers come from

Let’s step back. The whole chain, from the first figure, now reads:

numbers → DAC → filter → amplifier → speaker → air

Only one question is left: who makes the numbers? They can come from two kinds of place:

  • The CPU writes them. The program works out each sample and hands it to a DAC (or switches a single bit), on time, every time. That can play anything, speech or recorded drums, but it costs the CPU work for every sample.
  • A sound chip makes them. The program tells the chip “play this pitch at this loudness”, and the chip’s own counters produce the waveform from then on, until the program changes the settings. That frees the CPU, but the sound is limited to what the chip can generate.

Most computers of the 1980s had one or both. Their sounds go through the same chain: a mixer that adds them, a filter, an amplifier and a speaker. The chapters on particular machines start from here; the ZX Spectrum Next’s begin with the big picture, which shows how the Next’s four kinds of source meet in one mixer.

What to take home

A sound is air pressure going back and forth, and a speaker makes it by moving its cone with the voltage on its wires. How far the cone moves is the loudness, how often it moves is the pitch, counted in hertz: 440 Hz for the orchestra’s A, about 20 Hz to 20,000 Hz for human hearing.

A computer keeps a sound as samples, one number for each moment. The sample rate says how many a second, the resolution how many values each one may take: a CD stores 44,100 samples a second per side, 16 bits each. A DAC turns each number into a voltage and holds it, which gives a staircase, and a low-pass filter rounds the staircase into a smooth, truly analogue wave. That filter is the quiet hero of the whole chain.

The sample rate must be more than twice the highest frequency in the sound. Break that rule and you hear a note that was never there: a 7,000 Hz tone through an 8,000-sample telephone line comes out at 1,000 Hz, and no filter afterwards can take it back.

One bit is enough to make sound. Switched at a steady pace it plays a square wave, 880 switches a second for the 440 Hz A; switched much faster with varying on-times, and filtered, it gives any level, which is pulse-width modulation, and delta-sigma converters build on the same idea.

Several sounds become one by adding, which needs headroom so the sum does not clip, and a capacitor to remove the offset that sources above zero leave behind. Stereo does all of it twice, once per side: panning places a sound by its level in each channel, and delayed, quieter copies give it a room to live in. From here on, every machine tells its own version of this story.

Last updated on