Classic Computer Magazine Archive COMPUTE! ISSUE 41 / OCTOBER 1983 / PAGE 258

Apple Sounds-From Beeps To Music

Part 1

Baine Mathieu

In this first of a two-part series, the author takes us from the simplest possible sound on the Apple to musical notes. Several useful demonstration programs are included.

Since I first acquired an Apple II + about a year and a half ago, I have been fascinated by the strange noises I often hear. In this first of two articles I hope to save you all the trouble I went through in learning how to use APPLE sounds. Readers who already understand how to use CTRL-G and –16336 may want to skip the next section and go on to "Paddle Sounds."

Beeps And Clicks

Before you read this section, you should enter Program 1 on your computer and save it. Now run the program. If you entered it correctly, you should see SOUND #1 at the top, a line from the program, and a small menu.

Probably the first sound that you ever heard from your Apple's speaker was the bell sound. You can reproduce this in immediate mode by holding down the control key (labeled CTRL) and pressing the G key. In SOUND #1, you see that in line 30 a CHR$ (7) is being printed – 7 is the numeric code for CTRL-G. If you are in Integer BASIC, you will have to use the format shown in line 35. In this line you'll see a PRINT with two quotes. Inside these quotes is a CTRL-G. The REM statement in line 37 shows how to type line 35. As you can see, control characters don't show up in a line listing or when you type them. An interesting side effect is that when you LIST your program, you will hear all the bell sounds in your program that are printed using the method in line 35.

In Program 1, the computer waits for you to hit a key. If you hit R, it will repeat any sound that might be produced by the above program lines. If you hit C, you will proceed to the next sound in Program 1. Any other key (except RESET) will cause no change.

Clicking - 16336

Now hit C to go on to SOUND #2. In this program a simple FOR/NEXT loop is set up to beep the Apple's speaker ten times. Note the semicolon at the end of line 80; this prevents the screen from scrolling. If I hadn't used the semicolon, as each CTRL-G was printed the imaginary cursor would move down the screen until the screen started to scroll upward, which is, in most cases, undesirable.

Looking at SOUND #3, you will notice the number - 16336, which is the memory address of the Apple's speaker. Every time this address is accessed, the Apple gives a little push on its speaker, creating a small click. PEEKing, as I have done in line 130, is just one simple way of accessing this address. If you missed the sound the first time, press R to hear it again.

SOUND #4 includes another simple loop that will PEEK the speaker's memory address 100 times. Instead of typing - 16336 every time we wanted to use it, we assigned - 16336 to the variable NO (for NOise). You may use any variable you wish, of course.

In SOUND #5 you'll notice line 250, which strings a lot of "clicks" together. This produces a longer noise than in SOUND #3 and a higher-pitched noise than in SOUND #4. As a rule, the closer your PEEKs, the higher-pitched your noise is going to be. In line 250 you will notice that we PEEKed - 16336 a total of 15 times, a purely arbitrary number.

Finally, SOUND #6 demonstrates most of what we've learned about clicks. It uses a FOR/NEXT loop to cause line 320 to repeat 100 times. Line 320 has an assortment of minus and plus signs to show that it rarely makes a difference what you do to this location, as long as you access it.

Now on to something a little more exciting and complicated.

Paddle Sounds

Program 2 requires paddles or joystick. It is a simple BASIC program which reads a byte from the DATA statement and POKEs it into memory locations 768 ($300) to 786 ($312). The routine begins by CALLing 768. If you entered the program correctly, you should hear a fairly high-pitched whine, and as you move the paddles or joystick, this whine will change in pitch. You may leave the program by pressing RESET or CTRL-RESET, depending on your model.

Program 3 is the source code for this little machine language routine. Here is a quick explanation of the routine:

  1. Put the paddle number in the X register.
  2. Jump to the PREAD subroutine (see Apple II Reference Manual). PREAD acts as a delay, dependent on the paddle setting.
  3. Tweak the speaker by accessing - 16336 ($C030).
  4. Repeat for next paddle.
  5. Jump to beginning.

Since the pitch of the noise depends on how close together the tweaks are, the lower the paddle setting, the higher the pitch of the noise.

Making Music

Now we'll look at a program that lets you produce notes (and thus music) on your Apple. Of course, there are some limitations. For example, you won't be playing Beethoven's Fifth Symphony in five-part harmony with snare drum accompaniment. If you want that, many peripheral boards are available for the Apple which do amazing things. However, you can do quite a lot with the hardware already in your Apple.

Program 4 is a simple BASIC program that POKEs in a machine language subroutine, sets up a few parameters, and CALLs the subroutine. The program continues running until a key is pressed. Try running it. If you've never heard notes from your Apple, you will be quite pleased.

After the program has POKEd in the subroutine, it POKEs a random number (the pitch) into location 768 ($300) and POKEs a random number (the duration) into 769 ($301). The maximum value that can be POKEd into these locations is 255.

Program 5 is the source code for the "Note Producer" program that is POKEd into memory in Program 4. In essence, the program works much like "Paddle Sounds." The main difference is that instead of the paddles controlling the pitch of the sound, locations 768 and 769 control the pitch and duration of the tone. The source code contains comments that should help you understand what is happening.

As you can see by now, whenever you want a sound routine, you're going to have to access location -16336 ($C030). Try experimenting with Program 5 by POKEing in your own note values and hearing the results.

Next month, we'll look at a program called "Apple Music Writer," which will enable you to edit and play your own song. Until then, experiment with the programs here, and you're sure to come up with some surprising results.