BASS.NET API for the Un4seen BASS Audio LibraryMidiInputDevice ClassBASS.NET API for the Un4seen BASS Audio Library
This class handles all communication with a Midi input device.
Inheritance Hierarchy

SystemObject
  radio42.Multimedia.MidiMidiInputDevice

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

[SerializableAttribute]
public sealed class MidiInputDevice

The MidiInputDevice type exposes the following members.

Constructors

  NameDescription
Public methodMidiInputDevice(Int32)
Creates a new instance of a MidiInputDevice.
Public methodMidiInputDevice(Int32, MIDIINPROC)
Creates a new instance of a MidiInputDevice.
Top
Properties

  NameDescription
Public propertyAutoPairController
Gets or Sets if Channel Messages (a MidiShortMessage) with a ControlChange should automatically be paired (default is None).
Public propertyCode exampleColtrollerPairMatrix
Sets the controller pair matrix to automatic combine any ShortMessage with a StatusType of ControlChange.
Public propertyDevice
Returns the device handle for this input device.
Public propertyDeviceID
Returns the device id (number) which was used to create this instance.
Public propertyIsDisposed
Returns , if this class is being disposed.
Public propertyIsOpened
Returns if the Midi input device is opened - else .
Public propertyIsStarted
Returns if the Midi input device is started and recording messages - else .
Public propertyLastErrorCode
Returns the last Midi error code (see MIDIError for possible values).
Public propertyCode exampleMessageFilter
Gets or Sets the filter to be applied (messages types which should be suppressed).
Public propertyProcessErrorMessages
Gets or Sets if erroneous Midi messages should also be processed.
Public propertyShortMessage
Gets the current (last) MidiShortMessage which was received from the Device.
Public propertySysExBufferSize
Gets or Sets the maximum buffer size for system-exclusive messages (between 2 and 65536, default is 1024).
Public propertySysExMessage
Gets the current (last) MidiSysExMessage which was received from the Device.
Public propertyUser
User instance data to be used when providing system-exclusive messages.
Top
Methods

  NameDescription
Public methodAddSysExBuffer
Creates and adds a system exclusive buffer to the midi device.
Public methodCode exampleClose
Closes the Midi input device using the DeviceID.
Public methodConnect
Connects this input device to a MIDI thru or output device.
Public methodDisconnect
Disconnects this input device from a MIDI thru or output device.
Public methodDispose
Releases all managed and unmanaged resources used by this class.
Protected methodFinalize
Destructor of the MidiInputDevice for finalization code.
(Overrides ObjectFinalize.)
Public methodStatic memberGetDeviceCount
Returns the total number of available Midi input devices.
Public methodStatic memberGetDeviceDescription
Returns the name of the given input device ID.
Public methodStatic memberGetDeviceDescriptions
Returns all available Midi input device names.
Public methodStatic memberGetInfo
Determines the capabilities of a specified MIDI input device.
Public methodStatic memberCode exampleGetMidiPorts
Returns all available Midi input port IDs.
Public methodIsPairedControllerMessage
Is the given MidiShortMessage a paired controller message?
Public methodCode exampleOpen
Opens the Midi input device using the DeviceID.
Public methodCode exampleStart
Starts recording messages from the Midi input device using the DeviceID.
Public methodCode exampleStop
Stops recording messages from the Midi input device using the DeviceID.
Top
Events

  NameDescription
Public eventMessageReceived
Event handler used to notify that the input device has received a message or the status has changed.
Top
Remarks

Create an instance of this class and specify a DeviceID to be used. You might use the GetDeviceCount and the GetDeviceDescriptions to enumerate all available Midi input devices (use the GetInfo(Int32) method to retrieve the device capabilities).

After you have called the Open method the Device is being used. However, Midi messages are not yet recorded and received. To start recording Midi messages call the Start method.

It is a good idea to subscribe to the MessageReceived event handler before calling Open and Start. The property MessageFilter might be used to suppress sending certain events. The MidiMessageEventArgs contains a MidiMessageEventType which allows you to react to all possible scenarios and to process all Midi messages and events.

Call the Stop method to stop recording and receiving Midi message. Once you're done with the device call Close to close the device and release it.

Examples

