;WRITTEN BY IAIN MCCURDY, 2008

<CsoundSynthesizer>

<CsOptions>
-odevaudio -b400 -d -m0
</CsOptions>

<CsInstruments>

sr 		= 	44100	;SAMPLE RATE
ksmps 		= 	10	;NUMBER OF AUDIO SAMPLES IN EACH CONTROL CYCLE
nchnls 		= 	2	;NUMBER OF CHANNELS (2=STEREO)
0dbfs		=	1	;MAXIMUM AMPLITUDE REGARDLESS OF BIT DEPTH

;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLcolor	255,255,255,0,0,0
		FLpanel	" ",1045, 335, 0, 0

;BUTTONS                                             			ON | OFF | TYPE | WIDTH | HEIGHT | X | Y | OPCODE |  I  | STARTTIM | IDUR |  p4
gkOnOff,ihOnOff	FLbutton	"On/Off",				1,    0,     2,    100,     30,    0,  0,   0,        1,     0,       -1
FLsetColor2	255, 255, 50, ihOnOff		;SET SECONDARY COLOUR TO YELLOW
gkRndPitches,ihRndPitches	FLbutton	"Randomize!",		1,    0,     1,    100,     30,  200,  0,   0,      102,     0,       0.001

;COUNTERS						MIN | MAX | STEP1 | STEP2 | TYPE | WIDTH | HEIGHT | POS_X | POS_Y | IOPCODE
gkTempo,ihTempo 	FLcount		"Tempo",	1,    999,    1,     10,     21,   100,     20,     100,      0,      -1

;BORDERS				ITYPE, IFONT, ISIZE, IWIDTH, IHEIGHT, IX, IY
ih		 	FLbox  	" ", 	5,        9,    15,    80,    183,     0, 48		;DRAW A BOX AROUND EACH GROUP OF 4 BUTTONS
ih		 	FLbox  	" ", 	5,        9,    15,    80,    183,    80, 48            ;DRAW A BOX AROUND EACH GROUP OF 4 BUTTONS
ih		 	FLbox  	" ", 	5,        9,    15,    80,    183,   160, 48            ;DRAW A BOX AROUND EACH GROUP OF 4 BUTTONS
ih		 	FLbox  	" ", 	5,        9,    15,    80,    183,   240, 48            ;DRAW A BOX AROUND EACH GROUP OF 4 BUTTONS
ih		 	FLbox  	" ", 	5,        9,    15,    80,    183,   320, 48            ;DRAW A BOX AROUND EACH GROUP OF 4 BUTTONS
ih		 	FLbox  	" ", 	5,        9,    15,    80,    183,   400, 48            ;DRAW A BOX AROUND EACH GROUP OF 4 BUTTONS
ih		 	FLbox  	" ", 	5,        9,    15,    80,    183,   480, 48            ;DRAW A BOX AROUND EACH GROUP OF 4 BUTTONS
ih		 	FLbox  	" ", 	5,        9,    15,    80,    183,   560, 48            ;DRAW A BOX AROUND EACH GROUP OF 4 BUTTONS

;TEXT BOXES						TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"Gain",			1,      5,     13,     90,     12,   648,  7
ih		 	FLbox  	"Transpose",		1,      5,     13,     90,     12,   848, 35
ih		 	FLbox  	"Delay (0-0.1 s.)",	1,      5,     13,     90,     12,   948, 30
ih		 	FLbox  	"Cycle Range",		1,      5,     13,     90,     12,   748, 35
ih		 	FLbox  	"1",			1,      5,     13,      4,     12,     4, 35
ih		 	FLbox  	"2",			1,      5,     13,      4,     12,    84, 35
ih		 	FLbox  	"3",			1,      5,     13,      4,     12,   164, 35
ih		 	FLbox  	"4",			1,      5,     13,      4,     12,   244, 35
ih		 	FLbox  	"5",			1,      5,     13,      4,     12,   324, 35
ih		 	FLbox  	"6",			1,      5,     13,      4,     12,   404, 35
ih		 	FLbox  	"7",			1,      5,     13,      4,     12,   484, 35
ih		 	FLbox  	"8",			1,      5,     13,      4,     12,   564, 35

