BASS.NET API for the Un4seen BASS Audio Library

MIDIINPROC Delegate

BASS.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.17.5
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

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