Atari BASIC Music

Type
Sound
Category
Music tutorial program
Author
Jerry White
Published in
Antic, October/November 1982 (Vol. 1, No. 4), "Music with BASIC"; reprinted in The Best of Antic, Volume 1
Year
1982
Language
Atari BASIC
Requirements
Any Atari 8-bit, 8K RAM, Atari BASIC
Black GRAPHICS 0 menu: ATARI BASIC MUSIC by Jerry White, type 1 for POP GOES THE WEASEL, 2 for TEN LITTLE INDIANS, 3 for PROGRAM EXIT

About the program

Antic's fourth issue was about sound and music, and Jerry White contributed this small program with a long line-by-line explanation for "would-be composers". It plays two tunes, "Pop Goes the Weasel" and "Ten Little Indians", and a short exit fanfare. The point was not the songs but a clean, reusable way to play any melody from DATA. White dated the program 4 May 1982. The disk version (from The Best of Antic) is renumbered, so the line numbers in the original magazine article don't match it, but the logic is the same.

What you need

Any Atari with Atari BASIC.

Typing it in

Plain ASCII, 43 lines. The DATA comes in pairs: a pitch (the SOUND frequency value, with 0 for a rest) and a duration in jiffies (sixtieths of a second). Line 370 sets PLAY=43 and line 380 PLAY=44, the number of notes in each song. If you add or remove notes, change these counts.

How to use it

RUN, then press 1, 2 or 3. No RETURN is needed. After a song the program RUNs itself again. Option 3 plays the exit tune, restores the margin and cursor, and ends.

How it works

  • Keys without RETURN. Lines 370–390 PEEK location 764, which holds the code of the last key pressed (31, 30 and 26 are the keys 1, 2 and 3), and POKE 255 into it to clear it.
  • Timing with an OS timer. Line 130 POKEs the duration into location 540, one of the operating system's countdown timers, which counts down once per jiffy. The loop in lines 140–160 keeps playing until it reaches zero. This makes the tempo independent of how fast BASIC runs.
  • A fuller sound. Each note is played on three channels at once, at PITCH, PITCH+1 and PITCH-1. The slight detuning gives a chorus effect.
  • A built-in decay. The countdown value doubles as the volume (capped at 6), so each note fades over its last few jiffies.

Variations to try

Add your own song as PITCH,DUR pairs after line 290, with another menu line that RESTOREs to it. White noted that BASIC can also manage true four-part harmony with the lyrics on screen. He did exactly that two months later in his Atari Tree Christmas card.

Listing

10 REM ATARI BASIC MUSIC by Jerry White 5/4/82
20 GOTO 310
30 REM MAIN PROGRAM LOOP
40 IF NP=PLAY THEN 420
50 READ PITCH,DUR:NP=NP+1
60 IF PITCH=0 THEN 90
70 GOTO 130
80 REM REST TIME DELAY SUBROUTINE
90 POKE 540,DUR
100 IF PEEK(540)<>0 THEN 100
110 GOTO 40
120 REM PLAY NOTE SUBROUTINE
130 POKE 540,DUR:P1=PITCH+1:P2=PITCH-1
140 DUR=PEEK(540):IF DUR=0 THEN 170
150 IF DUR>6 THEN DUR=6
160 SOUND 0,PITCH,10,DUR:SOUND 1,P1,10,DUR:SOUND 2,P2,10,DUR:GOTO 140
170 SOUND 0,0,0,0:SOUND 1,0,0,0:SOUND 2,0,0,0:GOTO 40
180 REM DATA FOR POP GOES THE WEASEL
190 DATA 121,6,91,6,0,6,91,6,81,6,0,6,81,6,72,6,60,6,72,6,91,6,0,6
200 DATA 121,6,91,6,0,6,91,6,81,6,0,6,81,6,72,18,0,6,91,6,0,6
210 DATA 121,6,91,6,0,6,91,6,81,6,0,6,81,6,72,6,60,6,72,6,91,6,0,18
220 DATA 53,12,0,12,91,12,0,6,68,6,72,18,0,6,91,12
230 REM DATA FOR TEN LITTLE INDIANS
240 DATA 121,18,121,6,121,6,121,18,121,6,121,6,96,18,81,6,81,6,96,6,96,6,121,18
250 DATA 108,18,108,6,108,6,108,18,108,6,108,6,128,18,108,6,108,6,128,6,128,6,162,18
260 DATA 121,6,121,6,121,6,121,6,121,18,121,6,121,6,96,18,81,6,81,6,96,6,96,6,121,18
270 DATA 108,18,108,6,108,6,162,6,162,18,121,48
280 REM DATA FOR EXIT ROUTINE
290 DATA 91,12,0,6,121,6,128,6,121,6,108,24,121,24,0,24,96,24,91,24
300 REM SETUP/DISPLAY/OPTIONS
310 GRAPHICS 0:SETCOLOR 2,RND(0)*16,0:POKE 752,1:POKE 82,5:POKE 201,7:NP=0
320 ? :? :? ,"ATARI BASIC MUSIC"
330 ? :? ," by Jerry White":? :?
340 ? :? "Type 1 for POP GOES THE WEASEL"
350 ? :? "Type 2 for TEN LITTLE INDIANS"
360 ? :? "Type 3 for PROGRAM EXIT";:POKE 764,255
370 IF PEEK(764)=31 THEN POSITION 10,8:? "1";:POKE 764,255:PLAY=43:GOTO 40
380 IF PEEK(764)=30 THEN POSITION 10,10:? "2";:PLAY=44:RESTORE 240:GOTO 40
390 IF PEEK(764)=26 THEN POSITION 10,12:? "3";:POKE 764,255:PLAY=10:RESTORE 290:EXIT=1:GOTO 40
400 GOTO 370
410 REM EXIT/RERUN
420 IF EXIT THEN POKE 82,2:POKE 752,0:? :? :? "BASIC":? "IS";:END
430 RUN

Download

The original program from the Antic disk, to LOAD or ENTER instead of typing it: BASMUS.BAS, BASMUS.BAS.

Credits