Classic Computer Magazine Archive COMPUTE! ISSUE 39 / AUGUST 1983 / PAGE 204

Musical Atari Keyboard

Paul N. Havey

This program makes your Atari into a musical instrument. The sound of bells, piano, organ, etc. – it's all in your computer's keyboard.

"Music Keyboard 3" makes the Atari typewriter keyboard a music keyboard.

You can play the keyboard much like playing an organ. Strike a key, the note plays; release the key, the note stops. You can also change the decay of a note to sound like a piano, organ, or bells, and you can change registers to bass or treble with the SHIFT key.

Features

  1. Variable decay of a note
  2. Preset attack and release
  3. Five octave range (Cl to C6)
  4. Two-tier keyboard
  5. Keyboard display on the screen
  6. Monotonic (one note at a time)
  7. Fast action

When you run this program, a picture of the Atari keyboard appears on the screen. The keys that are outlined only are not usable as notes. The white and black keys represent the same keys on a keyboard instrument.

Here's how to use the program:

• Starting-up

  1. Insert the Atari BASIC cartridge.
  2. Load or type into RAM memory Music Keyboard.
  3. Type RUN, then press RETURN.
  4. When the prompt "INPUT DECAY (0-1)" appears in the lower left section of the screen, type in a decimal number between zero and one (example: 0.89) and then press the RETURN key.
  5. When the word "PLAY" appears, begin playing.

• Changing the decay of a note after starting up:

  1. Press the space bar.
  2. Press the BREAK key.
  3. Type GOTO 1000, then press the RETURN key.
  4. When "INPUT DECAY (0-1)" appears, type in the decay value, then press RETURN.
  5. When the word "PLAY" appears, begin playing.

• Changing registers

  1. Press the SHIFT key.

Fast Keyboard Action

The Serial Port Control register, SKCTL, changes whenever a key is pressed or released. By reading SKCTL and using the value it contains as an expression in a GOTO statement, the program can decide what to do. SKCTL detects one of four possible keyboard conditions: the SHIFT and one of the character keys pressed together; the SHIFT key pressed by itself; a character key pressed by itself; or no key pressed at all.

Here are a few tricks used to get fast keyboard action. First, the value in SKCTL is used in the GOTO statements to direct the flow of control. Second, placing the logic for note playing at the beginning of the program increases speed since a GOTO target is found by searching a program from beginning to end. Third, removing the REM statements at the beginning leaves fewer statements to search to find the referenced line number. Fourth, using GRAPHICS 2 increases speed over GRAPHICS 0 by 20 percent. The display exists for reasons other than just showing the keyboard. Fifth, converting audio frequency codes to notes on the keyboard by table look-up avoids time-consuming computation. The keyboard code is used as an index to an array containing the frequency for that note. Sixth, POKEs are used instead of the SOUND statement.

Program Description

This program has three major sections. Lines 243-256 will cause a note to play when you press a key. Table 3 lists the SKCTL keyboard condition codes. Lines 400-1530 set up the display, the redefined characters, and the variables for the play section. Lines 1560-4000 contain data for the note and character tables.

Line 240 directs the program to the setup section. After setup is complete, control goes to the play section.

Lines 243-244 contain the action the program takes with both the SHIFT and a character key pressed. A note decays while the register changes. The register cannot change again until the SHIFT key is released. Lines 247-248 are accessed only if the SHIFT key alone is pressed. A register change – from bass to treble or from treble to bass – occurs, but no decay or release action is necessary.

Lines 251-253 direct the program to play a note if a character key only is pressed. The note does not change unless it is different from the last note played. The register changes from bass to treble or vice versa when a key on the keyboard is first pressed. The note decays as long as the same key is held down. Lines 255-256 take effect if no key is pressed. The sound stops and remains that way until another key is pressed.

The setup logic begins on line 520. Program variables are initialized on lines 520-530. Lines 540-570 select the screen mode and colors and print the title to the screen. Lines 600-630 transfer the treble and bass register notes into an array.

