Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/naudio/NAudio
Browse files Browse the repository at this point in the history
  • Loading branch information
markheath committed Jul 28, 2023
2 parents 5d0e573 + 1c710c2 commit 80526a2
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions NAudio.Midi/Midi/MidiIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace NAudio.Midi
public class MidiIn : IDisposable
{
private IntPtr hMidiIn = IntPtr.Zero;
private bool disposeIsRunning = false; // true while the Dispose() method run.
private bool disposed = false;
private MidiInterop.MidiInCallback callback;

Expand Down Expand Up @@ -157,9 +158,14 @@ private void Callback(IntPtr midiInHandle, MidiInterop.MidiInMessage message, In
var sysexBytes = new byte[hdr.dwBytesRecorded];
Marshal.Copy(hdr.lpData, sysexBytes, 0, hdr.dwBytesRecorded);

SysexMessageReceived(this, new MidiInSysexMessageEventArgs(sysexBytes, messageParameter2.ToInt32()));
if (sysexBytes.Length!=0) // do not trigger the sysex event if no data in SYSEX message
SysexMessageReceived(this, new MidiInSysexMessageEventArgs(sysexBytes, messageParameter2.ToInt32()));

// Re-use the buffer - but not if we have no event handler registered as we are closing
MidiInterop.midiInAddBuffer(hMidiIn, messageParameter1, Marshal.SizeOf(typeof(MidiInterop.MIDIHDR)));
// BUT When disposing the (resetting the MidiIn port), LONGDATA midi message are fired with a zero length.
// In that case, buffer should no be ReAdd to avoid an inifinite loop of callback as buffer are reused forever.
if (!disposeIsRunning)
MidiInterop.midiInAddBuffer(hMidiIn, messageParameter1, Marshal.SizeOf(typeof(MidiInterop.MIDIHDR)));
}
break;
case MidiInterop.MidiInMessage.LongError:
Expand All @@ -185,17 +191,21 @@ public static MidiInCapabilities DeviceInfo(int midiInDeviceNumber)
}

/// <summary>
/// Closes the MIDI out device
/// Closes the MIDI in device
/// </summary>
/// <param name="disposing">True if called from Dispose</param>
protected virtual void Dispose(bool disposing)
{
if(!this.disposed)
{
disposeIsRunning = true;
//if(disposing) Components.Dispose();

if (SysexBufferHeaders.Length > 0)
{
//// When SysexMessageReceived contains event handlers (!=null) , the 'midiInReset' call generate a infinit loop of CallBack call with LONGDATA message having a zero length.
//SysexMessageReceived = null; // removin all event handler to avoir the infinit loop.

// Reset in order to release any Sysex buffers
// We can't Unprepare and free them until they are flushed out. Neither can we close the handle.
MmException.Try(MidiInterop.midiInReset(hMidiIn), "midiInReset");
Expand All @@ -215,6 +225,7 @@ protected virtual void Dispose(bool disposing)
MidiInterop.midiInClose(hMidiIn);
}
disposed = true;
disposeIsRunning = false;
}

/// <summary>
Expand Down

0 comments on commit 80526a2

Please sign in to comment.