<info>
ESM Csound Library instrument algorithm gran
Mono or stereo out. If stereo, p10
determines left-right spatial placement.
Allan Schindler  1/97
</info>
<tk_interface>


</tk_interface>
<mono>
 ; granular synthesis algorithm, using soundfiles as grains
 ; duty factor : determines amount of time overlap between successive grains
 ; p4 : cross-fade times for both fade-in & fade-out
 ; p5 : amplitude  {.001 - 10. = multiplier ; 10.1 - 32767 = new raw amplitude}
 ; p6 : gen1 function number
 ; p7 : legnth of soundfile to be used, in duration (seconds) OR # of samples
 ; p8 : skip time (if positive) OR (if negative, between -.001 & -.99) index
        ; into gen1 func
 ; p9 : pitch multiplier
 ; p10 : stereo pan location {for stereo only}

instr 33
   ;  init values : --------------
isound   = p6  ; number of gen1 function table of source soundfile
 ; iamp = amplitude multiplier for non-normalized soundfiles
iamp	 = (p5 = 0 ? 1. : p5)
iamp = (iamp > 10.0 ? iamp/32767 : iamp )
ipitch	 = (p9 = 0 ? 1. : p9 )
ipitch	 = (p9 = 0 ? 1. : p9 )

ilen	 = ( p7 > 44 ? p7 : p7 * sr) ; number of samples in soundfile to be used
inom 	 = sr/ilen  ; (phasor speed to get original base frequncy of sample)
isfdur     = ( p7 > 44. ? ilen/sr : p7 ) ; duration in seconds of soundfile

 ; if p8 is negative, between -.001 and -1., it indicates % of soundfile skipped
 ; if positive, it indicates skip time in seconds
index    = ( p8 < 0 ? abs(p8) : p8/isfdur)  
ifade    = p4  ; (normally  should be c. 1/2 overlap durations of input notes)
ipan	 = p10
   ; end of init values ; now do it  ---------
aph	phasor	 inom*ipitch                ;transpose pitch by p9
asi	tablei 	(aph+index)*ilen, isound    ;read the table at index for p3 time
a1	linen	  asi*iamp, ifade, p3, ifade  ; envelope and amp variation
 a2 = a1*(1-ipan)  ; stereo orchestra : right channel output
a1 = a1*ipan  ; left channel output
;Standard out statement
outs a1,a2
endin

</mono>
<stereo>
 ; granular synthesis algorithm, using soundfiles as grains
 ; duty factor : determines amount of time overlap between successive grains
 ; p4 : cross-fade times for both fade-in & fade-out
 ; p5 : amplitude  {.001 - 10. = multiplier ; 10.1 - 32767 = new raw amplitude}
 ; p6 : gen1 function number
 ; p7 : legnth of soundfile to be used, in duration (seconds) OR # of samples
 ; p8 : skip time (if positive) OR (if negative, between -.001 & -.99) index
        ; into gen1 func
 ; p9 : pitch multiplier
 ; p10 : stereo pan location {for stereo only}

instr 33
   ;  init values : --------------
isound   = p6  ; number of gen1 function table of source soundfile
 ; iamp = amplitude multiplier for non-normalized soundfiles
iamp	 = (p5 = 0 ? 1. : p5)
iamp = (iamp > 10.0 ? iamp/32767 : iamp )
ipitch	 = (p9 = 0 ? 1. : p9 )
ipitch	 = (p9 = 0 ? 1. : p9 )

ilen	 = ( p7 > 44 ? p7 : p7 * sr) ; number of samples in soundfile to be used
inom 	 = sr/ilen  ; (phasor speed to get original base frequncy of sample)
isfdur     = ( p7 > 44. ? ilen/sr : p7 ) ; duration in seconds of soundfile

 ; if p8 is negative, between -.001 and -1., it indicates % of soundfile skipped
 ; if positive, it indicates skip time in seconds
index    = ( p8 < 0 ? abs(p8) : p8/isfdur)  
ifade    = p4  ; (normally  should be c. 1/2 overlap durations of input notes)
ipan	 = p10
   ; end of init values ; now do it  ---------
aph	phasor	 inom*ipitch                ;transpose pitch by p9
asi	tablei 	(aph+index)*ilen, isound    ;read the table at index for p3 time
a1	linen	  asi*iamp, ifade, p3, ifade  ; envelope and amp variation
 a2 = a1*(1-ipan)  ; stereo orchestra : right channel output
a1 = a1*ipan  ; left channel output
;Standard out statement
outs a1,a2
endin


</stereo>
<quad>


</quad>
<score>

</score>

