Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thru filter overhaul #232

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/ThruFilterMap/ThruFilterMap.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

/**
* This example shows how to make MIDI processors.
*
* The `filter` function defines whether to forward an incoming
* MIDI message to the output.
*
* The `map` function transforms the forwarded message before
* it is sent, allowing to change things.
*
* Here we will transform NoteOn messages into Program Change,
* allowing to use a keyboard to change patches on a MIDI device.
*/

bool filter(const MIDIMessage& message)
{
if (message.type == midi::NoteOn)
{
// Only forward NoteOn messages
return true;
}
return false;
}

MIDIMessage map(const MIDIMessage& message)
{
// Make a copy of the message
MIDIMessage output(message);
if (message.type == midi::NoteOn)
{
output.type = midi::ProgramChange;
output.data2 = 0; // Not needed in ProgramChange
}
return output;
}

void setup()
{
MIDI.begin();
MIDI.setThruFilter(filter);
MIDI.setThruMap(map);
}

void loop()
{
MIDI.read();
}
2 changes: 0 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@ getData1 KEYWORD2
getData2 KEYWORD2
getSysExArray KEYWORD2
getSysExArrayLength KEYWORD2
getFilterMode KEYWORD2
getThruState KEYWORD2
getInputChannel KEYWORD2
check KEYWORD2
setInputChannel KEYWORD2
turnThruOn KEYWORD2
turnThruOff KEYWORD2
setThruFilterMode KEYWORD2
disconnectCallbackFromType KEYWORD2
setHandleNoteOff KEYWORD2
setHandleNoteOn KEYWORD2
Expand Down
19 changes: 11 additions & 8 deletions src/MIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,20 @@ class MidiInterface
// MIDI Soft Thru

public:
inline Thru::Mode getFilterMode() const;
inline bool getThruState() const;

inline void turnThruOn(Thru::Mode inThruFilterMode = Thru::Full);
using ThruFilterCallback = bool (*)(const MidiMessage& inMessage);
using ThruMapCallback = MidiMessage (*)(const MidiMessage& inMessage);
inline void turnThruOn(ThruFilterCallback fptr = thruOn);
inline void turnThruOff();
inline void setThruFilterMode(Thru::Mode inThruFilterMode);
inline void setThruFilter(ThruFilterCallback fptr) { mThruFilterCallback = fptr; }
inline void setThruMap(ThruMapCallback fptr) { mThruMapCallback = fptr; }

private:
void thruFilter(byte inChannel);
void processThru();
static inline bool thruOn(const MidiMessage& inMessage) { (void)inMessage; return true; }
static inline bool thruOff(const MidiMessage& inMessage) { (void)inMessage; return false; }
static inline MidiMessage thruEcho(const MidiMessage& inMessage) { return inMessage; }
ThruFilterCallback mThruFilterCallback;
ThruMapCallback mThruMapCallback;

