<info>
ESM Csound Library
orc: samp   sco: samp2
Stereo output.
Piano range extended to "93 keys" {g0 through ef8}. Stereo out {p13},
with lowest notes on right channel, highest tones on left channel.
Only fragments of some input soundfiles used {duty factor} to produce
staccato tones near beginning.

Allan Schindler  1/97
</info>
<tk_interface>


</tk_interface>
<mono>
</mono>
<stereo>


; p5  = amplitude (0 -1 ) integer 0 -127 = midi veloc or 1000 - 32000 raw value
; p4 = pitch :  0 - 13. = pch  |  14 - 127 = midi note | negative = herz
; p6 = detuning
; p7 = input gen1 function # { if f99 & f98 not used}
; p8 = input pitch  { if f99 & f98 not used}

instr 1

idur = p3
ibasepitch init 0
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 pitch & midi note number
if p4 > 13.0 igoto midinote
if p4  < 0  igoto herz
     ; p4 is in pch
     icps = cpspch(p4)
        ; convert pch to midi note number
        imnum =  int((p4) - 3) * 12
        imnum =  imnum + (frac(p4) * 100)
;            ioct = octcps(icps) ; DELETE -- NOT NEEDED HERE
     igoto gotnote
midinote: 
     ; p4 gives midi note number
     imnum = p4
        ; convert midi note num to cps
        ioct = (p4/12) + 3.  ; returns oct decimal
        icps  =  cpsoct(ioct)
     igoto gotnote
herz: ; p4 is negative, so it is in herz
     icps = abs(p4)
     ioct = octcps(icps) 
     ipch = pchoct(ioct) 
        ; convert pch to midi note number
     imnum =  int((ipch) - 3) * 12
     imnum =  imnum + (frac(ipch) * 100)
gotnote:

; ------------------------------------------
; get gen01 audio function number & base pitch of soundfile
if p7 = 0 igoto tables
    ifno = p7      ; input soundfile function number
   ; if gen1 func specified in p7, p8 = input pitch : pch, MIDI note or herz
     if p8 > 13.0 igoto inmidinote
     if p8  < 0  igoto inherz
          ; p8 is in pch
          ibasepitch = cpspch(p8)
          igoto gotinpitch
     inmidinote: if p8 < 0  igoto inherz
          ; p8 gives midi note number : convert this to cps
             inoct = (p8/12) + 3.  ; returns oct decimal
             ibasepitch  =  cpsoct(inoct)
          igoto gotinpitch
     inherz: ; p8 is negative, so it is in herz
          ibasepitch = abs(p8)
          igoto gotinpitch

tables:  ; use func tables 99 & 98 to get source soundfile
ifno	table	imnum, 99	;keyboard mapping to gen1 ftables 
ibasno	table	ifno, 98        ; returns midi notes of the sonudfile samples
ibasoct	=	ibasno/12. + 3.
ibasepitch = cpsoct(ibasoct)
gotinpitch:
; ------------------------------------------
        ;  --- Detuning  module (mostly for use with "chorus" ) ---
   idetunepf = p6
if idetunepf = 0 goto gotdetune ;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
icps = icps * idetune 
gotdetune:   ; ---end of detuning module -------
; -----------------------------------------------------------
; 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:
 ; ==============================================================
; insert for lpcpitch, xsynpitch, gxsynpitch:
kpitch init icps
 ; ==============================================================
       ; 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 = (110 - imnum) * .01 * 1.1 
        iplace = (iplace > 1. ? 1. : iplace) 
        iplace = (iplace < 0 ? 0 : iplace) 
     a2 = (1. - iplace) * a1
     a1 = iplace * a1
goto  done

