static MidiDeviceManager()
 {
     PortMidiMarshal.Pm_Initialize();
     AppDomain.CurrentDomain.DomainUnload += delegate(object o, EventArgs e)
     {
         PortMidiMarshal.Pm_Terminate();
     };
 }
        public void WriteSysEx(PmTimestamp when, byte[] sysex)
        {
            var ret = PortMidiMarshal.Pm_WriteSysEx(stream, when, sysex);

            if (ret != PmError.NoError)
            {
                throw new MidiException(ret, String.Format("Failed to write sysex message : {0}", PortMidiMarshal.Pm_GetErrorText((PmError)ret)));
            }
        }
        public void Write(PmTimestamp when, MidiMessage msg)
        {
            var ret = PortMidiMarshal.Pm_WriteShort(stream, when, msg);

            if (ret != PmError.NoError)
            {
                throw new MidiException(ret, String.Format("Failed to write message {0} : {1}", msg.Value, PortMidiMarshal.Pm_GetErrorText((PmError)ret)));
            }
        }
        public static MidiOutput OpenOutput(PmDeviceID outputDevice)
        {
            PortMidiStream stream;
            var            e = PortMidiMarshal.Pm_OpenOutput(out stream, outputDevice, IntPtr.Zero, 0, null, IntPtr.Zero, 0);

            if (e != PmError.NoError)
            {
                throw new MidiException(e, String.Format("Failed to open MIDI output device {0}", e));
            }
            return(new MidiOutput(stream, outputDevice, 0));
        }
        public static MidiInput OpenInput(PmDeviceID inputDevice, int bufferSize)
        {
            PortMidiStream stream;
            var            e = PortMidiMarshal.Pm_OpenInput(out stream, inputDevice, IntPtr.Zero, bufferSize, null, IntPtr.Zero);

            if (e != PmError.NoError)
            {
                throw new MidiException(e, String.Format("Failed to open MIDI input device {0}", e));
            }
            return(new MidiInput(stream, inputDevice));
        }
        public void Write(MidiEvent[] buffer, int index, int length)
        {
            var gch = GCHandle.Alloc(buffer);

            try
            {
                var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, index);
                var ret = PortMidiMarshal.Pm_Write(stream, ptr, length);
                if (ret != PmError.NoError)
                {
                    throw new MidiException(ret, String.Format("Failed to write messages : {0}", PortMidiMarshal.Pm_GetErrorText((PmError)ret)));
                }
            }
            finally
            {
                gch.Free();
            }
        }
        public int Read(byte[] buffer, int index, int length)
        {
            var gch = GCHandle.Alloc(buffer);

            try
            {
                var ptr  = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, index);
                int size = PortMidiMarshal.Pm_Read(stream, ptr, length);
                if (size < 0)
                {
                    throw new MidiException((MidiErrorType)size, PortMidiMarshal.Pm_GetErrorText((PmError)size));
                }
                return(size * 4);
            }
            catch
            {
                return(0);
            }
            finally
            {
                gch.Free();
            }
        }
 public static MidiDeviceInfo GetDeviceInfo(PmDeviceID id)
 {
     return(new MidiDeviceInfo(id, PortMidiMarshal.Pm_GetDeviceInfo(id)));
 }
 public void SetChannelMask(int mask)
 {
     PortMidiMarshal.Pm_SetChannelMask(stream, mask);
 }
 public void SetFilter(MidiFilter filters)
 {
     PortMidiMarshal.Pm_SetFilter(stream, filters);
 }
 public void Dispose()
 {
     PortMidiMarshal.Pm_Close(stream);
 }
 public void Abort()
 {
     PortMidiMarshal.Pm_Abort(stream);
 }