BASS.NET API for the Un4seen BASS Audio Library

MidiMIDI_InUnprepareHeader Method

BASS.NET API for the Un4seen BASS Audio Library
Cleans up the preparation performed by the MIDI_InPrepareHeader(IntPtr, IntPtr) function.

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

public static MIDIError MIDI_InUnprepareHeader(
	IntPtr handle,
	IntPtr headerPtr


Type: SystemIntPtr
Handle to the MIDI input device.
Type: SystemIntPtr
Pointer to a MIDI_HEADER structure identifying the buffer to be cleaned up.

Return Value

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

This function is complementary to MIDI_InPrepareHeader(IntPtr, IntPtr). You must use this function before freeing the buffer. After passing a buffer to the device driver by using the MIDI_InAddBuffer(IntPtr, IntPtr) function, you must wait until the driver is finished with the buffer before using MIDI_InUnprepareHeader(IntPtr, IntPtr). Unpreparing a buffer that has not been prepared has no effect, and the function returns 0.

For convenience the Unprepare(Boolean, IntPtr) might also be used, which calls this method internally. In order to convert an IntPtr (e.g. as given in the MIDIINPROC with the MIM_LONGDATA or MIM_LONGERROR as param1) back to a MIDI_HEADER structure use the appropriate constructor overload of the MIDI_HEADER class taking a headerPtr.


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
public 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...
        // on Win32 system the param1 and param2 values might be converted like this:
        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() );
        header.Unprepare(true, handle);

        // add a new buffer
        // since we should constantly provide new buffers until we finished recording
        AddSysExBuffer(handle, 1024);
See Also