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-06 10:25:38 +00:00
private const int cmd_instaTrig = 2 ;
private const int cmd_nextSelectedScene = 18 ;
private const int cmd_currentSelectedScene = 17 ;
2019-12-05 21:08:56 +00:00
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-06 10:25:38 +00:00
[SerializeField] private int currentSelectedScene ;
2019-12-05 21:08:56 +00:00
[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
2019-12-06 10:25:38 +00:00
if ( mes . Data1 = = cmd_nextSelectedScene )
2019-12-05 21:08:56 +00:00
{
nextSelectedScene = mes . Data2 ; //Get next Schene Update
}
2019-12-06 10:25:38 +00:00
if ( mes . Data1 = = cmd_instaTrig )
{
instaChangeActive = true ;
}
if ( mes . Data1 = = cmd_currentSelectedScene )
{
currentSelectedScene = mes . Data2 ; //Get current selected Scene
if ( instaChangeActive )
{
stateManager . SelectedState = currentSelectedScene ;
playableDirector . playableGraph . GetRootPlayable ( 0 ) . SetSpeed ( 10 ) ;
// Debug.Log("Instatrig " + currentSelectedScene);
// Debug.Log("Last selected Scene new " + lastSelectedScene);
instaChangeActive = false ;
}
}
2019-12-05 21:08:56 +00:00
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 ;
}
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
}
}
}