Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 9, NO. 7 / JULY 1983 / PAGE 188

Sound tables: fast sound effects from Basic. Christopher Adams.

Having survived a myriad of attacking space creatures, the sounds of dying aliens and exploding laser bases singing in my ears, I returned to Applesoft and resumed programming. Like many aspiring programmers, I dreamed of writing my own action-packed arcade games. But since the only language I knew was Basic, I knew I had a while to go.

Still, I had mastered shape tables and done some interesting graphics routines. Some of my programs could have been convincing except for one missing feature--sound. The best I could manage was a sickening buzz created by repeatedly POKEing -16336 or the familiar boring beep.

Then I discovered machine language. It was so easy to make sounds that I decided to write a small program that would enable virtually any sound effect to be executed through Basic.

You might have seen sound routines that enable you to select the tone and the duration for a single tone. These are fine for games, but they do have drawbacks. First of all they have a limited range of tones. Second, the higher notes have shorter durations. And most important, any string of tones is subject to pauses where Basic processes the next piece of data.

Wouldn't it be convenient I though, if any complete sound could be executed with one command? My answer was Sound Table. It is similar to shape tables in that you must create a table of data, select the sound number, then execute it. The table can consist of as many as of 256 sounds, each consisting of up to 255 tones or pauses. A sound can be anything from a single note to an explosion to the entire "Star Spangled Banner."

To use Sound Table, you must first POKE in the machine language routine in Listing 1 and then BSAVE it. From then on all that is required is to BLOAD it at the beginning of your program and put the table pointer in locations 982-983 (these are just like locations 232-233 for shape tables).

Once the machine language program and a sound table are in, memory location 249 is POKEd with the sound number to be used (the first sound being #0). When the Basic instruction call 768 is encountered, the sound table program executes the sound number currently in location 249.

Sound Table is very efficient, and is therefore useful in machine language programs so I have provided the assembler listing as well as the Basic listing to POKE it into memory. Getting Started

To get Sound Table going, just type in the Basic program in Listing 2 and run it. The program has a simple error-checking routine. If everything is in order, the program will create a simple sound table, and if it works a beep will be emitted.

If it doesn't do this, check the data again.

After successfully running the program, type BSAVE SOUND TABLE, A$300, L$D5. This will create a machine language file on your disk that can be retrieved with BLOAD SOUND TABLE. If you don't have a disk drive, just POKE in all the data at the beginning of any program that uses Sound Table. Making A Table

You can either learn to make tables by hand or you can use the Soundmaker program (Listing 3) to simplify the process. The first thing you must know is what makes up a tone. Each tone consists of three pieces of data: duration (2-255), coarse tone (2-255), and fine tone (2-255). Notice that the parameters for all these are 2-255--a 2 for the duration causes a duration of one unit and a 2 for fine tone causes a frequency of one unit. In other words, the actual unit used = desired unit + 1.

Next we must distinguish between coarse tone and fine tone. As I mentioned earlier, one byte isn't sufficient to designate all the possible tones, so the frequency is derived from coarse tone times fine tone (CT-1 sup.* FT-1). In this program the higher the tone (frequency) the lower the tone sounds. This is because Sound Table makes the tone by making a delay loop; the length is specified by the tone between clicks of the speaker.

Duration is a loop which specifies how many times the speaker is clicked. The longer the time between clicks (frequency), the longer the duration. Sound Table automatically counters this, but there will still be a slight stretcing of duration for very low tones.

To create pauses between notes, make the coarse tone equal to zero (this is the one exception to the 2-255 parameter. In this case, the duration is still duration, but fine tone becomes fine duration. The duration of a pause is much shorter than that of a tone, so make the duration a high number.

Before getting into the table structure it would be best to familiarize yourself with Sound Table by using Soundmaker. Type in Soundmaker and run it. To give you an idea of what tone all these pieces of data create, use the following procedures to make a tone similar to the CTRL-G beep, using the data pieces CD-3, CT-3, FT-50.

The first question asked is where to put the table in memory. Since the table isn't relocatable as are shape tables, it is necessary to decide now. If you have a 48K Apple, the best place to put it is about 36864 ($9000 hex) setting himem to 36863.

The next question is how many sounds you want in this table. This can be any number from 1 to 256. For this example enter 1. You will then enter the create mode.

Choose a length of 1 for the number of tones and pauses in this sound. You will then get the prompt CD,CT,FT (duration, coarse tone, fine tone).

Enter 3,3,50. Try listening to it; it should sound like the CTRL-G beep. Next enter the editing mode. Fiddle around by changing the data. Adjusting all CD will increase the length of all the tones in the sound you are working on (in this case only one tone). A similar action is taken when CT and FT are adjusted. Be careful that adjustments do not push any of the data beyond the 2 to 255 limits.

When you have made your adjustments, return to the main menu and select option 2. Re-create the sound. This time choose a longer length and try different combinations of tones and pauses--you might even try writing a short tune. Once you have satisfied your curiosity choose Next Sound. The program will finish creating the table and ask you what you want to name it. Even if you don't want to keep this table give it a name anyway to see what comes next (you can delete it later). Soundmaker will give you all the necessary information for using the shape table in your own program.

When using Sound Table you must always load the program itself and an actual table and POKE in the table pointers at locations 982 and 983 (Soundmaker will tell you what values to use). Table Structure

The first section of the table holds the sound pointers. The first two bytes are the absolute address of sound 1 (remember that if location 249 equals 0, then sound 1 is executed; if it equals 1 then sound 2 is executed). The next two bytes point to sound 2 and so on. Note that there is no place to designate the number of sounds in the table, so an attempt to execute a non-existent sound will cause random effects.

The absolute address goes in the order of least significant byte followed by the most significant byte. To find the decimal equivalent MSB=int (address/256) and LSB=address-(MSB.sup.*.256).

The data tables for the sounds do not have to appear in any sort of order. The datafield for each sound starts with a byte specifying the number of tones in the sound (for this discussion this byte will become the variable L).

The next L number of bytes contain all the durations. That is, the first byte after L is the duration for the first tone, the second byte is the duration of the second tone, and so on. Immediately after the last duration byte comes the first coarse tone byte, followed by the CT for the second tone. After the last coarse tone byte comes the first fine tone byte, followed by the second, etc.

Table 1 details the components of the tone CD-3,CT-2,FT-70 followed by tone CD-2,CT-3,FT-100. The table starts at 36864. If there were two sounds the pointer for the second sound would have been at 36866 and sound 1 would have started at 36868. Summary

Now that Sound Table is up and running, you can create a table with 256 sounds each of which can have 255 parts. The sound number can be selected by POKEing 249 and executed by call 768. The program is located starting at 768 ($300 hex), and the actual table is pointed ot by locations 982-983 (LSB-MSB).