; ex3-6:
 ; p4 : gen1 function number
 ; p5 : amplitude  {.001 - 10. = multiplier ; 10.1 - 32767 = new raw amplitude}
 ; p6 = length of input soundfile, in seconds or samples
 ; p7 : skip time (if positive) OR (if negative, between -.001 & -.99) index
        ; into gen1 func
 ; p8 : OPTIONAL end time (if positive) OR (if negative, between -.001 & -.99) index
 ; p9 : pitch multiplier
 ; p10  flag: if -1, soundfile read backwards from p8 to p7
 ; p11 : stereo pan location {for stereo only}

instr 1
   ;  init values : --------------
; p9 specifies pitch transposition ratio of output to input pitch levels
itransp	 = (p9 = 0 ? 1. : p9 )
ilength = (p6 < 400 ? p6 * sr : p6)
iamp    =  (p5 = 0 ? 1. : p5) ; amplitude multiplier; 0 = no attenuation or boost

   isound   = p4  ; number of gen1 function table of source soundfile
   iphspeed  = sr/ilength  ; phasor speed to read soundfile at original pitch
   isfdur   = ilength/sr ; duration in seconds of input soundfile

 ; if p7 is negative, between -.001 and -1., it indicates % of soundfile skipped
 ; if positive, it indicates skip time in seconds
istart = (p7 < 0 ? abs(p7) *isfdur : p7) ; time in seconds to begin
                ; reading in samples from the input soundfile
index    =  (p7 < 0 ? abs(p7) : p7/isfdur)  ; beginning index into gen 1 table
iend = (p8 = 0 ? isfdur : p8)
iend = (iend < 0 ? abs(p8) * isfdur : iend)

ioutdur = (iend - istart) / itransp ; compute actual output duration
p3 = ioutdur ; change score p3 value to computed output duration for note
print istart, iend, itransp, ioutdur

if p10 = -1 goto backwards
 ; read soundfile forwards:
     apointer  phasor   iphspeed*itransp   ;transpose pitch by p9
     asound  tablei   (index + apointer)*ilength, isound  ;read the table at current phasor index 
          goto gotsound
 backwards:  ; read soundfile backwards beginning at p7 time
        ibacklength = p3 * itransp * sr
        ibackspeed = sr/ibacklength
        apointer  phasor  ibackspeed * itransp
        apointer = 1. - apointer
        iratio = ( ibacklength/ilength)
        apointer = apointer * iratio
        asound   tablei  (index + apointer) *ilength, isound 

gotsound:
; mono out:
out iamp * asound
; stereo out
;    outs sqrt(p11) * asound, sqrt(1. - p11) * asound
endin

