Tech Tips
HERTZ GENERATOR
BY LARRY NOCELLA
BASIC's SOUND command gives you a range of five octaves. HERTZ.BAS uses 16-bit sound and the POKEY clock to increase this range to nine octave's.
Sounds and musical notes are made of sound waves. These waves have frequencies which can be measured in cycles per second, or Hertz. The pitch of a note is propertional to the frequency of its sound wave. Musicians are familiar with 440 Hertz, the frequency of the standard "Concert A." Middle C is 261.63 Hertz.
POKEY is a digital I/O chip that controls sound, among many other things. By adjusting POKEY's Audio Control register (AUDCTL) you can link together BASIC's four five-octave voices into two clearer nine-octave voices.
There is a clock in POKEY that continuously pulses to synchronize all computer functions. This clock pulses at 1,790,000 cycles per second, or 1.7p mHz (megaHertz). To generate the A at 440 Hertz, you tell your Atari to pulse every Nth clock pulse to get 440 pulses per second.
To find N, divide the number of clock pulses per second by the frequency of the note desired. For the A above, N equals 1,790,000 divided by 440, or 4068. Actually, N is approximately 4068.1818, but POKEY can't count a fraction of a pulse.
Since your Atari divides N by two before sending a sound pulse, your program must double N before using it (see line 470).
The program is simple to use. At the prompt, choose one or two voices, then type the desired frequency for each voice. Press [START] to RUN the program again and try more sounds, or press [OPTION] to end the program.
The highest frequency this program can generate is 1.79 mHz (or 1,790,000 Hertz), sending out one sound pulse for every clock pulse. Unfortunately, the human ear can only detect up to about 31,000 Hz.
The lowest sound the program generates has a frequency of 14 Hz. A lower number (13) would require a sound pulse every 68,000 clock pulses, but the 16-bit sound counter of the Atari can only go as high as 65,535 (the largest number you can represent in 16 bits).
On these super low notes, you can hear the difference between the pulses, like when you stick your tongue between your lips and blow. PLBPLBPLBT!
See Jerry White's 16-Bit Soundpower (Antic, September 1985) for more ways to use 16-bit sound from BASIC.
l00 REM HERTZ GENERATOP
Listing: HERTZ.BAS Download
Antic pays $25 for every original and exclusive Tech Tip submission that we publish. Send your 8-bit or ST disk and printout to: Antic Tech Tips, 544 Second Street, San Francisco, CA 84107. Tech Tips welcomes very short programs that demonstrate the Atari's powers, simple hardware modifications, or useful macros for popular software.
130 REM INIT
140 GRAPHICS 0:LO=14
150 AUDCTL=53768:CLOCK=1790000
160 AUDF1=53760:AUDC1=53761
170 AUDF3=53764:AUDC3=53765
180 REM INIT POKEY FOR SOUND
190 FOR I=0 TO 3:SOUND I,0,0,0:NEXT I
200 REM COMBINE, CLOCK 1.79MHz
210 POKE AUDCTL,I20:? CHR$(I25);
220 REM EVEN VOICES=VOLUME 8
230 POKE AUDC1,160:POKE AUDC1+2,168
240 POKE AUDC3,160:POKE AUDC3+2,188
250 REM START PROGRAM
260 TRAP 260
270 ? :? "One or two voices (1-2)? ";
280 INPUT #16,VOC:IF (VOC<>1) AND (VOC<>2) THEN 270
290 TRAP 44444
300 REM MAIN LOOP
310 IF VOC=1 THEN X=1:GOSU8 450:VH1=HIF:VL2=LOF:V1=HERTZ/2:VH3=0:VL4=0:GOTO 340
320 IT VOC=2 THEN X=1:GOSUB 450:VH1=HIF:VL2=LOF:X=2:V1=HERTZ/2:60SUB 450:VH3 =HIF:VL4=LOF:V2=HERTZ/2
330 REM MAKE SOUND
340 POKE AUDF1,VH1:POKE AUDF1+2,VL2
350 POKE AUDF3,VH3:POKE AUDF3+2,VL4
360 ? :? :? "Voice #1 generating ";V1;" hertz."
370 IF VOC=2 THEN ? :? "Voice #2 generating ";V2;" hertz."
380 REM END PROGRAM
390 ? :? "Press START to RUN again"
400 ? :? "Press OPTION to end...";
410 IF PEEK(53279)=6 THEN 190
420 IF PEEK(53279)=3 THEN ? :? :? "END.":END
430 GOTO 410
440 REM INPUT HERTZ
450 TRAP 450:? :? :? "Hertz to generate ( ";LO;" - ";CLOCK;" ) ":? "for voice #";X;" ->";
460 INPUT #I6,HERTZ:TRAP 44444:IF (HERTZ
470 HERTZ=HERTZ*2
480 GOSUB 500:RETURN
490 REM CALCULATE Nth CYCLE
500 N=INT(CLOCK/HERTZ)
510 LOF=INT(N/256):HIF=N-(256*LOF)
520 RETURN