;				            			MIN  | MAX | EXP | TYPE | DISP | WIDTH | HEIGHT | X | Y
gkMasterGain, ihMasterGain	FLslider 	"Master",	0,      1,    0,    3,     -1,   100,      12,  642,  24

iy	init	50

#define	ROW(COUNT)	
#
gk$COUNT,gih$COUNT_1	FLbutton	" ",			1,    0,      4,     20,     20,      2, iy,  0,     9+$COUNT,    0,        0,     1
gk$COUNT,gih$COUNT_2	FLbutton	" ",			1,    0,      4,     20,     20,     22, iy,  0,     9+$COUNT,    0,        0,     2
gk$COUNT,gih$COUNT_3	FLbutton	" ",			1,    0,      4,     20,     20,     42, iy,  0,     9+$COUNT,    0,        0,     3
gk$COUNT,gih$COUNT_4	FLbutton	" ",			1,    0,      4,     20,     20,     62, iy,  0,     9+$COUNT,    0,        0,     4
gk$COUNT,gih$COUNT_5	FLbutton	" ",			1,    0,      4,     20,     20,     82, iy,  0,     9+$COUNT,    0,        0,     5
gk$COUNT,gih$COUNT_6	FLbutton	" ",			1,    0,      4,     20,     20,    102, iy,  0,     9+$COUNT,    0,        0,     6
gk$COUNT,gih$COUNT_7	FLbutton	" ",			1,    0,      4,     20,     20,    122, iy,  0,     9+$COUNT,    0,        0,     7
gk$COUNT,gih$COUNT_8	FLbutton	" ",			1,    0,      4,     20,     20,    142, iy,  0,     9+$COUNT,    0,        0,     8
gk$COUNT,gih$COUNT_9	FLbutton	" ",			1,    0,      4,     20,     20,    162, iy,  0,     9+$COUNT,    0,        0,     9
gk$COUNT,gih$COUNT_10	FLbutton	" ",			1,    0,      4,     20,     20,    182, iy,  0,     9+$COUNT,    0,        0,    10
gk$COUNT,gih$COUNT_11	FLbutton	" ",			1,    0,      4,     20,     20,    202, iy,  0,     9+$COUNT,    0,        0,    11
gk$COUNT,gih$COUNT_12	FLbutton	" ",			1,    0,      4,     20,     20,    222, iy,  0,     9+$COUNT,    0,        0,    12
gk$COUNT,gih$COUNT_13	FLbutton	" ",			1,    0,      4,     20,     20,    242, iy,  0,     9+$COUNT,    0,        0,    13
gk$COUNT,gih$COUNT_14	FLbutton	" ",			1,    0,      4,     20,     20,    262, iy,  0,     9+$COUNT,    0,        0,    14
gk$COUNT,gih$COUNT_15	FLbutton	" ",			1,    0,      4,     20,     20,    282, iy,  0,     9+$COUNT,    0,        0,    15
gk$COUNT,gih$COUNT_16	FLbutton	" ",			1,    0,      4,     20,     20,    302, iy,  0,     9+$COUNT,    0,        0,    16
gk$COUNT,gih$COUNT_17	FLbutton	" ",			1,    0,      4,     20,     20,    322, iy,  0,     9+$COUNT,    0,        0,    17
gk$COUNT,gih$COUNT_18	FLbutton	" ",			1,    0,      4,     20,     20,    342, iy,  0,     9+$COUNT,    0,        0,    18
gk$COUNT,gih$COUNT_19	FLbutton	" ",			1,    0,      4,     20,     20,    362, iy,  0,     9+$COUNT,    0,        0,    19
gk$COUNT,gih$COUNT_20	FLbutton	" ",			1,    0,      4,     20,     20,    382, iy,  0,     9+$COUNT,    0,        0,    20
gk$COUNT,gih$COUNT_21	FLbutton	" ",			1,    0,      4,     20,     20,    402, iy,  0,     9+$COUNT,    0,        0,    21
gk$COUNT,gih$COUNT_22	FLbutton	" ",			1,    0,      4,     20,     20,    422, iy,  0,     9+$COUNT,    0,        0,    22
gk$COUNT,gih$COUNT_23	FLbutton	" ",			1,    0,      4,     20,     20,    442, iy,  0,     9+$COUNT,    0,        0,    23
gk$COUNT,gih$COUNT_24	FLbutton	" ",			1,    0,      4,     20,     20,    462, iy,  0,     9+$COUNT,    0,        0,    24
gk$COUNT,gih$COUNT_25	FLbutton	" ",			1,    0,      4,     20,     20,    482, iy,  0,     9+$COUNT,    0,        0,    25
gk$COUNT,gih$COUNT_26	FLbutton	" ",			1,    0,      4,     20,     20,    502, iy,  0,     9+$COUNT,    0,        0,    26
gk$COUNT,gih$COUNT_27	FLbutton	" ",			1,    0,      4,     20,     20,    522, iy,  0,     9+$COUNT,    0,        0,    27
gk$COUNT,gih$COUNT_28	FLbutton	" ",			1,    0,      4,     20,     20,    542, iy,  0,     9+$COUNT,    0,        0,    28
gk$COUNT,gih$COUNT_29	FLbutton	" ",			1,    0,      4,     20,     20,    562, iy,  0,     9+$COUNT,    0,        0,    29
gk$COUNT,gih$COUNT_30	FLbutton	" ",			1,    0,      4,     20,     20,    582, iy,  0,     9+$COUNT,    0,        0,    30
gk$COUNT,gih$COUNT_31	FLbutton	" ",			1,    0,      4,     20,     20,    602, iy,  0,     9+$COUNT,    0,        0,    31
gk$COUNT,gih$COUNT_32	FLbutton	" ",			1,    0,      4,     20,     20,    622, iy,  0,     9+$COUNT,    0,        0,    32
gk$COUNTGain, gih$COUNTGain		FLslider 	"",	0,      1,    0,    3,     -1,    100,      12,  642, iy+4
gk$COUNTPch, gih$COUNTPch		FLslider 	"",	25,   300,    0,    3,     -1,    100,      12,  842, iy+4
gk$COUNTDly, gih$COUNTDly		FLslider 	"",	0,     .1,    0,    3,     -1,    100,      12,  942, iy+4
;COUNTERS							MIN | MAX | STEP1 | STEP2 | TYPE | WIDTH | HEIGHT | POS_X | POS_Y | IOPCODE
gkLen$COUNT,gihLen$COUNT 		FLcount		"",	0,      8, 0.25,      1,     21,    100,     20,     742,    iy,      -1
gkphs$COUNT, gihtimpnt$COUNT		FLslider 	"",	0,      8,    0,      1,     -1,    625,      9,       8,    iy+18
FLsetColor2	0,255,0,gihtimpnt$COUNT

		FLsetVal_i	100, 	gih$COUNTPch
		FLsetVal_i	1, 	gih$COUNTGain
		FLsetVal_i	8, 	gihLen$COUNT
		iy	=	iy+30
