BASS.NET API for the Un4seen BASS Audio LibraryMidiMIDI_InPrepareHeader Method BASS.NET API for the Un4seen BASS Audio Library
Prepares a buffer for MIDI input.

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

public static MIDIError MIDI_InPrepareHeader(
	IntPtr handle,
	IntPtr headerPtr
)

Parameters

handle
Type: SystemIntPtr
Handle to the MIDI input device.
headerPtr
Type: SystemIntPtr
Pointer to a MIDI_HEADER structure that identifies the buffer to be prepared.

Return Value

Type: MIDIError
Returns 0 if successful or an error code otherwise. For possible error values see MIDIError.
Remarks

Preparing a header that has already been prepared has no effect, and the function returns zero.

After the header has been prepared, do not modify the buffer. To free the buffer, use the MIDI_InUnprepareHeader(IntPtr, IntPtr) function.

Before using this function, you must set the data, bufferLength, and flags members of the MIDI_HEADER structure. The flags member must be set to zero.

For convenience the Prepare(Boolean, IntPtr) might also be used, which calls the method internally and returns the header pointer in the HeaderPtr member.

Examples

VB
Private _midiInProc As MIDIINPROC
Private _midiInHandle As IntPtr
...
Private Sub StartRecording()
  _midiInProc = New MIDIINPROC(AddressOf MyMidiInProc)
  Dim ret As MIDIError = Midi.MIDI_InOpen(_midiInHandle, 1, _midiInProc, 0, MIDIFlags.MIDI_IO_STATUS)
  If ret = MIDIError.MIDI_OK Then
    ' supply the device with 2 buffers
    AddSysExBuffer(_midiInHandle, 1024)
    AddSysExBuffer(_midiInHandle, 1024)
    ret = Midi.MIDI_InStart(_midiInHandle)
  End If
End Sub

' 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

Private Sub MyMidiInProc(handle As IntPtr, msg As MIDIMessage, instance As IntPtr, param1 As IntPtr, param2 As IntPtr)
  ' handle all Midi messages here
  If msg = MIDIMessage.MIM_DATA Then
    ' process the short message...
    Dim p1 As Integer = param1.ToInt32()
    Dim p2 As Integer = param2.ToInt32()
    ...
    Console.WriteLine("Msg={0}" + ControlChars.Cr + ControlChars.Lf + "Param1={1}" + ControlChars.Cr + ControlChars.Lf + "Param2={2}", msg, p1, p2)
  Else
    If msg = MIDIMessage.MIM_LONGDATA Then
      ' process the system-exclusive message...
      Dim header As New MIDI_HEADER(param1)
      If header.IsDone Then
        Dim data As Byte() = header.Data
        ...
        Console.WriteLine(header.ToString())
      End If
      ' must unprepare the processed header
      header.Unprepare(True, handle)
      ' add a new buffer
      ' we must constantly provide new buffers until we finished recording
      AddSysExBuffer(handle, 1024)
    End If
    ...
  End If 
End Sub
private MIDIINPROC _midiInProc;
private IntPtr _midiInHandle;
...
private void StartRecording()
{
  _midiInProc = new MIDIINPROC(MyMidiInProc);
  MIDIError ret = Midi.MIDI_InOpen(ref _midiInHandle, 1, _midiInProc, 0, MIDIFlags.MIDI_IO_STATUS);
  if (ret == MIDIError.MIDI_OK)
  {
    // supply the device with 2 buffers
    AddSysExBuffer(_midiInHandle, 1024);
    AddSysExBuffer(_midiInHandle, 1024);
    ret = Midi.MIDI_InStart(_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);
  }
}

private void MyMidiInProc(IntPtr handle, MIDIMessage msg, IntPtr instance, IntPtr param1, IntPtr param2)
{
  // handle all Midi messages here
  if (msg == MIDIMessage.MIM_DATA)
  {
    // process the short message...
    int p1 = param1.ToInt32();
    int p2 = param2.ToInt32();
    ...
    Console.WriteLine("Msg={0}\r\nParam1={1}\r\nParam2={2}" , msg, p1, p2);
  }
  else if (msg == MIDIMessage.MIM_LONGDATA)
  {
    // process the system-exclusive message...
    MIDI_HEADER header = new MIDI_HEADER(param1);
    if (header.IsDone)
    {
      byte[] data = header.Data;
      ...
      Console.WriteLine(header.ToString());
    }
    // must unprepare the processed header
    header.Unprepare(true, handle);
    // add a new buffer
    // we must constantly provide new buffers until we finished recording
    AddSysExBuffer(handle, 1024);
  }
  ...
}
See Also

Reference