/// <summary> /// Returns true if the given long message describes a SysEx message. /// </summary> /// <param name="dwParam1">The dwParam1 arg passed to MidiInProc.</param> /// <param name="dwParam2">The dwParam2 arg passed to MidiInProc.</param> public static bool IsSysEx(UIntPtr dwParam1, UIntPtr dwParam2) { IntPtr newPtr = unchecked ((IntPtr)(long)(ulong)dwParam1); //http://stackoverflow.com/questions/3762113/how-can-an-uintptr-object-be-converted-to-intptr-in-c Win32API.MIDIHDR header = (Win32API.MIDIHDR)System.Runtime.InteropServices.Marshal.PtrToStructure(newPtr, typeof(Win32API.MIDIHDR)); return(typeof(Win32API.MIDIHDR) == header.GetType()); }
/// <summary> /// Returns true if the given long message describes a SysEx message. /// </summary> /// <param name="dwParam1">The dwParam1 arg passed to MidiInProc.</param> /// <param name="dwParam2">The dwParam2 arg passed to MidiInProc.</param> public static bool IsSysEx(UIntPtr dwParam1, UIntPtr dwParam2) { IntPtr newPtr = unchecked ((IntPtr)(long)(ulong)dwParam1); //http://stackoverflow.com/questions/3762113/how-can-an-uintptr-object-be-converted-to-intptr-in-c //Win32API.MIDIHDR header = (Win32API.MIDIHDR)Marshal.PtrToStructure(newPtr, typeof(Win32API.MIDIHDR)); // Replaced above line with below, due to deprecation Win32API.MIDIHDR header = Marshal.PtrToStructure <Win32API.MIDIHDR>(newPtr); return(typeof(Win32API.MIDIHDR) == header.GetType()); }
/// <summary> /// Sends a System Exclusive (sysex) message to this MIDI output device. /// </summary> /// <param name="data">The message to send (as byte array)</param> /// <exception cref="DeviceException">The message cannot be sent.</exception> public void SendSysEx(Byte[] data) { lock (this) { //Win32API.MMRESULT result; IntPtr ptr; UInt32 size = (UInt32)Marshal.SizeOf <Win32API.MIDIHDR>(); Win32API.MIDIHDR header = new Win32API.MIDIHDR { lpData = System.Runtime.InteropServices.Marshal.AllocHGlobal(data.Length) }; for (int i = 0; i < data.Length; i++) { System.Runtime.InteropServices.Marshal.WriteByte(header.lpData, i, data[i]); } header.dwBufferLength = data.Length; header.dwBytesRecorded = data.Length; header.dwFlags = 0; try { ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(Marshal.SizeOf <Win32API.MIDIHDR>()); } catch (Exception) { System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); throw; } try { System.Runtime.InteropServices.Marshal.StructureToPtr(header, ptr, false); } catch (Exception) { System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr); throw; } //result = Win32API.midiOutPrepareHeader(handle, ptr, size); //if (result == 0) result = Win32API.midiOutLongMsg(handle, ptr, size); //if (result == 0) result = Win32API.midiOutUnprepareHeader(handle, ptr, size); CheckReturnCode(Win32API.midiOutPrepareHeader(handle, ptr, size)); CheckReturnCode(Win32API.midiOutLongMsg(handle, ptr, size)); CheckReturnCode(Win32API.midiOutUnprepareHeader(handle, ptr, size)); System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr); } }
/// <summary> /// Releases the resources associated with the specified MidiHeader pointer. /// </summary> /// <param name="ptr"> /// The pointer to MIDIHDR buffer. /// </param> private bool DestroyLongMsgBuffer(UIntPtr ptr) { IntPtr newPtr = unchecked ((IntPtr)(long)(ulong)ptr); UInt32 size = (UInt32)Marshal.SizeOf <Win32API.MIDIHDR>(); CheckReturnCode(Win32API.midiInUnprepareHeader(handle, newPtr, size)); Win32API.MIDIHDR header = Marshal.PtrToStructure <Win32API.MIDIHDR>(newPtr); System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); System.Runtime.InteropServices.Marshal.FreeHGlobal(newPtr); LongMsgBuffers.Remove(newPtr); return(true); }
/// <summary> /// Decodes a SysEx long message. /// </summary> /// <param name="dwParam1">The dwParam1 arg passed to MidiInProc.</param> /// <param name="dwParam2">The dwParam2 arg passed to MidiInProc.</param> /// <param name="data">The SysEx data to send.</param> /// <param name="timestamp">Filled in with the timestamp in microseconds since /// midiInStart().</param> public static void DecodeSysEx(UIntPtr dwParam1, UIntPtr dwParam2, out byte[] data, out UInt32 timestamp) { //if (!IsSysEx(dwParam1, dwParam2)) //{ // throw new ArgumentException("Not a SysEx message."); //} IntPtr newPtr = unchecked ((IntPtr)(long)(ulong)dwParam1); //http://stackoverflow.com/questions/3762113/how-can-an-uintptr-object-be-converted-to-intptr-in-c Win32API.MIDIHDR header = (Win32API.MIDIHDR)System.Runtime.InteropServices.Marshal.PtrToStructure(newPtr, typeof(Win32API.MIDIHDR)); data = new byte[header.dwBytesRecorded]; for (int i = 0; i < header.dwBytesRecorded; i++) { //Array.Resize<byte>(ref data, data.Length + 1); //data[data.Length - 1] = System.Runtime.InteropServices.Marshal.ReadByte(header.lpData, i); data[i] = System.Runtime.InteropServices.Marshal.ReadByte(header.lpData, i); } timestamp = (UInt32)dwParam2; }
private IntPtr CreateLongMsgBuffer() { //add a buffer so we can receive SysEx messages IntPtr ptr; UInt32 size = (UInt32)Marshal.SizeOf <Win32API.MIDIHDR>(); Win32API.MIDIHDR header = new Win32API.MIDIHDR { lpData = Marshal.AllocHGlobal(4096), dwBufferLength = 4096, dwFlags = 0 }; try { ptr = Marshal.AllocHGlobal(Marshal.SizeOf <Win32API.MIDIHDR>()); } catch (Exception) { System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); throw; } try { System.Runtime.InteropServices.Marshal.StructureToPtr(header, ptr, false); } catch (Exception) { System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr); throw; } CheckReturnCode(Win32API.midiInPrepareHeader(handle, ptr, size)); CheckReturnCode(Win32API.midiInAddBuffer(handle, ptr, size)); //CheckReturnCode(Win32API.midiInUnprepareHeader(handle, ptr, size)); return(ptr); }
private IntPtr CreateLongMsgBuffer() { //add a buffer so we can receive SysEx messages IntPtr ptr; UInt32 size = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Win32API.MIDIHDR)); Win32API.MIDIHDR header = new Win32API.MIDIHDR(); header.lpData = System.Runtime.InteropServices.Marshal.AllocHGlobal(4096); header.dwBufferLength = 4096; header.dwFlags = 0; try { ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Win32API.MIDIHDR))); } catch (Exception) { System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); throw; } try { System.Runtime.InteropServices.Marshal.StructureToPtr(header, ptr, false); } catch (Exception) { System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr); throw; } CheckReturnCode(Win32API.midiInPrepareHeader(handle, ptr, size)); CheckReturnCode(Win32API.midiInAddBuffer(handle, ptr, size)); //CheckReturnCode(Win32API.midiInUnprepareHeader(handle, ptr, size)); return ptr; }
/// <summary> /// Sends a System Exclusive (sysex) message to this MIDI output device. /// </summary> /// <param name="data">The message to send (as byte array)</param> /// <exception cref="DeviceException">The message cannot be sent.</exception> public void SendSysEx(Byte[] data) { lock (this) { //Win32API.MMRESULT result; IntPtr ptr; UInt32 size = (UInt32)System.Runtime.InteropServices.Marshal.SizeOf(typeof(Win32API.MIDIHDR)); Win32API.MIDIHDR header = new Win32API.MIDIHDR(); header.lpData = System.Runtime.InteropServices.Marshal.AllocHGlobal(data.Length); for (int i = 0; i < data.Length; i++) System.Runtime.InteropServices.Marshal.WriteByte(header.lpData, i, data[i]); header.dwBufferLength = data.Length; header.dwBytesRecorded = data.Length; header.dwFlags = 0; try { ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(typeof(Win32API.MIDIHDR))); } catch (Exception) { System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); throw; } try { System.Runtime.InteropServices.Marshal.StructureToPtr(header, ptr, false); } catch (Exception) { System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr); throw; } //result = Win32API.midiOutPrepareHeader(handle, ptr, size); //if (result == 0) result = Win32API.midiOutLongMsg(handle, ptr, size); //if (result == 0) result = Win32API.midiOutUnprepareHeader(handle, ptr, size); CheckReturnCode(Win32API.midiOutPrepareHeader(handle, ptr, size)); CheckReturnCode(Win32API.midiOutLongMsg(handle, ptr, size)); CheckReturnCode(Win32API.midiOutUnprepareHeader(handle, ptr, size)); System.Runtime.InteropServices.Marshal.FreeHGlobal(header.lpData); System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr); } }