diff --git a/src/MIDI.h b/src/MIDI.h index 3461242..9660cad 100644 --- a/src/MIDI.h +++ b/src/MIDI.h @@ -120,8 +120,9 @@ public: static inline MidiType getTypeFromStatusByte(StatusByte inStatus); static inline byte getMessageLength(StatusByte inStatus); static inline bool isChannelMessage(MidiType inType); - static inline bool isRealtimeMessage(MidiType inType); - + static inline bool isSystemRealtimeMessage(MidiType inType); + static inline bool isSystemCommonMessage(MidiType inType); + private: bool inputFilter(Channel inChannel); bool parse(); diff --git a/src/MIDI.hpp b/src/MIDI.hpp index fd164be..60dddd8 100644 --- a/src/MIDI.hpp +++ b/src/MIDI.hpp @@ -397,7 +397,7 @@ void MidiInterface::sendSongSelect(DataByte inSongNumber) template void MidiInterface::sendRealTime(MidiType inType) { - if (isRealtimeMessage(inType) || inType == TuneRequest) + if (isSystemRealtimeMessage(inType) || inType == TuneRequest) { mSerial.write((byte)inType); } @@ -597,9 +597,9 @@ bool MidiInterface::parse() { // Reception of status bytes in the middle of an uncompleted message // are allowed only for interleaved Real Time message or EOX - if (isRealtimeMessage(extracted)) + if (isSystemRealtimeMessage(getTypeFromStatusByte(extracted))) { - if (shouldMessageBeForwarded(extracted) + if (shouldMessageBeForwarded(extracted)) { mSerial.write(extracted); } @@ -622,7 +622,7 @@ bool MidiInterface::parse() { if (mMessage.sysexArray[0] == SystemExclusive) { - if (shouldMessageBeForwarded(SystemExclusive) + if (shouldMessageBeForwarded(SystemExclusive)) { mSerial.write(extracted); } @@ -658,7 +658,7 @@ bool MidiInterface::parse() mPendingMessage[mPendingMessageIndex] = extracted; } - if (shouldMessageBeForwarded(mPendingMessage[0]) + if (shouldMessageBeForwarded(mPendingMessage[0])) { mSerial.write(extracted); } @@ -872,7 +872,7 @@ template byte MidiInterface::getMessageLength(StatusByte inStatus) { const MidiType type = getTypeFromStatusByte(inStatus); - if (isRealtimeMessage(type) || type == TuneRequest) + if (isSystemRealtimeMessage(type) || type == TuneRequest) { return 1; } @@ -913,7 +913,7 @@ bool MidiInterface::isChannelMessage(MidiType inType) } template -bool MidiInterface::isRealtimeMessage(MidiType inType) +bool MidiInterface::isSystemRealtimeMessage(MidiType inType) { return inType == Clock || inType == Start || @@ -923,6 +923,15 @@ bool MidiInterface::isRealtimeMessage(MidiType inType) inType == SystemReset; } +template +bool MidiInterface::isSystemCommonMessage(MidiType inType) +{ + return inType == TimeCodeQuarterFrame || + inType == SongPosition || + inType == SongSelect || + inType == TuneRequest; +} + // ----------------------------------------------------------------------------- #if MIDI_USE_CALLBACKS @@ -1090,21 +1099,26 @@ bool MidiInterface::hasThruFlag(byte inFlag) const template bool MidiInterface::shouldMessageBeForwarded(StatusByte inStatus) const { - if (isChannelMessage(inStatus)) + const MidiType type = getTypeFromStatusByte(inStatus); + if (isChannelMessage(type)) { - const Channel channel = getChannel(inStatus); + const Channel channel = (inStatus & 0x0F) + 1; const bool forwardSame = hasThruFlag(ThruFilterFlags::channelSame); const bool forwardDiff = hasThruFlag(ThruFilterFlags::channelDifferent); return (forwardSame && mInputChannel == channel) || (forwardDiff && mInputChannel != channel); } - else if (isRealtimeMessage(inStatus)) + else if (isSystemRealtimeMessage(type)) { - return hasThruFlag(ThruFilterFlags::realtime); + return hasThruFlag(ThruFilterFlags::systemRealtime); } - else if (inStatus == SystemExclusive) + else if (isSystemCommonMessage(type)) { - return hasThruFlag(ThruFilterFlags::system); + return hasThruFlag(ThruFilterFlags::systemCommon); + } + else if (type == SystemExclusive) + { + return hasThruFlag(ThruFilterFlags::systemExclusive); } else { @@ -1113,114 +1127,6 @@ bool MidiInterface::shouldMessageBeForwarded(StatusByte inStatus) co } } -// This method is called upon reception of a message -// and takes care of Thru filtering and sending. -template -void MidiInterface::thruFilter(Channel inChannel) -{ - - /* - This method handles Soft-Thru filtering. - - Soft-Thru filtering: - - All system messages (System Exclusive, Common and Real Time) are passed to output unless filter is set to Off - - Channel messages are passed to the output whether their channel is matching the input channel and the filter setting - - */ - - // If the feature is disabled, don't do anything. - if (!mThruActivated || (mThruFilterMode == Off)) - return; - - - // First, check if the received message is Channel - if (mMessage.type >= NoteOff && mMessage.type <= PitchBend) - { - const bool filter_condition = ((mMessage.channel == mInputChannel) || - (mInputChannel == MIDI_CHANNEL_OMNI)); - - // Now let's pass it to the output - switch (mThruFilterMode) - { - case Full: - send(mMessage.type, - mMessage.data1, - mMessage.data2, - mMessage.channel); - return; - break; - case SameChannel: - if (filter_condition) - { - send(mMessage.type, - mMessage.data1, - mMessage.data2, - mMessage.channel); - return; - } - break; - case DifferentChannel: - if (!filter_condition) - { - send(mMessage.type, - mMessage.data1, - mMessage.data2, - mMessage.channel); - return; - } - break; - case Off: - // Do nothing. - // Technically it's impossible to get there because - // the case was already tested earlier. - break; - default: - break; - } - } - else - { - // Send the message to the output - switch (mMessage.type) - { - // Real Time and 1 byte - case Clock: - case Start: - case Stop: - case Continue: - case ActiveSensing: - case SystemReset: - case TuneRequest: - sendRealTime(mMessage.type); - return; - break; - - case SystemExclusive: - // Send SysEx (0xF0 and 0xF7 are included in the buffer) - sendSysEx(mMessage.data1,mMessage.sysexArray,true); - return; - break; - - case SongSelect: - sendSongSelect(mMessage.data1); - return; - break; - - case SongPosition: - sendSongPosition(mMessage.data1 | ((unsigned)mMessage.data2<<7)); - return; - break; - - case TimeCodeQuarterFrame: - sendTimeCodeQuarterFrame(mMessage.data1,mMessage.data2); - return; - break; - default: - break; - } - } -} - #endif // MIDI_BUILD_THRU // ----------------------------------------------------------------------------- diff --git a/src/midi_Defs.h b/src/midi_Defs.h index 394695b..9dcd3bc 100644 --- a/src/midi_Defs.h +++ b/src/midi_Defs.h @@ -80,7 +80,7 @@ struct ThruFilterFlags , junk = (1 << 7) ///< Send mis-formated data back (unadvisable) /// Fully enabled Thru (every incoming message is sent back). - , all = channel | system + , all = channel | systemExclusive | systemCommon | systemRealtime }; };