// mirrorSong.ltgs
//
// Arguments:  no arguments
//
// Author: LoopToGo
//
// Description: Script to "synchronize" 2 instances of LoopToGo running on 2 different computers. This is very usefull for setup with
//    			many musicians or with instruments at different locations
//
// Important notes:
//		1: Corresponding OSC connectors must be defined on both instances of LoopToGo
//		2: OSC connector on the "master" instance must be called OtherPC or changed accordingly with the otherPcOscConnector definition below
//		3: Master computer must be able to send message to receiving port of Slave computer. Firewall configuration might be necessary

// Kill last script instance if any

killScript $g_lastScriptInstanceID
globalVariable g_lastScriptInstanceID $scriptID

variable otherPcOscConnector OtherPC


while 1 // This while will allow to resend all information to other LTG instance when user load a new song

	sendOscMessage $otherPcOscConnector "/script/setUserArgs" 1 // Force (not asking for saving current song)
	sendOscMessage $otherPcOscConnector "/script/shortcutByID" 13 // New song

	sendOscMessage $otherPcOscConnector "/script/setTempo" $tempo // Set Tempo


	// Copy tracks

	for i 1 $nbTracks

		variable trackName trackName & $i
		getTrackColor red blue green $$trackName
		getTrackDisplayState displayState $$trackName
		
		sendOscMessage $otherPcOscConnector "/script/addChain" $$trackName
		sendOscMessage $otherPcOscConnector "/script/setChainColor" $$trackName  $red   $blue  $green 
		sendOscMessage $otherPcOscConnector "/script/setTrackDisplayState" $$trackName  $displayState
			
	endFor

	//Copy loops	
	for n 1 $nbLoops
		
		variable loopID loopID & $n
		
		getLoopTrack trackName $$loopID
		getLoopName loopName $$loopID
		getLoopStart start $$loopID
		getLoopEnd end $$loopID
		getLoopType type $$loopID
		
		
		if $type = Record
			sendOscMessage $otherPcOscConnector "/script/addRecordLoop" $trackName $loopName $start $end
		else
			sendOscMessage $otherPcOscConnector "/script/addPlayLoop" $trackName $loopName $start $end 0 0 // 0 0 = delay and speed
		endIf

	endFor

	// Copy song (Title, author, Lyrics and chords)

	for i 1 $nbSongLines

		variable songLine songLine & $i
		variable songLineType songLineType & $i
		
		
		ifNot $$songLine
			variable lineText " "
		else
			variable lineText $$songLine
		endIf	
		
		sendOscMessage $otherPcOscConnector "/script/addSongLine" $lineText $$songLineType

	endFor

	// Copy song configuration
	getSongFirstChordPosition fcPos
	sendOscMessage $otherPcOscConnector "/script/setSongFirstChordPosition" $fcPos // Enable Zoom on Lyrics and chords

	getSongTextScaleFactor tsf
	sendOscMessage $otherPcOscConnector "/script/setSongTextScaleFactor" $tsf // Enable Zoom on Lyrics and chords

	getSongBarBetweenChords bbc
	sendOscMessage $otherPcOscConnector "/script/setSongBarBetweenChords" $bbc // Enable Zoom on Lyrics and chords

	for i 1 6

		getSongSymbol symbol value showSymbol $i

		sendOscMessage $otherPcOscConnector "/script/setSongSymbol" $i $symbol $value $showSymbol // Enable Zoom on Lyrics and chords

	endFor

	// Copy display configuration

	// Enable Zoom on Lyrics and chords
	sendOscMessage $otherPcOscConnector "/script/setUserArgs" 1 
	sendOscMessage $otherPcOscConnector "/script/shortcutByID" 118 

	// Hide Plugin browser
	sendOscMessage $otherPcOscConnector "/script/setUserArgs" 0
	sendOscMessage $otherPcOscConnector "/script/shortcutByID" 21 

	// Hide Structure browser
	sendOscMessage $otherPcOscConnector "/script/setUserArgs" 0
	sendOscMessage $otherPcOscConnector "/script/shortcutByID" 101

	// Hide Mixing console 
	sendOscMessage $otherPcOscConnector "/script/setUserArgs" 0
	sendOscMessage $otherPcOscConnector "/script/shortcutByID" 22

	// Always zoom vertically 
	sendOscMessage $otherPcOscConnector "/script/setUserArgs" 1
	sendOscMessage $otherPcOscConnector "/script/shortcut" T // Fit Zoom vertically

	// Select looper view 
	sendOscMessage $otherPcOscConnector "/script/setUserArgs" 1
	sendOscMessage $otherPcOscConnector "/script/shortcut" L // Fit Zoom vertically

	variable sameSong 1
	variable currectSong $songName

	// Synchronize transport
	while $sameSong
	
		while $isPlaying + $isRecording + $isPaused = 1   // Playing or recording but not paused
			sendOscMessage $otherPcOscConnector "/script/setBar" $songBar // Synchronise playhead. Needed to take into accounbt goto and jam
			waitDeltaTime 4
		endWhile
		
		sendOscMessage $otherPcOscConnector "/script/shortcutByID" 17 // Stop

		variable pauseAndStoppedCondition		$isPaused = $isPlaying + $isRecording
		
		while  $pauseAndStoppedCondition // Stopped or paused
			sendOscMessage $otherPcOscConnector "/script/setBar" $songBar // Synchronise playhead
			waitDeltaTime 4
					
			variable pauseAndStoppedCondition		$isPaused = $isPlaying + $isRecording
			
			variable  sameSong $currectSong = $songName
			
			ifNot $sameSong
				variable pauseAndStoppedCondition // We must get out of the while loop (no break implkemented at this time)
			endIf

		endWhile
		
		sendOscMessage $otherPcOscConnector "/script/shortcutByID" 16 // Play

		
	endWhile

endWhile