2019-12-04 18:14:04 +00:00
using UnityEngine ;
using UnityEngine.Events ;
2019-12-05 12:37:20 +00:00
using UnityEngine.Timeline ;
using UnityEngine.Playables ;
2019-12-04 18:14:04 +00:00
namespace cylvester
{
public class MidiTransitionController : MonoBehaviour
{
[SerializeField] private int oneBarLoopButton = 86 ;
[SerializeField] private int fourBarLoopButton = 94 ;
2019-12-05 12:37:20 +00:00
[SerializeField] private PlayableDirector playableDirector ;
2019-12-04 18:14:04 +00:00
private int oneBarLoop = 96 ;
private int fourBarLoop = 384 ;
2019-12-05 21:08:56 +00:00
private int instaTrig = 2 ;
private int nextSelectedSceneOutput = 18 ;
private int lastSelectedSceneOutput = 17 ;
private bool instaChangeActive ;
2019-12-04 18:14:04 +00:00
private int currentTick ;
private float transitionLength = 16 ; //sets the duration in Seconds, how long a transition has to be in "TimeLine" to be played back correctly when CYLVESTER is hooked up correctly
private float restTimeS = 1f ; //init transTime is 1 Second
2019-12-05 21:08:56 +00:00
[SerializeField] private int lastSelectedScene = 0 ;
[SerializeField] private int currentSelectedScene = 0 ;
[SerializeField] private int nextSelectedScene ;
2019-12-04 18:14:04 +00:00
[SerializeField, Range(1, 16)] private int channel = 1 ;
[SerializeField] StateManager stateManager ;
public void OnSyncReceived ( MidiSync midiSync , int counter )
{
currentTick = counter ;
}
public void OnMidiMessageReceived ( MidiMessage mes )
{
2019-12-05 21:08:56 +00:00
2019-12-04 18:14:04 +00:00
if ( mes . Status - 176 = = channel - 1 ) //Which Channel
{
2019-12-05 21:08:56 +00:00
if ( mes . Data1 = = nextSelectedSceneOutput )
{
nextSelectedScene = mes . Data2 ; //Get next Schene Update
}
if ( mes . Data1 = = lastSelectedSceneOutput )
lastSelectedScene = mes . Data2 ; //Get current selected Scene
2019-12-04 18:14:04 +00:00
if ( mes . Data1 = = oneBarLoopButton ) //Button fourBarLoop
{
RestTime ( fourBarLoop - currentTick % fourBarLoop ) ;
2019-12-05 12:37:20 +00:00
TimelinePlaybackSpeed ( ) ;
2019-12-05 21:08:56 +00:00
stateManager . SelectedState = nextSelectedScene ;
2019-12-04 18:14:04 +00:00
}
if ( mes . Data1 = = fourBarLoopButton ) //Button oneBarLoop
{
RestTime ( oneBarLoop - currentTick % oneBarLoop ) ;
2019-12-05 12:37:20 +00:00
TimelinePlaybackSpeed ( ) ;
2019-12-05 21:08:56 +00:00
stateManager . SelectedState = nextSelectedScene ;
}
if ( mes . Data1 = = instaTrig )
{
instaChangeActive = true ;
if ( instaChangeActive & & ( currentSelectedScene ! = lastSelectedScene ) )
{
currentSelectedScene = lastSelectedScene ;
stateManager . SelectedState = lastSelectedScene ;
playableDirector . playableGraph . GetRootPlayable ( 0 ) . SetSpeed ( 10 ) ;
Debug . Log ( "Instatrig " + currentSelectedScene ) ;
Debug . Log ( "Last selected Scene new " + lastSelectedScene ) ;
instaChangeActive = false ;
}
2019-12-04 18:14:04 +00:00
}
}
}
public void RestTime ( int restTick )
{
restTimeS = restTick / 24.0f / stateManager . CurrentState . Bpm * 60 ;
}
2019-12-05 21:08:56 +00:00
public void TimelinePlaybackSpeed ( )
2019-12-04 18:14:04 +00:00
{
2019-12-05 21:08:56 +00:00
float timelinePlaybackSpeed = transitionLength / Mathf . Clamp ( restTimeS , 0.001f , transitionLength ) ;
2019-12-05 12:37:20 +00:00
playableDirector . playableGraph . GetRootPlayable ( 0 ) . SetSpeed ( timelinePlaybackSpeed ) ; //set playbackspeed of Timeline
2019-12-04 18:14:04 +00:00
}
}
}