BASS.NET API for the Un4seen BASS Audio Library

MidiMIDI_InAddBuffer Method

BASS.NET API for the Un4seen BASS Audio Library
Sends an input buffer to a specified opened MIDI input device. This function is used for system-exclusive messages.

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

public static MIDIError MIDI_InAddBuffer(
	IntPtr handle,
	IntPtr headerPtr


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

Return Value

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

When the buffer is filled, it is sent back to the application (see MIM_LONGDATA).

The buffer must be prepared by using the MIDI_InPrepareHeader(IntPtr, IntPtr) function before it is passed to this function.

For convenience the Prepare(Boolean, IntPtr) might also be used, which calls MIDI_InPrepareHeader(IntPtr, IntPtr) internally and provides the prepared header pointer in it's HeaderPtr member. 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