2019-12-06 20:49:52 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
2019-12-05 12:37:20 +00:00
|
|
|
|
using UnityEngine.Playables;
|
2019-12-04 18:14:04 +00:00
|
|
|
|
|
|
|
|
|
namespace cylvester
|
2019-12-06 14:19:36 +00:00
|
|
|
|
{
|
2019-12-06 16:11:43 +00:00
|
|
|
|
public class CylMidiTransitionController : MonoBehaviour
|
2019-12-04 18:14:04 +00:00
|
|
|
|
{
|
2019-12-06 21:21:24 +00:00
|
|
|
|
private enum CylCommand
|
|
|
|
|
{
|
|
|
|
|
OneBarLoopButton = 94,
|
|
|
|
|
FourBarLoopButton = 86,
|
|
|
|
|
NextSelectedScene = 18,
|
|
|
|
|
CurrentSelectedScene = 17,
|
|
|
|
|
InstantTrigger = 2,
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 12:37:20 +00:00
|
|
|
|
[SerializeField] private PlayableDirector playableDirector;
|
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;
|
|
|
|
|
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_;
|
|
|
|
|
private int currentSelectedScene_ ;
|
|
|
|
|
private int nextSelectedScene_;
|
2019-12-04 18:14:04 +00:00
|
|
|
|
|
2019-12-06 21:21:24 +00:00
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
scheduledAction_ = new ScheduledAction(() =>
|
|
|
|
|
{
|
|
|
|
|
stateManager.SelectedState = currentSelectedScene_;
|
|
|
|
|
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(instaTransitionSpeed);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 18:14:04 +00:00
|
|
|
|
public void OnSyncReceived(MidiSync midiSync, int counter)
|
|
|
|
|
{
|
2019-12-06 20:49:52 +00:00
|
|
|
|
currentTick_ = counter;
|
2019-12-04 18:14:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnMidiMessageReceived(MidiMessage mes)
|
|
|
|
|
{
|
2019-12-06 20:49:52 +00:00
|
|
|
|
if (mes.Status - 176 != channel - 1) return;
|
2019-12-05 21:08:56 +00:00
|
|
|
|
|
2019-12-06 21:21:24 +00:00
|
|
|
|
var command = (CylCommand)mes.Data1;
|
2019-12-06 20:49:52 +00:00
|
|
|
|
switch (command)
|
2019-12-04 18:14:04 +00:00
|
|
|
|
{
|
2019-12-06 21:21:24 +00:00
|
|
|
|
case CylCommand.NextSelectedScene:
|
2019-12-05 21:08:56 +00:00
|
|
|
|
{
|
2019-12-06 20:49:52 +00:00
|
|
|
|
nextSelectedScene_ = mes.Data2;
|
2019-12-06 14:19:36 +00:00
|
|
|
|
break;
|
2019-12-06 20:49:52 +00:00
|
|
|
|
}
|
2019-12-06 21:21:24 +00:00
|
|
|
|
case CylCommand.InstantTrigger:
|
2019-12-06 20:49:52 +00:00
|
|
|
|
{
|
2019-12-06 21:21:24 +00:00
|
|
|
|
scheduledAction_.Ready();
|
2019-12-06 14:19:36 +00:00
|
|
|
|
break;
|
2019-12-06 20:49:52 +00:00
|
|
|
|
}
|
2019-12-06 21:21:24 +00:00
|
|
|
|
case CylCommand.CurrentSelectedScene:
|
2019-12-06 20:49:52 +00:00
|
|
|
|
{
|
|
|
|
|
currentSelectedScene_ = mes.Data2;
|
2019-12-06 21:21:24 +00:00
|
|
|
|
scheduledAction_.Go();
|
2019-12-06 14:19:36 +00:00
|
|
|
|
break;
|
2019-12-06 20:49:52 +00:00
|
|
|
|
}
|
2019-12-06 21:21:24 +00:00
|
|
|
|
case CylCommand.FourBarLoopButton:
|
2019-12-06 20:49:52 +00:00
|
|
|
|
{
|
2019-12-06 21:29:46 +00:00
|
|
|
|
var restTime = UpdateRestTime(FourBarTrigger - currentTick_ % FourBarTrigger);
|
2019-12-06 20:49:52 +00:00
|
|
|
|
if (nextSelectedScene_ > currentSelectedScene_)
|
|
|
|
|
{
|
2019-12-06 21:29:46 +00:00
|
|
|
|
UpdateTimelinePlaybackSpeed(1f, restTime);
|
2019-12-06 20:49:52 +00:00
|
|
|
|
stateManager.SelectedState = nextSelectedScene_;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-06 21:29:46 +00:00
|
|
|
|
UpdateTimelinePlaybackSpeed(-1f, restTime);
|
2019-12-06 20:49:52 +00:00
|
|
|
|
stateManager.SelectedState = nextSelectedScene_ + 2;
|
|
|
|
|
}
|
2019-12-06 14:19:36 +00:00
|
|
|
|
|
|
|
|
|
break;
|
2019-12-06 20:49:52 +00:00
|
|
|
|
}
|
2019-12-06 21:21:24 +00:00
|
|
|
|
case CylCommand.OneBarLoopButton:
|
2019-12-06 20:49:52 +00:00
|
|
|
|
{
|
2019-12-06 21:29:46 +00:00
|
|
|
|
var restTime = UpdateRestTime(OneBarTrigger - currentTick_ % OneBarTrigger);
|
|
|
|
|
UpdateTimelinePlaybackSpeed(1f, restTime);
|
2019-12-06 20:49:52 +00:00
|
|
|
|
stateManager.SelectedState = nextSelectedScene_;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
throw new Exception("Unexpected CYL command");
|
|
|
|
|
|
2019-12-04 18:14:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 21:29:46 +00:00
|
|
|
|
private float UpdateRestTime(int restTicks)
|
2019-12-04 18:14:04 +00:00
|
|
|
|
{
|
2019-12-06 21:29:46 +00:00
|
|
|
|
return restTicks / 24f / stateManager.CurrentState.Bpm * 60f;
|
2019-12-04 18:14:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 21:29:46 +00:00
|
|
|
|
private void UpdateTimelinePlaybackSpeed(float speed, float restTime)
|
2019-12-04 18:14:04 +00:00
|
|
|
|
{
|
2019-12-06 21:29:46 +00:00
|
|
|
|
var timelinePlaybackSpeed = TransitionLength / Mathf.Clamp(restTime, 0.001f, TransitionLength);
|
|
|
|
|
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(timelinePlaybackSpeed * speed);
|
2019-12-04 18:14:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|