soundvision/UnityProject/Assets/Scripts/PdConnection/MidiSequencer.cs
Chikashi Miyama 8fae5f3707 implement #94
2019-10-30 18:45:05 +01:00

33 lines
909 B
C#

using System;
using UnityEngine;
using UnityEngine.Events;
namespace cylvester
{
[Serializable]
class UnitySequenceEvent : UnityEvent<int>
{ }
public class MidiSequencer : MonoBehaviour
{
[SerializeField] private bool[] sequence = new bool[24]; // max 6 beat
[SerializeField] private int time = 4;
[SerializeField] private UnitySequenceEvent triggered;
private void OnValidate()
{
if(sequence.Length != 24)
Array.Resize(ref sequence, 24);
}
public void OnSyncReceived(MidiSync midiSync, int count)
{
if (count % 6 != 0) // 24 ppqn (pulse per quater note). %6 means every 16th note because 24/4
return;
var index = count /6 % (time * 4);
if (sequence[index])
triggered.Invoke(index);
}
}
}