Making a beeper sing: how the music and the farts in Fart Attack work
The original 48K ZX Spectrum had no sound chip. It had a beeper: a tiny speaker the processor could switch on and off, and nothing else. There was no volume control, no second voice and no way to play two notes at once. Every tune on the machine was made by flicking that one switch fast enough, in the right pattern, to sound like music.
Fart Attack is written as a 1984 Spectrum game, so its music keeps to that rule. Its sound effects, on the other hand, are a different story.
None of it is a recording. There are no sound files in the game at all. Every note and every fart is built in your browser, while you play, by the Web Audio API that every modern browser has.
One square wave, one note at a time
A beeper can only make a square wave: the speaker is either on or off, so the sound jumps between two levels with nothing in between. That gives the hard, buzzy tone everyone remembers. Fart Attack’s music uses exactly one square-wave voice, and it only ever plays one note at a time.
The problem is that a tune with only one note at a time sounds thin. There’s a melody but no chords underneath it. Spectrum composers had a famous way round this, and Fart Attack uses it: alternate. The music runs in steps of 0.09 seconds. On even steps the voice plays the next note of the melody. On odd steps it plays a note from the chord underneath, cycling through the three notes of the chord. Played that fast, your ear stops hearing a single wobbling line and starts hearing a tune with harmony under it.
The tune is four bars long and loops every 64 steps, a little under six seconds. The chords under it go A minor, F major, C major, G major, one of the most used progressions in pop. The melody starts on A and climbs through C and E, the notes of that first chord, so the tune and the harmony line up even though they never sound at the same moment.
Notes as numbers
Inside the game the notes are stored as numbers, the same numbering electronic instruments use: 69 is the A above middle C, and each step up or down is one semitone. One line of maths turns a number into a pitch:
pitch = 440 × 2^((note − 69) ÷ 12)
So note 69 is 440 Hz, note 81 (twelve semitones higher) is 880 Hz, an octave up, and everything in between falls into place. The whole tune fits in a few short rows of numbers.
Playing it on time
A browser is busy. It is drawing the game fifty times a second, handling keys and running everything else on the page, so it can’t promise to start a note at exactly the right instant if it waits until the last moment. Fart Attack doesn’t try.
Instead, every twentieth of a second it looks a fifth of a second ahead and books every note that falls in that window, each with an exact start and stop time on the audio clock. The audio system then plays them precisely, however busy the rest of the page is. Each note is cut off at 85% of its step, which leaves a tiny gap before the next and gives the music its clipped, chippy feel.
The volume is set very low on purpose. A real beeper wasn’t loud, and a square wave at full volume is painful.
The farts cheat
That’s the music. The farts throw the rulebook away, because farts on a real beeper would all sound the same, and a game called Fart Attack needs variety.
There are fourteen farts, each with a name on screen: FART!, SQUEEEEK!, KA-BRAAAAM!, P-P-P-PFFT!, pfffsssss…, BWOOOOMP!, POOT POOT!, SPLLLURRT!, BRRRRRRRT! and more. Each one is a small recipe of numbers, and the same engine plays all of them:
- A sliding tone. Usually a sawtooth wave (brighter and raspier than a square) that starts at one pitch and slides down to another. KA-BRAAAAM! drops from 55 Hz to 28 Hz over 1.6 seconds, which is mostly felt rather than heard.
- A wobble. A second, much slower oscillator shakes the pitch of the first up and down, from 3 times a second for a slow pfffsssss… to 55 times a second for a wet SPLLLURRT!. That flutter is most of what makes it sound like a fart rather than a horn.
- Hiss. Random noise mixed in, from almost none for a clean BWOOOOMP! to pure noise for pfffsssss…
- A filter that cuts the high frequencies, so it sounds muffled, as if through trousers.
- Bursts. Some farts are one long blast, others are several short ones in a row. P-P-P-PFFT! is eight bursts a ninth of a second apart.
Every time one plays, all its pitches shift by up to 10% at random, so the same fart is never quite the same twice.
The burps use the same engine with one change: the filter only lets a band of frequencies through, instead of cutting the top off. That band acts like a throat and mouth, and gives burps a vowel sound. That’s how AAAY-BEEE-CEE! manages to sound like letters.
Small enough to fit
All of this, the tune, fourteen farts, six burps and the beeps and bloops of the game itself, is a few hundred lines of code. The whole of Fart Attack, sound, graphics and game together, is a single file of about 43 kilobytes.
Hear it for yourself in Fart Attack (press M to turn the music on or off). If you want the high score, the Fart Attack high-score guide has the numbers. Fart Attack 2 moves the music on to three channels, the way the Spectrum 128 and Amstrad CPC could.
How to play, the controls and a guide are on the Fart Attack page.