Skip to content

Commit

Permalink
Cleanup and set debug buffered messages count to zero after buffer flush
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytra committed Jul 23, 2024
1 parent b8325cb commit 453eec6
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions ProjectObsidian/Components/Devices/MIDI/MIDI_InputDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ public class MIDI_InputDevice : Component

private const bool DEBUG = true;

private struct TimestampedMidiEvent
{
public MidiEvent midiEvent;
public long timestamp;
public TimestampedMidiEvent(MidiEvent _midiEvent, long _timestamp)
{
midiEvent = _midiEvent;
timestamp = _timestamp;
}
}

// I am using this like a Queue so it could possibly be turned into a Queue instead...
private List<TimestampedMidiEvent> _eventBuffer = new();

private const long MESSAGE_BUFFER_TIME_MILLISECONDS = 3;

private long _lastMessageBufferStartTime = 0;

private int _bufferedMessagesToHandle = 0;

protected override void OnStart()
{
base.OnStart();
Expand Down Expand Up @@ -91,7 +111,7 @@ private async Task ReleaseDeviceAsync()
UniLog.Log("Device released.");
_inputDevice = null;
_eventBuffer.Clear();
_lastBatchStartTime = 0;
_lastMessageBufferStartTime = 0;
}

private async void ReleaseDeviceAndConnectAsync(IMidiAccess access, string deviceId)
Expand Down Expand Up @@ -207,22 +227,6 @@ private ushort CombineBytes(byte First, byte Second)
return _14bit;
}

private struct TimestampedMidiEvent
{
public MidiEvent midiEvent;
public long timestamp;
public TimestampedMidiEvent(MidiEvent _midiEvent, long _timestamp)
{
midiEvent = _midiEvent;
timestamp = _timestamp;
}
}

// I am using this like a Queue so it could possibly be turned into a Queue instead...
private List<TimestampedMidiEvent> _eventBuffer = new();

private const long BATCH_TIME_SIZE_MILLISECONDS = 3;

private bool IsCCFineMessage()
{
if (_eventBuffer.Count == 0) return false;
Expand Down Expand Up @@ -313,12 +317,9 @@ private void FlushMessageBuffer()
// Just in case some messages got lost somehow
UniLog.Warning("Did not handle all buffered messages! " + _bufferedMessagesToHandle.ToString());
}
_bufferedMessagesToHandle = 0;
}

private long _lastBatchStartTime = 0;

private int _bufferedMessagesToHandle = 0;

private async void OnMessageReceived(object sender, MidiReceivedEventArgs args)
{
if (DEBUG) UniLog.Log($"*** New midi message");
Expand All @@ -327,8 +328,6 @@ private async void OnMessageReceived(object sender, MidiReceivedEventArgs args)

var events = MidiEvent.Convert(args.Data, args.Start, args.Length);

//if (events.Count() == 0) return;

if (args.Length == 1)
{
// system realtime message, do not buffer these, execute immediately
Expand Down Expand Up @@ -417,11 +416,11 @@ private async void OnMessageReceived(object sender, MidiReceivedEventArgs args)
_bufferedMessagesToHandle += 1;
}

if (events.Count() > 0 && args.Timestamp - _lastBatchStartTime > BATCH_TIME_SIZE_MILLISECONDS)
if (events.Count() > 0 && args.Timestamp - _lastMessageBufferStartTime > MESSAGE_BUFFER_TIME_MILLISECONDS)
{
_lastBatchStartTime = args.Timestamp;
_lastMessageBufferStartTime = args.Timestamp;
if (DEBUG) UniLog.Log("* New message batch created: " + args.Timestamp.ToString());
await Task.Delay((int)BATCH_TIME_SIZE_MILLISECONDS);
await Task.Delay((int)MESSAGE_BUFFER_TIME_MILLISECONDS);
FlushMessageBuffer();
}
}
Expand Down

0 comments on commit 453eec6

Please sign in to comment.