;Written by Iain McCurdy, 2009

<CsoundSynthesizer>

<CsOptions>
-odevaudio -b400
</CsOptions>

<CsInstruments>

sr 		= 	44100	;SAMPLE RATE
kr 		= 	4410	;CONTROL RATE
ksmps 		= 	10	;NUMBER OF AUDIO SAMPLES IN EACH CONTROL CYCLE
nchnls 		= 	2	;NUMBER OF CHANNELS (2=STEREO)

;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;					LABEL            | WIDTH | HEIGHT | X | Y
				FLpanel	"Risset Rhythms",   500,    490,    0,  0
;BUTTONS							ON | OFF | TYPE | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | IDUR
gkOnOff,ihOnOff			FLbutton	"On/Off",	1,    0,    2,     150,     28,    5,  5,    0,      3,      0,       -1

;BORDERS				ITYPE, IFONT, ISIZE, IWIDTH, IHEIGHT, IX, IY
ih		 	FLbox  	" ", 	6,        9,    15,    230,   150,    10, 150	;BORDER AROUND SOURCE
ih		 	FLbox  	" ", 	6,        9,    15,    230,   280,   260, 150	;BORDER AROUND LAYERS MIXER
ih		 	FLbox  	" ", 	6,        9,    15,    230,   110,    10, 320	;BORDER AROUND AMPLITUDE WINDOWS

;VALUE DISPLAY BOXES			LABEL  | WIDTH | HEIGHT | X | Y
idrate				FLvalue	" ",       70,    16,    10,  75

;TEXT BOXES						TYPE | FONT | SIZE | WIDTH | HEIGHT | X |  Y
ih		 	FLbox  	"SOURCE", 		1,       6,    14,     95,     20,   20,  155
ih		 	FLbox  	"LAYERS MIXER", 	1,       6,    14,    210,     20,  270,  155
ih		 	FLbox  	"AMPLITUDE WINDOWS", 	1,       6,    14,    210,     20,   20,  325

;SLIDERS						MIN | MAX | EXP | TYPE |    DISP | WIDTH | HEIGHT | X |  Y
gkrate, ihrate		FLslider 	"Rate (Hz.)",	-.1,   .1,   0,    3,     idrate,  480,     20,     10,  50
gkGSpeed, ihGSpeed	FLslider 	"Global Speed",	.25,    4,  -1,    3,         -1,  480,     20,     10, 100
gk1, gih1		FLslider 	"1",		0,      1,   0,    3,         -1,  200,     20,    280, 215
gk2, gih2		FLslider 	"2",		0,      1,   0,    3,         -1,  200,     20,    280, 235
gk3, gih3		FLslider 	"3",		0,      1,   0,    3,         -1,  200,     20,    280, 255
gk4, gih4		FLslider 	"4",		0,      1,   0,    3,         -1,  200,     20,    280, 275
gk5, gih5		FLslider 	"5",		0,      1,   0,    3,         -1,  200,     20,    280, 295
gk6, gih6		FLslider 	"6",		0,      1,   0,    3,         -1,  200,     20,    280, 315
gk7, gih7		FLslider 	"7",		0,      1,   0,    3,         -1,  200,     20,    280, 335
gk8, gih8		FLslider 	"8",		0,      1,   0,    3,         -1,  200,     20,    280, 355
gkamp1, gihamp1		FLslider 	"1",		0,      1,   0,    2,         -1,  25,      35,    280, 375
gkamp2, gihamp2		FLslider 	"2",		0,      1,   0,    2,         -1,  25,      35,    305, 375
gkamp3, gihamp3		FLslider 	"3",		0,      1,   0,    2,         -1,  25,      35,    330, 375
gkamp4, gihamp4		FLslider 	"4",		0,      1,   0,    2,         -1,  25,      35,    355, 375
gkamp5, gihamp5		FLslider 	"5",		0,      1,   0,    2,         -1,  25,      35,    380, 375
gkamp6, gihamp6		FLslider 	"6",		0,      1,   0,    2,         -1,  25,      35,    405, 375
gkamp7, gihamp7		FLslider 	"7",		0,      1,   0,    2,         -1,  25,      35,    430, 375
gkamp8, gihamp8		FLslider 	"8",		0,      1,   0,    2,         -1,  25,      35,    455, 375
gkOutGain, ihOutGain	FLslider 	"Output Gain",	0,      1,   0,    3,         -1,  480,     20,     10, 440

