Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: while (midi.read())

  1. #1
    Senior Member
    Join Date
    Jun 2015
    Posts
    190

    while (midi.read())

    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
    }

  2. #2
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,060
    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.

  3. #3

  4. #4
    Senior Member
    Join Date
    Jun 2015
    Posts
    190
    Quote Originally Posted by oddson View Post
    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) ?

  5. #5
    Senior Member
    Join Date
    Jun 2015
    Posts
    190
    Quote Originally Posted by markonian View Post
    Thanks also.
    It was more the midi.read() piece of that code I was after - sorry.

    I didnt appreciate that if there were no msgs to be read that it returned false.

  6. #6
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,060
    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.

  7. #7
    Senior Member
    Join Date
    Jun 2015
    Posts
    190
    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)

    ??

  8. #8
    Senior Member
    Join Date
    Jun 2015
    Posts
    190
    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.

  9. #9
    Senior Member
    Join Date
    Jan 2017
    Location
    Maastricht
    Posts
    155
    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.

  10. #10
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,060
    Quote Originally Posted by bossredman View Post
    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.

    Quote Originally Posted by Gerrit
    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).

  11. #11
    Senior Member
    Join Date
    Jun 2015
    Posts
    190
    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?

  12. #12
    Senior Member
    Join Date
    Jun 2015
    Posts
    190
    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());

  13. #13
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,060
    .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 by oddson; 01-17-2019 at 05:05 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •