soundvision/UnityProject/Assets/Scripts/PdConnection/CylMidiTransitionController.cs

117 lines
4.2 KiB
C#
Raw Normal View History

2019-12-06 20:49:52 +00:00
using System;
using UnityEngine;
2019-12-05 12:37:20 +00:00
using UnityEngine.Playables;
namespace cylvester
2019-12-06 14:19:36 +00:00
{
public class CylMidiTransitionController : MonoBehaviour
{
2019-12-06 21:21:24 +00:00
private enum CylCommand
2020-01-08 15:59:22 +00:00
{
2019-12-06 21:21:24 +00:00
OneBarLoopButton = 94,
FourBarLoopButton = 86,
2020-01-08 15:59:22 +00:00
NextSelectedTransition = 18,
CurrentSelectedAnimation = 17,
2019-12-06 21:21:24 +00:00
InstantTrigger = 2,
}
2020-01-08 15:59:22 +00:00
2019-12-05 12:37:20 +00:00
[SerializeField] private PlayableDirector playableDirector;
[SerializeField] private TimelineController timelineController;
2019-12-06 14:19:36 +00:00
[SerializeField, Range(1, 16)] private int channel = 1;
2019-12-06 20:49:52 +00:00
[SerializeField] private StateManager stateManager;
[SerializeField] private float instaTransitionSpeed = 10;
private const int OneBarTrigger = 96;
private const int FourBarTrigger = OneBarTrigger * 4;
2020-01-08 15:59:22 +00:00
private const float TransitionLength = 16;
2019-12-06 21:21:24 +00:00
private ScheduledAction scheduledAction_;
2019-12-06 20:49:52 +00:00
private int currentTick_;
2020-01-08 15:59:22 +00:00
private int currentSelectedAnimation_;
private int nextSelectedMarker_;
2019-12-06 21:21:24 +00:00
private void Start()
{
scheduledAction_ = new ScheduledAction(() =>
{
2020-01-08 15:59:22 +00:00
stateManager.SelectedState = currentSelectedAnimation_;
//playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(instaTransitionSpeed);
timelineController.instantTrigger(instaTransitionSpeed);
2019-12-06 21:21:24 +00:00
});
2020-01-08 15:59:22 +00:00
currentSelectedAnimation_ = 0;
nextSelectedMarker_ = 1;
2019-12-06 21:21:24 +00:00
}
2020-01-08 15:59:22 +00:00
public void OnSyncReceived(MidiSync midiSync, int counter)
{
2019-12-06 20:49:52 +00:00
currentTick_ = counter;
}
public void OnMidiMessageReceived(MidiMessage mes)
{
2019-12-06 20:49:52 +00:00
if (mes.Status - 176 != channel - 1) return;
2019-12-06 21:21:24 +00:00
var command = (CylCommand)mes.Data1;
2019-12-06 20:49:52 +00:00
switch (command)
{
2020-01-08 15:59:22 +00:00
case CylCommand.NextSelectedTransition:
nextSelectedMarker_ = mes.Data2; //next marker only used for direction
Debug.Log("command nextSelectedMarker_=" + nextSelectedMarker_);
2019-12-06 14:19:36 +00:00
break;
2020-01-08 15:59:22 +00:00
2019-12-06 21:21:24 +00:00
case CylCommand.InstantTrigger:
2020-01-08 15:59:22 +00:00
2019-12-06 21:21:24 +00:00
scheduledAction_.Ready();
2019-12-06 14:19:36 +00:00
break;
2020-01-08 15:59:22 +00:00
case CylCommand.CurrentSelectedAnimation:
currentSelectedAnimation_ = mes.Data2;
Debug.Log("command currentSelectedAnimation_=" + currentSelectedAnimation_);
2019-12-06 21:21:24 +00:00
scheduledAction_.Go();
2019-12-06 14:19:36 +00:00
break;
2020-01-08 15:59:22 +00:00
2019-12-06 21:21:24 +00:00
case CylCommand.FourBarLoopButton:
2020-01-08 15:59:22 +00:00
case CylCommand.OneBarLoopButton:
float restTime=0;
switch(command)
2019-12-06 20:49:52 +00:00
{
2020-01-08 15:59:22 +00:00
case CylCommand.FourBarLoopButton:
restTime = CalculateRestTime(FourBarTrigger - currentTick_ % FourBarTrigger);
break;
case CylCommand.OneBarLoopButton:
restTime = CalculateRestTime(OneBarTrigger - currentTick_ % OneBarTrigger);
break;
2019-12-06 20:49:52 +00:00
}
2020-01-08 15:59:22 +00:00
Debug.Log("currentSelectedAnimation_=" + currentSelectedAnimation_);
Debug.Log("nextSelectedScene_=" + nextSelectedMarker_);
bool _reverse = nextSelectedMarker_ <= currentSelectedAnimation_; //nextSelectedMarker_ used to calculate direction
2019-12-06 14:19:36 +00:00
2020-01-08 15:59:22 +00:00
if (timelineController.animationPaused())
{
2020-01-08 15:59:22 +00:00
timelineController.UpdateTransitionTargetRealTime(restTime, _reverse);
stateManager.SelectedState = currentSelectedAnimation_ + 1; //marker at which transition starts
}
2020-01-08 15:59:22 +00:00
else //trigger pressed while animation running
{
2020-01-08 15:59:22 +00:00
timelineController.abortAnimation();
}
2019-12-06 20:49:52 +00:00
break;
2020-01-08 15:59:22 +00:00
2019-12-06 20:49:52 +00:00
default:
2020-01-08 15:59:22 +00:00
Debug.Log("Unexpected Command: " + mes.Data1);
2019-12-06 20:49:52 +00:00
throw new Exception("Unexpected CYL command");
}
}
private float CalculateRestTime(int restTicks)
{
2019-12-06 21:29:46 +00:00
return restTicks / 24f / stateManager.CurrentState.Bpm * 60f;
}
}
}