Sound Equivalence

Type
Sound
Category
Programming technique demo: string pointed at the POKEY sound registers
Author
Doug White
Published in
Antic, February 1989 (Vol. 7, No. 10), "Equivalence"
Year
1989
Language
Atari BASIC
Requirements
Any Atari 8-bit, Atari BASIC
Text screen of the Sound Equivalence demo: messages about filling CH$ with sound control data, two iterations with the SOUND command, then NOW USING A POINTER STRING and the first pointer-string iteration

About the program

Doug White, a 1200XL owner from Arlington, Texas, who used his Atari to help design and test loudspeakers, wrote "Equivalence" for the February 1989 Antic. In this "Programming Power" article he showed intermediate programmers how to make Atari BASIC treat any block of memory as a string. String assignments run at machine-code speed inside the interpreter, while numbers, SOUND and POKE all go through slow floating-point conversions. Two listings came with it. Listing 2, EQUIV2.BAS, overlays a string on a numeric array to speed up disk I/O. This one, Listing 1, demonstrates the trick with sound.

The program fills a 256-byte string CH$ with pairs of frequency and distortion/volume bytes. First it plays them through all four voices with ordinary SOUND statements. Then it points an 8-byte string S$ at the eight POKEY audio registers (53760–53767), so that S$=CH$(I,I+7) sets every voice in one go. Run it and listen to the difference in speed: the second part runs ten passes to the first part's two.

Typing it in

There are no special characters apart from {CLEAR} in line 330 (ESC, then SHIFT-CLEAR). What matters is variable order. Line 120 assumes S$ is variable number 0, the first name in BASIC's variable table, which is why DIM S$(8) is the first statement. Type NEW before you type or ENTER the program, so that no leftover variable names come first. White told TYPO II users to remove the checker before running, since its own variables would otherwise be first in the table.

How it works

Atari BASIC keeps an 8-byte entry for every variable in the variable value table, whose address is in locations 134–135. For a string the entry holds:

Bytes Meaning
1 type (128/129 = string)
2 variable number
3–4 offset of the data from the start of the string/array area (address in 140–141)
5–6 current length
7–8 DIMensioned length

The data's address is not stored directly, only its offset from the string/array area. Lines 180–250 therefore subtract that area's start (STARP) from 53760 and poke the result into bytes 3 and 4 of S$'s entry. From then on BASIC believes S$ lives in POKEY. S$(1,1) is AUDF1, S$(2,2) is AUDC1, and so on (the REMs in lines 280–310 list them). The assignment in line 670 copies eight bytes straight into the hardware.

Each control byte in CH$ is 160 plus a volume (line 380): 160 means pure-tone distortion 10 in the high nibble and volume in the low nibble, exactly the format of the AUDC registers. The first half of the program has to split those bytes apart again with INT(X/16) for SOUND, which shows how much work the direct method saves.

Gotchas

  • Only write to the string. The same addresses read back as POKEY's read registers (paddle values and so on), so PRINT S$ shows nonsense.
  • A CLR resets the table and breaks the link. LIST and ENTER can change the variable order; SAVE/LOAD preserve it.
  • Pointing a string at the wrong place can overwrite anything. SAVE first.

Variations to try

White suggested pointing a string the size of the screen at screen memory for instant full-screen updates. In GRAPHICS 0 that's 960 bytes starting at PEEK(88)+256*PEEK(89). You can also fill CH$ with your own frequency and volume patterns.

Listing

10 REM SOUND EQUIVALENCE
20 REM BY DOUG WHITE
30 REM (c)1988, ANTIC PUBLISHING
40 DIM S$(8)
50 DIM V$(20)
60 DIM CH$(256)
70 REM
80 VVTST=PEEK(134)+256*PEEK(135)
90 ?
100 STARP=PEEK(140)+256*PEEK(141)
110 REM
120 SVNUM=0:REM S$ IS VARIABLE # 0
130 REM IN THE VARIABLE NAME TABLE
140 REM
150 REM CALCULATE OFFSET OF
160 REM FREQ. OF AUDIO CH.#0
170 REM
180 FREQ=53760
190 OFFSET=FREQ-STARP
200 AOFFSET2=INT(OFFSET/256)
210 AOFFSET1=OFFSET-256*AOFFSET2
220 REM
230 VVLOC=VVTST+(SVNUM)*8
240 POKE VVLOC+2,AOFFSET1
250 POKE VVLOC+3,AOFFSET2
260 REM
270 REM AFTER THE OFFSET CHANGE
280 REM S$(1,1)=AUDF1  S$(2,2)=AUDC1
290 REM S$(3,3)=AUDF2  S$(4,4)=AUDC2
300 REM S$(5,5)=AUDF3  S$(6,6)=AUDC3
310 REM S$(7,7)=AUDF4  S$(8,8)=AUDC4
320 REM
330 ? "{CLEAR}":? "      SOUND EQUIVALENCE DEMO"
340 ? :? "FILLING CH$ WITH SOUND CONTROL DATA"
350 FOR I=1 TO 255 STEP 2
360 CH$(I,I)=CHR$(255-I)
370 V=V-0.33:IF V<1 THEN V=12
380 CH$(I+1,I+1)=CHR$(160+INT(V))
390 NEXT I
400 REM
410 ? :? :? "CHARACTERS IN CH$ ARE CONVERTED INTO"
420 ? "FREQUENCY, DISTORTION, & VOLUME"
430 ? "PARAMETERS FOR THE SOUND COMMAND"
440 ? "WITHOUT USING A POINTER STRING."
450 ?
460 FOR LOOP=1 TO 2
470 ? "ITERATION # ";LOOP
480 FOR I=1 TO 248 STEP 2
490 FOR N=0 TO 3
500 F=ASC(CH$(I,I)):X=ASC(CH$(I+1,I+1))
510 D=INT(X/16):V=X-16*INT(X/16)
520 SOUND N,F,D,V
530 NEXT N
540 NEXT I
550 NEXT LOOP
560 REM
570 ? :? :? "NOW USING A POINTER STRING"
580 ? :? "CH$ SUBSTRINGS ARE ASSIGNED INTO S$."
590 ? "S$ IS STORED IN THE SAME MEMORY"
600 ? "LOCATIONS AS THE SOUND CONTROL "
610 ? "REGISTERS."
620 ?
630 SOUND 0,0,10,4:SOUND 1,0,10,4
640 SOUND 2,0,10,4:SOUND 3,0,10,4
650 FOR LOOP=1 TO 10
660 ? "ITERATION # ";LOOP
670 FOR I=1 TO 249 STEP 2:S$=CH$(I,I+7):NEXT I
680 NEXT LOOP
690 END

Download

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

Credits