梅n 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 usingvararginand then sends the MIDI messages. Thevararginsyntax is for convenience and includes a call tomidimsgwith the call tomidisend.

Examples

collapse all

Query your system for available MIDI device output ports. Use theavailableDevicesstruct to specify a valid MIDI device and create amidideviceobject.

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

创建一个pair ofNoteOnmessages (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)

midisendenables you to combine the definition and sending of amidimsginto 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;

创建一个midideviceobject.

device = mididevice(midiDeviceName);

创建一个MIDI message array.

msgs = [];forii = 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 callmcc.

functionplayMusic1() mInfo = mididevinfo; midiDeviceName = mInfo.output(1).Name; device = mididevice(midiDeviceName); msgs = [];forii = 1:8 msgs = [msgs;midimsg('Note',1,20+8*ii,64,1,ii)];endmidisend(device,msgs)end
mccplayMusic1-m-wdisable

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.

functionplayMusic2() mInfo = mididevinfo; midiDeviceName = mInfo.output(1).Name; device = mididevice(midiDeviceName); msgs = [];forii = 1:8 msgs = [msgs;midimsg('Note',1,20+8*ii,64,1,ii)];endmidisend(device,msgs) pause(msgs(end).Timestamp)end
mccplayMusic2-m-wdisable

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

Specifydeviceas an object created bymididevice.

Specifymsgas an object created bymidimsg.

Specifyvararginas a valid combination of arguments that can construct a MIDI message. Seemidimsg描述的有效参数。

版本History

Introduced in R2018a