16-Bit Sound Power

Type
Sound
Category
16-bit POKEY sound demo
Author
Jerry White
Published in
Antic, September 1985 (Vol. 4, No. 5), Power Programming: "16 Bit Sound Power"
Year
1985
Language
Atari BASIC (with machine-language subroutine)
Requirements
Any Atari 8-bit computer, any memory size, Atari BASIC; disk or cassette
GRAPHICS 2 screen reading 16-BIT SOUND BY JERRY WHITE, with the current octave, pitch and note name

About the program

Atari BASIC's SOUND statement gives each of POKEY's four voices an 8-bit pitch value. With pure tones (distortion 10) that covers only about four octaves. The lowest note is the B you get from SOUND 0,255,10,8, and the high notes are noticeably out of tune. In this Antic "Power Programming" article, Jerry White (a contributing editor and founder of PAPIE, the Professional Atari Programmers Information Exchange) showed the way out that professional programmers used. Join two voices into one 16-bit voice and clock it at 1.79 MHz. You can then have only two notes at once, but they are accurately tuned and reach seven octaves, with much deeper bass.

Typing it in

The listing is plain Atari BASIC with no special characters. Antic printed TYPO II checksums beside each line of the magazine listing, and they are not part of the program. Lines 20010–20040 are the machine-language routine, so check them carefully. Lines 30010–30070 hold 84 frequency values, from the highest note of octave 1 down to the lowest of octave 7.

How to use it

RUN it and listen. The program climbs through all 84 notes from the deepest bass upwards, showing the octave, pitch number and note name for each. It then plays every note again together with the note an octave below. It finishes with a short tune and returns to GRAPHICS 0.

To use 16-bit sound in your own programs, keep lines 120–160 and the DATA, then call X=USR(ADR(S16$),F,V) for one voice or X=USR(ADR(S16$),F1,V1,F2,V2) for two. Use a volume of 0 to switch a voice off. Middle C is FREQ(4,12), which is 3414.

How it works

  • Line 150 does SOUND 0,0,0,0 (as White noted, this and the POKE must come before the first USR call) and writes 120 to AUDCTL (53768). That joins voices 1+2 and 3+4 into 16-bit pairs and clocks voices 1 and 3 at 1.79 MHz.
  • The USR routine pulls its arguments off the stack. The two bytes of each frequency go into the paired AUDF registers (53760/53762 and 53764/53766). The volume is masked to 0–15 and combined with pure-tone distortion (160) in AUDC2 or AUDC4.
  • Timing uses the OS countdown timer at location 540, which decreases once every jiffy (1/60 second). The delay routine at line 480 pokes a value there and waits for it to reach zero. In the scales, the same timer doubles as a volume envelope: line 310 reads it back as the volume, so each note fades from 8 to silence.

Listing