// -------------------------------------------------------------------------
// MIDI Parsing
Expand Down Expand Up @@ -277,8 +282,6 @@ class MidiInterface
unsigned mPendingMessageIndex;
unsigned mCurrentRpnNumber;
unsigned mCurrentNrpnNumber;
bool mThruActivated : 1;
Thru::Mode mThruFilterMode : 7;
MidiMessage mMessage;
unsigned long mLastMessageSentTime;
unsigned long mLastMessageReceivedTime;
Expand Down
103 changes: 22 additions & 81 deletions src/MIDI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ inline MidiInterface<Transport, Settings, Platform>::MidiInterface(Transport& in
, mPendingMessageIndex(0)
, mCurrentRpnNumber(0xffff)
, mCurrentNrpnNumber(0xffff)
, mThruActivated(true)
, mThruFilterMode(Thru::Full)
, mLastMessageSentTime(0)
, mLastMessageReceivedTime(0)
, mSenderActiveSensingPeriodicity(0)
Expand Down Expand Up @@ -93,8 +91,8 @@ void MidiInterface<Transport, Settings, Platform>::begin(Channel inChannel)
mMessage.data2 = 0;
mMessage.length = 0;

mThruFilterMode = Thru::Full;
mThruActivated = mTransport.thruActivated;
mThruFilterCallback = Transport::thruActivated ? thruOn : thruOff;
mThruMapCallback = thruEcho;
}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -771,7 +769,7 @@ inline bool MidiInterface<Transport, Settings, Platform>::read(Channel inChannel
if (channelMatch)
launchCallback();

thruFilter(inChannel);
processThru();

return channelMatch;
}
Expand Down Expand Up @@ -1343,42 +1341,16 @@ void MidiInterface<Transport, Settings, Platform>::launchCallback()
@{
*/

/*! \brief Set the filter for thru mirroring
\param inThruFilterMode a filter mode

@see Thru::Mode
*/
template<class Transport, class Settings, class Platform>
inline void MidiInterface<Transport, Settings, Platform>::setThruFilterMode(Thru::Mode inThruFilterMode)
{
mThruFilterMode = inThruFilterMode;
mThruActivated = mThruFilterMode != Thru::Off;
}

template<class Transport, class Settings, class Platform>
inline Thru::Mode MidiInterface<Transport, Settings, Platform>::getFilterMode() const
{
return mThruFilterMode;
}

template<class Transport, class Settings, class Platform>
inline bool MidiInterface<Transport, Settings, Platform>::getThruState() const
{
return mThruActivated;
}

template<class Transport, class Settings, class Platform>
inline void MidiInterface<Transport, Settings, Platform>::turnThruOn(Thru::Mode inThruFilterMode)
inline void MidiInterface<Transport, Settings, Platform>::turnThruOn(ThruFilterCallback fptr)
{
mThruActivated = true;
mThruFilterMode = inThruFilterMode;
mThruFilterCallback = fptr;
}

template<class Transport, class Settings, class Platform>
inline void MidiInterface<Transport, Settings, Platform>::turnThruOff()
{
mThruActivated = false;
mThruFilterMode = Thru::Off;
mThruFilterCallback = thruOff;
}

template<class Transport, class Settings, class Platform>
Expand All @@ -1397,56 +1369,25 @@ inline void MidiInterface<Transport, Settings, Platform>::UpdateLastSentTime()
// - Channel messages are passed to the output whether their channel
// is matching the input channel and the filter setting
template<class Transport, class Settings, class Platform>
void MidiInterface<Transport, Settings, Platform>::thruFilter(Channel inChannel)
void MidiInterface<Transport, Settings, Platform>::processThru()
{
// If the feature is disabled, don't do anything.
if (!mThruActivated || (mThruFilterMode == Thru::Off))
return;
if (!Transport::thruActivated || !mThruFilterCallback(mMessage))
return;

MidiMessage thruMessage = mThruMapCallback(mMessage);

// First, check if the received message is Channel
if (mMessage.type >= NoteOff && mMessage.type <= PitchBend)
if (thruMessage.type >= NoteOff && thruMessage.type <= PitchBend)
{
const bool filter_condition = ((mMessage.channel == inChannel) ||
(inChannel == MIDI_CHANNEL_OMNI));

// Now let's pass it to the output
switch (mThruFilterMode)
{
case Thru::Full:
send(mMessage.type,
mMessage.data1,
mMessage.data2,
mMessage.channel);
break;

case Thru::SameChannel:
if (filter_condition)
{
send(mMessage.type,
mMessage.data1,
mMessage.data2,
mMessage.channel);
}
break;

case Thru::DifferentChannel:
if (!filter_condition)
{
send(mMessage.type,
mMessage.data1,
mMessage.data2,
mMessage.channel);
}
break;

default:
break;
}
send(thruMessage.type,
thruMessage.data1,
thruMessage.data2,
thruMessage.channel);
}
else
{
// Send the message to the output
switch (mMessage.type)
switch (thruMessage.type)
{
// Real Time and 1 byte
case Clock:
Expand All @@ -1456,24 +1397,24 @@ void MidiInterface<Transport, Settings, Platform>::thruFilter(Channel inChannel)
case ActiveSensing:
case SystemReset:
case TuneRequest:
sendRealTime(mMessage.type);
sendRealTime(thruMessage.type);
break;

case SystemExclusive:
// Send SysEx (0xf0 and 0xf7 are included in the buffer)
sendSysEx(getSysExArrayLength(), getSysExArray(), true);
sendSysEx(thruMessage.getSysExSize(), thruMessage.sysexArray, true);
break;

case SongSelect:
sendSongSelect(mMessage.data1);
sendSongSelect(thruMessage.data1);
break;

case SongPosition:
sendSongPosition(mMessage.data1 | ((unsigned)mMessage.data2 << 7));
sendSongPosition(thruMessage.data1 | ((unsigned)thruMessage.data2 << 7));
break;

case TimeCodeQuarterFrame:
sendTimeCodeQuarterFrame(mMessage.data1,mMessage.data2);
sendTimeCodeQuarterFrame(thruMessage.data1,thruMessage.data2);
break;

default:
Expand Down
15 changes: 0 additions & 15 deletions src/midi_Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ static const uint16_t ActiveSensingTimeout = 300;
typedef byte StatusByte;
typedef byte DataByte;
typedef byte Channel;
typedef byte FilterMode;

// -----------------------------------------------------------------------------
// Errors
Expand Down Expand Up @@ -123,20 +122,6 @@ enum MidiType: uint8_t

// -----------------------------------------------------------------------------

/*! Enumeration of Thru filter modes */
struct Thru
{
enum Mode
{
Off = 0, ///< Thru disabled (nothing passes through).
Full = 1, ///< Fully enabled Thru (every incoming message is sent back).
SameChannel = 2, ///< Only the messages on the Input Channel will be sent back.
DifferentChannel = 3, ///< All the messages but the ones on the Input Channel will be sent back.
};
};

// -----------------------------------------------------------------------------

/*! \brief Enumeration of Control Change command numbers.
See the detailed controllers numbers & description here:
http://www.somascape.org/midi/tech/spec.html#ctrlnums
Expand Down
16 changes: 15 additions & 1 deletion src/midi_Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ struct Message
memset(sysexArray, 0, sSysExMaxSize * sizeof(DataByte));
}

inline Message(const Message& inOther)
: channel(inOther.channel)
, type(inOther.type)
, data1(inOther.data1)
, data2(inOther.data2)
, valid(inOther.valid)
, length(inOther.length)
{
if (type == midi::SystemExclusive)
{
memcpy(sysexArray, inOther.sysexArray, sSysExMaxSize * sizeof(DataByte));
}
}

/*! The maximum size for the System Exclusive array.
*/
static const unsigned sSysExMaxSize = SysExMaxSize;
Expand Down Expand Up @@ -94,7 +108,7 @@ struct Message
/*! Total Length of the message.
*/
unsigned length;

inline unsigned getSysExSize() const
{
const unsigned size = unsigned(data2) << 8 | data1;
Expand Down
20 changes: 13 additions & 7 deletions src/serialMIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SerialMIDI

public:
static const bool thruActivated = true;

void begin()
{
// Initialise the Serial port
Expand Down Expand Up @@ -103,9 +103,12 @@ END_MIDI_NAMESPACE
Example: MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midi2);
Then call midi2.begin(), midi2.read() etc..
*/
#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \
MIDI_NAMESPACE::SerialMIDI<Type> serial##Name(SerialPort);\
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);
#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name) \
using Name##SerialTransport = MIDI_NAMESPACE::SerialMIDI<Type>; \
using Name##Interface = MIDI_NAMESPACE::MidiInterface<Name##SerialTransport>; \
using Name##Message = Name##Interface::MidiMessage; \
Name##SerialTransport serial##Name(SerialPort); \
Name##Interface Name((Name##SerialTransport&)serial##Name);

#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
// Leonardo, Due and other USB boards use Serial1 by default.
Expand All @@ -125,6 +128,9 @@ END_MIDI_NAMESPACE
@see DefaultSettings
@see MIDI_CREATE_INSTANCE
*/
#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \
MIDI_NAMESPACE::SerialMIDI<Type> serial##Name(SerialPort);\
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>, Settings> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);
#define MIDI_CREATE_CUSTOM_INSTANCE(Type, SerialPort, Name, Settings) \
using Name##SerialTransport = MIDI_NAMESPACE::SerialMIDI<Type>; \
using Name##Interface = MIDI_NAMESPACE::MidiInterface<Name##SerialTransport, Settings>; \
using Name##Message = Name##Interface::MidiMessage; \
Name##SerialTransport serial##Name(SerialPort); \
Name##Interface Name((Name##SerialTransport&)serial##Name);
2 changes: 2 additions & 0 deletions test/unit-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ add_executable(unit-tests
tests/unit-tests_MidiThru.cpp
)

set_source_files_properties(tests/unit-tests_MidiThru.cpp PROPERTIES COMPILE_FLAGS -Wno-shadow)

target_link_libraries(unit-tests
gtest
gmock
Expand Down
Loading