ifdef([OUTBIGSAMP],,define(OUTBIGSAMP,[ifelse(NCHNLS,1,[],
[; mono in, stereo output : left-right output spatial placement
if p13 = -1 goto leftlow
if p13 = -2 goto  rightlow
if p13 = -3 goto  randrand
if p13 = -4 goto  randper
if p13 = -5 goto  done
a2 = (1. - p13) * a1
a1 = p13 * a1
goto done

leftlow: ;  p13 = -1 : lowest notes to LC, highest notes to RC 
        iplace = (110 - imnum) * .01 * 1.1 
        iplace = (iplace > 1. ? 1. : iplace) 
        iplace = (iplace < 0 ? 0 : iplace) 
     a2 = sqrt(iplace) * a1
     a1 = sqrt(1. - iplace) * a1
goto  done

rightlow:  ; p13 = -2 : lowest notes to RC, highest notes to LC 
        iplace = (110 - imnum) * .01 * 1.1 
        iplace = (iplace > 1. ? 1. : iplace) 
        iplace = (iplace < 0 ? 0 : iplace) 
     a2 = sqrt(1. - iplace) * a1
     a1 = sqrt(iplace) * a1
goto done

randrand:  ; p13 = -3 : random spatial placement with random moving pan : 
        iseed = frac(icps + iamp - idur) 
kplace  randi .25, 2. + (frac(iamp - icps + idur)) 
        kplace = kplace + .25 
        isign = (iseed > .5 ? -1 : 1 ) 
        kplace = (kplace * isign) + iseed 
     a2 = sqrt(1. - kplace) * a1
     a1 = sqrt(kplace) * a1
goto done

randper:   ; p13 = -4 : random spatial placement with periodic moving pan 
        iseed = frac(icps + iamp - idur) * .9
        iseed2 = ((1. - iseed) + .2) * .6
        ipancenter = (iseed * .5) + .25 
kspeed  linseg iseed*1.3,.2*idur, .8*iseed ,.3*idur,iseed2 , .35*idur ,.7*iseed ,.15*idur, .4*iseed2 
kspeed  linseg 1., .2 * idur , .4 , .3*idur , .9 , .35*idur , .4 , .15*idur, .3 
kplace  oscil .25, kspeed * 1.6 + (frac(icps - iamp)), 100,iseed 
        kplace = kplace + .25 
        isign = (iseed > .5 ? -1 : 1 ) 
        kplace = (kplace * isign) + ipancenter 
     a2 = sqrt(1. - kplace) * a1
     a1 = sqrt(kplace) * a1

done:
])]))dnl

; p5  = amplitude (0 -1 ) integer 0 -127 = midi veloc or 1000 - 32000 raw value
; p4 = pitch :  0 - 13. = pch  |  14 - 127 = midi note | negative = herz
; p6 = detuning
; p7 = input gen1 function # { if f99 & f98 not used}
; p8 = input pitch  { if f99 & f98 not used}

instr bigsamp

idur = p3
ibasepitch init 0
p12 = (p12 = 0 ? 2 : p12 ) ; dummy end loop point for loscil
                           ; csound aborts without an end loop value, even
                           ; if no looping is specified
;========get pitch & midi note number
if p4 > 13.0 igoto midinote
if p4  < 0  igoto herz
     ; p4 is in pch
     icps = cpspch(p4)
        ; convert pch to midi note number
        imnum =  int((p4) - 3) * 12
        imnum =  imnum + (frac(p4) * 100)
;            ioct = octcps(icps) ; DELETE -- NOT NEEDED HERE
     igoto gotnote
midinote: 
     ; p4 gives midi note number
     imnum = p4
        ; convert midi note num to cps
        ioct = (p4/12) + 3.  ; returns oct decimal
        icps  =  cpsoct(ioct)
     igoto gotnote
herz: ; p4 is negative, so it is in herz
     icps = abs(p4)
     ioct = octcps(icps) 
     ipch = pchoct(ioct) 
        ; convert pch to midi note number
     imnum =  int((ipch) - 3) * 12
     imnum =  imnum + (frac(ipch) * 100)
gotnote:

