Inheritance: IDisposable, IMessageFilter
Exemplo n.º 1
0
        /// <summary>
        /// Open MidiIn.
        /// MidiIn��J���܂��B
        /// </summary>
        /// <param name="deviceId">index of GetDeviceNames(). GetDeviceNames()��index</param>
        public MidiIn(uint deviceId)
        {
            eventHandler = new MessageThread(ThreadPriority.AboveNormal);
            eventHandler.MessageHandlers[Win32.MM_MIM_LONGDATA] = delegate(Message m)
            {
                Win32.MidiHeader header = Win32.MidiHeader.FromIntPtr(m.LParam);
                MidiBuffer buf = MidiBuffer.FromMidiHeader(header);
                int bytesRecorded = (int)header.dwBytesRecorded;
                if (OnLongMsg != null && bytesRecorded != 0)
                {
                    byte[] data = buf.Data;
                    if (bytesRecorded != buf.Data.Length)
                    {
                        data = new byte[bytesRecorded];
                        Array.Copy(buf.Data, data, bytesRecorded);
                    }
                    OnLongMsg(data);
                }

                if (recording)
                {
                    Win32.midiInAddBuffer(deviceHandle, m.LParam, Win32.MidiHeader.SizeOfMidiHeader);
                }
                else
                {
                    Win32.midiInUnprepareHeader(deviceHandle, m.LParam, Win32.MidiHeader.SizeOfMidiHeader);
                    buf.Dispose();
                    Interlocked.Decrement(ref enqueuedBufferCount);
                }
            };

            eventHandler.MessageHandlers[Win32.MM_MIM_DATA] = delegate(Message m)
            {
                if (OnShortMsg != null)
                    OnShortMsg((uint)m.LParam);
            };

            int mmret = Win32.midiInOpen(out deviceHandle, deviceId, new IntPtr(eventHandler.Win32ThreadID), IntPtr.Zero, Win32.CALLBACK_THREAD);

            if (mmret != Win32.MMSYSERR_NOERROR)
            {
                eventHandler.Dispose();
                throw new Exception("Error on midiInOpen. (ret=" + mmret + ")");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Open MidiOut.
        /// MidiOut��J���܂��B
        /// </summary>
        /// <param name="deviceId">MidiOut.MidiMapper or index of GetDeviceNames(). MidiOut.MidiMapper���AGetDeviceNames()��index</param>
        public MidiOut(int deviceId)
        {
            eventHandler = new MessageThread(ThreadPriority.AboveNormal);
            eventHandler.MessageHandlers[Win32.MM_MOM_DONE] = delegate(Message m)
            {
                Win32.MidiHeader hdr = Win32.MidiHeader.FromIntPtr(m.LParam);
                MidiBuffer buf = MidiBuffer.FromMidiHeader(hdr);
                Win32.midiOutUnprepareHeader(deviceHandle, buf.pHeader, Win32.MidiHeader.SizeOfMidiHeader);
                buf.Dispose();
                Interlocked.Add(ref enqueuedBufferSize, -buf.BufferLength);
                if (OnDone != null)
                    OnDone();
            };

            int mmret = Win32.midiOutOpen(out deviceHandle, (uint)deviceId, new IntPtr(eventHandler.Win32ThreadID), IntPtr.Zero, Win32.CALLBACK_THREAD);
            if (mmret != Win32.MMSYSERR_NOERROR)
            {
                eventHandler.Dispose();
                throw new Exception("Error on midiOutOpen. (ret=" + mmret + ")");
            }
        }