$ filename "HearingTest.exe"

'uses rtl32
'uses rtl64

uses console

! Beep  lib "kernel32.dll" (dword dwFreq=800, dword dwDuration=200) as bool
'! Sleep lib "kernel32.dll" 'dwMilliseconds

% f_min = 20
% f_max = 20000
int f = f_min
int k

SetConsoleTitle "Test your hearing"

string InfoTxt = quote !!!
  The range of human hearing is between 20 and 20,000 Hz, but
  it is age-dependent. Almost everyone can still hear up to 8000 Hz.
  The ability to hear decreases with age:

     age 50 and older:  up to 8000 Hz - 12000 Hz
     age 40 to 50:      up to 12000 Hz
     age 30 to 40:      up to 15000 Hz
     age 20 to 30:      up to 17000 Hz
     younger than 20:   up to 19000 Hz
     baby:              up to 20000 Hz

  This test cannot be very accurate, if only because of the limited
  capabilities of the speakers or headphones. But it already gives
  an approximate overview. To be sure, you should visit
  a hearing care professional.
!!!

cls
print InfoTxt
printl : print "Enter ..." : waitkey

cls
SetPos(3,3) : print "Test your Hearing"
SetPos(3,4) : print "If you can hear the sound between the clicks,"
SetPos(3,5) : print "then please press X"

SetPos(3,8) : print "Starting"
Sleep(2000)

'The lower frequencies
do
  f += 10
  Beep(f, 1000)
  k=inkey 'non-blocking
  sleep 10 'important!
  if k=27 then goto fin: ' Esc
  SetPos(3,8) : print f " Hertz  "
  if f = f_max then exit do
  if f > 300 then
     SetPos(3,10) : print "You are probably deaf or have blocked ears. If this is the case,"
     SetPos(3,11) : print "then maybe you need a hearing aid."
     goto fin
  endif   
  if k = 88 then 'X
     f -= 10
     SetPos(3,8) : print f " Hertz   "
     exit do
  endif     
loop

SetPos(3,3) : print "Now testing the upper frequencies:"
SetPos(3,4) : print "If you cannot hear the sound between the clicks any longer,"
SetPos(3,5) : print "then please press X"
SetPos(3,10) : print "Starting"
Sleep(2000)

'The upper frequencies
f = 6900
do
  if f > 9900 then f += 400
  f += 100
  Beep(f, 1000)
  k=inkey 'non-blocking
  sleep 10 'important!
  if k=27 then goto fin: ' Esc
  SetPos(3,10) : print f " Hertz   "
  if f = f_max then exit do
  if k = 88 then 'X
     if f > 9900 then f -= 400
     f -= 100
     SetPos(3,10) : print f " Hertz   "
     exit do
  endif     
loop

fin:
SetPos(0,12)
print "Enter ..." : waitkey