<Cabbage>
form caption("Sequencer-sf2player"), size(300, 200)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -m0d
</CsOptions>
<CsInstruments>
sr 	= 	44100 
ksmps 	= 	32
nchnls 	= 	2
0dbfs	=	1 

gisf1	sfload	"sf_GMbank.sf2"
gisf2 	sfload "HS_Boss_DR-550_Drums.sf2"
	;sfilist	gisf2


instr ROW_SEQUENCER
	
	;each Row will take one of these notes and use it when a beat is enabled
	;notes - modifiable by Layer control script (MainController)
	;C major scale by default
	kNoteValues[] fillarray 60, 62, 64, 65, 67, 69, 70, 72
	;amps - 0 by default. changed by cube state
	kNotesAmps[] fillarray 0, 0, 0, 0, 0, 0, 0, 0

	;run through note amps array and trigger note if amp is set to 1/on
	kBeat init 0
	kTempo chnget "tempo"
	kInst chnget "inst"
	kSeqLength chnget "seqLength"
	kDur chnget "duration"
	kOffset chnget "noteOffset"
	kNoteList chnget "noteRowList"
	
	
	if metro(kTempo) != 0 then
		;if amplitude/vel is greater than 0, set event
		if kBeat < 8 then
			if kNotesAmps[kBeat] != 0 then
				if kInst == 0 then
					event "i", "inst0", 0, kDur, kNoteValues[p4-1]+kOffset		
				elseif kInst == 1 then
					event "i", "inst1", 0, kDur, kNoteValues[p4-1]+kOffset
				elseif kInst == 2 then
					event "i", "inst2", 0, kDur, kNoteValues[p4-1]+kOffset,kNotesAmps[p4-1]
				elseif kInst == 3 then
					event "i", "inst3", 0, kDur, kNoteValues[p4-1]+kOffset,kNotesAmps[p4-1]
				elseif kInst == 4 then
					event "i", "inst4", 0, kDur, kNoteValues[p4-1]+kOffset,kNotesAmps[p4-1]
				elseif kInst == 5 then
					event "i", "inst5", 0, kDur, kNoteValues[p4-1]+kOffset,kNotesAmps[p4-1]
				elseif kInst == 6 then
					event "i", "inst6", 0, kDur, kNoteValues[p4-1]+kOffset,kNotesAmps[p4-1]
				elseif kInst == 7 then
					event "i", "inst7", 0, kDur, kNoteValues[p4-1]+kOffset,kNotesAmps[p4-1]
				endif
			endif	
		endif
		;set next beat if less than seqLength
		chnset kBeat, "beat"
		;advance kBeat if less than seqLength - otherwise reset count
		kBeat = (kBeat<kSeqLength-1 ? kBeat+1 : 0)

	endif

	;get row number and create unique channel name
	SRowCubeIndex sprintf "rowCubeIndex%d", p4
	SRowCubeState sprintf "rowCubeState%d", p4
	SrowCubeNote sprintf "rowCubeNote%d",p4
	kCubeIndex chnget SRowCubeIndex
	kCubeState chnget SRowCubeState
	kCubeNote chnget SrowCubeNote

	;if user has enabled a note, update note amp array
	kChanged changed kCubeState, kCubeIndex
	if changed(kChanged) == 1 then
		kNotesAmps[kCubeIndex] = kCubeState
		kNoteValues[kCubeIndex] = kCubeNote
		printks "Updating row %d - index: %d - value: %d - note: %d - inst: %d",0, p4, kCubeIndex,kNotesAmps[kCubeIndex],kNoteValues[kCubeIndex],kInst
	endif

endin


instr inst0
	a1 expon .1, .5, 0.001
	aOut oscili a1, cpsmidinn(p4)
	outs aOut, aOut
endin

instr inst1
	a1 expon .1, .5, 0.001
	aOut oscili a1, cpsmidinn(p4)
	outs aOut, aOut
endin

instr inst2
; play from score and midi keyboard

	mididefault	60, p3
	midinoteonkey	p4, p5
inum	init	p4
ivel	init	p5
ivel	init	ivel/127					;make velocity dependent
kamp	linsegr	1, 1, 1, .1, 0
kamp	= kamp/5000				;scale amplitude
kfreq	init	1						;do not change freq from sf

      
a1,a2	sfinstr	ivel, inum, kamp*ivel, kfreq, 11, gisf1
	outs	a1, a2
	
endin

instr inst3
; play from score and midi keyboard

	mididefault	60, p3
	midinoteonkey	p4, p5
inum	init	p4
ivel	init	p5
ivel	init	ivel/127					;make velocity dependent
kamp	linsegr	1, 1, 1, .1, 0
kamp	= kamp/5000				;scale amplitude
kfreq	init	1						;do not change freq from sf

      
a1,a2	sfinstr	ivel, inum, kamp*ivel, kfreq, 11, gisf1
	outs	a1, a2
	
endin

instr inst4
; play from score and midi keyboard

	mididefault	60, p3
	midinoteonkey	p4, p5
inum	init	p4
ivel	init	p5
ivel	init	ivel/127					;make velocity dependent
kamp	linsegr	1, 1, 1, .1, 0
kamp	= kamp/5000				;scale amplitude
kfreq	init	1						;do not change freq from sf

      
a1,a2	sfinstr	ivel, inum, kamp*ivel, kfreq, 15, gisf1
	outs	a1, a2
	
endin

instr inst5
; play from score and midi keyboard
;
	mididefault	60, p3
	midinoteonkey	p4, p5
inum	init	p4
ivel	init	p5
ivel	init	ivel/127					;make velocity dependent
kamp	linsegr	1, 1, 1, .1, 0
kamp	= kamp/5000				;scale amplitude
kfreq	init	1						;do not change freq from sf

      
a1,a2	sfinstr	ivel, inum, kamp*ivel, kfreq, 11, gisf1
	outs	a1, a2
	
endin


instr inst6
; play from score and midi keyboard

	mididefault	60, p3
	midinoteonkey	p4, p5
inum	init	p4
ivel	init	p5
ivel	init	ivel/127					;make velocity dependent
kamp	linsegr	1, 1, 1, .1, 0
kamp	= kamp/5000				;scale amplitude
kfreq	init	1						;do not change freq from sf

      
a1,a2	sfinstr	ivel, inum, kamp*ivel, kfreq, 0, gisf2
	outs	a1, a2
	
endin

instr inst7
; play from score and midi keyboard

	mididefault	60, p3
	midinoteonkey	p4, p5
inum	init	p4
ivel	init	p5
ivel	init	ivel/127					;make velocity dependent
kamp	linsegr	1, 1, 1, .1, 0
kamp	= kamp/5000				;scale amplitude
kfreq	init	1						;do not change freq from sf

      
a1,a2	sfinstr	ivel, inum, kamp*ivel, kfreq, 0, gisf2
	outs	a1, a2
	
endin




</CsInstruments>
<CsScore>
f100 0 8 2 0 10 0 0 8 0 0 0
i"ROW_SEQUENCER" 0 [3600*12] 1
i"ROW_SEQUENCER" 0 [3600*12] 2
i"ROW_SEQUENCER" 0 [3600*12] 3
i"ROW_SEQUENCER" 0 [3600*12] 4
i"ROW_SEQUENCER" 0 [3600*12] 5
i"ROW_SEQUENCER" 0 [3600*12] 6
i"ROW_SEQUENCER" 0 [3600*12] 7
i"ROW_SEQUENCER" 0 [3600*12] 8
</CsScore>
</CsoundSynthesizer>