VB
Imports radio42.Multimedia.Midi
...
Private _inDevice As MidiInputDevice
...
' open and start a certain Midi input device
Private Sub OpenAndStartDevice(device As Integer)
  _inDevice = New MidiInputDevice(device)
  _inDevice.AutoPairController = True
  _inDevice.MessageFilter = MIDIMessageType.SystemRealtime Or MIDIMessageType.SystemExclusive
  AddHandler _inDevice.MessageReceived, AddressOf InDevice_MessageReceived
  If _inDevice.Open() Then
    If Not _inDevice.Start() Then
      MessageBox.Show(Me, "Midi device could not be started! Error " + _inDevice.LastErrorCode.ToString(), "Midi Error")
    End If
  Else
    MessageBox.Show(Me, "Midi device could not be opened! Error " + _inDevice.LastErrorCode.ToString(), "Midi Error")
  End If
End Sub

' stop and close the Midi input device
Private Sub StopAndCloseDevice()
  If Not (_inDevice Is Nothing) AndAlso _inDevice.IsStarted Then
    _inDevice.Stop()
    _inDevice.Close()
    _inDevice.MessageReceived -= New MidiMessageEventHandler(InDevice_MessageReceived)
  End If
End Sub

Private Sub InDevice_MessageReceived(sender As Object, e As MidiMessageEventArgs)
  If e.IsShortMessage Then
    Console.WriteLine("{0} : {1}", e.ShortMessage.ID, e.ShortMessage.ToString())
  Else
    If e.IsSysExMessage Then
      Console.WriteLine("{0} : {1}", e.SysExMessage.ID, e.SysExMessage.ToString())
    Else
      If e.EventType = MidiMessageEventType.Opened Then
        Console.WriteLine("Midi device {0} opened.", e.DeviceID)
      Else
        If e.EventType = MidiMessageEventType.Closed Then
          Console.WriteLine("Midi device {0} closed.", e.DeviceID)
        Else
          If e.EventType = MidiMessageEventType.Started Then
            Console.WriteLine("Midi device {0} started.", e.DeviceID)
          Else
            If e.EventType = MidiMessageEventType.Stopped Then
              Console.WriteLine("Midi device {0} stopped.", e.DeviceID)
            End If
          End If
        End If
      End If
    End If
  End If
End Sub
using radio42.Multimedia.Midi;
...
private MidiInputDevice _inDevice;
...
// open and start a certain Midi input device
private void OpenAndStartDevice(int device)
{
  _inDevice = new MidiInputDevice(device);
  _inDevice.AutoPairController = true;
  _inDevice.MessageFilter = MIDIMessageType.SystemRealtime | MIDIMessageType.SystemExclusive;
  _inDevice.MessageReceived += new MidiMessageEventHandler(InDevice_MessageReceived);
  if (_inDevice.Open())
  {
    if (!_inDevice.Start())
      MessageBox.Show(this, "Midi device could not be started! Error: " + _inDevice.LastErrorCode.ToString(), "Midi Error");
  }
  else
    MessageBox.Show(this, "Midi device could not be opened! Error: " + _inDevice.LastErrorCode.ToString(), "Midi Error");
}

// stop and close the Midi input device
private void StopAndCloseDevice()
{
  if (_inDevice != null && _inDevice.IsStarted)
  {
    _inDevice.Stop();
    _inDevice.Close();
    _inDevice.MessageReceived -= new MidiMessageEventHandler(InDevice_MessageReceived);
  }
}

private void InDevice_MessageReceived(object sender, MidiMessageEventArgs e)
{
  if (e.IsShortMessage)
  {
    Console.WriteLine("{0} : {1}", e.ShortMessage.ID, e.ShortMessage.ToString());
  }
  else if (e.IsSysExMessage)
  {
    Console.WriteLine("{0} : {1}", e.SysExMessage.ID, e.SysExMessage.ToString());
  }
  else if (e.EventType == MidiMessageEventType.Opened)
  {
    Console.WriteLine("Midi device {0} opened.", e.DeviceID);
  }
  else if (e.EventType == MidiMessageEventType.Closed)
  {
    Console.WriteLine("Midi device {0} closed.", e.DeviceID);
  }
  else if (e.EventType == MidiMessageEventType.Started)
  {
    Console.WriteLine("Midi device {0} started.", e.DeviceID);
  }
  else if (e.EventType == MidiMessageEventType.Stopped)
  {
    Console.WriteLine("Midi device {0} stopped.", e.DeviceID);
  }
}
See Also

Reference