// Script:         autoParamRamp.ltgs 
// Description:    Script to automate the change of a plugin parameter. The parameter value will change linearly within the loop (linear interpolation).
//                 Must be used with both the Loop Enter and Loop Leave events.
//                 To set up, open the Properties dialog of a loop and select this script for both the Enter and Leave event scripts.
//                 Set the arguments for the Enter script only. Set no arguments for the Leave script.
//                 When the playhead is outside of the loop, the parameter value will be set as follows: valueAtStart if it is before the loop, and valueAtEnd if it is after the loop.
//
//                 The a_setPluginParameterCommand alias can be changed to use normalized (0.0 to 1.0) or plain (range as defined in the parameter reference) values. 		
//
//                 This script is designed to be used in side play loops. It can be used in side record loops but the loop's name won't be set properly as in Play loops
//	   
// Arguments: 
//                 Label (string): Should represent the parameter. Displayed in the loop with valueAtStart, currentValue, and valueAtEnd.  
//                 TrackNameOrID (string): The track name or ID. Optional, but recommended for performance if there are many plugins in the song.
//                 PluginNameOrID (string): The plugin name or ID. If the name corresponds to more than one plugin, the first one will be used. To target another one, the ID must be used.
//                 ParameterNameOrID (string): The parameter name or ID. It can be found by clicking on the MIDI button beside the plugin button.
//                 valueAtStart (float): Plugin parameter value at the beginning of the loop.
//                 valueAtEnd (float): Plugin parameter value at the end of the loop.
//                 resolution (float): Resolution in bars. A lower number means higher precision. (e.g., if set to 0.25, the parameter value will be updated every 0.25 bar within the loop.)
//	
// Author:         LoopToGo



if $loopEvent = Enter
	if $nbArgs < 6
		print "ERROR: wrong number of arguments"
		print "Usage: " & $scriptFileName & " label trackName pluginID parameterID minValue maxValue [resolution]" 
		endScriptDynamic
	endIf
endIf

alias a_setPluginParameterCommand setPluginParameterValue // Set to setPluginParameterValue or setPluginParameterPlain to use normalized or plain values

alias a_defaultResolution .25

alias a_waitBeforeCheckBars .1   // In bars

alias a_true 1
alias a_false 0

alias print  doNothing	// Comment this line to debug. Uncomment for final version

print Start Script $scriptID loopID : $eventLoopID

namespace $eventLoopID // We use the loopID as namespave for all variables. This allow to use global variables specific to each loop


	getLoopStart loopStart $eventLoopID
	getLoopEnd loopEnd $eventLoopID
	variable loopWidth $loopEnd - $loopStart


	if $loopEvent = Enter
	
		variable playhead $songBar

		globalVariable g_label $arg1
		globalVariable g_trackName $arg2
		globalVariable g_pluginID $arg3
		globalVariable g_paramID $arg4
		globalVariable g_startValue $arg5
		globalVariable g_endValue $arg6
		
		globalVariable g_insideLoop a_true
		
		if $nbArgs > 6
			globalVariable g_resolution $arg7 // Optionnal arg
		else 
			globalVariable g_resolution a_defaultResolution
		endIf
		
		if $g_startValue < $g_endValue
			globalVariable g_minVal $g_startValue
			globalVariable g_maxVal $g_endValue
		else
			globalVariable g_minVal $g_endValue
			globalVariable g_maxVal $g_startValue
		endIf
		
		
	else // Leave
		print Leaving loop : $songBar
		
		globalVariable g_insideLoop a_false // Will end this script and also the enter script which is in the while loop 
		
		// Let's see if we exit the loop by jumping before or after the loop
		if $songBar < $loopStart + $loopWidth/2 // Middle
			variable playhead $loopStart
		else
			variable playhead $loopEnd
		endIf
		
		callFunction computeNewValue
		
		globalVariable g_lastPlayhead $playhead

		
	endIf

	while $g_insideLoop
		
		variable ph $playhead $g_resolution   // Quantize to g_resolution
		variable lph $g_lastPlayhead $g_resolution // Quantize to g_resolution
		variable deltaPlayhead $ph - $lph
			
		if  $deltaPlayhead // Different from 0		
		
			print g_lastPlayhead : $g_lastPlayhead playhead : $playhead deltaPlayhead: $deltaPlayhead g_resolution : $g_resolution
		
			callFunction computeNewValue
			
			globalVariable g_lastPlayhead $playhead

		endIf
		
		variable playhead $songBar
		
		if $songBar < $loopStart - $g_resolution/2
			globalVariable g_insideLoop a_false  // In case the Leave event was never called (the user deactivate the track for example)		
		endIf
		if $songBar > $loopEnd + $g_resolution/2
			globalVariable g_insideLoop a_false  // In case the Leave event was never called (the user deactivate the track for example)		
		endIf
		
		waitDelta a_waitBeforeCheckBars // In bar time

	endWhile
	

endNamespace  

function computeNewValue

	namespace $eventLoopID

		variable deltaValue $g_endValue - $g_startValue
		variable deltaBar0 $playhead - $loopStart

		variable value  $g_startValue + $deltaValue*$deltaBar0/$loopWidth 0.01
		
		// Check limits
		if $value  < $g_minVal
			variable value $g_minVal
		else
			if $value  > $g_maxVal
				variable value $g_maxVal			
			endIf
		endIf

		print value : $value 

		a_setPluginParameterCommand	$g_trackName $g_pluginID $g_paramID $value
			
		// Comment next lines for Release below 3.1.10 because setLoopAltName is new
		
		// Select preferred formatting
		//setLoopAltName 	$eventLoopID $g_label & " " & $value     // Reverb 0.25
		//setLoopAltName 	$eventLoopID $g_label & " [" & $g_startValue & " : " & $g_endValue & "] " & $value // Reverb [0 : 0.5] .25
		
		variable rangeAndValue " [" & $g_startValue & " : " & $value & " : " & $g_endValue & "]"
		setLoopAltName 	$eventLoopID $g_label & $rangeAndValue  // Reverb [0 : .25 : 0.5] 
		
	endNamespace

endFunction

