/// <summary>
 /// The input callback for midiOutOpen.
 /// </summary>
 private void InputCallback(Win32API.HMIDIIN hMidiIn, Win32API.MidiInMessage wMsg,
                            UIntPtr dwInstance, UIntPtr dwParam1, UIntPtr dwParam2)
 {
     if (wMsg == Win32API.MidiInMessage.MIM_DATA)
     {
         var msg = MidiMessage.FromShortMessage((uint)dwParam1);
         if (msg != null)
         {
             Receive(msg);
         }
     }
 }
        /// <summary>
        /// The input callback for midiOutOpen.
        /// </summary>
        private void InputCallback(Win32API.HMIDIIN hMidiIn, Win32API.MidiInMessage wMsg,
                                   UIntPtr dwInstance, UIntPtr dwParam1, UIntPtr dwParam2)
        {
            // Package up the callback data and place onto a queue to be processed on another thread.
            // This allows the callback thread to return immediately and avoids the possibility of the callback thread
            // performing any unsafe operations, such as raising an event that might ultimately result in calling back
            // into the midi device on that same thread (which is not thread safe and will likely cause a deadlock).
            MidiInCallbackData cbData = new MidiInCallbackData();

            cbData.hMidiIn    = hMidiIn;
            cbData.wMsg       = wMsg;
            cbData.dwInstance = dwInstance;
            cbData.dwParam1   = dwParam1;
            cbData.dwParam2   = dwParam2;
            _msgQueue.Add(cbData);
        }
