Skip to content

Commit 80526a2

Browse files
committed
Merge branch 'master' of https://github.com/naudio/NAudio
2 parents 5d0e573 + 1c710c2 commit 80526a2

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

NAudio.Midi/Midi/MidiIn.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace NAudio.Midi
99
public class MidiIn : IDisposable
1010
{
1111
private IntPtr hMidiIn = IntPtr.Zero;
12+
private bool disposeIsRunning = false; // true while the Dispose() method run.
1213
private bool disposed = false;
1314
private MidiInterop.MidiInCallback callback;
1415

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

160-
SysexMessageReceived(this, new MidiInSysexMessageEventArgs(sysexBytes, messageParameter2.ToInt32()));
161+
if (sysexBytes.Length!=0) // do not trigger the sysex event if no data in SYSEX message
162+
SysexMessageReceived(this, new MidiInSysexMessageEventArgs(sysexBytes, messageParameter2.ToInt32()));
163+
161164
// Re-use the buffer - but not if we have no event handler registered as we are closing
162-
MidiInterop.midiInAddBuffer(hMidiIn, messageParameter1, Marshal.SizeOf(typeof(MidiInterop.MIDIHDR)));
165+
// BUT When disposing the (resetting the MidiIn port), LONGDATA midi message are fired with a zero length.
166+
// In that case, buffer should no be ReAdd to avoid an inifinite loop of callback as buffer are reused forever.
167+
if (!disposeIsRunning)
168+
MidiInterop.midiInAddBuffer(hMidiIn, messageParameter1, Marshal.SizeOf(typeof(MidiInterop.MIDIHDR)));
163169
}
164170
break;
165171
case MidiInterop.MidiInMessage.LongError:
@@ -185,17 +191,21 @@ public static MidiInCapabilities DeviceInfo(int midiInDeviceNumber)
185191
}
186192

187193
/// <summary>
188-
/// Closes the MIDI out device
194+
/// Closes the MIDI in device
189195
/// </summary>
190196
/// <param name="disposing">True if called from Dispose</param>
191197
protected virtual void Dispose(bool disposing)
192198
{
193199
if(!this.disposed)
194200
{
201+
disposeIsRunning = true;
195202
//if(disposing) Components.Dispose();
196203

197204
if (SysexBufferHeaders.Length > 0)
198205
{
206+
//// When SysexMessageReceived contains event handlers (!=null) , the 'midiInReset' call generate a infinit loop of CallBack call with LONGDATA message having a zero length.
207+
//SysexMessageReceived = null; // removin all event handler to avoir the infinit loop.
208+
199209
// Reset in order to release any Sysex buffers
200210
// We can't Unprepare and free them until they are flushed out. Neither can we close the handle.
201211
MmException.Try(MidiInterop.midiInReset(hMidiIn), "midiInReset");
@@ -215,6 +225,7 @@ protected virtual void Dispose(bool disposing)
215225
MidiInterop.midiInClose(hMidiIn);
216226
}
217227
disposed = true;
228+
disposeIsRunning = false;
218229
}
219230

220231
/// <summary>

0 commit comments

Comments
 (0)