
  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 phase1,phdis1,freq1
  single phase2,phdis2,freq2
  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 n,rn,pn,ch
  sys percuss,percussv,percussq
  '

  'REVERB SETTINGS

  reverba=.25
  nreverba=.75
  reverbt=0.35*88200 'max buffersize 64k
  '
  du1=10
  du2=10 'nominal
  FirstScale=1
  LastScale=1
  scaleNo=FirstScale
  volume=1
  amp=000
  basefreq=110 'A3
  filtba=1
  q=2000
  '
  startfreq=basefreq
  nfreq=basefreq
  freq=basefreq
  '
  w.WavoStart 
  '
  do
    p=w.wavopos
    if p+2000>q then 'ahead ~1/40 sec

  '
  '
  for i=1 to 1000
  '
  'PHASE AND WAVE COUNTER
  '
  v=freq*q*ipuls+phdis : phase=frac(v)
  pc=v-phase 'wave counter
  if pcf then opc=pc : pcf=0
  '
  'CHORDS/HARMONICS
  '
  phase1=frac(freq1*q*ipuls+phdis1)
  phase2=frac(freq2*q*ipuls+phdis2)
  '
  'NEW WAVE OR PULSE
  '
  if pc>opc then
    '
    pcf=1
    '
    pphase=frac(.25*ipuls*q)
    vphase=frac(6*ipuls*q)
    '
    '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)
    freq1=freq*1.333
    phdis1=frac(phase1-freq1*q*ipuls)
    freq2=freq*1.500
    phdis2=frac(phase2-freq2*q*ipuls)
    '
    'ENVELOPE
  
    '
    if decay then
      amp*=.99
    else
      amp+=300
    end if
    if amp>=3000 then amp=3000 : decay=1
    'amp=3000
    '
  end if
  '
  'GENERAL WAVE FORMS
  '
  'a+=amp*noiseInterp(pc,phase)
  'a+=amp*noise(q)
  'a+=amp*sawtooth(phase)
  a+=amp*triangle(phase2)
  'a+=amp*square(phase1)
  'a+=amp*square(phase2)

  a+=amp*sin(pi2*phase)  'sine wave  fundamental
  'a+=amp*sin(pi2*phase1) 'sine wave  1
  'a+=amp*sin(pi2*phase2) 'sine wave 2

  '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
  a+=amp*pulsewidth(phase1,.45-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
  'a+=amp*pulsewidth(phase1,.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)*2+hp*du*2
  'a=hp*(1-du)*2+lp*du*2
  '
  '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
    if GetAsyncKeyState 27 then exit do
    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=1 then d2++
    else
      n=d2
    end if
    '
    'CHOOSE A RANDOM DIFFERENT NOTE
    '
    'while rn=pn
    '  rn=rand(1,scaleLen)
    'wend
    'pn=rn
    '
    '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)*.025   'PITCH SLIDING
    '
    'basefreq=220   'FIXED PITCH

    nfreq=basefreq 'NO PITCH SLIDE
    '
    decay=0
    amp=0
    '
    'FADE OUT
    '
    if basefreq>2000 then volume*=.90
    '
    'EXIT
    '
    if basefreq>22050 then exit do
    if volume<.01 then exit do
    '
    if d2>ScaleLen then
      d2=0
      downscale++
      '
      if downscale=2 then
        downscale=0
        '
        'SCALE BASE CHANGE
        '
        if ++scaleNo>LastScale then
          scaleNo=FirstScale 'START AGAIN WITH FIRST SCALE
          sfi+=1 'INCREASE BY SEMITONES
          startfreq=110*pow(2,sfi/12)
        end if
      end if
      '
      'filtba-=.1
      'if filtba<=0 then exit do
    end if
  end if

  end if

  sleep 5
  enddo
  '
  w.WavoEnd

   
