; ### Eastman Orchestra Library Instrument  p_l_u_n_k ###
define([NEWPAR],23)dnl

ifelse(NCHNLS,2,[define([STEREO_OUT],[ipan	 = p22
[a2 = a1*sqrt(1-ipan)  ; stereo orchestra : right channel output]
[a1 = a1*sqrt(ipan)  ; left channel output]
[outs a1, a2]
])])dnl
ifdef([PTSOURCE],[define([PTPITCH],k2)])dnl
; function needed : sine wave (100)
; p fields :
; p6 attack time (usually < .01)
; p7   imeth : 0 = method 5 (weighted averaging) producing short "pizz"
;            1 = method 6 (recursive filter) -- unaffected by p8
;            2 = method 2 (strethed averaging) -- buzziest -- p8 >=1 for stretch
; p8  stretch (if p7 = 2) or weighted average (if p7 = 0) factor
; p9  brightness : range -1. to +1. ; 0 = ord. , +1. = brightest, -1.= mellowest
;  p10 - p17 tremolo, vibrato or trill : 
; p10 flag : 0 = do nothing ; 1 = tremolo ; 2 = vibrato
; p11 1st trem, vib or tr rate ; p12 2nd rate ; p13 trem, vib or tr fade-in time
; p14  "  "   "    "   "  depth ; p15 2nd depth  
; p16  r.d. rate ; p17 r.d. depth
; p18-20 optional glissando: p18 = 2nd pitch, p19 = p4 pitch dur.,
;      p20 = gliss. function
; p21 = chorus detuning p-fields
; p22 : stereo pan location {for stereo only}

instr plunk
   p9 = (p9 = 0 ? 1. : p9)  ; brightness
i1 = (p4>13.00? p4 : cpspch(p4))
        ;  --- Detuning  module (mostly for use with "chorus" ) ---
  idetunepf = p21
if idetunepf = 0 goto detunedone ;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
i1 = i1 * idetune 
detunedone:   ; ---end of detuning module -------
i2 = octcps(i1)
i3 = (18-i2) * .1  ; scalar
i4 = 1/i3  ; scalar : 8.00 = 1 / 5.00 = .77 / 12.00 = 1.67
i5 = i4*i4  ;  8.00 = 1 / 5.00 = .625 / 12.00 = 2.78
; imethod :
i6 init 0
i6 = (p7 =  0 ? 5 : p7)
i6 = (p7 =  1 ? 6 : i6)
i6 = (p7 =  2 ? 2 : i6)
; stretch or shrink dur. & decay rate of higher partials
i8 init 0
i9 init 0
   if p7 = 1 goto didit
   if p7 = 2 goto stretch
; for p7 = 0 (method 5) , p8 = contract factor, must be <= 1.
   i7 = p8*i4
   i7 = p8+.1
   i7 = (i7 < 1. ? i7 : .99)
   i8 = (i4*.5)*i7
   i9 = i7 - i8
   goto didit
stretch:
     ; for p7 = 2 (method 2) , p8 = stretch factor, must be >= 1.
     i7 = (p8+1)*i4*.6        ; but timbre gets very buzzy if over 1.5 or so
     i7 = (i7 > 1.01 ? i7 : 1.01)
     i8 = (i7 < 1.8 ? i7 : 1.8)
didit:
i10 = (i2 < 8. ? p6 : p6/i5*i3)
i11 = i5*p5
k1 expseg 1, i10,i11,i4*.5*p3,p5/i4, i3*.5*p3,1 ; fade out of dc bias
k2 init i1
; Glissando :
if p18 = 0 goto nogliss
i14 = (p18> 13 ? p18 : cpspch(p18))
p19 = (p19 < 100 ? p19 : (p19-100)*p3)
p20 = (p20 < 100 ? p20 : (p20-100)*p3)
k2 expseg i1,p19,i1,p20,i14,p3-(p19+p20),i14
nogliss:
; Tremolo or Vibrato
if p10 = 0 goto ready
i16 =  (p13 = 0? .001 : p13)
k3 linseg .01,i16,p11,.8*p3-i16,p12,.2*p3,p12   ; trem, vib or tr rate
k4 randi p16,p17,frac(i4)      ; rate r.d.
k3 = k3+(k4*k3)        ; trem, vib, or tr rate
k4 linseg 0,i16,p14,.7*p3-i16,p15,.3*p3,p15    ; depth
i13 = (p10 = 1? ((i4+1)/2)*.5 : .0007 )  ; adjust depth for tremolo or vib
k4 = k4*i13
k5 randi .6*p16,p17, frac(i5)
k4 = k4+(k5*k4)
k6 oscil k4,k3,100

if p10 = 1. kgoto ready
k2 = k2 + (k6*k2)     ; vib. or trill added to pitch
ready:
ifdef([PTSOURCE],[include(CSLIBDIR/Include/lpcptrack.h)])dnl
i15 = (i2 < 8. ? i1 : i1/i5/i5)  ; larger buffer for high pitches
;  display k2,p3
a1 pluck k1,k2,i15,0,i6,i8,i9
if p10 != 1. kgoto notrem

;   		k3 oscili .35,4.,100  ; YAY!!!!
a1 = ((1-p14)*a1) + (k6*a1)
		 ; a1 = ((1-.35)*a1) + (k3*a1)
notrem:
;   k1 rms a1
;  display k1,p3
	if p9 = 0 goto nofilt
	a2 tone a1,(2*i1)+(p9*i1)
	a3 atone a1,(2*i1)+(p9*i1)
	i12 = (p9+1)/2
	i12 = (i12>=0? i12 : 0)
	i12 = (i12 < 1.001 ? i12 : .99)
	a1 balance (i12*a3)+((1-i12)*a2),a1
nofilt:
