Sound Effects Recipes for Atari BASIC
The POKEY Sound Basics guide explains where the numbers in SOUND come from. This one is the cookbook. Each recipe is a self-contained subroutine you can drop into a game, with notes on which ingredient does what so you can season to taste. They all fit together into one menu program, and the menu screen is shown above.
What you'll learn: how to shape a sound's volume over time, when to use POKEs instead of SOUND, ready-made explosion, gunshot, laser, siren, footstep, motor and interface sounds, and how to keep an effect playing while your game carries on.
What you need: Atari BASIC on any Atari 8-bit or emulator, with the volume turned up. Read POKEY Sound Basics first if SOUND 0,121,10,8 doesn't mean anything to you yet.
1. The ingredients
Every effect is built from four things: a voice (0–3), a pitch (0–255, bigger is lower), a distortion (the texture) and a volume (0–15) that usually changes over time. For effects, the distortions sort into three families:
| Distortion | Texture | Good for |
|---|---|---|
| 8 | White noise (17-bit poly) | Explosions, gunshots, hiss, surf, footsteps |
| 0, 2, 4, 12 | Buzzes and rattles (short polys) | Engines, motors, error buzzers, machine guns |
| 10 | Pure square wave | Lasers, sirens, beeps, bells, music |
To skip SOUND, POKE the registers directly. Voice 0's pitch is AUDF1 at 53760 and its control byte is AUDC1 at 53761. Voices 1–3 follow in pairs (53762/53763, 53764/53765, 53766/53767). The control value is 16*DISTORTION+VOLUME, so noise at volume V is 128+V and a pure tone is 160+V.
POKEs earn their place in effects because volume and pitch change many times per effect. A POKE changes one register. SOUND evaluates four arguments, writes both of the voice's registers, and also clears AUDCTL (53768) and writes 3 to SKCTL (53775) every time. That reset is useful: POKEY stays silent after disk or cassette I/O until it is re-initialised. So every program below starts with one SOUND 0,0,0,0 and then uses POKEs freely.
Keep the total volume of all voices playing at once at 32 or below. Above that, POKEY's output overloads and distorts.
2. Envelopes: the secret sauce
What makes a burst of noise sound like a gunshot rather than a radio between stations is the envelope, meaning how its volume changes over time. There are two easy shapes:
- Linear decay:
FOR V=15 TO 0 STEP -1 ... NEXT V. It is quick and crisp, and the step size sets the length. - Exponential decay: multiply the volume by a factor below 1 on each pass (
V=V*0.85) and stop when it drops below 1. This fades the way real sounds do: loud for a moment, then a long tail. Richard M. Kruse built his whole "Atari Library of Sound" (The Creative Atari, 1983) on this idea. Factors near 0.5 give a short strike, and factors near 0.95 give a lingering ring.SOUNDrounds fractional volumes for you; with POKEs, useINT.
3. The recipes
The menu driver below calls the recipes. Line 9000 silences all four voices, and lines 9100–9120 wait D jiffies (sixtieths of a second on NTSC) using system timer 3 at location 540. That gives the same timing whatever the program is doing, unlike an empty FOR loop.
10 OPEN #1,4,0,"K:"
20 GRAPHICS 0:POKE 752,1:SOUND 0,0,0,0
30 PRINT "SOUND EFFECT RECIPES":PRINT
40 PRINT "E EXPLOSION G GUNSHOT"
50 PRINT "L LASER S SIREN"
60 PRINT "F FOOTSTEPS M MOTOR"
70 PRINT "B BEEPS N NON-BLOCKING"
80 PRINT :PRINT "PRESS A KEY (Q QUITS)"
90 GET #1,K:POSITION 2,10:PRINT "PLAYING ";CHR$(K);" "
100 IF K=69 THEN GOSUB 1000
110 IF K=71 THEN GOSUB 1500
120 IF K=76 THEN GOSUB 2000
130 IF K=83 THEN GOSUB 3000
140 IF K=70 THEN GOSUB 5000
150 IF K=77 THEN GOSUB 6000
160 IF K=66 THEN GOSUB 7000
170 IF K=78 THEN GOSUB 8000
180 GOSUB 9000:IF K<>81 THEN 90
190 CLOSE #1:END
9000 FOR I=0 TO 3:SOUND I,0,0,0:NEXT I:RETURN
9100 POKE 540,D
9110 IF PEEK(540) THEN 9110
9120 RETURN
Explosion
Kruse's method uses three noise voices at different pitches, each fading at a different rate, with the deepest one fading slowest. That gives the "rolling" tail of a big bang:
1000 REM EXPLOSION: 3 NOISE VOICES
1010 V1=12:V2=10:V3=10:K1=0.84
1020 SOUND 0,20,8,V1:SOUND 1,40,8,V2:SOUND 2,70,8,V3
1030 V1=V1*K1:V2=V2*(K1+0.05):V3=V3*(K1+0.08)
1040 IF V3>1 THEN 1020
1050 RETURN
The starting volumes add up to exactly 32. Raise K1 towards 0.9 for a bigger, longer blast; keep K1+0.08 below 1 or the loop never ends. For a distant explosion, start all three volumes lower.
Gunshot
A gunshot is the same idea squeezed into a fraction of a second: a single high-pitched burst of noise with a fast linear decay. POKEs make it snappy:
1500 REM GUNSHOT
1510 POKE 53760,10
1520 FOR V=15 TO 0 STEP -1:POKE 53761,128+V:NEXT V
1530 RETURN
Change the pitch in line 1510 for a different calibre: a larger value such as 30 gives lower, duller noise, and a smaller one such as 2 gives a higher, sharper crack.
Laser
POKEY Sound Basics has a rising zap. This version falls in pitch while it fades, the classic "pew":
2000 REM LASER: FALLING PITCH
2010 FOR P=20 TO 120 STEP 4
2020 POKE 53760,P:POKE 53761,175-INT(P/10)
2030 NEXT P:POKE 53761,160:RETURN
The control value slides from 173 (pure tone, volume 13) down to 163 as the pitch value climbs. For a fatter "phaser", run voice 1 one or two pitch steps away from voice 0 in the same loop: the two tones beat against each other.
Siren
The European two-tone siren is in POKEY Sound Basics. This is the American wail: a pure tone sweeping smoothly up and down. Each pitch step lasts one jiffy, so the speed is the same on every machine:
3000 REM SIREN WAIL, POKE VERSION
3010 POKE 53761,172
3020 FOR T=1 TO 3
3030 FOR P=60 TO 30 STEP -1:POKE 53760,P:D=1:GOSUB 9100:NEXT P
3040 FOR P=30 TO 60:POKE 53760,P:D=1:GOSUB 9100:NEXT P
3050 NEXT T:RETURN
Widen the range (for example 80 to 25) for a more frantic siren, or set D=2 for a slower one. Carl Evans's Atari BASIC Faster and Better (1983) adds a klaxon to this family. It holds two pure tones five steps apart on voices 0 and 1 and adds a quiet distortion-2 buzz on voice 2.
Footsteps
A footstep is a very short, low burst of noise. Alternate two pitches for left and right feet and leave a gap between steps:
5000 REM FOOTSTEPS
5010 FOR S=1 TO 8
5020 P=200:IF S=INT(S/2)*2 THEN P=230
5030 POKE 53760,P
5040 FOR V=10 TO 0 STEP -2:POKE 53761,128+V:NEXT V
5050 D=20:GOSUB 9100:NEXT S:RETURN
D sets the walking pace: 20 jiffies is a stroll and 8 is a run. Kruse's ticking clock uses the same recipe at a high pitch (values 6–20), with two alternating pitches for "tick" and "tock".
Motor
The short polynomial counters give engine noises. Distortion 2 at a low pitch is a rough motor, and lowering the pitch value makes it rev:
6000 REM MOTOR REVS UP AND DOWN
6010 FOR SP=0 TO 150 STEP 3:SOUND 0,250-SP,2,8:NEXT SP
6020 FOR SP=150 TO 0 STEP -2:SOUND 0,250-SP,2,8:NEXT SP
6030 RETURN
In a driving game, compute the pitch from the player's speed each time round the main loop instead of using a FOR loop. Evans's "tank" puts SOUND V,255,2,4 on all four voices at once for a heavy idle. The Creative Atari suggested SOUND 0,70,2,8 for a truck and SOUND 0,145,2,V for an aeroplane, with V rising from 1 to 12 and falling again.
Interface beeps
Menus and puzzles need small, polite noises. These three are a "correct" chirp (two rising notes), an "error" buzz (distortion 12 at a low pitch) and a bonus arpeggio (C, E, G, C):
7000 REM BEEPS
7010 SOUND 0,81,10,10:D=4:GOSUB 9100
7020 SOUND 0,60,10,10:D=8:GOSUB 9100
7030 SOUND 0,0,0,0:D=30:GOSUB 9100
7040 SOUND 0,200,12,10:D=20:GOSUB 9100
7050 SOUND 0,0,0,0:D=30:GOSUB 9100
7060 RESTORE 7090:FOR I=1 TO 4:READ P
7070 SOUND 0,P,10,10:D=3:GOSUB 9100:NEXT I
7080 RETURN
7090 DATA 121,96,81,60
Keep interface sounds short (under a quarter of a second) and quieter than game sounds. Players hear them hundreds of times.
4. Effects that don't stop the game
Every recipe so far takes over the program until it finishes, which is fine between levels and fatal during play. The fix is to turn the loop inside out. Store the effect's state in variables, and let the main game loop advance it one step per pass:
8000 REM NON-BLOCKING EFFECT IN A LOOP
8010 EV=0:N=0
8020 N=N+1:IF N=10 OR N=40 THEN EV=15:EP=10
8030 IF EV THEN POKE 53760,EP:POKE 53761,128+EV:EV=EV-1:EP=EP+3
8040 POSITION 2,12:PRINT "GAME LOOP ";N;" ";
8050 IF N<60 THEN 8020
8060 POSITION 2,12:PRINT " ":RETURN
Here N stands in for the game loop and line 8040 for the game's own work. Line 8020 "fires" twice. In a real game this would be IF STRIG(0)=0 THEN EV=15:EP=10. Line 8030 plays the next step of a falling noise burst only while EV is above zero, so the sound costs almost nothing when it is silent. Give each simultaneous effect its own voice and its own pair of variables. A common layout is voice 0 for the player's shots, voice 1 for explosions, voice 2 for the engine and voice 3 for music or alerts.
Because the effect advances once per loop, its length depends on how fast the loop runs. If that matters, step it from the jiffy clock instead. For sound that runs with no help from BASIC at all, see the vertical-blank music driver in Programming Music on the Atari.
Troubleshooting
- No sound from the POKE recipes. POKEY has not been initialised since the last disk or cassette access. Execute any
SOUNDstatement first;SOUND 0,0,0,0does it silently. - A sound keeps playing after the program stops.
BREAKdoesn't silence POKEY. End with line 9000 orEND. - Only a click instead of a sound. You poked an odd multiple of 16 into an AUDC register (for example 176), which sets the volume-only bit. Use
16*DISTORTION+VOLUMEwith an even distortion. - Effects sound harsh or quieter when several play together. The volumes add up to more than 32. Lower them.
- The explosion never ends. A decay factor of 1 or more. Check that K1+0.08 is below 1.
- An effect runs at different speeds in different places. It uses an empty FOR loop for timing. Switch to the jiffy wait at line 9100, which runs at the same speed anywhere in the program (but at 50 Hz on PAL machines, so effects there last 20% longer).
Further reading
- The Creative Atari (Creative Computing Press, 1983): Richard M. Kruse's "An Atari Library of Sound", a dozen effects including doorbell, telephone, alarm bell, klaxon, whistle and bomb, steam whistle and sawing wood.
- Atari BASIC Faster and Better by Carl M. Evans (IJG, 1983), Chapter 14: AUDC bit by bit, plus a library of effects from steam trains to thunderstorms.
- De Re Atari (Atari, Inc., 1982), Chapter 7: sound theory and POKEY's distortions.
- Atari Sound and Graphics by Herb Moore, Judy Lower and Bob Albrecht (Wiley, 1982).
- On this site: POKEY Sound Basics, Advanced POKEY, Programming Music on the Atari and Keeping Time.
Credits
- Picture: The recipe menu running in an emulator. Each letter plays one of the subroutines in this guide. — Program authors; screenshot by the site
- The Creative Atari (1983) — An Atari Library of Sound (Richard M. Kruse) — AtariArchives.org, 1983
- The Creative Atari (1983) — Star Raiders and the Atari SOUND Command — AtariArchives.org, 1983
- Mapping the Atari (revised edition) — 540 CDTMV3, 752 CRSINH, 53760-53768 AUDF/AUDC/AUDCTL — AtariArchives.org