FLsetAlign	4, gih1	;
FLsetAlign	4, gih2	;
FLsetAlign	4, gih3	;
FLsetAlign	4, gih4	;
FLsetAlign	4, gih5	;
FLsetAlign	4, gih6	;
FLsetAlign	4, gih7	;
FLsetAlign	4, gih8	;

;            R    G   B
FLsetColor2 190, 190,190, gihamp1
FLsetColor2 190, 190,190, gihamp2
FLsetColor2 190, 190,190, gihamp3
FLsetColor2 190, 190,190, gihamp4
FLsetColor2 190, 190,190, gihamp5
FLsetColor2 190, 190,190, gihamp6
FLsetColor2 190, 190,190, gihamp7
FLsetColor2 190, 190,190, gihamp8
FLsetColor    0,   0,  0, gihamp1
FLsetColor    0,   0,  0, gihamp2
FLsetColor    0,   0,  0, gihamp3
FLsetColor    0,   0,  0, gihamp4
FLsetColor    0,   0,  0, gihamp5
FLsetColor    0,   0,  0, gihamp6
FLsetColor    0,   0,  0, gihamp7
FLsetColor    0,   0,  0, gihamp8

;COUNTERS							MIN | MAX | STEP1 | STEP2 | TYPE | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | DUR
gklayers, ihlayers		FLcount  "No. of Layers", 	2,     8,     1,      1,      2,    100,     20,   325,175,   0,      1,       0,     .1

;GENERAL_TEXT_SETTINGS			SIZE | FONT |  ALIGN | RED | GREEN | BLUE
			FLlabel		13,      4,      1,    190,   190,   190		;LABELS MADE INVISIBLE (I.E. SAME COLOR AS PANEL)

;BUTTON BANKS					TYPE | NUMX | NUMY | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | DUR
gkfile, ihfile			FLbutBank	14,     1,     4,     18,      80,    20, 180,   -1
gkAmpWindow, ihAmpWindow	FLbutBank	14,     1,     3,     18,      60,    20, 350,   -1

;GENERAL_TEXT_SETTINGS			SIZE | FONT |  ALIGN | RED | GREEN | BLUE
			FLlabel		13,      4,      1,     0,     0,     0			;LABELS MADE VISIBLE AGAIN

;TEXT BOXES						TYPE | FONT | SIZE | WIDTH | HEIGHT | X |  Y
ih		 	FLbox  	"Loop 1", 		1,       5,    14,     60,      20,   40, 180
ih		 	FLbox  	"Loop 2", 		1,       5,    14,     60,      20,   40, 200
ih		 	FLbox  	"Loop 3", 		1,       5,    14,     60,      20,   40, 220
ih		 	FLbox  	"Loop 4", 		1,       5,    14,     60,      20,   40, 240
ih		 	FLbox  	"Half-sine          ", 	1,       5,    14,    170,      20,   40, 350
ih		 	FLbox  	"Bartlett (triangle)", 	1,       5,    14,    170,      20,   40, 370
ih		 	FLbox  	"Blackman-Harris    ", 	1,       5,    14,    170,      20,   40, 390

;SET INITIAL VALUES OF FLTK VALUATORS
;						VALUE | HANDLE
				FLsetVal_i	0.003, 	ihrate
				FLsetVal_i	1, 	gih1
				FLsetVal_i	1, 	gih2
				FLsetVal_i	1, 	gih3
				FLsetVal_i	1, 	gih4
				FLsetVal_i	1, 	gih5
				FLsetVal_i	1, 	gih6
				FLsetVal_i	1, 	gih7
				FLsetVal_i	1, 	gih8
				FLsetVal_i	8, 	ihlayers
				FLsetVal_i	1, 	ihGSpeed
				FLsetVal_i	2, 	ihfile
				FLsetVal_i	2, 	ihAmpWindow
				FLsetVal_i	0.3, 	ihOutGain

				FLpanel_end