The table on page 58 of the Atari BASIC Reference Manual gives the hardware frequency codes with musical note values for the treble register. The bass register table, not found in any Atari manual, is in Table 4 of this article.

Lines 710-750 read the redefined character set data and place the entire character set in a new location. Lines 760-820 display the redefined characters as a picture of the keyboard. Table 5 gives the color factor to add to the character code. Lines 1000-1020 prompt the user to input the decay value and play. Lines 1520-1530 wait until no key is pressed to start the play logic.

Table 1: Program Variables*
Name Description
A temporary data
CHR character number plus color code
DECAY user option value
I temporary index
L current loudness
LOUD maximum loudness
NP new pitch table index
P current pitch table index
R Audio Control value (AUDCTL)
REG pitch table register index
REP number of character repeats
S prior key pressed code
T Audio Tone value
X current character position – horizontal
XREF left-most keyboard position
Y current character position – vertical
YREF upper-most keyboard position
*Also see Table 2
Table 2: Hardware And O/S (PEEK & POKE) Descriptions
Name* Address Description
AUDF1 53760 Audio Channel 1 Freq.
AUDC1 53761 Audio Channel 1 Control
AUDCTL 53768 Audio Control
KBCODE 53769 Keyboard Code
SKCTL 53775 Serial Port Control
RAMTOP 106 Size defined by power ON
CRSINH 752 Cursor Inhibit (0 = Cursor On)
CHBAS 756 Character Base Register
CHRORG 57344 Character Set
*Also see the Atari Operating System User's Manual
Table 3: Serial Port Control Keyboard Codes
Keys Pressed Code
Character* 251
Shift 247
Shift/Character 243
No Key 255
*Excludes the BREAK, SHIFT, and CTRL keys.

Musical Atari Keyboard

240 GOTO 500
243 POKE 53761, T + L : L = L * DECAY * (L>0.5) : IF S<3 THEN R = R = 0
244 S = 4 : GOTO PEEK(53775)
247 POKE 53761, T + L : L = L * 0.5 * (L>0.5) : IF S<3 THEN R = R = 0
248 S = 3 : GOTO PEEK (53775)
251 NP = PEEK(53769) : IF NP-P OR S-2 THEN P = NP : POKE 53768,
        R : REG = 64 * R : L= LOUD : POKE 53760, P(REG + P)
