BASS.NET API for the Un4seen BASS Audio LibraryMIDIINPROC DelegateBASS.NET API for the Un4seen BASS Audio Library
Represents the general callback delegate for handling Midi Input messages.

Namespace: radio42.Multimedia.Midi
Assembly: Bass.Net (in Bass.Net.dll) Version: 2.4.12.0
Syntax

public delegate void MIDIINPROC(
	IntPtr handle,
	MIDIMessage msg,
	IntPtr instance,
	IntPtr param1,
	IntPtr param2
)

Parameters

handle
Type: SystemIntPtr
Handle to the MIDI input device.
msg
Type: radio42.Multimedia.MidiMIDIMessage
MIDI input message (one of the MIM_xxx values, see MIDIMessage).
instance
Type: SystemIntPtr
Instance data supplied with the MIDI_InOpen(IntPtr, Int32, MIDIINPROC, IntPtr, MIDIFlags) method.
param1
Type: SystemIntPtr
Message parameter 1.
param2
Type: SystemIntPtr
Message parameter 2.
Remarks

NOTE: instance, param1 and param2 are implemented here as IntPtr values for maximum Win32 and Win64 compatibility. If the parameters are actually numerial values you might use the ToInt32() resp. ToInt64() members to convert to these values.

The meaning of the param1 and param2 parameters is specific to the message type. For more info see MIM_DATA. The MIM_DATA or MIM_ERROR message might be used with the MidiShortMessage class to construct and unpack the message into it's components. The MIM_LONGDATA or MIM_LONGERROR message might be used with the MidiSysExMessage class to construct and unpack the message into it's components.

If param1 contains a pointer to a MIDI_HEADER structure you might use the MIDI_HEADER constructor overload taking a headerPtr in order to create an instance of a MIDI_HEADER in such case.

Applications should not call any system-defined functions from inside a callback function, except for MIDI_OutLongMsg(IntPtr, IntPtr), MIDI_OutShortMsg(IntPtr, Int32).

Also note, that you should not make any GUI control changes within this procedure. If needed use an Invoke to make sure to execute GUI changes in the related GUI thread.

Examples

VB
Private _midiProc As MIDIINPROC
Private _midiInHandle As IntPtr
...
' Open the Midi device #2
_midiProc = New MIDIINPROC(AddressOf MyMidiProc)
Dim ret As MIDIError = Midi.MIDI_InOpen(_midiInHandle, 2, _midiProc, IntPtr.Zero, MIDIFlags.MIDI_IO_STATUS)
If ret = MIDIError.MIDI_OK Then
  ' supply the device with 2 buffers
  AddSysExBuffer(_midiInHandle, 1024)
  AddSysExBuffer(_midiInHandle, 1024)
  ' Start the device
  ret = Midi.MIDI_InStart(_midiInHandle)
End If
...
' Stop the device
Midi.MIDI_InReset(_midiInHandle)
' when not needed anymore...close the device
Midi.MIDI_InClose(_midiInHandle)
...
' prepare receiving system-exclusive messages
Private Sub AddSysExBuffer(handle As IntPtr, size As Integer)
  ' prepare a empty midi header
  Dim header As New MIDI_HEADER(size)
  header.Prepare(True, handle)
  ' If the header was perpared successfully.
  If header.HeaderPtr <> IntPtr.Zero Then
    ' Add the buffer to the InputDevice.
    Midi.MIDI_InAddBuffer(handle, header.HeaderPtr)
  End If
End Sub