;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ", 512, 700, 512, 0
				FLscroll     512, 700, 0,   0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"                     risset rhythms                          ", 	1,      5,     14,    490,    15,     5,   0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    15,     5,  20
ih		 	FLbox  	"Risset rhythms is a variation of the Shepard-Risset Glissando", 	1,      5,     14,    490,    15,     5,  40
ih		 	FLbox  	"which employs a rhythmic loop instead of a sine tone.        ", 	1,      5,     14,    490,    15,     5,  60
ih		 	FLbox  	"This implementation uses speed control of sampled loops to   ", 	1,      5,     14,    490,    15,     5,  80
ih		 	FLbox  	"vary tempo, therefore pitch as well as tempo is modulated.   ", 	1,      5,     14,    490,    15,     5, 100
ih		 	FLbox  	"                                                             ", 	1,      5,     14,    490,    15,     5, 120
ih		 	FLbox  	"A repeating loop fades in as it gradually increases in tempo ", 	1,      5,     14,    490,    15,     5, 140
ih		 	FLbox  	"(and pitch). By the time it has doubled its tempo (and risen ", 	1,      5,     14,    490,    15,     5, 160
ih		 	FLbox  	"in pitch by an octave) a second layer of this loop is faded  ", 	1,      5,     14,    490,    15,     5, 180
ih		 	FLbox  	"in at the same initial tempo of the original layer. It then  ", 	1,      5,     14,    490,    15,     5, 200
ih		 	FLbox  	"follows the trajectory of the first layer, always half its   ", 	1,      5,     14,    490,    15,     5, 220
ih		 	FLbox  	"tempo (and an octave lower). As the layers get faster and    ", 	1,      5,     14,    490,    15,     5, 240
ih		 	FLbox  	"faster, they eventually fade out, just as they faded in. As  ", 	1,      5,     14,    490,    15,     5, 260
ih		 	FLbox  	"many layers can be used as desired but the greater the number", 	1,      5,     14,    490,    15,     5, 280
ih		 	FLbox  	"of layers, the greater the range of tempi (& pitch) covered. ", 	1,      5,     14,    490,    15,     5, 300
ih		 	FLbox  	"The effect of this should be a cognative contradiction in    ", 	1,      5,     14,    490,    15,     5, 320
ih		 	FLbox  	"which, from moment to moment, the music appears to be getting", 	1,      5,     14,    490,    15,     5, 340
ih		 	FLbox  	"faster, but over the longer term doesn't get faster.         ", 	1,      5,     14,    490,    15,     5, 360
ih		 	FLbox  	"A frequently cited visual analog is that of the spinning     ", 	1,      5,     14,    490,    15,     5, 380
ih		 	FLbox  	"barber's pole in which parallel lines appear to be rising but", 	1,      5,     14,    490,    15,     5, 400
ih		 	FLbox  	"obviously aren't.                                            ", 	1,      5,     14,    490,    15,     5, 420
ih		 	FLbox  	"In this implementation 'Rate' controls the rate of           ", 	1,      5,     14,    490,    15,     5, 440
ih		 	FLbox  	"acceleration of the loops. One period will be the time it    ", 	1,      5,     14,    490,    15,     5, 460
ih		 	FLbox  	"take for a loop to begin fading in at its lowest tempo, to   ", 	1,      5,     14,    490,    15,     5, 480
ih		 	FLbox  	"when it finally fades our at its highest tempo. Negative     ", 	1,      5,     14,    490,    15,     5, 500
ih		 	FLbox  	"values here will result in loops slowing down (getting lower ", 	1,      5,     14,    490,    15,     5, 520
ih		 	FLbox  	"in pitch). The greater the number of layers employed the     ", 	1,      5,     14,    490,    15,     5, 540
ih		 	FLbox  	"greater the rate of acceleration will appear as each loop    ", 	1,      5,     14,    490,    15,     5, 560
ih		 	FLbox  	"will cover a greater range of tempi from fading in to fading ", 	1,      5,     14,    490,    15,     5, 580
ih		 	FLbox  	"out. Careful tuning of the 'Number of Layers' and 'Rate' is  ", 	1,      5,     14,    490,    15,     5, 600
ih		 	FLbox  	"needed to properly create the illusion. Setting rate too high", 	1,      5,     14,    490,    15,     5, 620
ih		 	FLbox  	"will reveal the workings of the illusion.                    ", 	1,      5,     14,    490,    15,     5, 640
ih		 	FLbox  	"The amplitudes of each of the layers are adjustable using the", 	1,      5,     14,    490,    15,     5, 660
ih		 	FLbox  	"numbered sliders. These are normally all left at maximum but ", 	1,      5,     14,    490,    15,     5, 680
ih		 	FLbox  	"listening to one layer on its own can help in understanding  ", 	1,      5,     14,    490,    15,     5, 700
ih		 	FLbox  	"the mechanism of this example. The yellow bars indicate how  ", 	1,      5,     14,    490,    15,     5, 720
ih		 	FLbox  	"the different layers fade in and out.                        ", 	1,      5,     14,    490,    15,     5, 740
ih		 	FLbox  	"A choice of amplitude window functions are available. These  ", 	1,      5,     14,    490,    15,     5, 760
ih		 	FLbox  	"determine exactly how each layer fades in and out.           ", 	1,      5,     14,    490,    15,     5, 780
ih		 	FLbox  	"A choice of four source loops are provided (including a sine ", 	1,      5,     14,    490,    15,     5, 800
ih		 	FLbox  	"tone which allows creation of the Shepard-Risset Glissando). ", 	1,      5,     14,    490,    15,     5, 820
ih		 	FLbox  	"'Global Speed' scales the speed of all layers equally.       ", 	1,      5,     14,    490,    15,     5, 840
                                                                                                                                                  
				FLscroll_end                
				FLpanel_end                                                                                       

				FLrun	;RUN THE FLTK WIDGET THREAD