252 POKE 53761, T + L : L = L * DECAY * (L>0.5)
253 S = 2 : GOTO PEEK(53775)
255 POKE 53761, T + L : L = L * 0.5 * (L>0.5)
256 S = 1 : GOTO PEEK(53775)
400 REM  *** SET-UP SECTION ***
500 REM REDEF. CHARS. - DISPLAY
520 LOUD = 15 : T = 160 : SOUND 0, 0, 0, 0
530 REG = 0 : R = 0
540 GRAPHICS 2 : SETCOLOR 1, 0, 12
550 SETCOLOR 2, 15, 6 : SETCOLOR 3, 0, 4
560 SETCOLOR 4, 15, 6 : SETCOLOR 0, 0, 0
570 ? #6;" MUSIC KEYBOARD  2.0" : POKE 752, 1 : ? "PLEASE WAIT"
600 DIM P(255)
610 FOR I = 0 TO 127
620 READ P : P(I) = P : P(1 + 128) = P
630 NEXT I
710 CHSET = (PEEK(106) - 8) * 256
720 CHORG = 57344
730 FOR I = 0 TO 511 : POKE CHSET + I, PEEK (CHORG + I) : NEXT I
740 FOR I = 8 TO 11 * 8 + 7 : READ A : POKE CHSET + I, A : NEXT I
750 POKE 756, CHSET/256 : XREF = 1 : YREF = 2760 FOR Y = 1 TO 5 : X = 1
770 READ CHR, REP : IF CHR + REP = 0 THEN 820
780 COLOR CHR
790 PLOT XREF + X, YREF + Y : X = X + 1
800 REP = REP - 1 : IF REP THEN 790
810 GOTO 770
820 ? #6 : NEXT Y
1000 POSITION 13, 9:? #6; "{4 SPACES}"
1010 POKE 752, 0:? "{CLEAR}INPUT DECAY [0-1]" : INPUT DECAY : POKE 752, 1 : ?
1020 POSITION 13, 9:? #6; "PLAY"
1520 IF PEEK(53775)-255 THEN 1520
1530 GOTO 255
1560 REM TREBLE REGISTER DATA
1600 DATA 114,136,102,0,0,0,0,85
1610 DATA 35,0,31,45,0,40,30,0
1620 DATA 182,0,193,0,0,162,217,243
1630 DATA 0,0,68,50,0,57,76,85
1640 DATA 121,0,108,144,0,128,96,91
1650 DATA 60,0,64,47,91,53,72,81
1660 DATA 37,0,33,0,0,42,0,0
1670 DATA 0,153,204,0,0,172,230,0
1674 REM BASS REGISTER DATA
1680 DATA 112,134,100,0,0,0,0,84
1690 DATA 35,0,31,44,0,39,29,0
1700 DATA 179,0,190,0,0,159,213,239
1710 DATA 0,0,66,49,0,56,75,84
1720 DATA 119,0,106,142,0,126,94,89
1730 DATA 59,0,63,47,89,52,70,79
1740 DATA 37,0,33,0,0,41,0,0
1750 DATA 0,150,201,0,0,169,226,0
1790 REM NEW CHARACTERS
1800 DATA 0,60,66,66,66,66,60,0
1810 DATA 0,3,4,4,4,4,3,0
1820 DATA 0,195,36,36,36,36,195,0
1830 DATA 0,60,126,126,126,126,60,0
1840 DATA 0,3,7,7,7,7,3,0
1850 DATA 0,195,231,231,231,231,195,0
1860 DATA 0,255,255,255,255,255,255,0
1870 DATA 0,252,2,2,2,2,252,0
1880 DATA 0,252,254,254,254,254,252,0
1890 DATA 0,63,127,127,127,127,63,0
1900 DATA 0,195,228,228,228,228,195,0
2000 REM CHARACTER DISPLAY DATA
2020 DATA 129,1,36,3,129,1,36,2
2030 DATA 129,1,36,3,129,4,0,0
2040 DATA 10,1,6,11,11,1,3,1
2050 DATA 8,1,0,0
2060 DATA 37,1,41,1,129,1,36,2
2070 DATA 129,1,36,3,129,1,36,2
2080 DATA 129,1,36,1,129,1,0,0
2090 DATA 5,1,7,1,6,12,9,1,0,0
3000 DATA 160,3,138,1,135,7,137,1
3010 DATA 160,3,0,0
4000 END
Table 4: Bass Register Pitch Codes (TONE = 10 AUDCTL = 1)
Pitch AUDFX Pitch AUDFX
C4 29 F2 89
B3 31 E2 94
(Bb3 or A#3) 33 (Eb2 or D#2) 100
A3 35 D2 106
(Ab3 or G#3) 37 (Db2 or C#2) 112
G3 39 C2 119
(Gb3 or F#3) 41 B1 126
F3 44 (Bbl or A#1) 134
E3 47 Al 142
(Eb3 or D#3) 49 (Ab1 or G#1) 150
D3 52 G1 159
(Db3 or C#3) 56 (Gb1 or F#1) 169
C3 59 Fl 179
B2 63 E1 190
(Bb2 or A#2) 66 (Eb1 or D#1) 201
A2 70 D1 213
(Ab2 or G#2) 75 (Db1 or C#1) 226
G2 79 C1 239
(Gb2 or F#2) 84 B0 253
Table 5: Character Color Codes
Color Code
Black 32
White 0
Gray 128
Orange* 160
*Background
Notes:
1. Redefined characters have Atari internal code numbers one to eleven inclusive.
2. Adding color code above to internal code displays characters in that color.
3. For more information, see the Atari BASIC Reference Manual, chapter nine.