Public Sub MyMidiProc(handle As IntPtr, msg As MIDIMessage, instance As IntPtr, param1 As IntPtr, param2 As IntPtr)
  ' handle all Midi messages here
  If msg = MIDIMessage.MIM_OPEN Then
    ' nothing to do
  Else
    If msg = MIDIMessage.MIM_CLOSE Then
      ' handle is from now on invalid
    Else
      If msg = MIDIMessage.MIM_DATA Then
        ' process the message...
        Dim p1 As Integer = param1.ToInt32()
        Dim p2 As Integer = param2.ToInt32()
        ...
        ' or
        Dim shortMsg As New MidiShortMessage(param1, param2)
        ...
      Else
        If msg = MIDIMessage.MIM_MOREDATA Then
          ' we are not fast enough in this callback to keep up
          ' the input device is sending messages to fast
          ...
        Else
          If msg = MIDIMessage.MIM_LONGDATA Then
            ' process the message...
            ' param1 will contain the pointer to the MIDI_HEADER
            Dim header As MIDI_HEADER = MIDI_HEADER(param1)
            Dim data As Byte() = header.Data
            ...
            header.Unprepare(True, handle)
            ' add a new buffer
            ' since we should constantly provide new buffers until we finished recording
            AddSysExBuffer(handle, 1024)
          Else
            If msg = MIDIMessage.MIM_ERROR Then
              ' process the invalid message...
              Dim errorMsg As New MidiShortMessage(param1, param2)
              ...
            Else
              If msg = MIDIMessage.MIM_LONGERROR Then
                ' process the invalid message...
                ' param1 will contain the pointer to the MIDI_HEADER
                Dim errorSysExMsg As New MidiSysExMessage(True, handle, param1)
                ...
                ' add a new buffer
                ' since we should constantly provide new buffers until we finished recording
                AddSysExBuffer(handle, 1024)
              End If
            End If
          End If
        End If
      End If
    End If
  End If
End Sub
private MIDIINPROC _midiProc;
private IntPtr _midiInHandle;
...
// Open the Midi device #2
_midiProc = new MIDIINPROC(MyMidiProc);
MIDIError ret = Midi.MIDI_InOpen(ref _midiInHandle, 2, _midiProc, IntPtr.Zero, MIDIFlags.MIDI_IO_STATUS);
if (ret == MIDIError.MIDI_OK)
{
  // supply the device with 2 buffers
  AddSysExBuffer(_midiInHandle, 1024);
  AddSysExBuffer(_midiInHandle, 1024);
  // Start the device
  ret = Midi.MIDI_InStart(_midiInHandle);
}
...
// Stop the device
Midi.MIDI_InReset(_midiInHandle);
// when not needed anymore...close the device
Midi.MIDI_InClose(_midiInHandle);
...
// prepare receiving system-exclusive messages
private void AddSysExBuffer(IntPtr handle, int size)
{
  // prepare a empty midi header
  MIDI_HEADER header = new MIDI_HEADER(size);
  header.Prepare(true, handle);
  // If the header was perpared successfully.
  if (header.HeaderPtr != IntPtr.Zero)
  {
    // Add the buffer to the InputDevice.
    Midi.MIDI_InAddBuffer(handle, header.HeaderPtr);
  }
}

public void MyMidiProc(IntPtr handle, MIDIMessage msg, IntPtr instance, IntPtr param1, IntPtr param2)
{
  // handle all Midi messages here
  if (msg == MIDIMessage.MIM_OPEN)
  {
    // nothing to do
  }
  else if (msg == MIDIMessage.MIM_CLOSE)
  {
    // handle is from now on invalid
  }
  else if (msg == MIDIMessage.MIM_DATA)
  {
    // process the message...
    int p1 = param1.ToInt32();
    int p2 = param2.ToInt32();
    ...
    // or
    MidiShortMessage shortMsg = new MidiShortMessage(param1, param2);
    ...
  }
  else if (msg == MIDIMessage.MIM_MOREDATA)
  {
    // we are not fast enough in this callback to keep up
    // the input device is sending messages to fast
    ...
  }
  else if (msg == MIDIMessage.MIM_LONGDATA)
  {
    // process the message...
    // param1 will contain the pointer to the MIDI_HEADER
    MIDI_HEADER header = MIDI_HEADER(param1);
    byte[] data = header.Data;
    ...
    header.Unprepare(true, handle);
    // add a new buffer
    // since we should constantly provide new buffers until we finished recording
    AddSysExBuffer(handle, 1024);
  }
  else if (msg == MIDIMessage.MIM_ERROR)
  {
    // process the invalid message...
    MidiShortMessage errorMsg = new MidiShortMessage(param1, param2);
    ...
  }
  else if (msg == MIDIMessage.MIM_LONGERROR)
  {
    // process the invalid message...
    // param1 will contain the pointer to the MIDI_HEADER
    MidiSysExMessage errorSysExMsg = new MidiSysExMessage(true, handle, param1);
    ...
    // add a new buffer
    // since we should constantly provide new buffers until we finished recording
    AddSysExBuffer(handle, 1024);
  }
}
See Also

Reference