#
FLcolor2	255,0,0
$ROW(1)
$ROW(2)
$ROW(3)
$ROW(4)
$ROW(5)
$ROW(6)

;VALUE DISPLAY BOXES
idRvbSnd	FLvalue	" ",      50,    15,     0, 255
idfblvl		FLvalue	" ",      50,    15,     0, 285
idfco		FLvalue	" ",      50,    15,     0, 315
idDlySnd	FLvalue	" ",      50,    15,   240, 255
idDlyFB		FLvalue	" ",      50,    15,   240, 285
gkRvbSnd, ihRvbSnd	FLslider 	"Reverb Send",		0,        1,   0,    3,     idRvbSnd,   235,    15,     0, 240
gkfblvl, ihfblvl	FLslider 	"Reverb Time",		0,        1,   0,    3,      idfblvl,   235,    15,     0, 270
gkfco, ihfco		FLslider 	"Reverb Filter Cutoff",	20,   20000,   0,    3,        idfco,   235,    15,     0, 300
gkDlySnd, ihDlySnd	FLslider 	"Delay Send",		0,        1,   0,    3,     idDlySnd,   235,    15,   240, 240
gkDlyFB, ihDlyFB	FLslider 	"Delay Feedback",	0,        1,   0,    3,      idDlyFB,   235,    15,   240, 270
;COUNTERS							MIN | MAX | STEP1 | STEP2 | TYPE | WIDTH | HEIGHT | POS_X | POS_Y | IOPCODE
gkDelTim1,ihDelTim1 		FLcount		"Delay L",	1,    32,     1,      1,      2,    100,     18,     240,   300,      -1
gkDelTim2,ihDelTim2 		FLcount		"Delay R",	1,    32,     1,      1,      2,    100,     18,     350,   300,      -1

		FLsetVal_i	0.4, 	ihRvbSnd
		FLsetVal_i	.7, 	ihfblvl
		FLsetVal_i	10000, 	ihfco
		FLsetVal_i	0.2, 	ihDlySnd
		FLsetVal_i	.4, 	ihDlyFB
		FLsetVal_i	12, 	ihDelTim1
		FLsetVal_i	20, 	ihDelTim2
		FLsetVal_i	220, 	ihTempo
		FLsetVal_i	.5, 	ihMasterGain
		FLpanel_end	;END OF PANEL CONTENTS

