/*
;------------------------------------------------
;------------------------------------------------
;----------- 2008 Luis Antunes Pena -------------
;----http://icem.folkwang-hochschule.de/~pena----
;------------------------------------------------
;------------------------------------------------


(C)2008 Luis Antunes Pena
Licensed under Creative Commons

(cc) Creative Commons Attribution-ShareAlike 3.0
	 
	You are free:
	
		# to Share -- to copy, distribute and transmit the work
		# to Remix -- to adapt the work
		

	Under the following conditions:

		# Attribution. You must attribute the work in the manner specified by the author
		  or licensor (but not in any way that suggests that they endorse you or your use of the work).
		# Share Alike. If you alter, transform, or build upon this work, you may 
		  distribute the resulting work only under the same, similar or a compatible license.
		

	# For any reuse or distribution, you must make clear to others the license
	  terms of this work. The best way to do this is with a link to this web page.
	# Any of the above conditions can be waived if you get permission from the copyright holder.
	# Nothing in this license impairs or restricts the author's moral rights.
	  
	http://creativecommons.org/licenses/by-sa/3.0/
	http://freedomdefined.org/Definition
*/



<CsoundSynthesizer>
<CsOptions>
-odac -m0 -d
</CsOptions>

<CsInstruments>



  sr        =  44100
  ksmps     =  128
  nchnls    =  2

  gktime    init      1
  gkmeth    init      1
  gkrandicps   init   0
  garsend1  init      0
  garsend2  init      0
  gaout1    init      0
  gaout2    init      0
  gkampalgoonoff      init  0

            FLpanel   "pluck", 550+150, 270, 250, 200

  idisp_v_slider1     FLvalue                     "amp", 45, 17, 38, 170
  gkamp, idisp_slider1  FLslider                  "", 1, 0.001,  -1, 24, idisp_v_slider1, 20, 150, 50, 10

  ixshift   =  60

  idisp_v_slider2     FLvalue                     "cps", 45, 17, 38+ixshift, 170
  gkcps, idisp_slider2  FLslider                  "", 1, 10000,  -1, 24, idisp_v_slider2, 20, 150, 50+ixshift, 10
  

  gkrand, idisp_rand  FLbutton                    "rand", 1, 0, 22, 55, 25,  35+ixshift, 205, -1
  gidisp_v_rand       FLvalue                     "r cps", 45, 17, 38+ixshift, 235


  idisp_v_slider3     FLvalue                     "icps", 45, 17, 38+ixshift*2, 170
  gkicps, idisp_slider3 FLslider                  "", 20, 10000,  -1, 24, idisp_v_slider3, 20, 150, 50+ixshift*2, 10

  gkrandicps, idisp_randicps  FLbutton            "rand", 1, 0, 22, 55, 25,  35+ixshift*2, 205, -1
  gidisp_v_randicps   FLvalue                     "r cps", 45, 17, 38+ixshift*2, 235

  gkmeth, idisp_counter FLcount                   "meth", 1, 6, 1, 2, 1, 100, 30, 38+ixshift*3, 50, -1

  idisp_v_slider4     FLvalue                     "tempo", 45, 17, 38+ixshift*5, 170
  gktime, idisp_slider4 FLslider                  "", 10, 240,  0, 24, idisp_v_slider4, 20, 150, 50+ixshift*5, 10

  idisp_v_slider5     FLvalue                     "parm 1", 45, 17, 38+ixshift*6, 170
  gkparm1, idisp_slider5  FLslider                "", 0.001, 1,  0, 24, idisp_v_slider5, 20, 150, 50+ixshift*6, 10

  idisp_v_slider6     FLvalue                     "parm 2", 45, 17, 38+ixshift*7, 170
  gkparm2, idisp_slider6  FLslider                "", 0.001, 1,  0, 24, idisp_v_slider6, 20, 150, 50+ixshift*7, 10

  idisp_v_slider7     FLvalue                     "", 48, 17, 50+ixshift*5, 240
  gkrsend, idisp_slider7  FLslider                "rev send", 0.001, 0.5,  0, 3, idisp_v_slider7, 150, 20, 50+ixshift*5, 220

  gidisp_v_slider8     FLvalue                     "", 45, 17, 38+ixshift*8, 180
  gkampalgo, idisp_slider8  FLslider                "algo", 0.001, 1,  0, 24, gidisp_v_slider8, 20, 150, 50+ixshift*8, 10
   gkampalgoonoff, giampalgoonoff FLbutton  "", 1, 0, 3, 10, 10, 25+ixshift*8+30, 220, -1