;END OF FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

giloop1		ftgen	1, 0, 524289, -1, "808loopMono.wav", 0, 0, 0
giloop2		ftgen	2, 0, 262144, -1, "loop.wav", 0, 0, 0
giloop3		ftgen	3, 0, 131073, -1, "MFEM.wav", 0, 0, 0
giloop4		ftgen	4, 0, 131073, -1, "sine.wav", 0, 0, 0

giAmpWindow1	ftgen	11, 0, 131073, 9, 0.5, 1, 0	;HALF SINE
giAmpWindow2	ftgen	12, 0, 131073, 20, 3, 1,	;BARTLETT (TRIANGLE)
giAmpWindow3	ftgen	13, 0, 131073, 20,  5, 1	;BLACKMAN-HARRIS


instr	1	;
	ilayers	init	i(gklayers)
	
	if	ilayers<3	then
		FLhide	gih3
		FLhide	gihamp3
	else
		FLshow	gih3
		FLshow	gihamp3
	endif
	if	ilayers<4	then
		FLhide	gih4
		FLhide	gihamp4
	else
		FLshow	gih4
		FLshow	gihamp4
	endif
	if	ilayers<5	then
		FLhide	gih5
		FLhide	gihamp5
	else
		FLshow	gih5
		FLshow	gihamp5
	endif
	if	ilayers<6	then
		FLhide	gih6
		FLhide	gihamp6
	else
		FLshow	gih6
		FLshow	gihamp6
	endif	
	if	ilayers<7	then
		FLhide	gih7
		FLhide	gihamp7
	else
		FLshow	gih7
		FLshow	gihamp7
	endif
	if	ilayers<8	then
		FLhide	gih8
		FLhide	gihamp8
	else
		FLshow	gih8
		FLshow	gihamp8
	endif
endin

instr	2	;(ALWAYS ON - SEE SCORE) PLAYS FILE AND SENSES FADER MOVEMENT AND RESTARTS INSTR 2 FOR I-RATE CONTROLLERS
	kSwitch	changed		gkfile, gkAmpWindow, gklayers	;GENERATE A MOMENTARY '1' PULSE IN OUTPUT 'kSwitch' IF ANY OF THE SCANNED INPUT VARIABLES CHANGE. (OUTPUT 'kSwitch' IS NORMALLY ZERO)
	;			ITRIGGER | IMINTIM | IMAXNUM | IINSNUM | IWHEN | IDUR (-1 MEANS A NOTE OF INFINITE DURATION)
		schedkwhen	kSwitch,      0,        0,        3,        0,    -1	;RESTART INSTRUMENT 3 WITH A SUSTAINED (HELD) NOTE WHENEVER kSwitch=1
