;
; wavetable.orc
;
; wavetable synthesis algorithm
;
        sr=44100
        kr=882
        ksmps=50
        nchnls=1

; initialize waveforms
gitri   ftgen   1,0,8192,7,-1,4096,1,4096,-1
gipls   ftgen   2,0,8192,7,-1,4096,-1,1,1,1,4095
gisaw   ftgen   3,0,8192,7,-1,8192,1
gisin   ftgen   4,0,8192,10,1

gklfo   init    0
        schedule 100, 0, 200

#include "waves.orc"
#include "wavetab.orc"

; uWave oscillator opcode
; usage:
; aosc1 uWaveOsc acps,kwavemod,ksync,iwavetab
;
        opcode  uWaveOsc,a,akkkk
  acps,kwavemod,kexp,ksync,kwavetab xin         

kwavet  =       (2000+3*kwavetab)
kxfade  tableikt kwavemod,kwavet,  0, 0, 0
kfn1    tablekt kwavemod,kwavet+1, 0, 0, 0
kfn2    tablekt kwavemod,kwavet+2, 0, 0, 0

aphasor	phasor	acps
aphasor = (1-exp(aphasor*kexp))/(1-exp(kexp))
; add softsync
async   mirror  aphasor*ksync, 0, 1
awave1  tablexkt async, kfn1, 0, 4, 1, 0, 1
awave2  tablexkt async, kfn2, 0, 4, 1, 0, 1
;aout    =       (awave1*(1-kxfade))+(awave2*kxfade)
aout    ntrpol  awave1,awave2,kxfade
        xout    aout
        endop

;
;global lfo
;
        instr   100,GLFO
gklfo   oscil   0.499, .25, gitri
        endin

;
; test instrument
;
        instr   1

idur	=	2
iamp	=	2000
icps    cpsmidi
kcps	cpsmidib
isync	=	3.25

kwavetab midictrl       74,1,22
ksync midictrl       71,1,16
kexp    midictrl        79
kexp    =       20*kexp/127 - 10
klfo    =       gklfo+.5
klfo    midictrl 72,0,63
printk2 kwavetab
printk2 ksync
printk2 kexp
printk2 klfo
acps    =       a(kcps)
aosc1   uWaveOsc acps,     klfo,kexp,ksync,int(kwavetab)
aosc2   uWaveOsc acps*1.01,klfo,kexp,ksync,int(kwavetab)

; apply amp envelope
;ifreq   limit   icps * 16, 1, 20000
;afilt   bqrez   aosc, ifreq, 20
;afilt   =       aosc1 * aosc2
afilt   bqrez   aosc1 + aosc2, 10000, 5
aenv    linsegr 0, .001, iamp, 0, iamp, .5, 0 ; create a envelope
aout    =       aenv * afilt
        display afilt, 2/icps
	out	aout

        endin