;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ",1045, 310, 0, 385
;TEXT BOXES																				TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"                                   Looping Sequencer With Individually Variable Length Loops                                      ", 	1,       5,    14,    1040,    20,    5,   0
ih		 	FLbox  	"----------------------------------------------------------------------------------------------------------------------------------", 	1,       5,    14,    1040,    20,    5,  20
ih		 	FLbox  	"This example expands on the design from the 'Simple Drum Sequencer' example. Besides expanding the basic loop length to 8 beats   ", 	1,       5,    14,    1040,    20,    5,  40
ih		 	FLbox  	"(32 semiquavers) the principle innovation is that the user can shorten the range of the loop for each sound. The moving green bars", 	1,       5,    14,    1040,    20,    5,  60
ih		 	FLbox  	"representing current loop position for each sound should help clarify exactly what is happening. Each beat is divided into four   ", 	1,       5,    14,    1040,    20,    5,  80
ih		 	FLbox  	"semiquavers so that 0.25 in the 'Cycle Length' value represents 1 semiquaver, a value of 1 represents 1 beat.                     ", 	1,       5,    14,    1040,    20,    5, 100
ih		 	FLbox  	"If changes are made to cycle lengths, all loops will restart in order to resync.                                                  ", 	1,       5,    14,    1040,    20,    5, 120
ih		 	FLbox  	"Notes that are outside the range of the loop for that sound will not be played.                                                   ", 	1,       5,    14,    1040,    20,    5, 140
ih		 	FLbox  	"The sounds produced are kalimba-like sounds produced using Stefan Bilbao's 'barmodel' physical model. The pitch of each of the six", 	1,       5,    14,    1040,    20,    5, 160
ih		 	FLbox  	"sounds can be modified using the transpose sliders, they can be randomised on mass using the 'Randomize!' button and are in fact  ", 	1,       5,    14,    1040,    20,    5, 180
ih		 	FLbox  	"randomised automatically upon startup.                                                                                            ", 	1,       5,    14,    1040,    20,    5, 200
ih		 	FLbox  	"The 'Delay' sliders allow each sound to be individually delayed by a small amount with respect to the others. This is intended to ", 	1,       5,    14,    1040,    20,    5, 220
ih		 	FLbox  	"allow the creation of 'flam'-like gestures.                                                                                       ", 	1,       5,    14,    1040,    20,    5, 240
ih		 	FLbox  	"A reverb effect (making use of the 'reverbsc' opcode) is included.                                                                ", 	1,       5,    14,    1040,    20,    5, 260
ih		 	FLbox  	"A tempo synced delay effect is also included. Reverb times for the left and right channel are defined separately in semiquavers.  ", 	1,       5,    14,    1040,    20,    5, 280

				FLpanel_end

		FLrun		;RUN THE WIDGET THREAD!