rightlow:  ; p13 = -2 : lowest notes to RC, highest notes to LC 
        iplace = (110 - imnum) * .01 * 1.1 
        iplace = (iplace > 1. ? 1. : iplace) 
        iplace = (iplace < 0 ? 0 : iplace) 
     a2 = iplace * a1
     a1 = (1. - 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/string/pn.bf0" 0 0 0
f2 0 0 -1 "/sflib/string/pn.d1" 0 0 0
f3 0 0 -1 "/sflib/string/pn.g1" 0 0 0
f4 0 0 -1 "/sflib/string/pn.c2" 0 0 0
f5 0 0 -1 "/sflib/string/pn.f2" 0 0 0
f6 0 0 -1 "/sflib/string/pn.bf2" 0 0 0
f7 0 0 -1 "/sflib/string/pn.ef3" 0 0 0
f8 0 0 -1 "/sflib/string/pn.af3" 0 0 0
f9 0 0 -1 "/sflib/string/pn.cs4" 0 0 0
f10 0 0 -1 "/sflib/string/pn.fs4" 0 0 0
f11 0 0 -1 "/sflib/string/pn.b4" 0 0 0
f12 0 0 -1 "/sflib/string/pn.e5" 0 0 0
f13 0 0 -1 "/sflib/string/pn.a5" 0 0 0
f14 0 0 -1 "/sflib/string/pn.d6" 0 0 0
f15 0 0 -1 "/sflib/string/pn.g6" 0 0 0
f16 0 0 -1 "/sflib/string/pn.c7" 0 0 0
f17 0 0 -1 "/sflib/string/pn.ef7" 0 0 0
f18 0 0 -1 "/sflib/string/pn.fs7" 0 0 0
f19 0 0 -1 "/sflib/string/pn.a7" 0 0 0
f20 0 0 -1 "/sflib/string/pn.c8" 0 0 0
f99 0 128 -17 0 1 24 2 29 3 34 4 39 5 43 6 49 7 54 8 59 9 64 10 69 11 74 12 79 13 84 14 89 15 94 16 98 17 100 18 104 19 107 20
f98 0 32 -2 0 22 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 99 102 105 108
f100 0 1024 10 1.
  i1 0.000 2.368 5.02 7000 0 0 0 0 0 0 0 -2 0 0.046 0.625 0 0 0 0
  i1 0.183 0.168 5.09 7821 0 0 0 0 0 0 0 -2 0 0.054 0.756 0 0 0 0
  i1 0.367 0.168 6.04 8739 0 0 0 0 0 0 0 -2 0 0.050 0.841 0 0 0 0
  i1 0.555 0.168 6.11 9764 0 0 0 0 0 0 0 -2 0 0.053 0.630 0 0 0 0
  i1 0.748 0.257 7.06 10909 0 0 0 0 0 0 0 -2 0 0.043 0.838 0 0 0 0
  i1 0.959 0.257 8.01 12188 0 0 0 0 0 0 0 -2 0 0.042 0.901 0 0 0 0
  i1 1.151 0.257 8.08 13618 0 0 0 0 0 0 0 -2 0 0.046 0.867 0 0 0 0
  i1 1.332 0.600 9.03 15215 0 0 0 0 0 0 0 -2 0 0.054 0.819 0 0 0 0
 ci1 1.535 0.600 9.03 0 0 0 0 0 0 0 0 -2 0 0.043 0.661 0 0 0 0 <rest>
  i1 2.308 2.000 10.06 13224 0 0 0 0 0 0 0 -2 0 0.050 0.630 0 0 0 0
  i1 2.321 2.000 9.11 13773 0 0 0 0 0 0 0 -2 0 0.042 0.872 0 0 0 0
  i1 2.529 1.500 4.07 12767 0 0 0 0 0 0 0 -2 0 0.040 0.710 0 0 0 0
  i1 2.916 0.257 9.00 12535 0 0 0 0 0 0 0 -2 0 0.043 0.840 0 0 0 0
  i1 3.101 0.257 9.09 10715 0 0 0 0 0 0 0 -2 0 0.053 0.824 0 0 0 0
  i1 3.305 0.257 10.03 9806 0 0 0 0 0 0 0 -2 0 0.042 0.612 0 0 0 0
  i1 3.512 1.000 10.08 9157 0 0 0 0 0 0 0 -2 0 0.055 0.672 0 0 0 0
  i1 3.692 1.000 11.01 9310 0 0 0 0 0 0 0 -2 0 0.042 0.846 0 0 0 0
  i1 3.886 1.000 11.10 8789 0 0 0 0 0 0 0 -2 0 0.044 0.759 0 0 0 0
  i1 4.097 1.000 11.03 8466 0 0 0 0 0 0 0 -2 0 0.052 0.659 0 0 0 0
 ci1 4.292 2.000 11.03 8023 0 0 0 0 0 0 0 -2 0 0.051 0.777 0 0 0 0 <rest>
  i1 5.073 2.000 10.01 12466 0 0 0 0 0 0 0 -2 0 0.051 0.651 0 0 0 0
  i1 5.270 2.000 12.00 14625 0 0 0 0 0 0 0 -2 0 0.049 0.622 0 0 0 0
  i1 5.476 2.000 12.02 17157 0 0 0 0 0 0 0 -2 0 0.050 0.715 0 0 0 0
  i1 5.676 2.000 12.03 17806 0 0 0 0 0 0 0 -2 0 0.048 0.754 0 0 0 0
  i1 5.885 2.000 11.11 13589 0 0 0 0 0 0 0 -2 0 0.054 0.669 0 0 0 0
  i1 6.073 2.368 10.10 10370 0 0 0 0 0 0 0 -2 0 0.043 0.798 0 0 0 0
e
</score>


