void Update()
{
// Update the note state array.
foreach (var cs in channelArray) {
for (var i = 0; i < 128; i++) {
var x = cs.noteArray [i];
if (x > 1.0f) {
// Key down -> Hold.
cs.noteArray [i] = x - 1.0f;
cs.lastNote = i;
cs.noteOnCount+= 1;
} else if (x < 0) {
// Key up -> Off.
cs.noteArray [i] = 0.0f;
cs.lastNote = i;
cs.noteOnCount-= 1;
}
}
}
// Calculate the filter coefficient.
var filterCoeff = (knobSensibility > 0.0f) ? Mathf.Exp (-knobSensibility * Time.deltaTime) : 0.0f;
// Update the filtered value.
foreach (var cs in channelArray) {
foreach (var k in cs.knobMap.Values) {
k.UpdateFilter (filterCoeff);
}
}
// Process the message queue.
while (MidiBridge.instance.incomingMessageQueue.Count > 0) {
// Pop from the queue.
var message = MidiBridge.instance.incomingMessageQueue.Dequeue ();
// Split the first byte.
var statusCode = message.status >> 4;
var channelNumber = message.status & 0xf;
// Note on message?
if (statusCode == 9) {
var velocity = 1.0f / 127 * message.data2 + 1.0f;
channelArray [channelNumber].noteArray [message.data1] = velocity;
channelArray [(int)MidiChannel.All].noteArray [message.data1] = velocity;
}
// Note off message?
if (statusCode == 8 || (statusCode == 9 && message.data2 == 0)) {
channelArray [channelNumber].noteArray [message.data1] = -1.0f;
channelArray [(int)MidiChannel.All].noteArray [message.data1] = -1.0f;
}
// CC message?
if (statusCode == 0xb) {
// Normalize the value.
var value = 1.0f / 127 * message.data2;
// Update the channel if it already exists, or add a new channel.
if (channelArray [channelNumber].knobMap.ContainsKey (message.data1)) {
channelArray [channelNumber].knobMap [message.data1].Update (value);
} else {
channelArray [channelNumber].knobMap [message.data1] = new KnobState (value);
}
// Do again for All-ch.
if (channelArray [(int)MidiChannel.All].knobMap.ContainsKey (message.data1)) {
channelArray [(int)MidiChannel.All].knobMap [message.data1].Update (value);
} else {
channelArray [(int)MidiChannel.All].knobMap [message.data1] = new KnobState (value);
}
}
}
}