;FUNCTION TABLE FOR STORAGE OF SEQUENCE DATA FOR SOUND 1
gi1		ftgen		0,0,256,-2,	0,	0.25,	.001,	0,\
						0,	0.5,	.001,	0,\
						0,	0.75,	.001,	0,\
						0,	1,	.001,	0,\
						0,	1.25,	.001,	0,\
						0,	1.5,	.001,	0,\
						0,	1.75,	.001,	0,\
						0,	2,	.001,	0,\
						0,	2.25,	.001,	0,\
						0,	2.5,	.001,	0,\
						0,	2.75,	.001,	0,\
						0,	3,	.001,	0,\
						0,	3.25,	.001,	0,\
						0,	3.5,	.001,	0,\
						0,	3.75,	.001,	0,\
						0,	4,	.001,	0,\
						0,	4.25,	.001,	0,\
						0,	4.5,	.001,	0,\
						0,	4.75,	.001,	0,\
						0,	5,	.001,	0,\
						0,	5.25,	.001,	0,\
						0,	5.5,	.001,	0,\
						0,	5.75,	.001,	0,\
						0,	6,	.001,	0,\
						0,	6.25,	.001,	0,\
						0,	6.5,	.001,	0,\
						0,	6.75,	.001,	0,\
						0,	7,	.001,	0,\
						0,	7.25,	.001,	0,\
						0,	7.5,	.001,	0,\
						0,	7.75,	.001,	0,\
						0,	8,	.001,	0,\
						-1,	8,	-1,	-1

gi2		ftgen		0,0,256,-2,0	;EMPTY TABLES INITIALLY. CONTENTS WILL BE COPIED FROM TABLE gi1
gi3		ftgen		0,0,256,-2,0	;EMPTY TABLES INITIALLY. CONTENTS WILL BE COPIED FROM TABLE gi1
gi4		ftgen		0,0,256,-2,0	;EMPTY TABLES INITIALLY. CONTENTS WILL BE COPIED FROM TABLE gi1
gi5		ftgen		0,0,256,-2,0	;EMPTY TABLES INITIALLY. CONTENTS WILL BE COPIED FROM TABLE gi1
gi6		ftgen		0,0,256,-2,0	;EMPTY TABLES INITIALLY. CONTENTS WILL BE COPIED FROM TABLE gi1

zakinit	8,8		;INITIALISE ZAK SPACE (8 A-RATE, 8 K-RATE)
giRvbSndChn	=	1	;ZAK CHANNEL NUMBER USED FOR REVERB SEND
giDlySndChn	=	3	;ZAK CHANNEL NUMBER USED FOR REVERB SEND

seed	0

instr	1	; NOTE TRIGGERING INSTRUMENT
	if	gkOnOff=0	then
		turnoff
	endif
	kSwitch		changed		gkLen1, gkLen2, gkLen3, gkLen4, gkLen5, gkLen6 	;GENERATE A MOMENTARY '1' PULSE IN OUTPUT 'kSwitch' IF ANY OF THE SCANNED INPUT VARIABLES CHANGE. (OUTPUT 'kSwitch' IS NORMALLY ZERO)
	if		kSwitch=1	then	;IF kSwitch=1, I.E. IF ANY OF THE ABOVE LISTED PARAMETERS HAVE CHANGED...
		reinit		START		;...PERFORM AN I-RATE PASS BEGINNING FROM THE GIVEN LABEL
	endif					;END OF CONDITIONAL BRANCHING
	START:					;A LABEL	
	kTempoRatio	=	gkTempo/60
	
	kp1		init	0
	kp2		init	0
	kp3		init	0
	kp4		init	0
