<info>
ESM Csound Library instrument algorithm carillon
Mono only
Allan Schindler  1/97
</info>
<tk_interface>


</tk_interface>
<mono>
; ### Eastman Csound Library Instrument c_a_r_i_l_l_o_n ###
; Functions needed:  61 100
; p fields:
; p7 attack hardness (.8-1.4)           ; p8 = brightness (.5-1.4)  
; p9 = % tremolo (c. .07)               ; p10 = tremolo rate (c. 5.5-6.)
; p11 = chorus detuning

instr 34
; Set defaults
	p7 = (p7 = 0 ? 1. : p7)
	p8 = (p8 = 0 ? 1. : p8)
p4 = (p4>0?p4:(abs(p4))/100)  ; for microtones; -800.050 = quarter tone above c
i1 = (p4<13.01? cpspch(p4) : p4 )
        ;  --- Detuning  module (mostly for use with "chorus" ) ---
  idetunepf = p11
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                         ; c4=1.,c3=1.1,c5=.9,etc.
i4 = (i3+1)/2
i4 = (i4*p8<1.4?i4:1.4/p8)              ; protection against bomb outs
i5 = ((12.75-i2)*.4*((p7+1)/2))+3.7    ; duration is frequency dependent
i5 = (i5<p3?i5:p3)                     ; for shorter notes
p5 = p5*((p7+1)/2)*i4       
p6 = i3*p6/p7
p8 = p8*i4
p10 = i4*p10

a1 linseg 0,p6,1,((2/(p7+1))*.05)-p6,(3/(p7+2))*i4*.53,.1*i5,i4*.26,.15*i5,i4*.15,.25*i5,i4*.1,(.5*i5)-.05,0,i5,0
a1 = a1*p5
k1 rms a1
k2 linseg 1000,p6,0,p3,0     ; used only to avoid turning instr off at outset
k2 = k2+k1
if k2 > 20 kgoto contin
turnoff

; Tremolo
contin:
p9 = (p9=0? .001 : p9)
p9 = p9*i4
p10 = (p10 = 0? .001 : p10)
k2 = k1*p9                             ;% of tremolo
a1 = a1-k2
p10 = p10*(1/i4)			; faster tremolo for higher notes
k3 expon 1.2*p10,p3,.6*p10
;  k4 randi .07*k3,2.1
k4 randi .09*k3,2.8
k2 oscili k2,k3+k4,100
; Random amplitude deviation
;   k3 expseg p7*.18,p6,p7*.06,i5-p6,.04
;   k3 randi k3,50/i4
;  k3 expseg p7*.19,p6,p7*.066,i5-p6,i4*.04
k3 expseg p7*.19,p6,p7*.073,i5-p6,i4*.046
k3 randi k3,30/i4
a1 = a1+k2+(k3*a1)            ; Total amplitude

; ATTACK CHIFF
k2 expseg p7*i3,i4*.1*p6,i4,((p7+1)/2)*(.65*p6),.004,p3,.002
k2 randi k2*.5*i1,999                         
k2 = k2+i1
k3 expseg p7*(p8*i2*300),p7*.7*p6,.01,p3,.01
a5 randi k3,800  ; 999 before               
a5 = a5+(p8*420*i2)
k3 expseg p7*.4,p7*.65*p6,.001,p3,.0001
a5 oscili k3*a1,a5,100          ; Attack chiff

; - - -
; Pitch oscillators: 3  freq. mod. carriers & 1 sine wave oscillator (a4)
i8 = p7*p8*i4*(i2<9?1:((12.3-i2)*.3))
k4 oscil1i p7*p8*p6,i8*.3,(i5-p6)*i8*.62,61  ;amplitude of carrier 2
k5 oscil1i p7*p8*p6,p8*i4*.28,(i5-p6)*p8*i3*.23,61  ; amplitude of carrier 3
k6 linseg .1,p6,.1,(.38*i5)-p6,.2,.6*i5,.05          ; amplitude for a4
k6 = p8*k6
k7 expseg p7*i3* 1.6,p6,i4*1.2*p8,i5,i4*p8 ; filter mulitplier for attack chiff

; - - Carrier 2
k3 expseg p7*3,p6,2.5,i4*.07*i5,2.,i4*.1*i5,1.2,.8*i5,.8         
k3 = k3*p8*i4
a2 foscili k4*a1,k2+.5,.504,.498,k3,100
a2 reson a2,1.5*i1,k7*3.5*i1,1
; - - Carrier 3
k3 expseg p7*i4*2.8,p6,1.8,i3*p8*.06*i5,1.2,i3*p8*.09*i5,1.,p3,.1
k3 = k3*p8*i3
i6 = (4*i1<sr/2?3.996:2.996)             ; foldover protection
i7 = (i1<1800*(sr/20000)?1.:0)          ; 0 = foldover protection
a3 foscili k5*a1,k2+.7,i6,1.33,k3*i7,100
a3 reson a3,5.33*i1,k7*2.8*i1,1

a4 oscili k6*a1,1.22*k2,100
; - - Carrier 1
k3 expseg p7*i4*2.8,p7*.7*p6,p7,1/p7*.3*p6,2.2,.12*i5,1.6,.13*i5,1.2,.75*i5-p6,.3         
k3 = k3*p8*i4
a1 foscili a1-((k4+k5+k6)*a1),k2,1,1.004,k3,100
a1 reson a1,2.5*i1,k7*3.1*i1,1

a1 = 1.7*(a1+a2+a3+a4)                      ; restore gain lost in filters
a1 = a1+a5
;Standard out statement
out a1
endin
    
</mono>
<stereo>


</stereo>
<quad>


</quad>
<score>

</score>