; ------------------------------------------
; get gen01 audio function number & base pitch of soundfile
if p7 = 0 igoto tables
    ifno = p7      ; input soundfile function number
   ; if gen1 func specified in p7, p8 = input pitch : pch, MIDI note or herz
     if p8 > 13.0 igoto inmidinote
     if p8  < 0  igoto inherz
          ; p8 is in pch
          ibasepitch = cpspch(p8)
          igoto gotinpitch
     inmidinote: if p8 < 0  igoto inherz
          ; p8 gives midi note number : convert this to cps
             inoct = (p8/12) + 3.  ; returns oct decimal
             ibasepitch  =  cpsoct(inoct)
          igoto gotinpitch
     inherz: ; p8 is negative, so it is in herz
          ibasepitch = abs(p8)
          igoto gotinpitch

tables:  ; use func tables 99 & 98 to get source soundfile
ifno	table	imnum, 99	;keyboard mapping to gen1 ftables 
ibasno	table	ifno, 98        ; returns midi notes of the sonudfile samples
ibasoct	=	ibasno/12. + 3.
ibasepitch = cpsoct(ibasoct)
gotinpitch:
; ------------------------------------------
        ;  --- Detuning  module (mostly for use with "chorus" ) ---
   idetunepf = p6
if idetunepf = 0 goto gotdetune ;skip all this if detuning set to 0 in score
       idtcount init 0  ; counter
       idtmult =  (idetunepf > 0 ? 1.05946 : .94387) ;1/2 step freqency ratios
   idetune init 1.  ; detuning multiplier for p4 pitch
        icheck = abs(idetunepf)
        icheck = int(icheck)

  dumbloop:
	      idtcount = idtcount + 1   ; increment counter
	      idtmult = (icheck = 0 ? 1 : idtmult)
   idetune = idetune * idtmult 
   if idtcount < icheck igoto dumbloop
 ; microtonal detuning:
	idtmult = abs(idetunepf)
	idtmult = frac(idtmult)
	idtmult = idtmult * .05496
	idtmult = (idetunepf > 0 ? idtmult : - idtmult)
   idetune = idetune + idtmult
icps = icps * idetune 
gotdetune:   ; ---end of detuning module -------
; -----------------------------------------------------------
; optional TURN OFF INSTRUMENT when p9 time reached {only a portion of the
; input soundfile, specified in p9,  is used}
if p9 = 0 goto playon
        istoptime = p9   ; 
;	inotedur = ( p3 < istoptime ? p3 : istoptime)
;	inoteoff = inotedur + p2
        itransp = ibasepitch/icps
	istoptime = istoptime * itransp 
	istoptime = istoptime - .005
	inotedur = ( p3 < istoptime ? p3 : istoptime)
        idur = inotedur
	inoteoff = inotedur + p2

        print p2, p4, inotedur, inoteoff  ; print note start time, dur & end time for user
	timout istoptime , p3 , shutdown
	goto playon
		shutdown: 
		turnoff
		kgoto output
playon:
 ; ==============================================================
; OPTIONAL PITCH PROCESSING : (1) glissando (2) freq. mod. (3) random deviation
kcps  = icps
;  (1) GLISSANDO : 
;     p21 = 2nd pitch ; p22 = duration of p4 pitch
;     p23 = duration of gliss . ; p24 = gliss. function {default = 60}
if p21 == 0 || p21 = 4.0 goto gotgliss
if p22 == 0  goto gotgliss
   ; determine 2nd pitch :
   if p21 < 14. igoto glisspch
       iglissoct = (p21/12)+3  ; p21 gives MIDI note num for 2nd pitch
       icps2 = cpsoct(iglissoct)
       igoto dogliss
   glisspch:  ; p21 is in pch, herz or a multiplier for icps {p4}
       icps2 init icps
       icps2 = (p21 < 0 ? abs(p21) : icps2 ) ; if p21 negative, it is in herz
       icps2 = (p21 > 4.1 && p21 < 13.1? cpspch(p21) : icps2)  ; p21 in pch
       icps2 = (p21 > 0 && p21 < 4.1 ? p21 * icps : icps2)
dogliss:
   ; if p22 = 0 no gliss
       iglissdur1 = (p22 < 100 ? p22 : (p22 - 100) * idur)
       iglissdur1 = (p22 < 0 ? abs(p22) * idur : iglissdur1)
       iglissdur2 init p3 - iglissdur1

       iglissdur2 = (p23 > 100 ? (p23 - 100) * idur : iglissdur2)
       iglissdur2 = (p23 < 0 ? abs(p23) * idur : iglissdur2)
       iglissdur2 = (p23 > 0  && p23 < 100 ?  p23 : iglissdur2)
       iglissfunc = (p24 = 0 ? 60 : p24)  ; default gliss function shape
   kgliss  oscil1i  iglissdur1,  icps2 - icps, iglissdur2, iglissfunc
   kcps = icps + kgliss
gotgliss:
 ; ------------------------------------------------------
 ;  (2) PERIODIC FREQUENCY MODULATION : VIBRATO, TRILL, audio rate FREQ. MOD.
; p25 = freq. mod. fade-in duration  ; p26 = initial rate ; p27 = closing rate
; p28 = initial depth  ; p29 = closing depth
ifmtest =  p28 + p29
if ifmtest = 0 goto gotfm
        ifmfade = (p25 = 0 ? .001 : p25)
        ifmfade = (p25  < 0 ? abs(p25) * idur : ifmfade)
        ifmfade = (p25  > 100 ? (p25 - 100) * idur : ifmfade)
        ifmfunc = (p30 = 0 ? 100 : p30)

if p26 < 0 goto audiofm
        p28 = (p28 = 0 ? .001 : .0595 * p28)   ; initial depth
        p29 = (p29 = 0 ? .001 : .0595 * p29)   ; closing depth
kfmdepth expseg .001, ifmfade, p28,idur - (ifmfade + (.15 * idur)), p29,.15*idur,.8*p29
kfmrate  linseg 0, ifmfade, p26,idur - (ifmfade  + (.1 * idur)), p27
        goto dofm

audiofm:   ; audio rate frequency modulation creates sidebands:
           ; high control rate recommended, such as 11025 or 8820
        i26 = abs(p26)
        k25 = (i26 * kcps)
        p27 = (p27 < 0 ? p27 : p26)
        i27 = abs(p27)
        k26 = (i27 * kcps)
    kfmfade  expseg .001, ifmfade, 1, p3, 1
    kfmrate  oscil k26 - k25, 1/p3 ,50
    kfmrate = (kfmrate + k25) 
    kfmdepth oscil  p29 - p28, 1/p3 , 60
    kfmdepth = (kfmdepth + p28)  * kfmfade

dofm:
     afm    oscil  kfmdepth * kcps, kfmrate, ifmfunc, frac(idur + icps)
     kfm  downsamp  afm
kcps = kcps + kfm
gotfm:
 ; ------------------------------------------------------
 ; (3) RANDOM FREQUENCY DEVIATION
irfdtest = p31 + p32
if irfdtest == 0 goto gotfreqrd
     p31 = (p31 = 0 ? .001 : p31 * .059)
     p32 = (p32 = 0 ? .001 : p32 * .059)
k1   expon  p31 , idur, p32
krandfreq   randi  k1, p33
kcps = kcps + (krandfreq * kcps)

gotfreqrd:
 ; ==============================================================
       ; Determine "peak" amplitude:
	iamp init 0
	iamp = (p5  < 10.1 ? p5  : iamp) ; p5  = multiplier for original soundfile amp.
	iamp = (p5  > 10.1 ? p5/32767 : iamp) ; p5  gives new raw amplitude
	iamp = (iamp = 0 ? 1. : iamp)
a1	loscil	iamp, kcps , ifno, ibasepitch , p10, p11, p12
 ; ==============================================================
