logo
By Lawrence

20 minutes

Sound and Shipping

The road war is all there. Rammers, gunners, a bomber, police, a truck that swallows your car, a score and a HUD. And it is silent. The guns fire with no crack, and the bomber's blades turn without a sound. The game also runs only on your own machine, from a dev server.

This lesson fixes both. First comes sound, with no audio files at all. Then comes a production build: a folder you can put on the web.

Sound from numbers

A speaker plays a list of numbers, called samples, 44,100 or 48,000 of them a second. Each number says how far to push the speaker, from −1 to +1. Make the list and you have sound. No recording is needed.

Web Audio, built into the browser, makes those lists from a few parts. There are oscillators, which make a steady wave at one pitch, and noise buffers, lists of random numbers that sound like a hiss. Filters keep some pitches and drop the rest. Last, a gain multiplies the sound by a number: 1 lets it through, 0 silences it.

One part can also turn another part's knob. Plug a wave into a gain's gain setting, and Web Audio adds the wave to the setting's own value, sample by sample:

gain(t) = base + depth × square(t)

Continue reading

Unlock the Full Course

Every lesson, the runnable examples, and the finished build — yours to keep.

$9one-time

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search