Copyright © 2013-2014 MultiMedia Soft

How to manage MIDI devices

Previous pageReturn to chapter overviewNext page

Active MIDI DJ Console, through the MidiDevices property which encapsulates functionalities of the MidiDevices COM object, can separately enumerate MIDI input and output devices installed inside the system through the MidiDevices.CountGet method. After completing the enumeration, information about each device can be obtained through the MidiDevices.InfoGet method.

 

Enumeration of MIDI devices is not automatically updated when a new MIDI device is connected or disconnected from the system but the container application is immediately informed about the change through the MidiDevicesConfigChanged event. After receiving this event, the container application can reset the current configuration through the MidiDevices.Reset method, allowing an updated enumeration when invoking again the MidiDevices.CountGet and MidiDevices.InfoGet methods.

 

The container application can establish communication with MIDI devices connected to the system in three ways:

 

input only, for example when the connected device is a MIDI keyboard sending keystrokes to the application through MIDI events.
output only, for example when the connected device is a synthesizer which needs to convert a MIDI event, like a "note on" or a "note off", into an audible sound.
both input and output, for example when the connected device is a DJ Console sending MIDI events to notify the application about a button being pushed or a slider being moved and receiving MIDI events when there is the need to switch a led on or off.

 

The component can establish communication with a MIDI device through the MidiDevices.Open method; if the MIDI device works as both an input and an output device there is the need to perform two calls to the MidiDevices.Open method: a first one for the input device and a second one for the output device. Each call to the MidiDevices.Open method returns a unique identifier of the opened device and this identifier will be used for accessing the device itself through other existing methods: for example, the unique identifier is used when there is the need to obtain information about the opened device through the MidiDevices.InfoGetByUniqueId method or when there is the need to close the communication with the device through the MidiDevices.Close method; if the MIDI device was opened in both input and output modes, through two separate calls to the MidiDevices.Open method, there will be the need to close the device through two separate calls to the MidiDevices.Close method.

 

You can verify if communication with a certain input or output device is currently opened or closed through the MidiDevices.IsOpened method.

 

After the connection with a MIDI device has been opened, if the related physical MIDI device should be disconnected from the system the container application would be notified through the MidiDevicesOpenRemoved event.

 

 

Management of input devices

 

When an input MIDI device is opened, the component stays tuned in order to catch any incoming MIDI event sent by the external MIDI device: when an incoming MIDI event is detected, the component notifies the container application through the MidiDevicesEventReceived event; this event reports the nature of the MIDI event (short or raw) and a unique identifier that can be used to obtain further detailed information about the MIDI event itself through:

 

the MidiDevices.EventReadShort method for short MIDI events like "note on", "note off", "control change" and many others. If the container application shouldn't be interested in receiving notifications about each different type of incoming short MIDI event, it could set a filter through the MidiDevices.InputEventFilterSet method.
the MidiDevices.EventReadRawInfoGet and MidiDevices.EventReadRaw methods for raw MIDI events (also known as "SysEx"): differently from short MIDI events, which are always formatted and of a known size, raw MIDI events can be of different sizes; for this reason, the MidiDevices.EventReadRawInfoGet allows knowing how many bytes are required to store  data of the event into the container application memory buffer; once the memory buffer has been properly allocated, the application can read raw data into the memory buffer through the MidiDevices.EventReadRaw method.

 

Both types of MIDI events are "time-stamped", meaning that each event is marked with a value expressed in milliseconds reporting the time elapsed from the moment in which the MIDI input device was opened through the call to the MidiDevices.Open method.

 

 

Management of output devices

 

When an output MIDI device is opened, the component can send it MIDI events. As seen for input devices, outgoing MIDI events can be short or raw MIDI events:

 

the MidiDevices.EventWriteShort method allows sending short MIDI events like "note on", "note off", "control change" and others.
the MidiDevices.EventWriteRaw method allows sending raw MIDI events

 

 

Direct communication between input and output MIDI devices

 

When the container application doesn't need a direct processing of incoming MIDI events, for example when an external MIDI keyboard needs to send MIDI notes to an external synthesizer that will produce the corresponding audible sound, it can work as a sort of bridge and establish a direct connection between the input MIDI device (the MIDI keyboard) and the output MIDI device (the synthesizer) by routing all of the incoming MIDI events to the output MIDI device: this communication can be established through the MidiDevices.InputOutputConnect method and interrupted at a later time through the MidiDevices.InputOutputDisconnect method.

 

 

 

Samples of usage of MIDI devices can be found inside the various Visual Basic 6 and Visual C++ projects installed with the component's setup package:

- ProfileEditor

- MidiMonitor

- PianoKeyboard