Classic Computer Magazine Archive BEST OF ANTIC VOLUME 1

Music with BASIC

Two songs and a tutorial for would-be composers.

This tutorial and example program demonstrates one ofthe many ways of playing music using Atari BASIC. Those of you with no knowledge of music may simply type in the program and follow the instructions on the screen. If you have some knowledge of music, and you'd like further information on how this program works, read on.

The program begins with a GOTO 310. This bypasses the main program loop, subroutines, and song DATA, and brings us to our setup and screen display. Here we specify GRAPHICS 0, set the background color at random, turn off the cursor, set the left margin at 5, set the print tab width at 7, and NP=0. The numeric variable NP will be used to count the Notes Played. Lines 320-360 display our program description, author name, and user options. POKE 764,255 tells the computer to ignore the last key pressed.

The routine beginning at line 370 and ending at line 390 waits for the user to press a normal video 1, 2, or 3. Nothing will happen until one of these keys is pressed. The checking is done by PEEKing at location 764 until it contains a 31, 30, or 26. These are the internal keycodes for 1, 2, and 3. By checking the last key pressed, we eliminate the need to press the [RETURN] key.

Once we have a valid key, we position the cursor at the appropriate option number on the screen, and print that number using inverse video. The numeric variable PLAY is used to store the number of notes we are about to play.

If option 1 was selected, we do not have to use a RESTORE command since the DATA for this song preceeds any other DATA. If either of the other options has been chosen, we use the RESTORE command to point to the line number where the appropriate DATA begins.

If the number 3 key was pressed, we also must set a flag to indicate a special condition. Since this program reruns itself when a song is over, we set the variable EXIT = 1 in line 390 before GOTO 40 instruction.

Look at line 40. In English, it says that if the number of Notes Played is equal to the number of notes we wanted to PLAY, then go to line 420. Line 420 begins with "IF EXIT." This is the same as saying "IF EXIT < > 0". So IF EXIT=0, the program falls through to line 430 where we have a RUN command. If EXIT< > 0 then we reset the left margin, turn the cursor back on, tell the user that BASIC has control, and END the program.

Now that we know how the program starts and how it ends, let's see what happens in between. Let's assume you have chosen option number 3. As you pressed the number 3 key, an ASCII 26 was automatically stored in location 764. At line 390 we hit a true condition and highlight the number 3 on the screen, set PLAY=10, RESTORE 290, set EXIT=1, and GOTO 40.

The routine from line 40 through 170 is called the main program loop. We haven't played any notes yet so NP=0 and we fall through to line 50. Here we read two bytes of DATA. This will result in the variable PITCH being set to 91 and DUR being set to 12. Remember, we are reading the DATA that begins in line 290. Also in line 50 we add 1 to NP.

In line 60, we see if PITCH =0, and if it is, we GOTO our REST routine which begins at line 90. PITCH=91 so we GOTO our SOUND routine at line 130.

We will POKE the value of DUR into a countdown timer at RAM location 540. Countdown timers count backwards at the rate of 60 per second until zero is reached. In other words, when we POKE 540,DUR, since DUR = 12, exactly 12/60 of a second later, the countdown timer will reach zero. In that same line we calculate the pitches we will use in SOUND registers 1 and 2, and store the value of PITCH + 1 in P1 and PITCH-1 in P2.

At line 140 we turn the tables and set DUR=PEEK(540), and check to see if it is equal to zero. At this point it isn't zero yet, so we continue on to line 150 and see if DUR > 6. Six will be our maximum volume of each of three SOUND commands. In any case, we continue on to execute three SOUND commands, then go back to line 140 and check the value in our countdown timer again. We stay in this loop until we find that our countdown timer has reached zero.

When PEEK(540)=0, we GOTO line 170 where all sounds are turned off, and we can finally go back to where this whole thing started, line 40.

Remember line 40? That's the main program loop. We have played one note and have nine to go. But what if the PITCH is a 0? When we want no sound for a period of time (a REST), we enter a zero as the pitch, and use the routine beginning at line 90 to rest for the period of time specified by DUR. By the way, 60ths of a second are also known as "jiffies."

By using DUR as the volume value in the SOUND commands, we get a slight decay or decreasing volume at the end of each note. By using two additional SOUND channels, and setting their frequency levels slightly higher and lower than the desired pitch, we achieve a richer, fuller sound.

This program demonstrates only one method of playing music on your computer. BASIC can be used to play true four-part harmony and even display the Iyrics of your songs on the screen at the same time. This is demonstrated by Swifty Software's Singalong Sound & Music Tutorial package.

Atari's Music Composer provides another way to play music and displays musical notes on your screen. Unfortunately, you can't put the Music Composer Cartridge and BASIC in at the same time. But I found a way around that problem too.

P.D.I.'s Music Box will convert your Music Composer files and play them for you using vertical blank assembler subroutine. This is done while the BASIC cartridge is installed. The best part is that once the music begins, BASIC is at your disposal. You can even write a BASIC program while the music continues to play.

The possibilities provided by your computer's audio channels are almost limitless. Take advantage of this and let us know what you come up with.

by Jerry White

Jerry White lives in Levitown, New York, and is a prolific writer of BASIC and assembly language programs for the ATARI. He has many commercial products on the market, including Poker SAM and Chatterbee, from Don't Ask Software, that use the intriguing voice-synthesis-on a-disk known as Software Automatic Mouth (or S.A.M. for short).

Listing: BASMUS.BAS Download