HTBasic Help
×
Menu
Index

BEEP

 
BEEP frequency,period is used to play music or tones by producing notes of a certain frequency and duration (period). The frequency value is rounded to a multiple of 81.38 Hz and has a range of 40.7 Hz to 32.767 Khz. The period is rounded to a multiple of 0.838 micro-seconds.
 
In the example below, A4 (the "A" note in the fourth octave) is used as the reference note. The program starts by setting A4=440, which is the  standard for the Equal Tempered Chromatic Scale adopted by the American Standards Association in 1936. But "note" that you can also set A4=435, which is for the International standard adopted in 1891.
 
! Print table of musical notes and play C-major scale.
A4=440     ! Frequency of the reference note
R=2^(1/12) ! One octave doubles the frequency, 12 half-steps in octave
C4=A4/(R^9)! Scale goes from C to B
DATA C,C#,D,D#,E,F,F#,G,G#,A,A#,B
DIM Name$(11)[2]
READ Name$(*)
N=C4/8     ! start at C1
CLS        ! clear the screen
FOR Octave=1 TO 7
   Col=1+(Octave-1)*11
   PRINT TABXY(Col,1);"Note  Freq|";
   PRINT TABXY(Col,2);"—————+";
   FOR Note=0 TO 11
      OUTPUT A$ USING "3A,X,4D.D,#";Name$(Note)&VAL$(Octave),N                  
      PRINT TABXY(Col,Note+3);A$;"|"
      IF Octave>1 AND LEN(Name$(Note))=1 THEN BEEP N,.25
      N=N*R
   NEXT Note
   PRINT TABXY(Col,15);"—————+";
NEXT Octave
BEEP N,.5 ! complete the last scale
END
 
This example demonstrates BEEP by playing C Major scales for octaves two through seven. At the same time, it prints a table of musical notes and their associated frequencies. An increase of one octave (A4 to A5) doubles the frequency, while moving down one octave halves the frequency. Twelve half-steps compose an octave and are equally spaced geometrically (rather than arithmetically). That is, the ratio of frequencies between any two adjacent notes is a constant.