Esempio n. 1
0
 private bool InitData(byte[] buffer, MIDIHDR header)
 {
     if (buffer == null || header == null)
     {
         return(false);
     }
     try
     {
         header.Reset();
         header.bufferLength = buffer.Length;
         header.data         = Marshal.AllocHGlobal(buffer.Length);
         if (header.data != IntPtr.Zero)
         {
             for (int i = 0; i < buffer.Length; i++)
             {
                 Marshal.WriteByte(header.data, i, buffer[i]);
             }
         }
     }
     catch
     {
         header.Reset();
     }
     return(header.data != IntPtr.Zero);
 }
Esempio n. 2
0
        private IntPtr AllocHeader(MIDIHDR header)
        {
            IntPtr intPtr = IntPtr.Zero;

            try
            {
                intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MIDIHDR)));
            }
            catch (Exception)
            {
                try
                {
                    if (header.data != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(header.data);
                    }
                }
                catch
                {
                }
                header.data = IntPtr.Zero;
            }
            try
            {
                Marshal.StructureToPtr(header, intPtr, true);
            }
            catch (Exception)
            {
                try
                {
                    if (header.data != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(header.data);
                    }
                }
                catch
                {
                }
                header.data = IntPtr.Zero;
                try
                {
                    if (intPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(intPtr);
                    }
                }
                catch
                {
                }
                intPtr = IntPtr.Zero;
            }
            return(intPtr);
        }
Esempio n. 3
0
        private bool PrepareHeader(bool input, IntPtr handle, IntPtr headerPtr)
        {
            MIDIError midierror = MIDIError.MIDI_OK;

            if (headerPtr != IntPtr.Zero)
            {
                if (input)
                {
                    midierror = Midi.MIDI_InPrepareHeader(handle, headerPtr);
                }
                else
                {
                    midierror = Midi.MIDI_OutPrepareHeader(handle, headerPtr);
                }
            }
            if (midierror != MIDIError.MIDI_OK)
            {
                MIDIHDR midihdr = null;
                try
                {
                    midihdr = (MIDIHDR)Marshal.PtrToStructure(headerPtr, typeof(MIDIHDR));
                }
                catch
                {
                }
                if (midihdr != null)
                {
                    try
                    {
                        if (midihdr.data != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(midihdr.data);
                        }
                    }
                    catch
                    {
                    }
                    midihdr.data = IntPtr.Zero;
                    try
                    {
                        if (headerPtr != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(headerPtr);
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return(midierror == MIDIError.MIDI_OK);
        }
Esempio n. 4
0
        private void UnprepareHeader(bool input, IntPtr handle, IntPtr headerPtr)
        {
            if (headerPtr == IntPtr.Zero)
            {
                return;
            }
            MIDIHDR midihdr = null;

            try
            {
                midihdr = (MIDIHDR)Marshal.PtrToStructure(headerPtr, typeof(MIDIHDR));
            }
            catch
            {
            }
            if (midihdr != null)
            {
                try
                {
                    if (input)
                    {
                        Midi.MIDI_InUnprepareHeader(handle, headerPtr);
                    }
                    else
                    {
                        Midi.MIDI_OutUnprepareHeader(handle, headerPtr);
                    }
                }
                catch
                {
                }
                try
                {
                    if (midihdr.data != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(midihdr.data);
                    }
                }
                catch
                {
                }
                try
                {
                    Marshal.FreeHGlobal(headerPtr);
                }
                catch
                {
                }
            }
        }
Esempio n. 5
0
        public bool Prepare(bool input, IntPtr handle)
        {
            if (this._headerPtr != IntPtr.Zero)
            {
                this.Unprepare(input, handle);
            }
            IntPtr  headerPtr = IntPtr.Zero;
            MIDIHDR header    = new MIDIHDR();

            if (this.InitData(this._data, header))
            {
                headerPtr = this.AllocHeader(header);
                if (!this.PrepareHeader(input, handle, headerPtr))
                {
                    headerPtr = IntPtr.Zero;
                }
            }
            this._headerPtr = headerPtr;
            return(this._headerPtr != IntPtr.Zero);
        }
Esempio n. 6
0
        public MIDI_HEADER(IntPtr headerPtr)
        {
            if (headerPtr == IntPtr.Zero)
            {
                return;
            }
            this._headerPtr = headerPtr;
            MIDIHDR midihdr = null;

            try
            {
                midihdr = (MIDIHDR)Marshal.PtrToStructure(headerPtr, typeof(MIDIHDR));
            }
            catch
            {
                midihdr = null;
            }
            if (midihdr != null)
            {
                this._data  = midihdr.GetData();
                this._flags = midihdr.flags;
                this._user  = midihdr.user;
            }
        }