midisend
Send MIDI message to MIDI device
Description
Examples
Send MIDI Messages to Device
Query your system for available MIDI device output ports. Use theavailableDevices
struct to specify a valid MIDI device and create amididevice
object.
availableDevices = mididevinfo; device = mididevice(availableDevices.output(2).ID);
创建一个pair ofNoteOn
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 amidimsg
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;
创建一个mididevice
object.
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
device
—Object ofmididevice
scalar
Specifydevice
as an object created bymididevice
.
msg
—Object ofmidimsg
scalar|vector|array
Specifymsg
as an object created bymidimsg
.
varargin
—Variable number of arguments describing MIDI message
midimsg
input arguments
Specifyvarargin
as a valid combination of arguments that can construct a MIDI message. Seemidimsg
描述的有效参数。
版本History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)