Musical Instrument Digital Interface

About MIDI

The Musical Instrument Digital Interface (MIDI) was originally developed to interconnect electronic musical instruments. This interface is very flexible and has many uses in many applications far beyond musical instruments. Its simple unidirectional messaging protocol supports many different kinds of messaging.

Windows, Macintosh, and Linux platforms all have native support for MIDI, so software on any of these platforms can send and receive MIDI messages. See for more information about MIDI.

MIDI Control Surfaces

One kind of MIDI message is the Control Change message, used to communicate changes in controls, such as knobs, sliders, and buttons. A MIDI Control Surface is a device with controls that sends MIDI Control Change messages when you turn a knob, move a slider, or push a button on a MIDI control surface. This Control Change message indicates which control changed and what its new position is. MIDI control surfaces are quite generic because the interpretation of the Control Change message is entirely up to the message recipient. Even though some control surfaces are tailored for particular applications, the messages they send can be used to control anything.

Hardware MIDI control surfaces are widely available in a range of configurations and prices. MIDI control apps can turn a smartphone or tablet into a virtual MIDI control surface. For custom applications, MIDI control surfaces are not difficult to build using, for example, Arduino boards.

Because the MIDI messaging protocol is unidirectional, determining a particular control's position requires that the receiver listen for Control Change messages that control sends. The protocol does not support querying the control for its position.

The simplest MIDI control surfaces are unidirectional; they end MIDI Control Change messages, but do not receive them. More sophisticated control surfaces are bidirectional: They can both send and receive Control Change messages. These control surfaces have knobs or sliders that can be operated automatically. For example, a control surface can have sliders or knobs that are motorized. When it receives a Control Change message, the appropriate control is moved to the position in the message. You can use this feature to synchronize software GUI with MIDI control surface. For example, moving a slider on the MIDI control surface sends a Control Change message to a GUI slider, which then moves to match the control surface. Similarly, moving the GUI slider sends a Control Change message to the MIDI control surface, which then moves to match the GUI slider.

Using MIDI Control Surfaces with MATLAB and Simulink

The DSP System Toolbox™ product enables you to use MIDI control surfaces to control MATLAB® programs and Simulink® models by providing the capability to listen to Control Change messages. The toolbox also provides a limited capability to send Control Change messages to support synchronizing MIDI controls. The DSP System Toolbox interface to MIDI control surfaces includes five functions and one block:

Initial Setup

Your MIDI control surface should be connected to your computer, and turned on, before starting MATLAB. Instructions for connecting your MIDI device to your computer vary from device to device. See the instructions that came with your particular device. If you start MATLAB before connecting your device, MATLAB may not recognize your device when you connect it. To correct the problem, restart MATLAB with the device already connected.

Next, set the MATLAB preference, specifying the name of the default MIDI device. Use midiid to determine the name of the device, and then use setpref to set the preference:

>> [control, device] = midiid
Move the control you wish to identify; type ^C to abort.
Waiting for control message... done
control =
device =
>> setpref('midi', 'DefaultDevice', device)

This preference persists across MATLAB sessions, so you only have to set it once, unless you want to change devices.

If you do not set this preference, MATLAB and the host operating system choose a device for you. However, such autoselection can cause unpredictable results because many computers have "virtual" (software) MIDI devices installed that you may not be aware of. For predictable behavior, you should set the preference.

You can always override this default and explicitly specify a device name. Thus, you can use multiple MIDI devices simultaneously.

Identifying Controls

Before you can connect a MIDI control with MATLAB or Simulink, you must know the identifiers for that particular control:

  • Control number

  • Device name

The control number is a fixed integer assigned by the device manufacturer. Some devices may change the assigned number based on various modes, or you can reprogram the number. The device name is determined by the manufacturer and the host operating system. You use midiid to determine both.

You do not usually have to use midiid repeatedly. If you use a single device in most cases, then specify that device as the default hardware. the You can save the control numbers in a function, a .mat file, or whatever form you find convenient. This example shows a function returning a struct with all the control numbers for a Behringer BCF2000:

function ctls = BCF2000
    % BCF2000 return MIDI control number assignments
    % for Behringer BCF2000 MIDI control surface
    ctls.knobs = 1001:1008;
    ctls.buttons = [1065:1072;1073:1080];
    ctls.sliders = 1081:1088;

MATLAB Interface

To use the MATLAB interface functions, first call midicontrols to specify any devices or controls to listen to. midicontrols returns an object, which you pass to the other functions for subsequent operations. You can now read the values of the specified MIDI controls by calling midiread with that object. MATLAB can respond to changes in MIDI controls by periodically calling midiread.

You can also set a callback on the specified MIDI controls by calling midicallback with that object and a function handle. The next time the MIDI controls change value, the function handle is invoked and passed to the object. The callback function typically calls midiread to determine the new value of the MIDI controls. You can use this callback when you want a MIDI control to trigger an action (such as update a GUI). Using this approach prevents having a continuously running MATLAB program in the command window.


If midiread is called before the MIDI control sends a Control Change message, the midicontrols object has no information about the actual state of the MIDI control. During this time, the midicontrols object and the actual MIDI control are out of sync with each other. Thus, calling midiread returns the initial value that was specified in the call to midicontrols (0 by default). You can synchronize the object with the control by moving the MIDI control. The MIDI control responds by sending a Control Change message causing the midicontrols object to sync to the MIDI control. If your MIDI control surface is bidirectional, you can sync in the other direction by calling midisync to send the midicontrols object's initial value to the actual MIDI control. The MIDI control responds by moving into sync with the midicontrols object.

It is generally harmless to call midisync even if the MIDI control surface is not bidirectional, so it is usually good practice to call midisync immediately after calling midicontrols.

Synchronization is also useful to link a MIDI control with a GUI control (a uicontrol slider, for example), so that when one control is changed, the other control tracks it. Typically, you implement such tracking by setting callback functions on both the MIDI control (using midicallback) and the GUI control. The MIDI control callback sends its new value to the GUI control and the GUI control sends its value to the MIDI control, using midisync.

Simulink Interface

The MIDI Controls block provides the Simulink interface. See the block reference page MIDI Controls for more details.

Was this topic helpful?