#define	LOOP(COUNT)	
	#
	kphs$COUNT    	phasor  (kTempoRatio)/gkLen$COUNT
	gktimpnt$COUNT 	=        kphs$COUNT * gkLen$COUNT
	ktrig   timedseq gktimpnt$COUNT,gi$COUNT, kp1, kp2,kp3, kp4
	;	schedkwhen ktrigger, kmintim, kmaxnum, kinsnum,    kwhen,      kdur    p4    p5
		schedkwhen ktrig,       0,        0,     2,     gk$COUNTDly,   kp3,   kp4, $COUNT#
	
	$LOOP(1)
	$LOOP(2)
	$LOOP(3)
	$LOOP(4)
	$LOOP(5)
	$LOOP(6)
	rireturn
endin

instr	2
	if	p4=1	then
	event_i	"i", p1+p5, 0, p3
endif
	endin

#define	KALIMBA(I'COUNT)
	#
instr	$I
	p3	=	2.6		;DEFINE DURATION FOR THIS SOUND
	ivel	random	0.2,1
	asig 	barmodel	1, 1, i(gk$COUNTPch), 1, 0, 2.6, 0.5, 1000*ivel, 0.07	;KALIMBA SOUND CREATED USING barmodel OPCODE (SEE CSOUND MANUAL FOR MORE INFO.)
	asigL	=	asig*i(gk$COUNTGain)*i(gkMasterGain)*2	;DEFINE LEFT CHANNEL AUDIO
	asigR	=	asig*i(gk$COUNTGain)*i(gkMasterGain)*2	;DEFINE RIGHT CHANNEL AUDIO
		outs	asigL, asigR 	;SEND AUDIO TO OUTPUTS
	zawm	asigL*gkRvbSnd, giRvbSndChn
	zawm	asigR*gkRvbSnd, giRvbSndChn+1
	zawm	asigL*gkDlySnd, giDlySndChn
	zawm	asigR*gkDlySnd, giDlySndChn+1
endin
	#	

