USBMidi Bug with Realtime messages?

submo

Well-known member
Hello,

Been having some trouble getting realtime Midi messages (clock, stop, start etc) from USB Midi working as expected.

Basically my code was looking for what channel a Midi Message is sent on. I use usbMidi.read followed by usbMidi.getChannel() to check for a realtime message like clock, start, or stop, and I was expecting it to return '0' for any non channel message like start, stop, clock etc, as per the way that the standard MIDI library works.

Unfortunately if a realtime message is received to usbMidi and I use usbMidi.read() followed by usbMidi.getChannel(), it doesn't return '0' but returns '9' instead.

I'm gonna look into this some more, but if anyone could help or confirm this that would be great!

TIA

Edit:
Looks like:
clock messages are received on channel 9
start messages are received on channel 11
stop messages are received on channel 13
??
 
Last edited:
Ok.... it seems that usbMidi.c treats reatime Midi messages (start, stop, clock etc) as regular channel messages , and uses their byte value to try to determine a Midi channel....

Code:
int usb_midi_read(uint32_t channel)
{
    uint32_t n, ch, type1, type2, b1;
   
    n = usb_midi_read_message();
    if (n == 0) return 0;
    type1 = n & 15;
    type2 = (n >> 12) & 15;
    b1 = (n >> 8) & 0xFF;
    ch = (b1 & 15) + 1;  // Determine channel from Message Byte
    ....

Putting in a simple check in to see if the message type byte is actually a channel message and not a realtime one seems to work for now

Code:
int usb_midi_read(uint32_t channel)
{
    uint32_t n, ch, type1, type2, b1;
   
    n = usb_midi_read_message();
    if (n == 0) return 0;
    type1 = n & 15;
    type2 = (n >> 12) & 15;
    b1 = (n >> 8) & 0xFF;
    if (b1 < 240){ // If a channel message
    ch = (b1 & 15) + 1;
    }
    else{
        ch = 0; // If a realtime message
    }
    ....

.... Hopefully this doesn't break anything else....
 
From the Complete MIDI 1.0 specification, p.30

> System Real Time messages are used to synchronize clock-based MIDI equipment. These messages
serve as uniform timing information and do not have channel numbers.

These are the System Real Time messages:

Timing ClockF8
StartFA
ContinueFB
StopFC
Active SensingFE
System ResetFF
 
From the Complete MIDI 1.0 specification, p.30

> System Real Time messages are used to synchronize clock-based MIDI equipment. These messages
serve as uniform timing information and do not have channel numbers.

These are the System Real Time messages:

Timing ClockF8
StartFA
ContinueFB
StopFC
Active SensingFE
System ResetFF
Indeed.... Which is why I would expect getChannel() to return zero for any System Real Time Midi message. This is how it works with the regular MIDI library..... from MID.hpp:
Code:
/*! \brief Get the channel of the message stored in the structure.

 \return Channel range is 1 to 16.
 For non-channel messages, this will return 0.
 */
template<class Transport, class Settings, class Platform>
inline Channel MidiInterface<Transport, Settings, Platform>::getChannel() const
{
    return mMessage.channel;
}

Currently.... getChannel() from usbMidi.c is returning a channel number for realtime messages, which I think is incorrect, but the easy fix from post#2 seems to solve the issue.
 
Last edited:
Hallo, ich bin neu hier! Seit vielen Jahren benutze ich Arduino mit Teensy-Zusatz.für teensy 2.0. Mit dem jetzigen Sketch hat das Kompilieren vor ca. drei Wochen noch funktioniert. Jetzt habe ich eine Änderung in den Sketch eingebaut, und schon klappt das Kompilieren nicht mehr. Es kommt die nachfolgende Fehlermeldung. Vor ca. 2 Wochen habe ich ein Update für Arduino instalalliert. Kann das die Ursache sein ?
Benutzte MIDI-Befehle: usbMIDI.sendNoteON, usbMIDI.sendNoteOFF, usbMIDI.sendControlCHange
Hat jemand einen Tipp für mich ? Danke !
MfG IHNO

In file included from /home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/usb_api.cpp:10:0:
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp: In member function 'bool usb_midi_class::read(uint8_t)':
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp:395:3: error: 'ch' was not declared in this scope
ch = 0; // no channel, https://forum.pjrc.com/index.php?threads/77143/
^~
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp:395:3: note: suggested alternative: 'c'
ch = 0; // no channel, https://forum.pjrc.com/index.php?threads/77143/
^~
c
exit status 1

Compilation error: exit status 1
 
Looks like a small error (for Teensy2 only) has slipped through in the new teensyduino release caused by this new fix.

I'm sure @PaulStoffregen will fix this in an upcoming release, but for now you will need to locate the usb_api.cpp file mentioned in the error message, and change the offending line (395)...

from:
Code:
ch = 0;
to:
Code:
c = 0;

Don't forget to save the file after the change, and it should be good.
 
Last edited:
Hallo, ich bin neu hier! Seit vielen Jahren benutze ich Arduino mit Teensy-Zusatz.für teensy 2.0. Mit dem jetzigen Sketch hat das Kompilieren vor ca. drei Wochen noch funktioniert. Jetzt habe ich eine Änderung in den Sketch eingebaut, und schon klappt das Kompilieren nicht mehr. Es kommt die nachfolgende Fehlermeldung. Vor ca. 2 Wochen habe ich ein Update für Arduino instalalliert. Kann das die Ursache sein ?
Benutzte MIDI-Befehle: usbMIDI.sendNoteON, usbMIDI.sendNoteOFF, usbMIDI.sendControlCHange
Hat jemand einen Tipp für mich ? Danke !
MfG IHNO

In file included from /home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/usb_api.cpp:10:0:
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp: In member function 'bool usb_midi_class::read(uint8_t)':
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp:395:3: error: 'ch' was not declared in this scope
ch = 0; // no channel, https://forum.pjrc.com/index.php?threads/77143/
^~
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp:395:3: note: suggested alternative: 'c'
ch = 0; // no channel, https://forum.pjrc.com/index.php?threads/77143/
^~
c
exit status 1

Compilation error: exit status 1

Translation (using translate.google.com):

Hello, I'm new here! I've been using Arduino—specifically with the Teensy add-on for the Teensy 2.0—for many years. With my current sketch, compilation was working fine about three weeks ago. Now, however, I've incorporated a change into the sketch, and suddenly it won't compile anymore. I'm receiving the following error message. I installed an update for Arduino about two weeks ago; could that be the cause?

MIDI commands used: usbMIDI.sendNoteON, usbMIDI.sendNoteOFF, usbMIDI.sendControlChange

Does anyone have any tips for me? Thanks!

Best regards, IHNO

Code:
In file included from /home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/usb_api.cpp:10:0:
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp: In member function 'bool usb_midi_class::read(uint8_t)':
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp:395:3: error: 'ch' was not declared in this scope
ch = 0; // no channel, https://forum.pjrc.com/index.php?threads/77143/
^~
/home/lenonotebook/.arduino15/packages/teensy/hardware/avr/1.60.0/cores/teensy/../usb_midi/usb_api.cpp:395:3: note: suggested alternative: 'c'
ch = 0; // no channel, https://forum.pjrc.com/index.php?threads/77143/
^~
c
exit status 1

Compilation error: exit status 1
 
Back
Top