/// <summary> /// Open MidiIn. /// MidiInを開きます。 /// </summary> /// <param name="deviceId">index of GetDeviceNames(). GetDeviceNames()のindex</param> public MidiIn(int deviceId) { messageProc = new MessageThread(false, ThreadPriority.Normal, "MidiInProcThread"); messageProc.MessageHandlers[Win32.MM_MIM_LONGDATA] = delegate(Message m) { Win32.MidiHeader header = Win32.MidiHeader.FromIntPtr(m.LParam); MidiBuffer buf = MidiBuffer.FromMidiHeader(header); int bytesRecorded = (int)header.dwBytesRecorded; if (OnLongMsg != null && bytesRecorded != 0) { byte[] data = buf.Data; if (bytesRecorded != buf.Data.Length) { data = new byte[bytesRecorded]; Array.Copy(buf.Data, data, bytesRecorded); } System.Diagnostics.Trace.WriteLine("Recieved message from DEVICE: " + BitConverter.ToString(data)); try { OnLongMsg(data); } catch { } } if (recording) { Win32.midiInAddBuffer(deviceHandle, m.LParam, Win32.MidiHeader.SizeOfMidiHeader); } else { Win32.midiInUnprepareHeader(deviceHandle, m.LParam, Win32.MidiHeader.SizeOfMidiHeader); buf.Dispose(); Interlocked.Decrement(ref enqueuedBufferCount); } }; messageProc.MessageHandlers[Win32.MM_MIM_DATA] = delegate(Message m) { if (OnShortMsg != null) { OnShortMsg((uint)m.LParam); } }; int mmret = Win32.midiInOpen(out deviceHandle, (uint)deviceId, new IntPtr(messageProc.Win32ThreadID), IntPtr.Zero, Win32.CALLBACK_THREAD); if (mmret != Win32.MMSYSERR_NOERROR) { messageProc.Dispose(); throw new IOException("デバイスが開けませんでした。(" + mmret + ")"); } }
/// <summary> /// Open MidiIn. /// MidiInを開きます。 /// </summary> /// <param name="deviceId">index of GetDeviceNames(). GetDeviceNames()のindex</param> public MidiIn(uint deviceId) { eventHandler = new MessageThread(ThreadPriority.AboveNormal); eventHandler.MessageHandlers[Win32.MM_MIM_LONGDATA] = delegate(Message m) { Win32.MidiHeader header = Win32.MidiHeader.FromIntPtr(m.LParam); MidiBuffer buf = MidiBuffer.FromMidiHeader(header); int bytesRecorded = (int)header.dwBytesRecorded; if (OnLongMsg != null && bytesRecorded != 0) { byte[] data = buf.Data; if (bytesRecorded != buf.Data.Length) { data = new byte[bytesRecorded]; Array.Copy(buf.Data, data, bytesRecorded); } OnLongMsg(data); } if (recording) { Win32.midiInAddBuffer(deviceHandle, m.LParam, Win32.MidiHeader.SizeOfMidiHeader); } else { Win32.midiInUnprepareHeader(deviceHandle, m.LParam, Win32.MidiHeader.SizeOfMidiHeader); buf.Dispose(); Interlocked.Decrement(ref enqueuedBufferCount); } }; eventHandler.MessageHandlers[Win32.MM_MIM_DATA] = delegate(Message m) { if (OnShortMsg != null) { OnShortMsg((uint)m.LParam); } }; int mmret = Win32.midiInOpen(out deviceHandle, deviceId, new IntPtr(eventHandler.Win32ThreadID), IntPtr.Zero, Win32.CALLBACK_THREAD); if (mmret != Win32.MMSYSERR_NOERROR) { eventHandler.Dispose(); throw new Exception("Error on midiInOpen. (ret=" + mmret + ")"); } }
/// <summary> /// Open MidiOut. /// MidiOutを開きます。 /// </summary> /// <param name="deviceId">MidiOut.MidiMapper or index of GetDeviceNames(). MidiOut.MidiMapperか、GetDeviceNames()のindex</param> public MidiOut(int deviceId) { eventHandler = new MessageThread(ThreadPriority.AboveNormal); eventHandler.MessageHandlers[Win32.MM_MOM_DONE] = delegate(Message m) { Win32.MidiHeader hdr = Win32.MidiHeader.FromIntPtr(m.LParam); MidiBuffer buf = MidiBuffer.FromMidiHeader(hdr); Win32.midiOutUnprepareHeader(deviceHandle, buf.pHeader, Win32.MidiHeader.SizeOfMidiHeader); buf.Dispose(); Interlocked.Add(ref enqueuedBufferSize, -buf.BufferLength); if (OnDone != null) { OnDone(); } }; int mmret = Win32.midiOutOpen(out deviceHandle, (uint)deviceId, new IntPtr(eventHandler.Win32ThreadID), IntPtr.Zero, Win32.CALLBACK_THREAD); if (mmret != Win32.MMSYSERR_NOERROR) { eventHandler.Dispose(); throw new Exception("Error on midiOutOpen. (ret=" + mmret + ")"); } }