midisend
Send MIDI message to MIDI device
Description
Examples
Send MIDI Messages to Device
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)
Define and Send MIDI Messages to Device
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)
Compile and Play MIDI Messages
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
device
— Object of mididevice
scalar
Specify device
as an object created by mididevice
.
msg
— Object of midimsg
scalar | vector | array
Specify msg
as an object created by midimsg
.
varargin
— Variable number of arguments describing MIDI message
midimsg
input arguments
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)