$KALIMBA(3'1)
$KALIMBA(4'2)
$KALIMBA(5'3)
$KALIMBA(6'4)
$KALIMBA(7'5)
$KALIMBA(8'6)

;;;	WRITE CHANGED FL-GRID VALUES TO TABLE
#define	WRITE_VAL(I'COUNT)
#
instr	$I
	tableiw	i(gk$COUNT), ((p4-1)*4)+3, gi$COUNT 	
endin
#

$WRITE_VAL(10'1)
$WRITE_VAL(11'2)
$WRITE_VAL(12'3)
$WRITE_VAL(13'4)
$WRITE_VAL(14'5)
$WRITE_VAL(15'6)

instr	100	;COPY TABLE 1 TO ALL OTHER TABLES AND SET RANDOM INITIAL VALUES FOR SOME VALUATORS (PERFORMED ONCE AT THE BEGINNING OF THE PERFORMANCE)
	tableicopy gi2, gi1
	tableicopy gi3, gi1
	tableicopy gi4, gi1
	tableicopy gi5, gi1
	tableicopy gi6, gi1
	event_i	"i", 102, 0,0.001	;RANDOMISE SLIDERS
endin

instr	102	;RANDOMISE SLIDERS
#define	RANDOMIZE_SLIDERS(COUNT)
	#
	iPch$COUNT	random		25,300		;DEFINE RANDOM INITIAL VALUES FOR 'TRANSPOSE' SLIDERS
	FLsetVal_i	iPch$COUNT, gih$COUNTPch	;SEND RANDOM 'TRANSPOSE' VALUES TO SLIDERS
	iLen$COUNTDec	random		1,3						;DEFINE RANDOM INITIAL VALUES FOR 'RANGE' COUNTERS
	iLen$COUNTInt	random		4,8                                     	;DEFINE RANDOM INITIAL VALUES FOR 'RANGE' COUNTERS
	iLen$COUNT		=		(int(iLen$COUNTDec)*0.5)+int(iLen$COUNTInt)	;DEFINE RANDOM INITIAL VALUES FOR 'RANGE' COUNTERS
	FLsetVal_i	iLen$COUNT, gihLen$COUNT				;SEND RANDOM 'RANGE' VALUES TO COUNTERS
	iDly$COUNT	random		0, 0.03		;DEFINE RANDOM INITIAL VALUES FOR 'TRANSPOSE' SLIDERS
	FLsetVal_i	iDly$COUNT, gih$COUNTDly	;SEND RANDOM 'DELAY' VALUES TO SLIDERS
	#
	$RANDOMIZE_SLIDERS(1)
	$RANDOMIZE_SLIDERS(2)
	$RANDOMIZE_SLIDERS(3)
	$RANDOMIZE_SLIDERS(4)
	$RANDOMIZE_SLIDERS(5)
	$RANDOMIZE_SLIDERS(6)
endin	

instr	998	;DUAL TEMPO DELAY
	aL	zar		giDlySndChn
	aR	zar		giDlySndChn+1
	kSwitch		changed		gkTempo, gkDelTim1, gkDelTim2 	;GENERATE A MOMENTARY '1' PULSE IN OUTPUT 'kSwitch' IF ANY OF THE SCANNED INPUT VARIABLES CHANGE. (OUTPUT 'kSwitch' IS NORMALLY ZERO)
	if		kSwitch=1	then	;IF kSwitch=1, I.E. IF ANY OF THE ABOVE LISTED PARAMETERS HAVE CHANGED...
		reinit		START		;...PERFORM AN I-RATE PASS BEGINNING FROM THE GIVEN LABEL
	endif					;END OF CONDITIONAL BRANCHING
	START:					;A LABEL	
	ideltimL	=	(15*i(gkDelTim1))/i(gkTempo)
	ideltimR	=	(15*i(gkDelTim2))/i(gkTempo)
	abuffer	delayr	ideltimL
	atapL	deltap	ideltimL
		delayw	aL + (atapL*gkDlyFB)
	abuffer	delayr	ideltimR
	atapR	deltap	ideltimR
		delayw	aR + (atapR*gkDlyFB)
	outs	atapL, atapR
endin

instr 	999		;REVERB
	aL	zar		giRvbSndChn
	aR	zar		giRvbSndChn+1
	denorm		aL, aR	;DENORMALIZE BOTH CHANNELS OF AUDIO SIGNAL
	arvbL, arvbR 	reverbsc 	aL, aR, gkfblvl, gkfco, sr;, i(gkpitchm), i(gkskip) 
	outs		arvbL, arvbR
	zacl		1,8
endin
	
instr	1000	;UPDATE LOOP INDICATORS
	ktrigger	metro	20	;IF PERFORMANCE ISSUES ARISE REDUCING THIS VALUE MIGHT HELP
	FLsetVal	ktrigger, gktimpnt1, gihtimpnt1
	FLsetVal	ktrigger, gktimpnt2, gihtimpnt2
	FLsetVal	ktrigger, gktimpnt3, gihtimpnt3
	FLsetVal	ktrigger, gktimpnt4, gihtimpnt4
	FLsetVal	ktrigger, gktimpnt5, gihtimpnt5
	FLsetVal	ktrigger, gktimpnt6, gihtimpnt6
endin

instr	1001	;SET INITIAL PATTERN
	FLsetVal_i	1, gih1_1
	FLsetVal_i	1, gih5_3
	FLsetVal_i	1, gih2_5
	FLsetVal_i	1, gih6_7
	FLsetVal_i	1, gih3_9
	FLsetVal_i	1, gih1_11
	FLsetVal_i	1, gih4_13
	FLsetVal_i	1, gih2_15
	FLsetVal_i	1, gih4_17
	FLsetVal_i	1, gih5_19
	FLsetVal_i	1, gih2_21
	FLsetVal_i	1, gih6_23
	FLsetVal_i	1, gih1_25
	FLsetVal_i	1, gih3_27
	FLsetVal_i	1, gih2_29
	FLsetVal_i	1, gih5_31
endin

</CsInstruments>

<CsScore>
f 0 3600
i 100 0 0
i 998 0 3600	;DELAY
i 999 0 3600	;REVERB
i 1000 0 3600	;LOOP INDICATORS
i 1001 0.01 0.01	;SET INITIAL PATTERN
</CsScore>

</CsoundSynthesizer>