  uses minwin
  uses waves.h
  uses waves.inc
  '
  WaveOut w
  '
  double v
  single a,amp,freq,nfreq,ifreq,basefreq,startfreq
  single phase,phdis,vphase,pphase,pw
  single reverba,nreverba,filt,filtpr,filtba
  single lp,hp
  single volume

  sys b,c,d,bufo,d1,d2,d3,dc
  sys i,p,q,decay,reverbt
  sys k1,k2,pc,opc,pcf
  sys note,scaleNo,sfi
  '

  'REVERB SETTINGS

  reverba=.25
  nreverba=.75
  reverbt=0.1*88200 'max buffersize 64k
  '
  scaleNo=1
  volume=1
  amp=000
  basefreq=220
  startfreq=basefreq
  nfreq=basefreq
  freq=basefreq
  filtba=1
  '
  w.WavoStart 
  '
  do
    p=w.wavopos
    if p+2000>q then 'ahead ~1/40 sec

  '
  'ENVELOPE
  '
  for i=1 to 1000

  'ENVELOPE
  
  'if amp>=4000 then decay=1
  '
  'if decay then
  '  amp*=.9999
  '  if amp then amp-=.5
  'else
  '  'amp+=10
  'end if
  '
  'NO ENVELOPE
  '
  amp=3000
  '
  'PHASE AND WAVE COUNTER
  '
  v=freq*q*ipuls-phdis
  phase=frac(v)
  pc=v-phase 'wave counter
  if pcf then opc=pc : pcf=0
  '
  'NEW WAVE OR PULSE
  '
  if pc>opc then
    pcf=1
    pphase=frac(.25*ipuls*q)
    vphase=frac(4*ipuls*q)
    'basefreq+=.02
    'freq=basefreq
    '
    'SLIDE NOTES
    '
    if round(nfreq)<>round(basefreq)
      nfreq+=ifreq
    else 
      nfreq=basefreq
    end if
    '
    freq=nfreq
    '
    'VIBRATO (frequency modulation)
    '
    'v=sin(pi2*vphase)*basefreq*.03125
    'freq+=v
    '
    'SYNC PHASE
    '
    phdis=frac(freq*q*ipuls)-phase
    '
  end if
  '
  'GENERAL WAVE FORMS
  '
  'a+=amp*noiseInterp(pc,phase)
  'a+=amp*sawtooth(phase)
  a+=amp*triangle(phase)
  'a+=amp*square(phase)
  'a+=amp*square(phase*1.5)
  'a+=amp*sin(pi2*phase) 'sine wave
  'a+=amp*sin(pi2*phase*1.25) 'harmonic
  'a+=amp*sin(pi2*phase*1.5) 'harmonic
  'a+=amp*sin(pi2*phase*3) 'harmonic

  'PULSE WIDTH MODULATION
  '
  'pw=(1+sin(pi2*pphase))*.20
  'pw=(1+triangle(pphase))*.23
  'pw=.49-(1+sawtooth(pphase))*.245
  '
  '-FOR STRINGS
  '
  'pw=(1+sawtooth(pphase))*.245
  'pw=.45*amp/4000
  'pw=.45-.45*amp/4000
  '
  'a+=amp*pulsewidth(phase,pw) 'ph: max 0 to min <.5
  '
  'SYNC PULSEWIDTH TO NOTE DURATION (EXCEPT SLIDE TIME)
  '
  if basefreq=nfreq
    d=d1-dc
  else
    dc=d1 : d=0
  end if
  '
  'a+=amp*pulsewidth(phase,d*.025) 'ph: max 0 to min <.5
  '
  'FILTER
  '
  filtpr=.1
  filt=filt*(1-filtpr)+a*filtpr
  lp=filt       'low pass
  hp=a-filt     'high pass
  a=lp*filtba + hp*(1-filtba) 'moving filter mix
  'a=lp*2 'boosted
  '
  'TREMOLO (amplitude modulation)
  '
  'a=a*(0+(1+sin(pi2*vphase)))*.5
  '
  'POST WITH REVERB
  '
  a*=volume
  SendToBuffer 'macro
  a=0
  '
  next


  if ++d1=20 then
    d1=0
    d2++
    '
    'INCR PITCH
    '
    'basefreq*=1.0375
    '
    'USE SELECTED SCALES
    '
    scope (
      '
      'DECODE INTO FREQUENCY
      sys c
      c=asc(scale(scaleNo),d2)
      select c
      case 49 to 57 : c-=49
      case 48       : c=9
      case 45       : c=10
      case 61       : c=11
      case 0        : c=12 'octave
      end select
      c+=3 'shift to middle C
      '
      'PITCH SLIDING
      '
      nfreq=basefreq
      basefreq=pow(2,c/12)*startfreq
      ifreq=(basefreq-nfreq)*0.025 'IN 40 WAVES
    )
    decay=0
    amp=0
    '
    'FADE OUT
    '
    if basefreq>2000 then volume*=.90
    '
    'EXIT
    '
    if GetAsyncKeyState 27 then exit do
    if basefreq>22050 then exit do
    if volume<.01 then exit do
    '
    if d2>len(scale(scaleNo)) then
      d2=0
      '
      'SCALE BASE CHANGE
      '
      if ++scaleNo>8 then
        scaleNo=1 'START AGAIN WITH FIRST SCALE
        sfi+=1
        startfreq=220*pow(2,sfi/12) 'INCREASE BY SEMITONES
      end if
      '
      'filtba-=.1
      'if filtba<=0 then exit do
    end if
  end if

  end if

  sleep 5
  enddo
  '
  w.WavoEnd

   
