MATLAB Answers

How do I receive and use NoteOn messages from a midi device in MATLAB?

4 views (last 30 days)
I've been using MATLAB and Audio Toolbox to implement a program that reads notes in from 2 midi keyboards and loops a respective audio file of the note being played.
I'm using a seperate mididevice object for each keyboard and I'm able to see the midi messages being sent from both of these using midireceive(device).
I want to have a loop that receives midi messages continuously and when there's a NoteOn message (i.e. a key is pressed), I want a sound file to repeat until the NoteOff message (i.e. the note is released). I was thinking something along the lines of if message == noteOn and note == 60 then loop the note.
How do I take the NoteOn and NoteOff messages and the note value into variables to be used with an if statement like above? Thanks in advance and please don't hesitate to ask if you have any questions.

Accepted Answer

Swatantra Mahato
Swatantra Mahato on 9 Apr 2021
Hi Joel,
It is my understanding that you want to access the message and note value from the "midimsg" received and use it with an if statement.
You can refer to the properties of the "midimsg" object. In this case the properties of interest are "Type" for the type of message and "Note" for the value of the note.
The properties of MIDI messages are documented here: Documentation link
Note that the Type property is stored as an enumeration and can be compared directly as shown below:
>> msgs=midimsg('NoteOff',1,60,64,1)
msgs =
MIDI message:
NoteOff Channel: 1 Note: 60 Velocity: 64 Timestamp: 1 [ 80 3C 40 ]
The object "msgs" has the "Type"
>> msgs.Type
ans =
midimsgtype enumeration
NoteOff
and the type and note value can be checked as follows
>> msgs.Type==midimsgtype.NoteOff
ans =
logical
1
>> msgs.Note==60
ans =
logical
1
Hope this helps

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!