// Script to use pad Leds to show jam mode state on a Arturia Keylab mkII 
// 		this script use midi sysEx message instead of midiNoteOn
//		It is more versatile and does not depend on the keyboard setting (Analog Lab, DAW, or USER mode)
//		It also use the concept of function so it is more reusable
// Arguments:  
//		$arg1 : 1 to always run, none or 0 to stop with song
//
// Author: LoopToGo
//
// Description  : 	

loadScript ..\aliases.txt

loadScript ..\libs\arturiaKeylabMKIIUtilities.ltgl // For setPadColor


while $arg1 + $isPlaying + $isRecording

	getBankInfo actualBank nbTrackPerBank
	
	//print isInJamMode : $isInJamMode

	for track 1 4
		
		variable firstTrackButton $track*4 - 3 
		
		if $isInJamMode
		
			variable wentInJamMode&$track 1
		
			variable realTrackNumber $track + $actualBank * $nbTrackPerBank - $nbTrackPerBank
			variable trackName trackName&$realTrackNumber
			
			//print $track $realTrackNumber $track $actualBank $nbTrackPerBank $$trackName

			getNbLoopsForTrack nbLoopsInTrack $$trackName
			
			//print $track : nbLoopsInTrack $nbLoopsInTrack
			
			variable currentLoop ""

			
			for lp 1 $nbLoopsInTrack
				getLoopIDForTrack myLoop $realTrackNumber $lp
				getLoopStart loopStart $myLoop
				getLoopEnd loopEnd $myLoop
				
				variable condition1 $loopStart < $songBar
				variable condition2 $loopEnd > $songBar
				
				//print $myLoop $loopStart $loopEnd $condition1 $condition2 $songBar
				
				if $condition1 * $condition2
					variable currentLoop $myLoop
				endIf
				
				
			endFor
					

			if $currentLoop
			
				getLoopJamState jamState $currentLoop 			
				
				//print track : $track jamState : $jamState firstTrackButton : $firstTrackButton			
							
				if $jamState = 6	// Overdub			
					callFunction setPadColor PadLight $firstTrackButton + 2 127 127 0 // yellow
				else
					callFunction setPadColor PadLight $firstTrackButton + 2 0 0 0	
				endIf
				
				if $jamState = 2	// Record			
					callFunction setPadColor PadLight $firstTrackButton + 1 127 0 0 // red
				else
					callFunction setPadColor PadLight $firstTrackButton + 1 0 0 0	
				endIf
				
				variable cycleOut  $jamState = 1	// CycleOut  				grey
				variable cycleLocked $jamState = 8	// CycleLocked 				white
				variable cycleOutLocked  $jamState = 9	// CycleOutLocked  			grey
				variable cycleRepeatLocked  $jamState = 10	// CycleRepeatLocked 	white
				
				if $cycleLocked + $cycleRepeatLocked	// White			
					callFunction setPadColor PadLight $firstTrackButton + 3 127 127 127 // white
				else
					if $cycleOut + $cycleOutLocked	// White
						callFunction setPadColor PadLight $firstTrackButton + 3 100 100 100 // grey
					else					
						callFunction setPadColor PadLight $firstTrackButton + 3 0 0 0	
					endIf
				endIf
			else
				// Turn all button off for a track
				for button 1 4
					callFunction setPadColor PadLight $firstTrackButton + $button - 1 0 0 0
				endFor
			endIf
			
		else
			// Turn all button off for a track
			variable wentInJamMode wentInJamMode&$track
			
			if $$wentInJamMode
				//print "Turn button off!"
				for button 1 4
					callFunction setPadColor PadLight $firstTrackButton + $button - 1 0 0 0
				endFor
				variable wentInJamMode&$track 0
			endIf
		endIf
		
	endFor
	
	ifNot $isPlaying + $isRecording
		for n 1 16					
			callFunction setPadColor PadLight $n - 1 0 0 0
		endFor
	endIf
	
	waitDelta .2 // Very important to have at least 1 wait in the while so the script is non blocking!
endWhile

// Turn off the leds 
for n 1 16					
	callFunction setPadColor PadLight $n - 1 0 0 0
endFor
		
