while (midi.read())

Status
Not open for further replies.

bossredman

Well-known member
Hi - really basic question, but could someone pls explain exactly what sticking "while" in front of midi.read does pls

Code:
while (midi.read() )
{
      do something
}
 
It says to keep calling .read until it returns false, meaning there are no new messages pending.

A while loop runs as long as the test condition is true. Which in ths case is whenever the read command returns a midi message.

This will clear a burst of midi messages.

You need to call this even if you are not handling midi inputs to avoid the queue getting clogged with unprocessed messages.
 
It says to keep calling .read until it returns false, meaning there are no new messages pending.

A while loop runs as long as the test condition is true. Which in ths case is whenever the read command returns a midi message.

This will clear a burst of midi messages.

You need to call this even if you are not handling midi inputs to avoid the queue getting clogged with unprocessed messages.


Brilliant thanks.
I didn't understand teh difference between just

Code:
midi.read()

and

while (midi.read())

So if there are several msg waiting to be read & I only call midi.read() (ie not the while midi.read() ) - do I need to ensure I read/decode ALL those messages in 1 go otherwise I will loose them all.
Or does midi.read() on its own only delete teh last (or first) ?
 
do I need to ensure I read/decode ALL those messages in 1 go otherwise I will loose them all.
No, you won't lose them you just won't have cleared them yet and maybe you won't be able to keep up with one-at-a-time reads; but maybe it's better (shorter loop if you having time sensative code elsewhere).

The assumption appears to be; if you are ignoring midi input you might as well clear it all in a hurry... but if you where processing it you might only want one message per loop event to make sure your timing isn't delayed as you handle a burst of external midi.

But I'm guessing... I'm not really expert at this stuff.
 
Thanks again - so to summarise with regards midi sysex msg's:

1]calling Midi.read() [on its own] - checks to see if there are any sysex msg's in the serial buffer.
If there are (ie returns TRUE), then it will read only 1 msg (ie anything between bytes F0 & F7) - the "oldest" one in the buffer AND then delete it from the buffer.
You need to call midi.read() again to read what was the next oldest msg in the buffer (which is now obviously the oldest one).

2]while ( midi.read() ) - will loop through the above scenario until the buffer is empty (ie returns FALSE)

??
 
So any ideas on the best/prefered way to read multiple msgs from the buffer?

IN my application, I will be sending 1 or 2 sysex's & trying to read the several ( possibly up to 4 msgs) that get returned.
 
Use callback functions to handle the incoming MIDI data, see Using USB MIDI for details. Your code for processing incoming sysex data would go in the callback function.
 
Thanks again - so to summarise with regards midi sysex msg's:

1]calling Midi.read() [on its own] - checks to see if there are any sysex msg's in the serial buffer....
To be clear, any MIDI and not only sysex.

Gerrit said:
Use callback functions to handle the incoming MIDI data, see Using USB MIDI for details. Your code for processing incoming sysex data would go in the callback function.
.... the callback function will run whenever the midi message being processed is of the correct type. You still need to call .read somewhere in your main loop to start this processing for each message of any type.

I've always used the explict getSysExArray (I believe of necessity in earlier versions).
 
Thanks for all the help so far. I think I have a better understanding now.

One oither thing.

Is it possible to extract the FULL/WHOLE midi buffer in 1 go?
I think all we've discussed so far allows reading of a single msg - right?
 
At exactly what point does MIDI.read flip from true to false.

I tried monitoring the value Midi.Read() at various places in my code but its always 0.
even at teh start of my callback (void HandleSystemExclusive)

Code:
Serial.print("MidiRead Value:= "); Serial.println(MIDI.read());
 
.read is a method that returns a boolean result that is TRUE if and only if a MIDI message was read. You can't check it's current value, only the result of calling it again.

If you call it within a callback function it triggered I assume it's looking for the next message and not finding one.

The boolean result is needed for 'Read and Query' method as it's up to you to parse through the results.

But for 'Read and Callback' you just need to call .read once from your main loop and your callbacks handle different midi types.

https://www.pjrc.com/teensy/td_midi.html
 
Last edited:
Status
Not open for further replies.
Back
Top