Sound Menu
- Type
- Sound
- Category
- Sound effects library in BASIC
- Author
- David Plotkin
- Published in
- Antic, May 1987 (Vol. 6, No. 1), "New Owners Column" Lesson 14
- Year
- 1987
- Language
- Atari BASIC
- Requirements
- Any Atari 8-bit, 16K RAM, Atari BASIC
- Tags
- antic, new-owners-column, sound, sound-effects, pokey, explosion, siren, beginner, atari-basic
About the program
This is the second listing from the last lesson of Antic's New Owners Column, which covered sound. The first was a keyboard organ, Organ Grinder. This one is a menu of seven sound effects: rifle shot, bomb, siren, European siren, whistle and bomb, sawing wood, and "Gentlemen, start your engines". Each effect is a self-contained subroutine, so it's really a small library of sounds to lift into your own games.
David Plotkin pointed out the trade-off. A constant tone costs a BASIC program nothing, because the POKEY chip keeps playing it on its own. Realistic effects, though, are made by changing frequency, distortion and volume rapidly, and while BASIC is doing that it can't do much else.
Typing it in
The only special characters are the inverse digits in the menu (lines 16–20). Press the Atari (inverse) key, type the digit, then press the key again. Everything else is ordinary text. Each effect is a GOSUB block starting at 100, 300, 500, 700, 800, 1000 and 1200, and line 40's ON A GOSUB depends on those numbers.
How to use it
RUN it, type a number from 1 to 7 at "Which Sound?" and press RETURN. The menu stays on screen, so you can try them one after another. Press BREAK to stop.
How it works
- Explosions and gunshots (lines 160–260 and 360–460) are made of white noise. Distortion 8 is POKEY's noisiest setting, and the program plays it on three voices at slightly different pitches. Each pass multiplies the three volumes by a factor less than 1 (ICR), so the sound dies away exponentially, the way a real bang does. The rifle and the bomb use the same code. The bomb uses a lower pitch (50 instead of 20) and a decay factor closer to 1, so it rumbles on longer. The DUR variable sets that factor.
- The sirens. The American siren (500–600) sweeps a pure tone (distortion 10) up and down in half-steps of the pitch value, swapping LO and HI each time. The European two-tone siren (700–780) just flips between two pitches with a pause.
- Whistle and bomb (800–970) slides two slightly detuned pure tones downwards while the volume grows by 3% each step, then sets off the explosion routine.
- Sawing wood (1000–1150) pairs distortion 4 with distortion 8 at the same pitch and wobbles it up and down five steps. Each GOSUB to 1060 is one stroke of the saw.
- Start your engines (1200–1350) revs distortion 0 up and down three times, idles, then sweeps distortion 2 from pitch 200 to 50 as the car pulls away.
Line 30 pokes 0 into location 77 (the attract-mode timer), so the screen doesn't start colour-cycling while you experiment. Line 35 sets it to 128, which switches attract mode on straight away.
Variations to try
- Change
DURat the start of an effect: bigger values give longer bangs and longer sirens. - Change the distortions in lines 190–210 from 8 to 0 or 4 and listen to how the explosion's character changes.
- To use an effect in your own program, copy its block and keep the line numbers (or renumber its internal GOTOs). Then set any variables it needs, such as PITCH and DUR, and GOSUB it.
Listing
10 REM SOUND MENU
12 REM BY DAVID PLOTKIN
14 REM (c) 1987, ANTIC PUBLISHING
16 GRAPHICS 0:? "Choose Your Sound:":? :? "{inv}1{/inv} RIFLE SHOT"
18 ? "{inv}2{/inv} BOMB":? "{inv}3{/inv} SIREN":? "{inv}4{/inv} EUROPEAN SIREN"
20 ? "{inv}5{/inv} WHISTLE & BOMB":? "{inv}6{/inv} SAWING WOOD":? "{inv}7{/inv} GENTLEMEN, START YOUR ENGINES"
30 POKE 752,0:POKE 77,0:POSITION 0,15:? "Which Sound";:INPUT A
35 POKE 752,1:? :POKE 77,128
40 ON A GOSUB 100,300,500,700,800,1000,1200
50 GOTO 30
100 REM RIFLE SHOT
110 DUR=3
120 PITCH=20:GOSUB 160
130 SOUND 1,0,0,0:SOUND 2,0,0,0
140 SOUND 0,0,0,0:SOUND 1,0,0,0:SOUND 2,0,0,0:RETURN
160 SOUND 2,75,8,15
170 ICR=0.62+DUR/100
180 V1=15:V2=15:V3=15
190 SOUND 0,PITCH,8,V1
200 SOUND 1,PITCH+20,8,V2
210 SOUND 2,PITCH+50,8,V2
220 V1=V1*ICR
230 V2=V2*(ICR+0.05)
240 V3=V3*(ICR+0.08)
250 IF V3>1 THEN 190
260 SOUND 0,0,0,0:RETURN
300 REM BOMB
310 DUR=7
320 PITCH=50:GOSUB 360
330 SOUND 1,0,0,0:SOUND 2,0,0,0
340 SOUND 0,0,0,0:SOUND 1,0,0,0:SOUND 2,0,0,0:RETURN
360 SOUND 2,75,8,15
370 ICR=0.8+DUR/100
380 V1=15:V2=15:V3=15
390 SOUND 0,PITCH,8,V1
400 SOUND 1,PITCH+20,8,V2
410 SOUND 2,PITCH+50,8,V2
420 V1=V1*ICR
430 V2=V2*(ICR+0.05)
440 V3=V3*(ICR+0.08)
450 IF V3>1 THEN 390
460 SOUND 0,0,0,0:RETURN
500 REM SIREN
510 DUR=10
520 LO=50:HI=35:STP=-0.5
530 FOR TIME=1 TO DUR
540 FOR PITCH=LO TO HI STEP STP
550 SOUND 0,PITCH,10,14
560 FOR WAIT=1 TO 15:NEXT WAIT
570 NEXT PITCH
580 XX=LO:LO=HI:HI=XX:STP=-STP
590 NEXT TIME
600 SOUND 0,0,0,0:RETURN
700 REM EUROPEAN SIREN
710 DUR=5
720 LO=57:HI=45:PITCH=HI
730 FOR TIME=0 TO DUR*2
740 SOUND 0,PITCH,10,14
750 FOR WAIT=1 TO 180:NEXT WAIT
760 PITCH=LO:LO=HI:HI=PITCH
770 NEXT TIME
780 SOUND 0,0,0,0:RETURN
800 REM WHISTLE AND BOMB
810 DUR=5
820 V1=4:FOR PITCH=30 TO 75
830 SOUND 0,PITCH,10,V1
840 SOUND 1,PITCH+3,10,V1*0.7
850 FOR WAIT=1 TO DUR*3:NEXT WAIT
860 V1=V1*1.03:NEXT PITCH
870 SOUND 2,35,8,12
880 V1=15:V2=15:V3=15
890 PITCH=DUR+5:ICR=0.8+DUR/100
900 SOUND 0,PITCH,8,V1
910 SOUND 1,PITCH+20,8,V2
920 SOUND 2,PITCH+50,8,V3
930 V1=V1*ICR
940 V2=V2*(ICR+0.05)
950 V3=V3*(ICR+0.08)
960 IF V3>1 THEN 900
970 SOUND 0,0,0,0:SOUND 1,0,0,0:SOUND 2,0,0,0:RETURN
1000 REM SAWING WOOD
1010 DUR=8
1020 FOR TIME=1 TO DUR
1030 ST=16:VL=12:GOSUB 1060
1040 ST=18:VL=8:GOSUB 1060
1050 NEXT TIME:RETURN
1060 FOR PITCH=ST+5 TO ST STEP -1
1070 GOSUB 1130:NEXT PITCH
1080 FOR PITCH=ST TO ST+5
1090 GOSUB 1140:NEXT PITCH
1100 SOUND 0,0,0,0:SOUND 1,0,0,0
1110 FOR WAIT=1 TO 25:NEXT WAIT
1120 RETURN
1130 SOUND 0,PITCH,4,VL
1140 SOUND 1,PITCH,8,VL*0.7
1150 FOR WAIT=1 TO 3:NEXT WAIT:RETURN
1200 REM GENTLEMEN, START YOUR ENGINES
1210 FOR TIMES=1 TO 3
1220 FOR CYCLES=1 TO 5
1230 SOUND 0,200-CYCLES*20,0,14-CYCLES
1240 FOR WAIT=1 TO 25:NEXT WAIT:NEXT CYCLES
1250 FOR CYCLES=1 TO 7
1260 SOUND 0,CYCLES*20,0,14-CYCLES
1270 FOR WAIT=1 TO 25:NEXT WAIT:NEXT CYCLES
1280 SOUND 0,0,0,0:FOR WAIT=1 TO 100:NEXT WAIT
1290 NEXT TIMES
1300 SOUND 0,100,4,8
1310 FOR WAIT=1 TO 2000:NEXT WAIT
1320 FOR W=200 TO 50 STEP -1
1330 SOUND 0,W,2,10:FOR WAIT=1 TO 10:NEXT WAIT
1340 NEXT W:SOUND 0,50,2,8
1350 FOR WAIT=1 TO 2000:NEXT WAIT:RETURN
Download
The original program from the Antic disk, to LOAD or ENTER instead of typing it: SOUNDMEN.BAS.
Credits
- Program listing: Sound Menu — program listing (Antic, May 1987 (Vol. 6, No. 1), "New Owners Column" Lesson 14) — David Plotkin
- Antic, May 1987 - Vol. 6 No. 1 — article: Sound Menu by David Plotkin — David Plotkin, 1987
- Antic Software Archive — May 1987 - Vol. 6 No. 1 disk: SOUNDMEN.BAS — AtariMagazines.com (Kevin Savetz), 1987
