示例#1
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;
            }
        }