Main Content

midisend

Send MIDI message to MIDI device

Description

example

midisend(device,msg) sends the MIDI message, msg, to a MIDI device using the MIDI device interface, device.

example

midisend(device,varargin) creates MIDI messages using varargin and then sends the MIDI messages. The varargin syntax is for convenience and includes a call to midimsg with the call to midisend.

Examples

collapse all

Query your system for available MIDI device output ports. Use the availableDevices struct to specify a valid MIDI device and create a mididevice object.

availableDevices = mididevinfo;
device = mididevice(availableDevices.output(2).ID);

Create a pair of NoteOn messages (to indicate Note On and Note Off) and send them to your selected MIDI device.

msgs = midimsg('Note',1,48,64,0.25);
midisend(device,msgs)

midisend enables you to combine the definition and sending of a midimsg into a single function call. Send middle C on channel 3 with velocity 64.

mididevinfo
  MIDI devices available:
  ID  Direction  Interface   Name
   0   output    MMSystem   'Microsoft MIDI Mapper'
   1    input    MMSystem   'nanoKONTROL2'
   2    input    MMSystem   'USB Uno MIDI Interface'
   3   output    MMSystem   'Microsoft GS Wavetable Synth'
   4   output    MMSystem   'nanoKONTROL2'
   5   output    MMSystem   'USB Uno MIDI Interface'
device = mididevice('USB Uno MIDI Interface')
device = 
  mididevice connected to
     Input: 'USB Uno MIDI Interface' (2)
    Output: 'USB Uno MIDI Interface' (5)
midisend(device,'NoteOn',3,60,64)

Get the name of an available output MIDI device on your system.

mInfo = mididevinfo;
Disregard cmd.exe warnings about UNC directory pathnames.
Disregard cmd.exe warnings about UNC directory pathnames.
midiDeviceName = mInfo.output(1).Name;

Create a mididevice object.

device = mididevice(midiDeviceName);

Create a MIDI message array.

msgs = [];
for ii = 1:8
    msgs = [msgs;midimsg('Note',1,20+8*ii,64,1,ii)];
end

To listen to the MIDI messages, send the MIDI messages to your device.

midisend(device,msgs)

To compile the previous steps, encapsulate the code in a function and then call mcc.

function playMusic1()
    mInfo = mididevinfo;
    midiDeviceName = mInfo.output(1).Name;
    device = mididevice(midiDeviceName);

    msgs = [];
    for ii = 1:8
        msgs = [msgs;midimsg('Note',1,20+8*ii,64,1,ii)];
    end

    midisend(device,msgs)
end
mcc playMusic1 -m -w disable

Execute the compiled code. You will not hear any sound. This is because the executable opened, sent the MIDI messages to the queue, and then closed, aborting its commands before the MIDI messages had a chance to play.

!playMusic1.exe

To keep the executable open long enough for the MIDI messages to play, add a pause to the executable. Set the duration of the pause to equal the duration of the MIDI messages.

function playMusic2()
    mInfo = mididevinfo;
    midiDeviceName = mInfo.output(1).Name;
    device = mididevice(midiDeviceName);

    msgs = [];
    for ii = 1:8
        msgs = [msgs;midimsg('Note',1,20+8*ii,64,1,ii)];
    end

    midisend(device,msgs)
    pause(msgs(end).Timestamp)
end
mcc playMusic2 -m -w disable

Play the compiled executable. The sound that plays through your MIDI device is the same as the uncompiled version.

!playMusic2.exe

Input Arguments

collapse all

Specify device as an object created by mididevice.

Specify msg as an object created by midimsg.

Specify varargin as a valid combination of arguments that can construct a MIDI message. See midimsg for a description of valid arguments.

Version History

Introduced in R2018a