gidisp_v_time     FLvalue                     "rec", 45, 17, 38+ixshift*10-10, 130


  itype_b   =  21
  ii	    =  0
          ; record button
  krec, gihrec FLbutton	  "@circle", 1, 0, itype_b, 50, 50, 25+ixshift*10, 10, ii, 98, 0, 6000
	    ; record button

         ; stop record button
  ksrec, gihsrec FLbutton	  "@square", 1, 0, itype_b, 50, 50, 25+ixshift*10, 70, ii, 99, 0, 1
	    ; stop record button

            ; exit button
  kexit, gihexit      FLbutton "exit", 1, 0, itype_b, 50, 50, 25+ixshift*10, 200, ii, 20, 0, 0.1
            ; exit button


            FLpanelEnd  
            FLrun     

	    FLsetVal_i	60, idisp_slider4
	    FLsetVal_i	0, idisp_slider8
	    FLsetVal_i	1, idisp_slider1
	    FLsetVal_i	0, idisp_slider2
	    FLsetVal_i	0, idisp_slider3
	    FLsetVal_i	0, idisp_slider5
	    FLsetVal_i	0, idisp_slider6
	    FLsetVal_i	0, idisp_slider7
	    FLsetVal_i	1, idisp_counter

    instr 1


prints "%n---------------------------------%n------------P L U C K------------%n---- 2008 Luis Antunes Pena ----%n---------------------------------%n"

  kamp      =  gkamp * 10000
  kcps      =  gkcps

reset:

  icps      =  i(gkicps)
  ifn       =  0
  imeth     =  i(gkmeth)
  iparm1    =  i(gkparm1)            
  iparm2    =  i(gkparm2)            

  itime1     =  60 / (i(gktime) * 4) 		  ; 4 16th
itime = itime1+ birnd(itime1*.05)
  gitime    =  itime

if imeth == 1 then
  iparm1    =  1
elseif    imeth == 2 then
  iparm1    =  1
elseif    imeth == 3 then
  iparm2    =  1
elseif    imeth == 4 then
  iparm2    =  1
elseif    imeth == 5 then
     if ((iparm1+iparm2) > 0.5) then
          iparm1    =  0.1
          iparm2    =  0.1
      endif
elseif    imeth == 6 then
  iparm2    =  1
endif

            ; random freq
if gkrand == 1 then
  irand_max =  i(kcps)
  kcps_r    randomh   20,       irand_max, 1/itime
  kcps_p    =  i(kcps_r)
            FLprintk2   kcps_p, gidisp_v_rand
elseif gkrand == 0 then
  kcps_p    =  kcps
            FLprintk2   kcps_p, gidisp_v_rand

endif

            ; random i freq
if gkrandicps == 0 goto noirand
irand:
  iricps_max   =      i (gkicps)
  kicps     randomh   20, iricps_max, 1/itime
  icps      =  i(kicps)
            goto      contirand

noirand:
  icps      =  i (gkicps)
  
contirand:

            FLprintk2   icps, gidisp_v_randicps


            timout    0, itime, continue
            reinit    reset

continue:

  a1        pluck     kamp, kcps_p, icps, ifn, imeth, iparm1, iparm2

            ; random pan
  ipan1     =  rnd (1)
  ipan2     =  1-ipan1


            ; amp algo

  iampalgo  =  i(gkampalgo)
if iampalgo <= 0.001 then 
	    FLsetText	"OFF", gidisp_v_slider8
	    igoto     noampalgo
endif


	    FLsetText	"ON", gidisp_v_slider8
  iamp	    =  rnd (iampalgo)


if (gkampalgoonoff == 0) igoto continue_outs

if iamp < 0.5 then
  iamp	    =  0
elseif iamp >=0.5 then
  iamp	    =  1
endif

igoto continue_outs

noampalgo:
iamp = 0

continue_outs:

  aout1     =  a1 * ipan1 * (1 - iamp)
  aout2     =  a1 * ipan2 * (1 - iamp)


            outs      aout1, aout2

