midireceive
Receive MIDI message from MIDI device
Description
Examples
Receive MIDI Messages
To determine what MIDI devices are attached to your MIDI input ports, call
mididevinfo
. Use the availableDevices
struct to specify a valid MIDI device to create a mididevice
object.
availableDevices = mididevinfo; device = mididevice(availableDevices.input(1).ID);
Once your MIDI device
object is created, it begins
listening to MIDI messages from your specified device and storing them in a
buffer. To get all MIDI messages in the buffer, call
midireceive
. In this example, several keys on a
MIDI keyboard are played.
msgs = midireceive(device)
msgs = MIDI message: NoteOn Channel: 1 Note: 52 Velocity: 64 Timestamp: 3.94 [ 90 34 40 ] NoteOn Channel: 1 Note: 52 Velocity: 0 Timestamp: 4.179 [ 90 34 00 ] NoteOn Channel: 1 Note: 48 Velocity: 64 Timestamp: 4.19 [ 90 30 40 ] NoteOn Channel: 1 Note: 47 Velocity: 64 Timestamp: 4.382 [ 90 2F 40 ] NoteOn Channel: 1 Note: 48 Velocity: 0 Timestamp: 4.459 [ 90 30 00 ] NoteOn Channel: 1 Note: 48 Velocity: 64 Timestamp: 4.59 [ 90 30 40 ] NoteOn Channel: 1 Note: 47 Velocity: 0 Timestamp: 4.776 [ 90 2F 00 ] NoteOn Channel: 1 Note: 50 Velocity: 64 Timestamp: 4.788 [ 90 32 40 ] NoteOn Channel: 1 Note: 47 Velocity: 64 Timestamp: 4.802 [ 90 2F 40 ] NoteOn Channel: 1 Note: 52 Velocity: 64 Timestamp: 4.831 [ 90 34 40 ] NoteOn Channel: 1 Note: 47 Velocity: 0 Timestamp: 4.84 [ 90 2F 00 ] NoteOn Channel: 1 Note: 48 Velocity: 0 Timestamp: 4.912 [ 90 30 00 ] NoteOn Channel: 1 Note: 52 Velocity: 0 Timestamp: 4.953 [ 90 34 00 ] NoteOn Channel: 1 Note: 50 Velocity: 0 Timestamp: 5.079 [ 90 32 00 ]
Reading from the buffer clears the data. For example, if no more MIDI
messages are sent, and the buffer is reread,
midireceive
returns an empty MIDI message.
msgs = midireceive(device)
msgs = empty MIDI message array
Receive Limited Number of MIDI Messages
Query your system for available output from MIDI devices. Specify that the
output of a MIDI device is connected to the input of your mididevice
object.
mididevinfo
MIDI devices available: ID Direction Interface Name 0 output MMSystem 'Microsoft MIDI Mapper' 1 input MMSystem 'USB MIDI Interface ' 2 output MMSystem 'Microsoft GS Wavetable Synth' 3 output MMSystem 'USB MIDI Interface '
device = mididevice('Input','USB MIDI Interface ');
Once your MIDI device
object is created, it begins
listening to MIDI messages from your specified device and storing them in a
buffer. To get a limited number of MIDI messages from the buffer, call
midireceive
and specify the maximum number of
messages to return. In this example, five keys are played on a MIDI device.
A maximum of four MIDI messages are received at each call to
midireceive
.
midireceive(device,4)
ans = MIDI message: NoteOn Channel: 1 Note: 36 Velocity: 64 Timestamp: 2929.71 [ 90 24 40 ] NoteOn Channel: 1 Note: 36 Velocity: 0 Timestamp: 2929.91 [ 90 24 00 ] NoteOn Channel: 1 Note: 37 Velocity: 64 Timestamp: 2930.43 [ 90 25 40 ] NoteOn Channel: 1 Note: 37 Velocity: 0 Timestamp: 2930.59 [ 90 25 00 ]
midireceive(device,4)
ans = MIDI message: NoteOn Channel: 1 Note: 38 Velocity: 64 Timestamp: 2931.16 [ 90 26 40 ] NoteOn Channel: 1 Note: 38 Velocity: 0 Timestamp: 2931.32 [ 90 26 00 ] NoteOn Channel: 1 Note: 39 Velocity: 64 Timestamp: 2931.87 [ 90 27 40 ] NoteOn Channel: 1 Note: 39 Velocity: 0 Timestamp: 2932.01 [ 90 27 00 ]
midireceive(device,4)
ans = MIDI message: NoteOn Channel: 1 Note: 40 Velocity: 64 Timestamp: 2932.52 [ 90 28 40 ] NoteOn Channel: 1 Note: 40 Velocity: 0 Timestamp: 2932.66 [ 90 28 00 ]
Input Arguments
device
— Object of mididevice
object of mididevice
Specify device
as an object created by mididevice
.
maxmsgs
— Maximum number of messages to return
positive integer scalar
Maximum number of messages to return, specified as a positive integer scalar.
Data Types: single
| double
Output Arguments
msgs
— Object of midimsg
scalar | column vector
Object of midimsg
, returned as a scalar or column vector. The number of
MIDI messages in the mididevice
buffer and maxmsgs
determine
the size of msgs
.
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 (한국어)