Exemplo n.º 1
0
        /// <summary>
        /// サンプリング周波数とデバイス ID を指定して新しい MidiInConnector クラスのインスタンスを初期化します。
        /// </summary>
        /// <param name="id">オープンされる MIDI-IN デバイスの ID。</param>
        public MidiIn(int id)
        {
            CheckPlatform();

            midiInProc = MidiProc;
            handle = IntPtr.Zero;
            exclusiveQueue = new Queue<byte[]>();

            if (!Open(id))
                throw new InvalidOperationException();

            headerSize = (uint)Marshal.SizeOf(typeof(NativeMethods.MIDIHDR));
            midiHeader = new NativeMethods.MIDIHDR
            {
                data = Marshal.AllocHGlobal(BufferSize),
                bufferLength = BufferSize
            };
            Marshal.Copy(new byte[BufferSize], 0, midiHeader.data, BufferSize);

            ptrHeader = Marshal.AllocHGlobal((int)headerSize);
            Marshal.StructureToPtr(midiHeader, ptrHeader, true);

            NativeMethods.midiInPrepareHeader(handle, ptrHeader, headerSize);
            NativeMethods.midiInAddBuffer(handle, ptrHeader, headerSize);
        }
Exemplo n.º 2
0
        public MidiBuffer(int bufferSize, IntPtr userData)
        {
            // Allocate unmanaged buffer
            this.buffer = Marshal.AllocHGlobal(bufferSize);

            // Allocate unmanaged buffer descriptor
            this.header             = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NativeMethods.MIDIHDR)));
            this.headerSize         = (uint)Marshal.SizeOf(typeof(NativeMethods.MIDIHDR));
            this.preparedForMidiIn  = false;
            this.preparedForMidiOut = false;

            // Build a buffer descriptor in the managed space
            var managedHeader = new NativeMethods.MIDIHDR();

            managedHeader.lpData          = buffer;
            managedHeader.dwBufferLength  = (uint)bufferSize;
            managedHeader.dwBytesRecorded = 0;
            managedHeader.dwUser          = userData;
            managedHeader.dwFlags         = 0; // Required to be zero before calling the prepare function
            managedHeader.lpNext          = IntPtr.Zero;
            managedHeader.reserved        = IntPtr.Zero;
            managedHeader.dwOffset        = 0;

            // Copy the managed object into the newly created unmanaged buffer
            Marshal.StructureToPtr(managedHeader, header, false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// サンプリング周波数とデバイス ID を指定して新しい MidiInConnector クラスのインスタンスを初期化します。
        /// </summary>
        /// <param name="id">オープンされる MIDI-IN デバイスの ID。</param>
        public MidiIn(int id)
        {
            CheckPlatform();

            midiInProc     = MidiProc;
            handle         = IntPtr.Zero;
            exclusiveQueue = new Queue <byte[]>();

            if (!Open(id))
            {
                throw new InvalidOperationException();
            }

            headerSize = (uint)Marshal.SizeOf(typeof(NativeMethods.MIDIHDR));
            midiHeader = new NativeMethods.MIDIHDR
            {
                data         = Marshal.AllocHGlobal(BufferSize),
                bufferLength = BufferSize
            };
            Marshal.Copy(new byte[BufferSize], 0, midiHeader.data, BufferSize);

            ptrHeader = Marshal.AllocHGlobal((int)headerSize);
            Marshal.StructureToPtr(midiHeader, ptrHeader, true);

            NativeMethods.midiInPrepareHeader(handle, ptrHeader, headerSize);
            NativeMethods.midiInAddBuffer(handle, ptrHeader, headerSize);
        }
Exemplo n.º 4
0
        private void MidiProc(IntPtr hMidiIn, int wMsg, IntPtr dwInstance, int dwParam1, int dwParam2)
        {
            if (wMsg == NativeMethods.MIM_DATA)
            {
                if (IsDisposed || !IsPlaying)
                    return;

                if (ReceivedMidiEvent == null)
                    return;

                var @event = new MidiEvent((EventType)(dwParam1 & 0xf0),
                    dwParam1 & 0x0f,
                    dwParam1 >> 8 & 0xff,
                    dwParam1 >> 16 & 0xff);
                ReceivedMidiEvent(this, new ReceivedMidiEventEventArgs(@event, this));
            }
            else if (wMsg == NativeMethods.MIM_LONGDATA)
            {
                if (IsDisposed || !IsPlaying)
                    return;

                midiHeader = (NativeMethods.MIDIHDR)Marshal.PtrToStructure(ptrHeader,
                    typeof(NativeMethods.MIDIHDR));

                var buffer = new byte[BufferSize];
                Marshal.Copy(midiHeader.data, buffer, 0, BufferSize);
                var length = Array.IndexOf<byte>(buffer, 0xf7, 0, BufferSize);

                if (length == -1)
                {
                    exclusiveQueue.Enqueue(buffer);
                }
                else
                {
                    if (ReceivedExclusiveMessage != null)
                    {
                        var data = exclusiveQueue.SelectMany(a => a).Concat(buffer.Take(length)).Skip(1);
                        ReceivedExclusiveMessage(this, new ReceivedExclusiveMessageEventArgs(data.ToArray(), this));
                    }

                    exclusiveQueue.Clear();
                }

                if (!isClosing)
                    NativeMethods.midiInAddBuffer(handle, ptrHeader, headerSize);
            }
            else if (wMsg == NativeMethods.MIM_OPEN)
            {
                Opened?.Invoke(this, new EventArgs());
            }
            else if (wMsg == NativeMethods.MIM_CLOSE)
            {
                Closed?.Invoke(this, new EventArgs());
            }
        }
Exemplo n.º 5
0
        private void MidiProc(IntPtr hMidiIn, int wMsg, IntPtr dwInstance, int dwParam1, int dwParam2)
        {
            if (wMsg == NativeMethods.MIM_DATA)
            {
                if (IsDisposed || !IsPlaying)
                {
                    return;
                }

                if (ReceivedMidiEvent == null)
                {
                    return;
                }

                var @event = new MidiEvent((EventType)(dwParam1 & 0xf0),
                                           dwParam1 & 0x0f,
                                           dwParam1 >> 8 & 0xff,
                                           dwParam1 >> 16 & 0xff);
                ReceivedMidiEvent(this, new ReceivedMidiEventEventArgs(@event, this));
            }
            else if (wMsg == NativeMethods.MIM_LONGDATA)
            {
                if (IsDisposed || !IsPlaying)
                {
                    return;
                }

                midiHeader = (NativeMethods.MIDIHDR)Marshal.PtrToStructure(ptrHeader,
                                                                           typeof(NativeMethods.MIDIHDR));

                var buffer = new byte[BufferSize];
                Marshal.Copy(midiHeader.data, buffer, 0, BufferSize);
                var length = Array.IndexOf <byte>(buffer, 0xf7, 0, BufferSize);

                if (length == -1)
                {
                    exclusiveQueue.Enqueue(buffer);
                }
                else
                {
                    if (ReceivedExclusiveMessage != null)
                    {
                        var data = exclusiveQueue.SelectMany(a => a).Concat(buffer.Take(length)).Skip(1);
                        ReceivedExclusiveMessage(this, new ReceivedExclusiveMessageEventArgs(data.ToArray(), this));
                    }

                    exclusiveQueue.Clear();
                }

                if (!isClosing)
                {
                    NativeMethods.midiInAddBuffer(handle, ptrHeader, headerSize);
                }
            }
            else if (wMsg == NativeMethods.MIM_OPEN)
            {
                Opened?.Invoke(this, new EventArgs());
            }
            else if (wMsg == NativeMethods.MIM_CLOSE)
            {
                Closed?.Invoke(this, new EventArgs());
            }
        }
Exemplo n.º 6
0
        public void SendLongMessage( byte[] msg )
        {
            uint hdrSize = (uint)Marshal.SizeOf(typeof(NativeMethods.MIDIHDR));
            var midiHdr = new NativeMethods.MIDIHDR();
            byte[] hdrReserved = new byte[8];
            GCHandle alchandle = GCHandle.Alloc(msg, GCHandleType.Pinned);
            GCHandle revHandle = GCHandle.Alloc(hdrReserved, GCHandleType.Pinned);

            midiHdr.lpData = alchandle.AddrOfPinnedObject();
            midiHdr.dwBufferLength = (uint)msg.Length;
            midiHdr.dwFlags = 0;

            NativeMethods.midiOutPrepareHeader(handle, ref midiHdr, hdrSize);

            while ((midiHdr.dwFlags & NativeMethods.MidiHdrFlag.MHDR_PREPARED) != NativeMethods.MidiHdrFlag.MHDR_PREPARED) {
                Thread.Sleep(1);
            }
            NativeMethods.midiOutLongMsg(handle, ref midiHdr, hdrSize);
            while ((midiHdr.dwFlags & NativeMethods.MidiHdrFlag.MHDR_DONE) != NativeMethods.MidiHdrFlag.MHDR_DONE) {
                Thread.Sleep(1);
            }
            NativeMethods.midiOutUnprepareHeader(handle, ref midiHdr, hdrSize);

            alchandle.Free();
            revHandle.Free();
        }