// Script:         endRecordAndJam.ltgs 
// Description:    Allows to start jamming after a record loop. 
//				   Usage: Create a very long record loop (eg. CTRL-i), start recording and call this script
//					the script will set the loop end to the next bar, create a play loop and 
//					a record loop and start jamming in record mode
// Arguments: 
//		Arg0: nb of multiple of loop. If equal to 0 and userArg0 not specify, will be 1
//		userArg0: nb of multiple of loop (has priority over Arg0)
//	
// Author : LoopToGo 

// Taking care of arguments


ifNot $isRecording
	endScriptDynamic
endIf	

if $nbUserArgs
	variable nbRepeat $userArg1 1
	setUserArgs // make sure we reset for next userArgs
else
	if $nbArgs
		variable nbRepeat $arg1 1
	endIf
endIf

// make sure nbRepeat is between 1 and 50

if $nbRepeat < 1 
	variable nbRepeat 1
else
	if $nbRepeat > 50
		variable nbRepeat 50
	endIf
endIf	

unselectAllLoops

shortcutByID 64 // Select loop at selected track where the songBar is

getLoopType loopType $selectedLoopID

//print loopType: $loopType

ifNot $loopType = Record
	print "Song bar is not over a record loop on the selected track!"
	endScriptDynamic
endIf

variable loopNewLenght 1 + $songBar - $selectedLoopStart 1 // Compute the new loop length (quantize to 1 bar)

// Start and stop of new loops
variable startOfNewLoops  $selectedLoopStart + $loopNewLenght
variable endOfNewLoops	$startOfNewLoops + $loopNewLenght*$nbRepeat

//print nbRepeat: $nbRepeat
//print startOfNewLoops: $startOfNewLoops
//print endOfNewLoops: $endOfNewLoops

// resize the existing loop
setLoopStartAndEnd $selectedLoopID $selectedLoopStart $startOfNewLoops // Resize the loop

// Create the new loops
addPlayLoop $selectedTrack $selectedLoopID $startOfNewLoops $endOfNewLoops
addRecordLoop $selectedTrack $selectedLoopName& "-jam" $startOfNewLoops $endOfNewLoops

waitTilSongBar $selectedLoopEnd + .1

shortcutByID 37 // Jam mode

// shortcutByID 66 // Overdub now  (uncomment to start in overdub, comment to start in record mode)

	


 