Beispiel #3
0
        /// <summary>
        /// The input callback for midiOutOpen.
        /// </summary>
        private void InputCallback(Win32API.HMIDIIN hMidiIn, Win32API.MidiInMessage wMsg,
                                   UIntPtr dwInstance, UIntPtr dwParam1, UIntPtr dwParam2)
        {
            isInsideInputHandler = true;
            try
            {
                if (wMsg == Win32API.MidiInMessage.MIM_DATA)
                {
                    Channel channel;
                    Pitch   pitch;
                    int     velocity;
                    int     value;
                    UInt32  win32Timestamp;
                    if (ShortMsg.IsNoteOn(dwParam1, dwParam2))
                    {
                        if (NoteOn != null)
                        {
                            ShortMsg.DecodeNoteOn(dwParam1, dwParam2, out channel, out pitch,
                                                  out velocity, out win32Timestamp);
                            NoteOn(new NoteOnMessage(this, channel, pitch, velocity,
                                                     clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else if (ShortMsg.IsNoteOff(dwParam1, dwParam2))
                    {
                        if (NoteOff != null)
                        {
                            ShortMsg.DecodeNoteOff(dwParam1, dwParam2, out channel, out pitch,
                                                   out velocity, out win32Timestamp);
                            NoteOff(new NoteOffMessage(this, channel, pitch, velocity,
                                                       clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else if (ShortMsg.IsControlChange(dwParam1, dwParam2))
                    {
                        if (ControlChange != null)
                        {
                            Control control;
                            ShortMsg.DecodeControlChange(dwParam1, dwParam2, out channel,
                                                         out control, out value, out win32Timestamp);
                            ControlChange(new ControlChangeMessage(this, channel, control, value,
                                                                   clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else if (ShortMsg.IsProgramChange(dwParam1, dwParam2))
                    {
                        if (ProgramChange != null)
                        {
                            Instrument instrument;
                            ShortMsg.DecodeProgramChange(dwParam1, dwParam2, out channel,
                                                         out instrument, out win32Timestamp);
                            ProgramChange(new ProgramChangeMessage(this, channel, instrument,
                                                                   clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else if (ShortMsg.IsPitchBend(dwParam1, dwParam2))
                    {
                        if (PitchBend != null)
                        {
                            ShortMsg.DecodePitchBend(dwParam1, dwParam2, out channel,
                                                     out value, out win32Timestamp);
                            PitchBend(new PitchBendMessage(this, channel, value,
                                                           clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else
                    {
                        // Unsupported messages are ignored.
                    }
                }
                #region SysEx
                else if (wMsg == Win32API.MidiInMessage.MIM_LONGDATA)
                {
                    if (LongMsg.IsSysEx(dwParam1, dwParam2))
                    {
                        if (SysEx != null)
                        {
                            byte[] data;
                            uint   win32Timestamp;
                            LongMsg.DecodeSysEx(dwParam1, dwParam2, out data, out win32Timestamp);
                            if (data.Length != 0)
                            {
                                SysEx(new SysExMessage(this, data, clock == null ? win32Timestamp / 1000f : clock.Time));
                            }

                            if (isClosing)
                            {
                                //buffers no longer needed
                                DestroyLongMsgBuffer(dwParam1);
                            }
                            else
                            {
                                //prepare the buffer for the next message
                                RecycleLongMsgBuffer(dwParam1);
                            }
                        }
                    }
                }
                // The rest of these are just for long message testing
                else if (wMsg == Win32API.MidiInMessage.MIM_MOREDATA)
                {
                    SysEx(new SysExMessage(this, new byte[] { 0x13 }, 13));
                }
                else if (wMsg == Win32API.MidiInMessage.MIM_OPEN)
                {
                    //SysEx(new SysExMessage(this, new byte[] { 0x01 }, 1));
                }
                else if (wMsg == Win32API.MidiInMessage.MIM_CLOSE)
                {
                    //SysEx(new SysExMessage(this, new byte[] { 0x02 }, 2));
                }
                else if (wMsg == Win32API.MidiInMessage.MIM_ERROR)
                {
                    SysEx(new SysExMessage(this, new byte[] { 0x03 }, 3));
                }
                else if (wMsg == Win32API.MidiInMessage.MIM_LONGERROR)
                {
                    SysEx(new SysExMessage(this, new byte[] { 0x04 }, 4));
                }
                else
                {
                    SysEx(new SysExMessage(this, new byte[] { 0x05 }, 5));
                }
                #endregion
            }
            finally
            {
                isInsideInputHandler = false;
            }
        }
Beispiel #4
0
        /// <summary>
        /// The input callback for midiOutOpen.
        /// </summary>
        private void InputCallback(Win32API.HMIDIIN hMidiIn, Win32API.MidiInMessage wMsg,
                                   UIntPtr dwInstance, UIntPtr dwParam1, UIntPtr dwParam2)
        {
            isInsideInputHandler = true;
            try
            {
                if (wMsg == Win32API.MidiInMessage.MIM_DATA)
                {
                    Channel    channel;
                    Pitch      pitch;
                    int        velocity;
                    Control    control;
                    int        value;
                    Instrument instrument;
                    UInt32     win32Timestamp;

                    if (RawMessage != null)
                    {
                        RawMessage((uint)dwParam1, (uint )dwParam2);
                    }

                    if (ShortMsg.IsNoteOn(dwParam1, dwParam2))
                    {
                        if (NoteOn != null)
                        {
                            ShortMsg.DecodeNoteOn(dwParam1, dwParam2, out channel, out pitch,
                                                  out velocity, out win32Timestamp);
                            NoteOn(new NoteOnMessage(this, channel, pitch, velocity,
                                                     clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else if (ShortMsg.IsNoteOff(dwParam1, dwParam2))
                    {
                        if (NoteOff != null)
                        {
                            ShortMsg.DecodeNoteOff(dwParam1, dwParam2, out channel, out pitch,
                                                   out velocity, out win32Timestamp);
                            NoteOff(new NoteOffMessage(this, channel, pitch, velocity,
                                                       clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else if (ShortMsg.IsControlChange(dwParam1, dwParam2))
                    {
                        if (ControlChange != null)
                        {
                            ShortMsg.DecodeControlChange(dwParam1, dwParam2, out channel,
                                                         out control, out value, out win32Timestamp);
                            ControlChange(new ControlChangeMessage(this, channel, control, value,
                                                                   clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else if (ShortMsg.IsProgramChange(dwParam1, dwParam2))
                    {
                        if (ProgramChange != null)
                        {
                            ShortMsg.DecodeProgramChange(dwParam1, dwParam2, out channel,
                                                         out instrument, out win32Timestamp);
                            ProgramChange(new ProgramChangeMessage(this, channel, instrument,
                                                                   clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else if (ShortMsg.IsPitchBend(dwParam1, dwParam2))
                    {
                        if (PitchBend != null)
                        {
                            ShortMsg.DecodePitchBend(dwParam1, dwParam2, out channel,
                                                     out value, out win32Timestamp);
                            PitchBend(new PitchBendMessage(this, channel, value,
                                                           clock == null ? win32Timestamp / 1000f : clock.Time));
                        }
                    }
                    else
                    {
                        // Unsupported messages are ignored.
                    }
                }
            }
            finally
            {
                isInsideInputHandler = false;
            }
        }