<info>
ESM Csound Library example tsamp1
orc: tsamp   sco: tsamp1
Stereo out {p13}
Only the first 1.5 seconds of input soundfile voicetest used {p8}, transposed
up & down a major 3rd, minor 6th & octave {p4}. 
Allan Schindler  1/97

</info>
<tk_interface>


</tk_interface>
<mono>
    

</mono>
<stereo>
; p4 = transposition 
; p5  = amplitude (0 -1 ) integer 0 -127 = midi veloc or 1000 - 32000 raw value
; p6 = detuning (additional transposition, mostly for chorusing)
; p7 = input gen1 function # { if f99 & f98 not used}

instr 2

idur = p3
icps = 261.626  ; initial input pitch level for loscil
ibasepitch = 261.626  ; middle C reference input pitch for loscil
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 transposition {p4 & p6}
itransp init 1.  
idetune = p4 + p6  ; total pitch offset: transposition (p4) + detuning (p6)
if idetune = 0 goto gottransp ;skip all this if no transposition or detuning
       icount init 0  ; counter
       imult =  (idetune > 0 ? 1.05946 : .94387) ;1/2 step freqency ratios
        icheck = abs(idetune)
        icheck = int(icheck)

     dumbloop:
	      icount = icount + 1   ; increment counter
	      imult = (icheck = 0 ? 1 : imult)
	      itransp = itransp * imult 
     if icount < icheck igoto dumbloop
 ; microtonal transposition &/or detuning:
	imult = abs(idetune)
	imult = frac(imult)
	imult = imult * .05496
	imult = (idetune > 0 ? imult : - imult)
	itransp = itransp + imult
gottransp:
icps = icps * itransp
; ------------------------------------------
; get gen01 audio function number 
ifno = (p7 = 0 ? 1 : p7 )      ; input soundfile function number
; -----------------------------------------------------------
; 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:
 ; ==============================================================
       ; 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, icps , 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
; ------------------------------------------
; 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 : p19)
   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:
; 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 = (p4 > 12. ? 12. : p4)
        iplace = (p4 < -12. ? -12. : p4)
        iplace = .5 + (iplace * .0416)
     a2 = iplace * a1
     a1 = (1. - iplace) * a1
goto  done

rightlow:  ; p13 = -2 : lowest notes to RC, highest notes to LC 
        iplace = (p4 > 12. ? 12. : p4)
        iplace = (p4 < -12. ? -12. : p4)
        iplace = .5 + (iplace * .0416)
     a2 = (1. - iplace) * a1
     a1 = 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 = (1. - kplace) * a1
     a1 = 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 = (1. - kplace) * a1
     a1 = kplace * a1

done:
;Standard out statement
outs a1,a2
endin


</stereo>
<quad>


</quad>
<score>
f1 0 0 -1 "/sflib/x/voicetest" 0 0 0 
f100 0 1024 10 1.
f50 0 65 7 0 64 1.
f52 0 65 7 0 32 1. 32 0
f60 0 65 5 .01 64 1.
f57 0 64 7 0 31 0 1 1. 31 1. 1 0 
f58 0 64 7 0 21 0 1 1. 20 1. 1 -1 20 -1 1 0 
  i2 0.000 3.000 0 0.600 0 1 0 1.450 0 0 0 0.500 0 0 0 0 0 0 0
  i2 1.200 3.000 -4 0.600 0 1 0 1.450 0 0 0 0.650 0 0 0 0 0 0 0
  i2 2.400 3.000 4 0.600 0 1 0 1.450 0 0 0 0.350 0 0 0 0 0 0 0
  i2 3.600 3.000 -8 0.600 0 1 0 1.450 0 0 0 0.800 0 0 0 0 0 0 0
  i2 4.800 3.000 8 0.600 0 1 0 1.450 0 0 0 0.200 0 0 0 0 0 0 0
  i2 6.000 3.000 -12 0.600 0 1 0 1.450 0 0 0 0.950 0 0 0 0 0 0 0
  i2 7.200 3.000 12 0.600 0 1 0 1.450 0 0 0 0.050 0 0 0 0 0 0 0
e

</score>


