How do I receive and use NoteOn messages from a midi device in MATLAB?
5 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.
Swatantra Mahato on 9 Apr 2021
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:
NoteOff Channel: 1 Note: 60 Velocity: 64 Timestamp: 1 [ 80 3C 40 ]
The object "msgs" has the "Type"
and the type and note value can be checked as follows
Hope this helps