How to forward midi-RealTime messages

Ok so since i have some new parts in my studio i modified my midi keyboard to have an extra Midi output port and created a dual-merger to create an extra input port. It all worked using the simplest of sketches

#include <MIDI.h>

#include <SoftwareSerial.h>
SoftwareSerial softSerial(2, 3);
MIDI_CREATE_INSTANCE(HardwareSerial, Serial,     midiA);
MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, midiB);

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  midiA.begin(MIDI_CHANNEL_OMNI);
  midiB.begin(MIDI_CHANNEL_OMNI);
  midiB.turnThruOff();
  midiA.turnThruOff();
}

void loop() {
  static uint32_t lastmsg = millis();
  if (midiB.read())  {
    
    midiA.send(midiB.getType(), midiB.getData1(), midiB.getData2(), midiB.getChannel());
    lastmsg = millis();
  }
  if (midiA.read())  {
    midiA.send(midiA.getType(), midiA.getData1(), midiA.getData2(), midiA.getChannel());
    lastmsg = millis();
    }    
  }
  if (millis() - lastmsg < 1000) digitalWrite(LED_BUILTIN, HIGH);
  else digitalWrite(LED_BUILTIN, LOW);
}

But now i am working on getting my midi-pedal board connected and one of it's features should be sending midi-clock messages. I had a snippet somewhere that does that for me, and it works straight into my soundcard's midi input. But through the merger it doesn't. Then i remembered that for sending realtime messages you need to use

midiA.sendRealTime(midi::Clock);

and so i ended up doing this

if (midiB.read())  {
    if (midiB.getType() == midi::Clock) { 
      midiA.sendRealTime(midi::Clock);
    }
    else if (midiB.getType() == midi::Start) { 
      midiA.sendRealTime(midi::Start);
    }
    else if (midiB.getType() == midi::Stop) {  
      midiA.sendRealTime(midi::Stop);
    }
    else if (midiB.getType() == midi::Continue) {  
      midiA.sendRealTime(midi::Continue);
    }
    else {
      midiA.send(midiB.getType(), midiB.getData1(), midiB.getData2(), midiB.getChannel());
      lastmsg = millis();
    }    
  }

and that works, but is not very elegant.
so i looked at the midi namespace and the HEX values for the realtime messages are

    Clock                 = 0xF8,    ///< System Real Time - Timing Clock
    Start                 = 0xFA,    ///< System Real Time - Start
    Continue              = 0xFB,    ///< System Real Time - Continue
    Stop                  = 0xFC,    ///< System Real Time - Stop
    ActiveSensing         = 0xFE,    ///< System Real Time - Active Sensing
    SystemReset           = 0xFF,    ///< System Real Time - System Reset

So i figured that

if (midiB.getType() & 0xF8 == 0xF8) { 
      midiA.sendRealTime(midiB.getType());

should also work, but that somehow doesn't. Anybody have any idea ?

Really nobody ? Always disappointing.

Add parentheses around midiB.getType() & 0xF8:
https://en.cppreference.com/w/cpp/language/operator_precedence

1 Like

Good catch! I never remember that '==' has higher precedence than the bitwise operators.

Neither did i ! i add parentheses in lots of places where i don't even need them, but i never thought of that.
Ok time to take the midi keyboard apart again to verify.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.