Classic Computer Magazine Archive ANTIC VOL. 6, NO. 1 / MAY 1987

Starting Out

BY DAVID PLOTKIN, ANTIC CONTRIBUTING EDITOR

New Owners Column

Lesson 14: Sound

This lesson concludes the beginners' 8-bit Atari BASIC programming series that started in the March 1986 issue. Next month, Antic will introduce a new series that teaches Atari animated graphics and game design.

If you play computer games, you know what kind of sounds your computer can make. Sound is an important way to hold the user's attention. Entirely silent games soon lose their appeal. While machine language is required for really complex soundmaking-such as you'd find in Music Studio (Activision) or Music Construction Set (Electronic Arts)-there's quite a bit you can do with Atari BASIC.

You can create simple, constant sounds that give your program atmosphere without slowing it down. The SOUND command is passed to the POKEY chip, a special sound chip in your Atari which also handles the serial I/O bus and the keyboard. The sound you create, say a note or hiss, will play until you turn it off with another SOUND command. Because the sound chip is separate from the main processor, your BASIC program's speed will not be greatly affected by whether the sound is on or off.

Your Atari can produce four sounds at once because it has four independent voices (sound channels). Normally each voice has a range of 256 different frequencies (or tones, notes, pitches). Figure 1 shows how these frequency values correspond to the standard musical scale.

The available frequencies stretch over five octaves. Each voice has 16 different volume (loudness) levels, from a whisper to a roar. Finally, there are eight different levels of distortion to choose from. While your Atari can play pure musical notes, it can also make other sounds, such as a low rumble or a high-speed "engine" noise. The various distortions available can be combined to produce some very interesting noises.

SOUND COMMAND

The simplest way to produce noise on your Atari is the SOUND command, which is used in the following format:

SOUND voice, frequency, distortion, volume

Voice is represented by a number between 0 and 3. Frequency is the pitch of the note you want to play, 0 to 255 as shown in Figure 1. When the frequency number increases, the note gets lower.

Distortion values must be even numbers between 0 and 14. Distortion value 0 is a rumble, 2 and 6 sound like a racing car engine, 4 sounds like heavy machinery or an idling engine, 8 is like a rocket, 10 and 14 are pure musical notes, while 12 sounds like a high-speed engine.

Volume can be between 0 and 15, with 0 being off. If you use more than one voice, try not to let the sum of the volumes exceed 32, or else the sound quality will deteriorate.

Figure 1

POKE YOUR SOUND

You can also use POKEs to control the sound registers directly POKE works much faster than SOUND, so you have more control over your sound effects. Sound registers are memory locations which control the same properties as the SOUND command:

Memory
Location   Function
53760      Frequency of voice 1 (SOUND 0)
53761      Distortion and volume of voice 1
53762      Frequency of voice 2 (SOUND 1)
53763      Distortion and volume of voice 2
53764      Frequency of voice 3 (SOUND 2)
53765      Distortion and volume of voice 3
53766      Frequency of voice 4 (SOUND 3)
53767      Distortion and volume of voice 4

The even-numbered memory locations control the frequency of the sound, which is identical to the second number in the SOUND statement. For example, SOUND 0,100,10,8 is the same as POKE 53760,100.

The odd-numbered memory locations (5 3761,63,65,67) take care of the distortion and volume for each voice, using this formula:

VALUE=16*DISTORTION+VOLUME

Here DISTORTION is the third number in the SOUND statement and VOLUME is the fourth. Therefore the equivalent POKE in our example is 16 * 10 + 8, or 168. So to duplicate the above SOUND command, type POKE 53760,100:POKE 53761,168.

You can turn off a note by placing a zero in either FREQUENCY or the DISTORTION/VOLUME registers.

TWO LISTINGS

Listing 1 is a sound organ. Type in Listing 1, NEWOWN14.BAS, check it with TYPO II and SAVE a copy to disk before you RUN it. The onscreen display will show you which keys should be pressed to play a musical scale.

The program continuously executes its loop, counting and reading keys and keeping track of which voices are available. All the while, the sounds you have fingered are playing. If you want to try different sounds, change the note values in the DATA statements and use other notes from Figure 1.

Listing 2, SOUNDMEN.BAS , gives you a menu from which you can choose a sound effect. Some of these sounds are quite complex and can be astonishingly realistic. Such sounds are achieved by rapidly varying the frequency, distortion, and volume in the SOUND statements. This technique ties up the main 6502 processor chip, bringing other computing pretty much to a halt.


Create simple sounds
that give our program
atmosphere without
slowing it down.


Experiment with varying the numbers in SOUND statements to get complex custom sounds of your own. And congratulations on graduating from the New Owners Column. These lessons should give you a good start in BASIC programming on the 8-bit Atari computers.

(For more details on programming Atari sound, a good sourcebook is De Re Atari. Originally published by Atari, copies of this out-of-print reference guide are often available from Antic mail-order advertisers. -ANTIC ED)

IF YOU'D ENJOY SEEING MORE ARTICLES LIKE THIS ONE, CIRCLE 209 ON THE READER SERVICE CARD.

Listing 1: NEWOWN14.BAS Download

Listing 2: SOUNDMEN.BAS Download