
  include "$\inc\minwin.inc"
  include "waves.h"
  include "waves.inc"
  '
  WaveOut w
  '
  '------------------------------
  sub MakeSquareWave(sys b,d,e,v)
  '==============================
  static sys ph,u
  if d=0 then ph=0 : u=0 : exit sub
  sys a
  sys c
  sys i
  sys p=w.buf+b
  c=ph
  if u<0 then v=-v
  do
    if i>=e then exit do
    if b>=0x10000 then
      b=0
      p=w.buf
    end if
    a=v
    *p=a 
    p+=2  
    b+=2
    c+=2
    if c>d then v=-v : c=0
    i+=2
  end do
  ph=c : u=v
  end sub


  '--------------------------------
  sub MakeTriangleWave(sys b,d,e,v)
  '================================
  float f
  sys a
  static sys c
  static sys ci=2
  sys i
  sys p=w.buf+b
  if d=0 then c=0 : ci=2 : exit sub
  f=1/d
  do
    if i>=e then exit do
    if b>=0x10000 then
      b=0
      p=w.buf
    end if
    a=f*c*v
    *p=a 
    p+=2  
    b+=2
    c+=ci
    if c>=d then c=d : ci=-ci
    if c<=-d then c=-d : ci=-ci
    i+=2
  end do
  end sub

  '---------------------------
  sub MakeSawWave(sys b,d,e,v)
  '===========================
  static sys ph 'phase
  if d=0 then ph=0 : exit sub
  float f=1/d
  sys a
  sys c
  sys i
  sys p=w.buf+b
  do
    if i>=e then exit do
    if b>=0x10000 then
      b=0
      p=w.buf
    end if
    a=mod(ph+f*c*v,2*v)-v
    *p=a 
    p+=2  
    b+=2
    c+=2
    i+=2
  end do
  ph=mod (ph+f*c*v, 2*v) 'remember phase
  end sub


  '----------------------------
  sub MakeSineWave(sys b,d,e,v)
  '============================
  static float ph 'phase
  if d=0 then ph=0 : exit sub
  float f=pi/d
  sys a
  sys c
  sys i
  sys p=w.buf+b
  do
    if i>=e then exit do
    if b>=0x10000 then
      b=0
      p=w.buf
    end if
    a=sin(ph+f*c)*v
    *p=a 
    p+=2  
    b+=2
    c+=2
    i+=2
  end do
  ph=mod (ph+f*c, 2*pi) 'remember phase
  end sub

  '
  '
  w.WavoStart 
  '
  sys b,p,q,c,d,v,sv,fi,dem
  q=1000
  b=q
  '
  for dem=1 to 4
  '
  d=100
  v=10
  sv=1
  do
    sleep 5
    if GetAsyncKeyState 27 then exit do
    p=w.wavopos
    'if p>q 'behind
    if p+2000>q then 'ahead ~1/40 sec
      '
      'PITCH
      '
      if d>=100 then fi=-2
      if d<50 then fi=2
      d+=fi
      '
      'FADE IN, FADE OUT
      '
      if sv then
        v++
        if v>=512 then sv=0
      else
        if v then v-- else exit do
      end if
      select dem
      'offset,wavelength,duration,volume
      case 1 : MakeSquareWave b,d,1000,v
      case 2 : MakeTriangleWave b,d,1000,v
      case 3 : MakeSawWave b,d,1000,v
      case 4 : MakeSineWave b,d,1000,v
      end select
      q+=1000
      b+=1000
      if b>=0x10000 then b=0
    end if
  end do
  '
  next
  '
  w.WavoEnd
   
