Classic Computer Magazine Archive ANTIC VOL. 1, NO. 4 / OCTOBER 1982

Music With Basic
Two songs and a tutorial for would-be composers

By Jerry White

This tutorial and example program demonstrate one of the 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 370. 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 380-420 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 430 and ending at line 460 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 one 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 450 before GOTO 120 instruction.

Look at line 120. 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 470, Line 470 begins with "IF EXIT" This is the same as saying "IF EXIT <> 0". So IF EXIT = 0, the program falls through to lipe 480 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 450 we hit a true condition and highlight the number 3 on the screen, set PLAY = 10, RESTORE 360, set EXIT = 1, and GOTO 120.

The routine from line 120 through 150 is called the main program loop. We haven't played any notes yet so NP = O and we fall through to line 130. 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 3~~0. Also in line 130 we add 1 to NP.

In line 140, we see if PITCH = 0, and if it is, we GOTO our REST routine which begins at line 170. PITCH = 91 so we GOTO our SOUND routine at line 200.

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 210 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 220 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 210 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 240 where all sounds are turned off, and we can finally go back to where this whole thing started, line 120.

Remember line 120? 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 170 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.

Listing: BASMUS.BAS Download