    ; csound header
sr=44100
kr=2205
ksmps=20
nchnls=1

;  #############################################################
;  soundfile ex6-1 : harmonizer  ECMC Csound Tutorial
;  #############################################################

   ;  p4 =  gen1 function.# number of source soundfile} ,

   ;     p6 = rise {fade-in} time
   ;     p7 = decay {fade-out} time

instr 1
;----- SOUNDFILE INPUT --------------------
	ifunc = p4 ; gen1 function number of input soundfile
	iampscale = (p5 = 0 ? 1. : p5)  ; adjust input amplitude
	irise = (p6 = 0 ? .001 : p6)   ; fade-in time
	idecay =( p7  = 0 ? .001 : p7)  ; fade-out time

    ; loscil arguments
    ibasepitch = (p8 < 13. ? cpspch(p8) : p8) ; base pitch of soundfile in pch or cps
    ioutpitch = (p9 < 13. ? cpspch(p9) : p9) ; output pitch in pch or cps
    ioutpitch = (p9 < 3 ? p9 * ibasepitch :ioutpitch) ; or as multiplier of basepitch

 ; output amplitude envelope
kamp  expseg  .005 ,irise  , iampscale  , p3 - (irise + idecay) , iampscale , idecay , .005
asource  loscil  kamp, ioutpitch, ifunc, ibasepitch

;----- HARMONIZER --------------------
	imin init 0
	imin = (p10 < 1. ? p10 * ioutpitch : imin)
	imin = (p10 > 1. && p10 < 13. ? cpspch(p10) : imin)
	imin = (p10 > 13 ? p10 : imin)

      ; max. freq. variance
                     ; as % of output source pitch or in pch or cps
	imaxvar = (p11 < 1. ? p11 : p11 / ioutpitch)
        imode = p12 ; if 0, iharm1 & iharm2 are ratios * ioutpitch
                    ;  if 1, iharm1 & iharm2 are notes in pch or cps
        iprd = p15  ; pitch analysis window size
iharm1 init p13
iharm2 init p14
if imode = 0 igoto doit
if p13 = 0 igoto harm2
	iharm1 = (p13 < 13. ? cpspch(p13) : p13)
harm2: if p14 = 0 igoto doit
	iharm2 = (p14 < 13. ? cpspch(p14) : p14)
doit:

aharm harmon asource, ioutpitch, imaxvar, iharm1, iharm2, imode, imin, iprd
print ioutpitch, imaxvar, iharm1, iharm2, imode, imin, iprd
;----- SIGNAL OUTPUTS  {source/harmonizer mix} ---------------
out (p16 * asource) + (p17 * aharm)
endin