gaout1 = aout1
gaout2 = aout2
            ; reverb send
  irrand    =  int(rnd  (2))   
  garsend1  =  aout1 * gkrsend * irrand
  garsend2  =  aout2 * gkrsend * irrand


            rireturn  

    endin

    instr 2

  idur      =  0.1
  iwhen     =  0

  kout1a     trigger   gkmeth, 0.5, 0
            schedkwhen  kout1a, 0, 1, 11, iwhen, idur
  kout1b     trigger   gkmeth, 1.5, 1
            schedkwhen  kout1b, 0, 1, 11, iwhen, idur

  kout2a    trigger   gkmeth, 1.5, 0
            schedkwhen  kout2a, 0, 1,  12, iwhen, idur
  kout2b    trigger   gkmeth, 2.5, 1
            schedkwhen  kout2b, 0, 1,  12, iwhen, idur

  kout3a    trigger   gkmeth, 2.5, 0
            schedkwhen  kout3a, 0, 1,  13, iwhen, idur
  kout3b    trigger   gkmeth, 3.5, 1
            schedkwhen  kout3b, 0, 1,  13, iwhen, idur

  kout4a    trigger   gkmeth, 3.5, 0
            schedkwhen  kout4a, 0, 1,  14, iwhen, idur
  kout4b    trigger   gkmeth, 4.5, 1
            schedkwhen  kout4b, 0, 1,  14, iwhen, idur

  kout5a    trigger   gkmeth, 4.5, 0
            schedkwhen  kout5a, 0, 1,  15, iwhen, idur
  kout5b    trigger   gkmeth, 5.5, 1
            schedkwhen  kout5b, 0, 1,  15, iwhen, idur

  kout6a     trigger   gkmeth, 5.5, 0
            schedkwhen  kout6a, 0, 1,  16, iwhen, idur


    endin


    instr 11
            prints    "%n---------%nsimple averaging%nignore iparm 1 and 2%n---------%n" 
    endin


    instr 12
            prints    "%n---------%nstretched averaging%nparm 1 set to 1 / ignore parm 2%n---------%n"
    endin


    instr 13
             prints    "%n---------%nsimple drum%nparm1: roughness factor%n---------%n"
    endin


    instr 14
            prints    "%n---------%nstretched drum%nparm1: roughness factor / parm2: strech factor set to 1%n---------%n"
    endin


    instr 15
            prints    "%n---------%nweighted averaging%nparm1: roughness factor / parm2: strech factor set to 1%n(iparm1 + iparm2must be <= 1)%n---------%n"
    endin


    instr 16
            prints    "%n---------%n1st order recursive filter, with coefs .5. Unaffected by parameter values.%n---------%n"
    endin


   instr 20				; exit csound
	; event_i "scorechar", iinsnum, idelay, idur, [, ip4] [, ip5] [, ...]
	    event_i   "e", 1, 0, .01
    endin

    instr 31
            prints    "%n---------%amp algorythm active%namp has only values of zero or one---------%n"
    endin
    instr 32
            prints    "%n---------%amp algorythm non active%n---------%n"
    endin


    instr 97

  kfblvl    randomh   0.5, 0.95, 1/gitime
  ifco      =  sr/2
  ar1, ar2  reverbsc  garsend1, garsend2, kfblvl, ifco

            outs      ar1, ar2
garevout1 = ar1
garevout2 = ar2

  garsend1  =  0
  garsend2  =  0


    endin


    instr 98 					  ; RECORDING INSTRUMENT
gkenvrec = 1
  iformat   =  4
	    fout      "pluck_rec.aiff", iformat, gkenvrec * (gaout1+garevout1), gkenvrec * (gaout2+garevout2) 
ired = 255
igreen = 0
iblue = 0
FLsetColor ired, igreen, iblue, gihrec

prints "%n ...recording to file pluck_rec.aiff...%n"

  ixshift   =  60

ktime timeinsts
FLprintk 1, ktime, gidisp_v_time


    endin

    instr 99
ired = 0
igreen = 255
iblue = 0
FLsetColor ired, igreen, iblue, gihrec
; FLsetVal_i 0, gihrec
irel = 0.1
gkenvrec expon 1, irel, 0.001
prints "%nstop recording%n"
turnoff2 98, 0, irel
turnoff
    endin

</CsInstruments>

<CsScore>
i1  0  6000
i2  0  6000
i97 0  6000 				; reverb

</CsScore>
</CsoundSynthesizer>
