/// <summary>
        /// Releases the resources associated with the specified MidiHeader pointer.
        /// </summary>
        /// <param name="headerPtr">
        /// The MidiHeader pointer.
        /// </param>
        public void Destroy(IntPtr headerPtr)
        {
            MidiHeader header = (MidiHeader)Marshal.PtrToStructure(headerPtr, typeof(MidiHeader));

            Marshal.FreeHGlobal(header.data);
            Marshal.FreeHGlobal(headerPtr);
        }
Example #2
0
        private void HandleNoOp(object state)
        {
            IntPtr     headerPtr = (IntPtr)state;
            MidiHeader header    = (MidiHeader)Marshal.PtrToStructure(headerPtr, typeof(MidiHeader));

            byte[] midiEvent = new byte[SizeOfMidiEvent];

            for (int i = 0; i < midiEvent.Length; i++)
            {
                midiEvent[i] = Marshal.ReadByte(header.data, header.offset + i);
            }

            // If this is a NoOp event.
            if ((midiEvent[EventTypeIndex] & MEVT_NOP) == MEVT_NOP)
            {
                // Clear the event type byte.
                midiEvent[EventTypeIndex] = 0;

                NoOpEventArgs e = new NoOpEventArgs(BitConverter.ToInt32(midiEvent, EventCodeOffset));

                context.Post(new SendOrPostCallback(delegate(object s)
                {
                    OnNoOpOccurred(e);
                }), null);
            }
        }
        private void HandleInvalidSysExMessage(object state)
        {
            lock (lockObject)
            {
                IntPtr headerPtr = (IntPtr)state;

                MidiHeader header = (MidiHeader)Marshal.PtrToStructure(headerPtr, typeof(MidiHeader));

                if (!resetting)
                {
                    byte[] data = new byte[header.bytesRecorded];

                    Marshal.Copy(header.data, data, 0, data.Length);

                    OnInvalidSysExMessageReceived(new InvalidSysExMessageEventArgs(data));

                    int result = AddSysExBuffer();

                    if (result != DeviceException.MMSYSERR_NOERROR)
                    {
                        Exception ex = new InputDeviceException(result);

                        OnError(new ErrorEventArgs(ex));
                    }
                }

                ReleaseBuffer(headerPtr);
            }
        }
        /// <summary>
        /// Builds the pointer to the MidiHeader structure.
        /// </summary>
        public void Build()
        {
            MidiHeader header = new MidiHeader();

            // Initialize the MidiHeader.
            header.bufferLength  = BufferLength;
            header.bytesRecorded = BufferLength;
            header.data          = Marshal.AllocHGlobal(BufferLength);
            header.flags         = 0;

            // Write data to the MidiHeader.
            for (int i = 0; i < BufferLength; i++)
            {
                Marshal.WriteByte(header.data, i, data[i]);
            }

            try
            {
                result = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MidiHeader)));
            }
            catch (Exception)
            {
                Marshal.FreeHGlobal(header.data);

                throw;
            }

            try
            {
                Marshal.StructureToPtr(header, result, false);
            }
            catch (Exception)
            {
                Marshal.FreeHGlobal(header.data);
                Marshal.FreeHGlobal(result);

                throw;
            }

            built = true;
        }
        /// <summary>
        /// Builds the pointer to the MidiHeader structure.
        /// </summary>
        public void Build()
        {
            MidiHeader header = new MidiHeader();

            // Initialize the MidiHeader.
            header.bufferLength = BufferLength;
            header.bytesRecorded = BufferLength;
            header.data = Marshal.AllocHGlobal(BufferLength);
            header.flags = 0;

            // Write data to the MidiHeader.
            for(int i = 0; i < BufferLength; i++)
            {
                Marshal.WriteByte(header.data, i, data[i]);
            }

            try
            {
                result = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MidiHeader)));
            }
            catch(Exception)
            {
                Marshal.FreeHGlobal(header.data);

                throw;
            }

            try
            {
                Marshal.StructureToPtr(header, result, false);
            }
            catch(Exception)
            {
                Marshal.FreeHGlobal(header.data);
                Marshal.FreeHGlobal(result);

                throw;
            }

            built = true;
        }
Example #6
0
        private void HandleSysExMessage(object state)
        {
            lock (lockObject)
            {
                var    param     = (MidiInParams)state;
                IntPtr headerPtr = param.Param1;

                MidiHeader header = (MidiHeader)Marshal.PtrToStructure(headerPtr, typeof(MidiHeader));

                if (!resetting)
                {
                    for (int i = 0; i < header.bytesRecorded; i++)
                    {
                        sysExData.Add(Marshal.ReadByte(header.data, i));
                    }

                    if (sysExData.Count > 1 && sysExData[0] == 0xF0 && sysExData[sysExData.Count - 1] == 0xF7)
                    {
                        SysExMessage message = new SysExMessage(sysExData.ToArray());
                        message.Timestamp = param.Param2.ToInt32();

                        sysExData.Clear();

                        OnMessageReceived(message);
                        OnSysExMessageReceived(new SysExMessageEventArgs(message));
                    }

                    int result = AddSysExBuffer();

                    if (result != DeviceException.MMSYSERR_NOERROR)
                    {
                        Exception ex = new InputDeviceException(result);

                        OnError(new ErrorEventArgs(ex));
                    }
                }

                ReleaseBuffer(headerPtr);
            }
        }