Exemplo n.º 1
0
        private void Callback(IntPtr midiInHandle, MidiInterop.MidiInMessage message, IntPtr userData, IntPtr messageParameter1, IntPtr messageParameter2)
        {
            switch (message)
            {
            case MidiInterop.MidiInMessage.Open:
                // message Parameter 1 & 2 are not used
                break;

            case MidiInterop.MidiInMessage.Data:
                // parameter 1 is packed MIDI message
                // parameter 2 is milliseconds since MidiInStart
                if (MessageReceived != null)
                {
                    MessageReceived(this, new MidiInMessageEventArgs(messageParameter1.ToInt32(), messageParameter2.ToInt32()));
                }
                break;

            case MidiInterop.MidiInMessage.Error:
                // parameter 1 is invalid MIDI message
                if (ErrorReceived != null)
                {
                    ErrorReceived(this, new MidiInMessageEventArgs(messageParameter1.ToInt32(), messageParameter2.ToInt32()));
                }
                break;

            case MidiInterop.MidiInMessage.Close:
                // message Parameter 1 & 2 are not used
                break;

            case MidiInterop.MidiInMessage.LongData:
                // parameter 1 is pointer to MIDI header
                // parameter 2 is milliseconds since MidiInStart
                if (SysexMessageReceived != null)
                {
                    MidiInterop.MIDIHDR hdr = (MidiInterop.MIDIHDR)Marshal.PtrToStructure(messageParameter1, typeof(MidiInterop.MIDIHDR));

                    //  Copy the bytes received into an array so that the buffer is immediately available for re-use
                    var sysexBytes = new byte[hdr.dwBytesRecorded];
                    Marshal.Copy(hdr.lpData, sysexBytes, 0, hdr.dwBytesRecorded);

                    SysexMessageReceived(this, new MidiInSysexMessageEventArgs(sysexBytes, messageParameter2.ToInt32()));
                    //  Re-use the buffer - but not if we have no event handler registered as we are closing
                    MidiInterop.midiInAddBuffer(hMidiIn, messageParameter1, Marshal.SizeOf(typeof(MidiInterop.MIDIHDR)));
                }
                break;

            case MidiInterop.MidiInMessage.LongError:
                // parameter 1 is pointer to MIDI header
                // parameter 2 is milliseconds since MidiInStart
                break;

            case MidiInterop.MidiInMessage.MoreData:
                // parameter 1 is packed MIDI message
                // parameter 2 is milliseconds since MidiInStart
                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a number of buffers and make them available to receive incoming Sysex messages
        /// </summary>
        /// <param name="bufferSize">The size of each buffer, ideally large enough to hold a complete message from the device</param>
        /// <param name="numberOfBuffers">The number of buffers needed to handle incoming Midi while busy</param>
        public void CreateSysexBuffers(int bufferSize, int numberOfBuffers)
        {
            SysexBufferHeaders = new IntPtr[numberOfBuffers];

            var hdrSize = Marshal.SizeOf(typeof(MidiInterop.MIDIHDR));

            for (var i = 0; i < numberOfBuffers; i++)
            {
                var hdr = new MidiInterop.MIDIHDR();

                hdr.dwBufferLength  = bufferSize;
                hdr.dwBytesRecorded = 0;
                hdr.lpData          = Marshal.AllocHGlobal(bufferSize);
                hdr.dwFlags         = 0;

                var lpHeader = Marshal.AllocHGlobal(hdrSize);
                Marshal.StructureToPtr(hdr, lpHeader, false);

                MmException.Try(MidiInterop.midiInPrepareHeader(hMidiIn, lpHeader, Marshal.SizeOf(typeof(MidiInterop.MIDIHDR))), "midiInPrepareHeader");
                MmException.Try(MidiInterop.midiInAddBuffer(hMidiIn, lpHeader, Marshal.SizeOf(typeof(MidiInterop.MIDIHDR))), "midiInAddBuffer");
                SysexBufferHeaders[i] = lpHeader;
            }
        }