Main Content

midireceive

Receive MIDI message from MIDI device

Description

example

msgs = midireceive(device) returns the MIDI messages, msgs, received from a MIDI device using the MIDI device interface, device.

example

msgs = midireceive(device,maxmsgs) specifies the maximum number of MIDI messages to return as maxmsgs.

Examples

collapse all

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

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

collapse all

Specify device as an object created by mididevice.

Maximum number of messages to return, specified as a positive integer scalar.

Data Types: single | double

Output Arguments

collapse all

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