
  uses minwin
  'uses console
  uses waves.h
  uses waves.inc

  '
  type tonetype
    single frequency,duration,amplitude
    int waveform
  end type

  tonetype tg[1000]
  int tc

  function tone(single frequency=440,duration=1, amplitude=0.1, int waveform=1)
  =============================================================================
  tg[tc]={frequency,duration,amplitude, waveform}
  tc++
  tg[tc].frequency=0 'assume stop
  end function


  function playtones(tonetype*tn)
  ===============================
  indexbase 0
  '
  single a,amp,freq,nfreq,basefreq,phase,phdis
  single reverba=0.0, nreverba=1.0
  '
  sys b,bufo,d1,d2,i,p,q,decay,reverbt
  sys k1,k2,pc,opc,pcf,qq,lt

  WaveOut w

  w.WavoStart
  tonetype *t
  @t=@tn
  '
  double v
  int ti
  '
  a=0
  '
  
  phase=0
  phdis=0
  pc=0
  opc=0
  pcf=0
  p=0
  q=0
  int xit=0
  '
  NewTone:
  ========
  '
  amp=t.amplitude*0x7fff
  basefreq=t.frequency
  lt=t.duration*44100
  '
  if basefreq=0
    goto closeplay
  endif
  '
  freq=basefreq
  'print freq tab p tab q cr
  do
    sleep 5 'msec
    p=w.wavopos
    if p+2000<=q 'ahead ~10ms
      continue do
    endif
  '
  'ENVELOPE
  '
  int i

  '
  ================================
  for i=1 to 1000
  ================================
  '
  v=freq*q*ipuls-phdis
  phase=frac(v)
  pc=v-phase 'wave counter
  if pcf
    opc=pc
    pcf=0
  endif
  '
  'NEW WAVE OR PULSE
  '
  if pc>opc
    pcf=1
    '
    'SYNC PHASE WHEN CHANGING FREQUENCY
    '
    phdis=frac(freq*q*ipuls)-phase
    '
  end if
  '
  if GetAsyncKeyState 27 'escape key
    xit=1
  endif
if lt<=0
    xit=1
  endif
  if q>0xffff
    amp=0
  endif
  '
  if pcf
    if xit
      'another tone
      @t+=sizeof(tonetype)
      xit=0
      goto NewTone
    endif
  endif
  '
  'WAVEFORM
  '
  select t.waveform
  case 0
    'null wave
  case 1
    a+=amp*sin(pi2*phase) 'sine wave
  case 2
    a+=amp*sawtooth(phase)
  case 3
    a+=amp*triangle(phase)
  case 4
    a+=amp*square(phase)
  case 5
    a+=amp*noiseInterp(pc,phase)
  end select

  '
  SendToBuffer 'macro
  a=0
 ' sys pp=w.wavopos()-10
 ' pp=pp and 0x1ffff
 ' shr pp,1
 ' das[pp]=0
   lt--
  '
  =================
  next 'in envelope
  =================
  loop
  closeplay:
  w.wavoend
  end function

  '    frequency, duration, amplitude, waveform
  tc=0
  tone 110, 0.125, 0.1, 1
  tone 220, 0.125, 0.1, 1
  tone 300, 0.125, 0.1, 1
  tone 440, 0.150, 0.1, 1
  tone 500, 0.150, 0.1, 1
  tone 600, 0.150, 0.1, 1
  tone 700, 0.150, 0.1, 1
  tone 220, 0.150, 0.1, 1
  tone 110, 1.150, 0.1, 1
  tone 220, 1.150, 0.1s, 1
  playtones(tg)

  'pause

   
