public static int SendLongMessage(int handle, byte[] data) { /*Debug.Write("Midi Out: "); * for(int i=0; i<data.Length; i++) * Debug.Write(data[i].ToString("X")+" "); * Debug.WriteLine("");*/ int result; IntPtr ptr; int size = Marshal.SizeOf(typeof(WinMM.MidiHeader)); WinMM.MidiHeader header = new WinMM.MidiHeader(); header.data = Marshal.AllocHGlobal(data.Length); for (int i = 0; i < data.Length; i++) { Marshal.WriteByte(header.data, i, data[i]); } header.bufferLength = data.Length; header.bytesRecorded = data.Length; header.flags = 0; try { ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WinMM.MidiHeader))); } catch (Exception) { Marshal.FreeHGlobal(header.data); throw; } try { Marshal.StructureToPtr(header, ptr, false); } catch (Exception) { Marshal.FreeHGlobal(header.data); Marshal.FreeHGlobal(ptr); throw; } result = WinMM.MidiOutPrepareHeader(handle, ptr, size); if (result == 0) { result = WinMM.MidiOutLongMessage(handle, ptr, size); } if (result == 0) { result = WinMM.MidiOutUnprepareHeader(handle, ptr, size); } Marshal.FreeHGlobal(header.data); Marshal.FreeHGlobal(ptr); return(result); }
public void CloseMidiIn() { if (midi_in_handle != null) { WinMM.MidiInStop(midi_in_handle); //resetting = true; WinMM.MidiInReset(midi_in_handle); WinMM.MidiInClose(midi_in_handle); midi_in_handle = (IntPtr)0; //resetting = false; DebugMsg(Direction.Out, Status.Closed); } }
public static string MidiOutGetName(int index) { WinMM.MIDIOUTCAPS caps = new WinMM.MIDIOUTCAPS(); int error = WinMM.MidiOutGetDevCaps(index, ref caps, 52); if (error == 0) { return(caps.szPname); } else { return(""); } }
public bool OpenMidiIn(int deviceIndex, string deviceName) { DeviceIndex = deviceIndex; this.DeviceName = deviceName; //this.VFOSelect = 0; callback = new WinMM.MidiInCallback(InCallback); int result = WinMM.MidiInOpen(out midi_in_handle, (uint)DeviceIndex, callback, IntPtr.Zero, WinMM.MidiInOpenFlags.Function | WinMM.MidiInOpenFlags.MidiIoStatus); if (result != 0) { StringBuilder error_text = new StringBuilder(256); WinMM.MidiInGetErrorText(result, error_text, 256); DebugMsg(Direction.In, Status.Error, "MidiInOpen Error: ", error_text.ToString()); return(false); } /* * for (int i = 0; i < 3; i++) * { * result = AddSysExBuffer(midi_in_handle); * if (result != 0) * { * StringBuilder error_text = new StringBuilder(256); * WinMM.MidiInGetErrorText(result, error_text, 256); * DebugMsg(Direction.In, Status.Error, "AddSysExBuffer Error:", error_text.ToString()); * return false; * } * } */ result = WinMM.MidiInStart(midi_in_handle); if (result != 0) { StringBuilder error_text = new StringBuilder(256); WinMM.MidiInGetErrorText(result, error_text, 256); DebugMsg(Direction.In, Status.Error, "MidiInStart Error:", error_text.ToString()); return(false); } // Try and open Midi Out and send a reset. if (OpenMidiOut()) { SendMsg(0x0F, 0x0F, 0, 0); } DebugMsg(Direction.In, Status.Open); return(true); }
public void SendMsg(int Event, int Channel, int Data1, int Data2) { if (midi_out_handle == null) { OpenMidiOut(); } byte status = (byte)(Event << 4); status |= (byte)Channel; byte[] bytes = new byte[4]; bytes[0] = status; bytes[1] = (byte)Data1; bytes[2] = (byte)Data2; bytes[3] = 0; uint msg = BitConverter.ToUInt32(bytes, 0); if (midi_out_handle != null) { int Rc = WinMM.MidiOutShortMessage(midi_out_handle, msg); } }