midi device interface
midi
this tutorial introduces the musical instrument digital interface (midi) protocol and how you can use audio toolbox™ to interact with midi devices. the tools described here enable you to send and receive all midi messages as described by the midi protocol. if you are interested only in sending and receiving control change messages with a midi control surface, see . if you are interested in using midi to control your audio plugins, see midi control for audio plugins. to learn more about midi in general, consult .
midi is a technical standard for communication between electronic instruments, computers, and related devices. midi carries event messages specific to audio signals, such as pitch and velocity, as well as control signals for parameters and clock signals to synchronize tempo.
midi devices
a midi device is any device capable of sending or receiving midi messages. midi devices have input ports, output ports, or both. the midi protocol defines messages as unidirectional. a midi device can be real-world or virtual.
audio toolbox enables you to create an interface to a midi device using . to
create a midi interface to a specific device, use to
query your system for available devices. then create a mididevice
object by
specifying a midi device by name or id.
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('usb midi interface ')
device = mididevice connected to input: 'usb midi interface ' (1) output: 'usb midi interface ' (3)
you can specify a mididevice
object to listen for input messages, send
output messages, or both. in this example, the mididevice
object receives
midi messages at the input port named 'usb midi interface '
, and sends
midi messages from the output port named 'usb midi interface '
.
midi messages
a midi message contains information that describes an audio-related action. for example, when you press a key on a keyboard, the corresponding midi message contains 3 bytes:
the first byte describes the kind of action and the channel. the first byte is referred to as the status byte.
the second byte describes which key is pressed. the second byte is referred to as a data byte.
the third byte describes how hard the key is played. the third byte is also a data byte.
this message is a note on message. note on is referred to as the message name, command, or type.
in matlab®, a midi message is packaged as a object and
can be manipulated as scalars or arrays. to create a midi message, call
midimsg
with a message type and then specify the required parameters for
the specific message type. for example, to create a note on message, specify the
midimsg
type
as 'noteon'
and then specify the required
inputs: channel, note, and velocity.
channel = 1;
note = 60;
velocity = 64;
msg = midimsg('noteon',channel,note,velocity)
msg = midi message: noteon channel: 1 note: 60 velocity: 64 timestamp: 0 [ 90 3c 40 ]
for convenience, midimsg
displays the message type, channel, additional
parameters, timestamp, and the constructed message in hexadecimal form. hexadecimal is the
preferred form because it has a straightforward interpretation:
sending and receiving midi messages
to send and receive midi messages, use the mididevice
object
functions and
.
when you create a mididevice
object, it begins receiving data at its
input and placing it in a buffer.
to retrieve midi messages from the buffer, call
midireceive
.
receivedmessages = midireceive(device)
receivedmessages = midi message: noteon channel: 1 note: 36 velocity: 64 timestamp: 15861.9 [ 90 24 40 ] noteon channel: 1 note: 36 velocity: 0 timestamp: 15862.1 [ 90 24 00 ]
midimsg
objects. in this
example, a midi keyboard key is pressed.to send midi messages to a midi device, call
midisend
.
midisend(device,msg)
midi message types
the type of midi message you create is defined as a character vector or string. to create a midi message, specify it by its type and the required property values. for example, create a channel pressure midi message by entering the following at the command prompt:
channelpressuremessage = midimsg('channelpressure',1,20)
channelpressuremessage = midi message: channelpressure channel: 1 channelpressure: 20 timestamp: 0 [ d0 14 ]
channelpressuremessage.channelpressure = 37
channelpressuremessage = midi message: channelpressure channel: 1 channelpressure: 37 timestamp: 0 [ d0 25 ]
the table summarizes valid midi message types.
the audio toolbox provides convenience syntaxes to create multiple midi messages used in sequence and to create arrays of midi messages. see for a complete list of syntaxes.
midi message timing
the midi protocol does not define message timing and assumes that messages are acted
on immediately. many applications require timing information for queuing and batch
processing. for convenience, the audio toolbox packages timing information with midi messages into a single object.
all midimsg
objects have a timestamp
property, which
is set during creation as an optional last argument or after creation. the default
timestamp
is zero.
the interpretation of the timestamp
property depends on how a
midi message is created and used:
when receiving midi messages using , the underlying infrastructure assigns a timestamp when receiving midi messages. conceptually, the timing clock starts when a object is created and attached as a listener to a given midi input port. if another
mididevice
is attached to the same input port, it receives timestamps from the same timing clock as the first object.when sending midi messages using , timestamps are interpreted as when to send the message.
if there have been no recent calls to
midisend
, thenmidisend
interprets timestamps as relative to the current real-world time. a message with a timestamp of zero is sent immediately. if there has been a recent call tomidisend
, thenmidisend
interprets timestamps as relative to the largest timestamp of the last call tomidisend
. the timestamp clock formidisend
is specific to the midi output port thatmididevice
is connected to.consider a pair of midi messages that turn a note on and off. the messages specify that the note starts after one second and is sustained for one second.
create note on and note off messages. to create the note off message, use the
'noteon'
midi message type and specify zero velocity. (if you want to specify a velocity, use the'noteoff'
message type.) for more information, see .onmsg = midimsg('noteon',1,59,64); offmsg = midimsg('noteon',1,59,0);
to send on and off messages using a single call to
midisend
, specify the timestamps of the messages relative to the same start time.onmsg.timestamp = 1; offmsg.timestamp = 2; midisend(device,[onmsg;offmsg]))
to send the note off message separately, specify the timestamp of the note off message relative to the largest timestamp of the previous call to
midisend
.onmsg.timestamp = 1; offmsg.timestamp = 1; midisend(device,onmsg) midisend(device,offmsg)
the "start" time, or reference time, for
midisend
is the max between the absolute time and the largest timestamp in the last call tomidisend
. for example, consider that x, the arbitrary start time, is equal to the current absolute time. if there is a 1.5-second pause between sending the note on and note off messages, the resulting note duration is 1.5 seconds.onmsg.timestamp = 1; offmsg.timestamp = 1; midisend(device,onmsg) pause(1.5) midisend(device,offmsg)
usually, midi messages are sent faster than or at real-time speeds so there is no need to track the absolute time.
for live performances or to enable interrupts in a midi stream, you can set timestamps to zero and then call
midisend
at appropriate real-world time intervals. depending on your use case, you can divide your midi stream into small repeatable time chunks.
see also
classes
- |
functions
- | |