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

  sys b,c,bufo,d1,d2,d3,dc,du1,du2
  sys i,p,q,decay,reverbt
  sys k1,k2,pc,opc,pcf
  sys note,scaleNo,scaleLen,firstscale,lastscale,downScale,sfi
  sys rn,pn,ch
  '

  'REVERB SETTINGS

  reverba=.25
  nreverba=.75
  reverbt=0.2*88200 'max buffersize 64k
  '
  du1=10
  du2=8 'nominal
  FirstScale=2
  LastScale=2
  scaleNo=FirstScale
  volume=1
  amp=000
  basefreq=220 'A below middle C
  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+=20
  end if
  '
  'NO ENVELOPE
  '
  'amp=2000
  '
  '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(6*ipuls*q)
    'basefreq+=.02
    'freq=basefreq
    '
    'SLIDE NOTES
    '
    'ensure no overshoot
    '
    if basefreq=nfreq then
      '
    elseif abs(nfreq-basefreq)<abs(ifreq) then
      nfreq=basefreq
    else 
      nfreq+=ifreq
    end if
    '
    freq=nfreq
    '
    'VIBRATO (frequency modulation)
    '
    v=sin(pi2*vphase)
    'freq+=v
    freq+=du*v*basefreq*.03125 'NOTE TIME-DEPENDENT VIBRATO
    '
    'SYNC PHASE FOR WAVE CONTINUITY
    '
    phdis=frac(phase-freq*q*ipuls)
    '
  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*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
    du=du1-dc
    if du>=1 then
      du=(d1-dc)/du
    else
      du=0
    end if
  else
    dc=d1 : du=0
  end if
  'if du>1 then du=1
  '
  'du=d1/20 '(INCLUDING SLIDE TIME)
  '
  'a+=amp*pulsewidth(phase,.45*du) 'ph: max 0 to min <.5
  '
  'FILTER
  '
  filtpr=.02 '440 HZ 
  filt=filt*(1-filtpr)+a*filtpr 'ACCUMULATE
  lp=filt       'low pass
  hp=a-.7*pa    'high pass
  '
  'a=lp*filtba + hp*(1-filtba) 'moving filter mix
  pa=a
  'a=lp
  'a=hp
  '
  'SYNC FILTER TO NOTE DURATION
  '
  'a=lp*(1-du)*4+hp*du*4
  '
  'TREMOLO (amplitude modulation)
  '
  'a=a*(0+(1+sin(pi2*vphase)))*.5
  '
  'POST WITH REVERB
  '
  a*=volume
  SendToBuffer 'macro
  a=0
  '
  next


  if ++d1=du1 then
    d1=0
    d2++
    '
    'INCR PITCH
    '
    'basefreq*=1.0375
    '
    'USE SELECTED SCALES
    '
    '
    sys ch
    scaleLen=len scale(ScaleNo)
    '
    'CHOOSE A NOTE FROM FORWARD OR REVERSE SEQUENCE
    '
    'if downScale then
    '  n=ScaleLen-d2+1
    '  if n=0 then n=1 : downscale=2
    'else
    '  n=d2
    'end if
    '
    'CHOOSE A RANDOM DIFFERENT NOTE
    '
    while rn=pn
      rn=rand(1,scaleLen)
    wend
    pn=rn
    '
    int n=rn
    '
    'DECODE PITCH CHARS INTO FREQUENCY
    '
    ch=asc(scale(scaleNo),n)
    select ch
    case 49 to 57 : ch-=49
    case 48       : ch=9
    case 45       : ch=10
    case 61       : ch=11
    case 0
      ch=12 'octave
    end select
    ch+=3 'shift to middle C
    '
    '
    'nfreq=basefreq
    basefreq=pow(2,ch/12)*startfreq  'NEW PITCH
    'ifreq=(basefreq-nfreq)*.0125   'PITCH SLIDING
    '
    nfreq=basefreq 'NO PITCH SLIDE
    '
    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>ScaleLen then
      d2=0
      downscale+=1
      '
      if downscale=2 then
        downscale=0
        '
        'SCALE BASE CHANGE
        '
        if ++scaleNo>LastScale then
          scaleNo=FirstScale 'START AGAIN WITH FIRST SCALE
          sfi+=2
          startfreq=220*pow(2,sfi/12) 'INCREASE BY SEMITONES
        end if
      end if
      '
      'filtba-=.1
      'if filtba<=0 then exit do
    end if
  end if

  end if

  sleep 5
  enddo
  '
  w.WavoEnd

   