10 REM SIXTEEN-BIT SOUND DEMO
20 REM BY JERRY WHITE
30 REM (c) 1985, ANTIC PUBLISHING
100 RESTORE
110 GRAPHICS 2+16:? #6:? #6;"    16-BIT SOUND":? #6:? #6;"   BY JERRY WHITE"
120 DIM S16$(56):REM HOLDS ML SUBROUTINE
130 FOR ME=1 TO 56:READ BYTE:S16$(ME,ME)=CHR$(BYTE):NEXT ME
150 SOUND 0,0,0,0:POKE 53768,120
160 DIM N$(24),FREQ(7,12):N$="B A#A G#G F#F E D#D C#C ":GOTO 220
170 REM DISPLAY SUBROUTINE
180 IF BOTH THEN POSITION 5,5:? #6;"OCTAVES: ";OCTAVE;"&";OCTAVE+1;:GOTO 190
185 POSITION 5,5:? #6;"OCTAVE: ";OCTAVE;
190 POSITION 5,7:? #6;"PITCH: ";PITCH;" ";
200 SP=PITCH*2-1:POSITION 5,9:? #6;"NOTE: ";N$(SP,SP+1):RETURN
210 REM CREATE FREQUENCY ARRAY
220 FOR OCTAVE=7 TO 1 STEP -1:FOR PITCH=12 TO 1 STEP -1
230 READ FREQ:FREQ(OCTAVE,PITCH)=FREQ:NEXT PITCH:NEXT OCTAVE
240 BOTH=0:LOWOCT=7:WAIT=10
250 REM MAIN SOUND LOOPS
260 FOR OCTAVE=LOWOCT TO 1 STEP -1:FOR PITCH=12 TO 1 STEP -1
270 SETCOLOR 4,PITCH,0:GOSUB 180:VOL=8:POKE 540,VOL
280 IF  NOT BOTH THEN JW=USR(ADR(S16$),FREQ(OCTAVE,PITCH),VOL):GOTO 300
290 JW=USR(ADR(S16$),FREQ(OCTAVE,PITCH),VOL,FREQ(OCTAVE+1,PITCH),VOL)
300 IF  NOT VOL THEN 320
310 VOL=PEEK(540):GOTO 280
320 GOSUB 480
330 NEXT PITCH:NEXT OCTAVE
340 IF  NOT BOTH THEN BOTH=1:LOWOCT=6:GOTO 260
350 FOR ME=5 TO 9 STEP 2:POSITION 5,ME:? #6;"            ";:NEXT ME
370 VOL=8:OCTAVE=5:PITCH=7:HOLD=16:WAIT=8:GOSUB 530
380 PITCH=12:HOLD=4:WAIT=0:GOSUB 530
390 OCTAVE=6:PITCH=1:HOLD=4:WAIT=0:GOSUB 530
400 OCTAVE=5:PITCH=12:HOLD=4:WAIT=0:GOSUB 530
410 PITCH=10:HOLD=16:WAIT=8:GOSUB 530
420 PITCH=12:HOLD=16:WAIT=32:GOSUB 530
430 PITCH=8:HOLD=16:WAIT=8:GOSUB 530
440 PITCH=7:HOLD=32:WAIT=0:GOSUB 530
450 GRAPHICS 0:END
460 REM TIME DELAY
480 POKE 540,WAIT
490 IF PEEK(540) THEN 490
500 RETURN
510 REM SUBROUTINE TO CONTROL
520 REM TWO 16-BIT SOUNDS
530 POKE 540,HOLD:X=USR(ADR(S16$),FREQ(OCTAVE,PITCH),VOL,FREQ(OCTAVE+1,PITCH),VOL)
540 IF PEEK(540) THEN 540
550 X=USR(ADR(S16$),FREQ(OCTAVE,PITCH),0,FREQ(OCTAVE+1,PITCH),0)
560 GOTO 480
20000 REM DATA FOR M-L SUBROUTINE
20010 DATA 104,201,2,240,33,201,4,240,12,170,224,0,240,41
20020 DATA 202,104,104,240,247,208,245,104,141,2,210,104,141,0
20030 DATA 210,104,104,41,15,9,160,141,3,210,104,141,6,210
20040 DATA 104,141,4,210,104,104,41,15,9,160,141,7,210,96
30000 REM FREQUENCIES FOR FREQ ARRAY
30010 DATA 27357,25821,24372,23003,21712,20493,19342,18256,17231,16264,15351,14489
30020 DATA 13675,12907,12182,11498,10852,10243,9668,9125,8612,8128,7626,7241
30030 DATA 6834,6450,6088,5746,5423,5118,4830,4559,4303,4061,3832,3617
30040 DATA 3414,3222,3040,2869,2708,2555,2412,2276,2148,2027,1913,1805
30050 DATA 1703,1607,1517,1431,1350,1274,1202,1134,1070,1010,953,899
30060 DATA 848,800,755,712,672,634,598,564,532,501,473,446
30070 DATA 421,397,374,353,332,313,295,278,262,247,233,219

Download

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

Credits