endin

instr	3
	if	gkOnOff!=0	goto	CONTINUE	;SENSE FLTK ON/OFF SWITCH - IF 'ON' JUMP TO LABEL 'CONTINUE'
				turnoff			;TURN THIS INSTRUMENT OFF IMMEDIATELY
	CONTINUE:					;LABEL 'CONTINUE'
	
	ifn		=	i(gkfile)+1
	ilen		=	nsamp(ifn)
	iAmpWindow	=	i(gkAmpWindow)+11
	kSpeedRatio	=	sr*gkGSpeed/ilen	;PLAYBACK SPEED RATIO BASED ON FILE LENGTH AND 'GLOBAL SPEED' SLIDER
	ilayers		=	i(gklayers)
	kupdate		metro	20
	if	gklayers==2	then
		kspeed1	phasor	gkrate
		kspeed2	phasor	gkrate, 1/ilayers
		kenv1	tablei	kspeed1, iAmpWindow, 1
		kenv2	tablei	kspeed2, iAmpWindow, 1
		FLsetVal	kupdate, 1-kenv1, gihamp1
		FLsetVal	kupdate, 1-kenv2, gihamp2
		kratio1	=	cpsoct(8+(kspeed1*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio2	=	cpsoct(8+(kspeed2*ilayers)-(ilayers*.5))/cpsoct(8)		
		aptr1	phasor	kratio1*kSpeedRatio
		aptr2	phasor	kratio2*kSpeedRatio
		asig1	tablei	aptr1*ilen, ifn
		asig2	tablei	aptr2*ilen, ifn		
		aout	sum	asig1*kenv1*gk1, asig2*kenv2*gk2
        elseif	gklayers==3	then
		kspeed1	phasor	gkrate
		kspeed2	phasor	gkrate, 1/ilayers
		kspeed3	phasor	gkrate, 2/ilayers
		kenv1	tablei	kspeed1, iAmpWindow, 1
		kenv2	tablei	kspeed2, iAmpWindow, 1
		kenv3	tablei	kspeed3, iAmpWindow, 1
		FLsetVal	kupdate, 1-kenv1, gihamp1
		FLsetVal	kupdate, 1-kenv2, gihamp2
		FLsetVal	kupdate, 1-kenv3, gihamp3
		kratio1	=	cpsoct(8+(kspeed1*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio2	=	cpsoct(8+(kspeed2*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio3	=	cpsoct(8+(kspeed3*ilayers)-(ilayers*.5))/cpsoct(8)
		aptr1	phasor	kratio1*kSpeedRatio
		aptr2	phasor	kratio2*kSpeedRatio
		aptr3	phasor	kratio3*kSpeedRatio
		asig1	tablei	aptr1*ilen, ifn
		asig2	tablei	aptr2*ilen, ifn		
		asig3	tablei	aptr3*ilen, ifn		
		aout	sum	asig1*kenv1*gk1, asig2*kenv2*gk2, asig3*kenv3*gk3
        elseif	gklayers==4	then
		kspeed1	phasor	gkrate
		kspeed2	phasor	gkrate, 1/ilayers
		kspeed3	phasor	gkrate, 2/ilayers
		kspeed4	phasor	gkrate, 3/ilayers
		kenv1	tablei	kspeed1, iAmpWindow, 1
		kenv2	tablei	kspeed2, iAmpWindow, 1
		kenv3	tablei	kspeed3, iAmpWindow, 1
		kenv4	tablei	kspeed4, iAmpWindow, 1
		FLsetVal	kupdate, 1-kenv1, gihamp1
		FLsetVal	kupdate, 1-kenv2, gihamp2
		FLsetVal	kupdate, 1-kenv3, gihamp3
		FLsetVal	kupdate, 1-kenv4, gihamp4
		kratio1	=	cpsoct(8+(kspeed1*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio2	=	cpsoct(8+(kspeed2*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio3	=	cpsoct(8+(kspeed3*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio4	=	cpsoct(8+(kspeed4*ilayers)-(ilayers*.5))/cpsoct(8)
		aptr1	phasor	kratio1*kSpeedRatio
		aptr2	phasor	kratio2*kSpeedRatio
		aptr3	phasor	kratio3*kSpeedRatio
		aptr4	phasor	kratio4*kSpeedRatio
		asig1	tablei	aptr1*ilen, ifn
		asig2	tablei	aptr2*ilen, ifn		
		asig3	tablei	aptr3*ilen, ifn		
		asig4	tablei	aptr4*ilen, ifn		
		aout	sum	asig1*kenv1*gk1, asig2*kenv2*gk2, asig3*kenv3*gk3, asig4*kenv4*gk4      
        elseif	gklayers==5	then
		kspeed1	phasor	gkrate
		kspeed2	phasor	gkrate, 1/ilayers
		kspeed3	phasor	gkrate, 2/ilayers
		kspeed4	phasor	gkrate, 3/ilayers
		kspeed5	phasor	gkrate, 4/ilayers
		kenv1	tablei	kspeed1, iAmpWindow, 1
		kenv2	tablei	kspeed2, iAmpWindow, 1
		kenv3	tablei	kspeed3, iAmpWindow, 1
		kenv4	tablei	kspeed4, iAmpWindow, 1
		kenv5	tablei	kspeed5, iAmpWindow, 1
		FLsetVal	kupdate, 1-kenv1, gihamp1
		FLsetVal	kupdate, 1-kenv2, gihamp2
		FLsetVal	kupdate, 1-kenv3, gihamp3
		FLsetVal	kupdate, 1-kenv4, gihamp4
		FLsetVal	kupdate, 1-kenv5, gihamp5
		kratio1	=	cpsoct(8+(kspeed1*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio2	=	cpsoct(8+(kspeed2*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio3	=	cpsoct(8+(kspeed3*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio4	=	cpsoct(8+(kspeed4*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio5	=	cpsoct(8+(kspeed5*ilayers)-(ilayers*.5))/cpsoct(8)
		aptr1	phasor	kratio1*kSpeedRatio
		aptr2	phasor	kratio2*kSpeedRatio
		aptr3	phasor	kratio3*kSpeedRatio
		aptr4	phasor	kratio4*kSpeedRatio
		aptr5	phasor	kratio5*kSpeedRatio
		asig1	tablei	aptr1*ilen, ifn
		asig2	tablei	aptr2*ilen, ifn		
		asig3	tablei	aptr3*ilen, ifn		
		asig4	tablei	aptr4*ilen, ifn		
		asig5	tablei	aptr5*ilen, ifn		
		aout	sum	asig1*kenv1*gk1, asig2*kenv2*gk2, asig3*kenv3*gk3, asig4*kenv4*gk4, asig5*kenv5*gk5
        elseif	gklayers==6	then
		kspeed1	phasor	gkrate
		kspeed2	phasor	gkrate, 1/ilayers
		kspeed3	phasor	gkrate, 2/ilayers
		kspeed4	phasor	gkrate, 3/ilayers
		kspeed5	phasor	gkrate, 4/ilayers
		kspeed6	phasor	gkrate, 5/ilayers
		kenv1	tablei	kspeed1, iAmpWindow, 1
		kenv2	tablei	kspeed2, iAmpWindow, 1
		kenv3	tablei	kspeed3, iAmpWindow, 1
		kenv4	tablei	kspeed4, iAmpWindow, 1
		kenv5	tablei	kspeed5, iAmpWindow, 1
		kenv6	tablei	kspeed6, iAmpWindow, 1
		FLsetVal	kupdate, 1-kenv1, gihamp1
		FLsetVal	kupdate, 1-kenv2, gihamp2
		FLsetVal	kupdate, 1-kenv3, gihamp3
		FLsetVal	kupdate, 1-kenv4, gihamp4
		FLsetVal	kupdate, 1-kenv5, gihamp5
		FLsetVal	kupdate, 1-kenv6, gihamp6
		kratio1	=	cpsoct(8+(kspeed1*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio2	=	cpsoct(8+(kspeed2*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio3	=	cpsoct(8+(kspeed3*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio4	=	cpsoct(8+(kspeed4*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio5	=	cpsoct(8+(kspeed5*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio6	=	cpsoct(8+(kspeed6*ilayers)-(ilayers*.5))/cpsoct(8)
		aptr1	phasor	kratio1*kSpeedRatio
		aptr2	phasor	kratio2*kSpeedRatio
		aptr3	phasor	kratio3*kSpeedRatio
		aptr4	phasor	kratio4*kSpeedRatio
		aptr5	phasor	kratio5*kSpeedRatio
		aptr6	phasor	kratio6*kSpeedRatio
		asig1	tablei	aptr1*ilen, ifn
		asig2	tablei	aptr2*ilen, ifn		
		asig3	tablei	aptr3*ilen, ifn		
		asig4	tablei	aptr4*ilen, ifn		
		asig5	tablei	aptr5*ilen, ifn		
		asig6	tablei	aptr6*ilen, ifn		
		aout	sum	asig1*kenv1*gk1, asig2*kenv2*gk2, asig3*kenv3*gk3, asig4*kenv4*gk4, asig5*kenv5*gk5, asig6*kenv6*gk6
        elseif	gklayers==7	then
		kspeed1	phasor	gkrate
		kspeed2	phasor	gkrate, 1/ilayers
		kspeed3	phasor	gkrate, 2/ilayers
		kspeed4	phasor	gkrate, 3/ilayers
		kspeed5	phasor	gkrate, 4/ilayers
		kspeed6	phasor	gkrate, 5/ilayers
		kspeed7	phasor	gkrate, 6/ilayers
		kenv1	tablei	kspeed1, iAmpWindow, 1
		kenv2	tablei	kspeed2, iAmpWindow, 1
		kenv3	tablei	kspeed3, iAmpWindow, 1
		kenv4	tablei	kspeed4, iAmpWindow, 1
		kenv5	tablei	kspeed5, iAmpWindow, 1
		kenv6	tablei	kspeed6, iAmpWindow, 1
		kenv7	tablei	kspeed7, iAmpWindow, 1
		FLsetVal	kupdate, 1-kenv1, gihamp1
		FLsetVal	kupdate, 1-kenv2, gihamp2
		FLsetVal	kupdate, 1-kenv3, gihamp3
		FLsetVal	kupdate, 1-kenv4, gihamp4
		FLsetVal	kupdate, 1-kenv5, gihamp5
		FLsetVal	kupdate, 1-kenv6, gihamp6
		FLsetVal	kupdate, 1-kenv7, gihamp7
		kratio1	=	cpsoct(8+(kspeed1*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio2	=	cpsoct(8+(kspeed2*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio3	=	cpsoct(8+(kspeed3*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio4	=	cpsoct(8+(kspeed4*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio5	=	cpsoct(8+(kspeed5*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio6	=	cpsoct(8+(kspeed6*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio7	=	cpsoct(8+(kspeed7*ilayers)-(ilayers*.5))/cpsoct(8)
		aptr1	phasor	kratio1*kSpeedRatio
		aptr2	phasor	kratio2*kSpeedRatio
		aptr3	phasor	kratio3*kSpeedRatio
		aptr4	phasor	kratio4*kSpeedRatio
		aptr5	phasor	kratio5*kSpeedRatio
		aptr6	phasor	kratio6*kSpeedRatio
		aptr7	phasor	kratio7*kSpeedRatio
		asig1	tablei	aptr1*ilen, ifn
		asig2	tablei	aptr2*ilen, ifn		
		asig3	tablei	aptr3*ilen, ifn		
		asig4	tablei	aptr4*ilen, ifn		
		asig5	tablei	aptr5*ilen, ifn		
		asig6	tablei	aptr6*ilen, ifn		
		asig7	tablei	aptr7*ilen, ifn		
		aout	sum	asig1*kenv1*gk1, asig2*kenv2*gk2, asig3*kenv3*gk3, asig4*kenv4*gk4, asig5*kenv5*gk5, asig6*kenv6*gk6, asig7*kenv7*gk7
        elseif	gklayers==8	then
		kspeed1	phasor	gkrate
		kspeed2	phasor	gkrate, 1/ilayers
		kspeed3	phasor	gkrate, 2/ilayers
		kspeed4	phasor	gkrate, 3/ilayers
		kspeed5	phasor	gkrate, 4/ilayers
		kspeed6	phasor	gkrate, 5/ilayers
		kspeed7	phasor	gkrate, 6/ilayers
		kspeed8	phasor	gkrate, 7/ilayers
		kenv1	tablei	kspeed1, iAmpWindow, 1
		kenv2	tablei	kspeed2, iAmpWindow, 1
		kenv3	tablei	kspeed3, iAmpWindow, 1
		kenv4	tablei	kspeed4, iAmpWindow, 1
		kenv5	tablei	kspeed5, iAmpWindow, 1
		kenv6	tablei	kspeed6, iAmpWindow, 1
		kenv7	tablei	kspeed7, iAmpWindow, 1
		kenv8	tablei	kspeed8, iAmpWindow, 1
		FLsetVal	kupdate, 1-kenv1, gihamp1
		FLsetVal	kupdate, 1-kenv2, gihamp2
		FLsetVal	kupdate, 1-kenv3, gihamp3
		FLsetVal	kupdate, 1-kenv4, gihamp4
		FLsetVal	kupdate, 1-kenv5, gihamp5
		FLsetVal	kupdate, 1-kenv6, gihamp6
		FLsetVal	kupdate, 1-kenv7, gihamp7
		FLsetVal	kupdate, 1-kenv8, gihamp8
		kratio1	=	cpsoct(8+(kspeed1*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio2	=	cpsoct(8+(kspeed2*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio3	=	cpsoct(8+(kspeed3*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio4	=	cpsoct(8+(kspeed4*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio5	=	cpsoct(8+(kspeed5*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio6	=	cpsoct(8+(kspeed6*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio7	=	cpsoct(8+(kspeed7*ilayers)-(ilayers*.5))/cpsoct(8)
		kratio8	=	cpsoct(8+(kspeed8*ilayers)-(ilayers*.5))/cpsoct(8)
		aptr1	phasor	kratio1*kSpeedRatio
		aptr2	phasor	kratio2*kSpeedRatio
		aptr3	phasor	kratio3*kSpeedRatio
		aptr4	phasor	kratio4*kSpeedRatio
		aptr5	phasor	kratio5*kSpeedRatio
		aptr6	phasor	kratio6*kSpeedRatio
		aptr7	phasor	kratio7*kSpeedRatio
		aptr8	phasor	kratio8*kSpeedRatio
		asig1	tablei	aptr1*ilen, ifn
		asig2	tablei	aptr2*ilen, ifn		
		asig3	tablei	aptr3*ilen, ifn		
		asig4	tablei	aptr4*ilen, ifn		
		asig5	tablei	aptr5*ilen, ifn		
		asig6	tablei	aptr6*ilen, ifn		
		asig7	tablei	aptr7*ilen, ifn		
		asig8	tablei	aptr8*ilen, ifn		
		aout	sum	asig1*kenv1*gk1, asig2*kenv2*gk2, asig3*kenv3*gk3, asig4*kenv4*gk4, asig5*kenv5*gk5, asig6*kenv6*gk6, asig7*kenv7*gk7, asig8*kenv8*gk8
        endif
	outs	aout*gkOutGain, aout*gkOutGain
endin                                    


  	                                 
</CsInstruments>                         
                                         
<CsScore>                                
;INSTR | START | DURATION                
i  2       0       3600	;INSTRUMENT 2 (SCANS FOR I-RATE CHANGES) PLAYS FOR 1 HOUR AND KEEPS REALTIME PERFORMANCE GOING
</CsScore>                               
                                         
</CsoundSynthesizer>                     
                                         
                                         
                                         
                                         
                                         
                                         
                                         
                                         
                                         
                                         
                                         
                                         
                                         