; FADE OUT and OPTIONAL NEW AMPLITUDE ENVELOPE
	irise = (p14 = 0? .0001 : p14)
	irise = (p14 < 100 ? irise : (p14 - 100)  * idur)
	irise = (p14 < 0 ? abs(p14)  * idur : irise)
	iatss = (p16 = 0  ? 1 : p16)
	idec = (p15 = 0? .0001 : p15)
	idec = (p15 < 100 ? idec : (p15 - 100)  * idur)
	idec = (p15 < 0 ? abs(p15)  * idur : idec)

        idec = (idec > .04 ? idec : .04) ; default decay of .04 to avoid clicks
                               ; if p3 is less than duration of input soundfile
	amp expseg .005, irise, 1., idur - (irise + idec), iatss , idec, .005
        a1 = a1 * amp
; ------------------------------------------
 ;  AMPLITUDE MODULATION : Tremolo or audio rate {sidebands}
; p34 = freq. mod. fade-in duration  ; p35 = initial am freq. ; p36 = closing freq.
; p37 = initial depth  ; p38 = closing depth ; p39 = function
iamtest =  p37 + p38
if iamtest = 0 goto gotam
        iamfade = (p34 = 0 ? .001 : p34)
        iamfade = (p34  < 0 ? abs(p34) * idur : iamfade)
        iamfade = (p34  > 100 ? (p34 - 100) * idur : iamfade)
        iamfunc = (p39 = 0 ? 100 : p39)

if p35 < 0 goto audioam
        p37 = (p37 = 0 ? .001 : p37)   ; initial depth
        p38 = (p38 = 0 ? .001 : p38)   ; closing depth
kamdepth expseg .001, iamfade, p37,idur - (iamfade) , p38
     kamfreq  linseg 0, iamfade, p35,idur - (iamfade  + (.1 * idur)), p36
     kam    oscili  kamdepth , kamfreq, iamfunc, frac(idur + icps)
     a1 = ((1. - kamdepth) * a1) + (kam * a1)
     goto gotam
audioam:   ; audio rate amplitude modulation creates sidebands:
        i34 = abs(p35)
        k34 = (i34 * kcps)
        p36 = (p36 < 0 ? p36 : p35)
        i35 = abs(p36)
        k35 = (i35 * kcps)
    kamfade  expseg .001, iamfade, 1, p3, 1
    kamfreq  oscil k35 - k34, 1/p3 ,50
    kamfreq = (kamfreq + k34) 
    kamdepth oscil  p38 - p37, 1/p3 , 60
    kamdepth = (kamdepth + p37)  * kamfade
    am   oscili  kamdepth , kamfreq, iamfunc, frac(idur + icps)
    a1 = ((1. - kamdepth) * a1) + (am * a1)
gotam:
;--------------------------------------------------------
 ; RANDOM AMPLITUDE DEVIATION
iramdtest = p40 + p41
if iramdtest == 0 goto gotamrd
        p40 = (p40 = 0 ? .001 : p40 )
        p41 = (p41 = 0 ? .001 : p41 )
   k1   expon  p40 , idur, p41
   krandam   randi  k1, p42
   a1 =  ((1. - k1) * a1) + (krandam * a1) 
gotamrd:

; ------------------------------------------
; OPTIONAL BRIGHTNESS -- use with new envelope for cresc & dim.
bright:  ibrightest = p17 + p18
if ibrightest = 0 goto output
kbright init p17
if p18 = 0 goto brightscale
   ip19 = (p19 > 100 ? ((p19 - 100) * idur) - .1 : p19)
   ip19 = (p19 < 0 ? (abs(p19) * idur) - .1 : ip19)
   ip19 = (ip19 = 0 ? (.9*idur) - idec : ip19)

   ip20 init icps
if p20 = 0 igoto gotcf
   ip20 = (p20 < 13.1 ? cpspch(p20) : p20 )
gotcf:
   kbright expseg p17, .1,p17,ip19, p18, idur, p18
brightscale:
      ihp = icps*1.5
        iscale = octcps(ihp)
        iscale = (18.25 - iscale) * .1
        iscale = (iscale < 1. ? iscale : iscale * iscale )
      ihp = ihp * iscale
   ahi atone .9*a1, ihp
   alo tone  .9*a1, ihp
   a1 balance (kbright*ahi) + ((2. - kbright)* alo), a1
 ; --------------------------------------------------------
output:
OUTBIGSAMP
