Skip to content

New thru mode: "UnmutedChannels" #231

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

Closed
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
2 changes: 2 additions & 0 deletions src/MIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ class MidiInterface
inline void turnThruOn(Thru::Mode inThruFilterMode = Thru::Full);
inline void turnThruOff();
inline void setThruFilterMode(Thru::Mode inThruFilterMode);
inline void setThruMutedChannels(bool (&inThruMutedChannels)[17]);

private:
void thruFilter(byte inChannel);
Expand Down Expand Up @@ -279,6 +280,7 @@ class MidiInterface
unsigned mCurrentNrpnNumber;
bool mThruActivated : 1;
Thru::Mode mThruFilterMode : 7;
bool mThruMutedChannels[17];
MidiMessage mMessage;
unsigned long mLastMessageSentTime;
unsigned long mLastMessageReceivedTime;
Expand Down
17 changes: 17 additions & 0 deletions src/MIDI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,12 @@ inline void MidiInterface<Transport, Settings, Platform>::setThruFilterMode(Thru
mThruActivated = mThruFilterMode != Thru::Off;
}

template<class Transport, class Settings, class Platform>
inline void MidiInterface<Transport, Settings, Platform>::setThruMutedChannels(bool (&inThruMutedChannels)[17])
{
memcpy(mThruMutedChannels, inThruMutedChannels, sizeof(inThruMutedChannels));
}

template<class Transport, class Settings, class Platform>
inline Thru::Mode MidiInterface<Transport, Settings, Platform>::getFilterMode() const
{
Expand Down Expand Up @@ -1439,6 +1445,17 @@ void MidiInterface<Transport, Settings, Platform>::thruFilter(Channel inChannel)
}
break;

case Thru::UnmutedChannels:
if ((mMessage.type == NoteOff) ||
(!mThruMutedChannels[0] && !mThruMutedChannels[mMessage.channel]))
{
send(mMessage.type,
mMessage.data1,
mMessage.data2,
mMessage.channel);
}
break;

default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/midi_Defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct Thru
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.
UnmutedChannels = 4, ///< Only the messages on channels that are not muted will be sent back.
};
};

Expand Down
119 changes: 119 additions & 0 deletions test/unit-tests/tests/unit-tests_MidiThru.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,123 @@ TEST(MidiThru, invalidMode)
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
}

TEST(MidiThru, unmutedChannelsNoneMuted) // acts like full
{
SerialMock serial;
Transport transport(serial);
MidiInterface midi((Transport&)transport);

Buffer buffer;

midi.begin(MIDI_CHANNEL_OMNI);
midi.setThruFilterMode(midi::Thru::UnmutedChannels);

static const unsigned rxSize = 6;
static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
serial.mRxBuffer.write(rxData, rxSize);

EXPECT_EQ(midi.read(), false);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
EXPECT_EQ(midi.read(), false);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
EXPECT_EQ(midi.read(), true);

buffer.clear();
buffer.resize(3);
EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
serial.mTxBuffer.read(&buffer[0], 3);
EXPECT_THAT(buffer, ElementsAreArray({
0x9b, 12, 34
}));

buffer.clear();
buffer.resize(3);
EXPECT_EQ(midi.read(), false);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
EXPECT_EQ(midi.read(), false);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
EXPECT_EQ(midi.read(), true);

EXPECT_EQ(serial.mTxBuffer.getLength(), 3); // Not using TX running status
serial.mTxBuffer.read(&buffer[0], 3);
EXPECT_THAT(buffer, ElementsAreArray({
0x9c, 56, 78
}));
}

TEST(MidiThru, unmutedChannelsWithMutedChannelTwelve)
{
SerialMock serial;
Transport transport(serial);
MidiInterface midi((Transport&)transport);

Buffer buffer;

midi.begin(MIDI_CHANNEL_OMNI);
midi.setThruFilterMode(midi::Thru::UnmutedChannels);
bool thruMutedChannels[17] = {
false,
false, false, false, false, false, false, false, false,
false, false, false, true, false, false, false, false
};
midi.setThruMutedChannels(thruMutedChannels);


static const unsigned rxSize = 6;
static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
serial.mRxBuffer.write(rxData, rxSize);

EXPECT_EQ(midi.read(), false);
EXPECT_EQ(midi.read(), false);
EXPECT_EQ(midi.read(), true);
EXPECT_EQ(midi.read(), false);
EXPECT_EQ(midi.read(), false);
EXPECT_EQ(midi.read(), true);

buffer.clear();
buffer.resize(3);
EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
serial.mTxBuffer.read(&buffer[0], 3);
EXPECT_THAT(buffer, ElementsAreArray({
0x9c, 56, 78
}));
}

TEST(MidiThru, unmutedChannelsWithChannelZeroMuted) // acts like off
{
SerialMock serial;
Transport transport(serial);
MidiInterface midi((Transport&)transport);

Buffer buffer;

midi.begin(MIDI_CHANNEL_OMNI);
midi.setThruFilterMode(midi::Thru::UnmutedChannels);
bool thruMutedChannels[17] = {
true,
false, false, false, false, false, false, false, false,
false, false, false, true, false, false, false, false
};
midi.setThruMutedChannels(thruMutedChannels);


static const unsigned rxSize = 6;
static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
serial.mRxBuffer.write(rxData, rxSize);

EXPECT_EQ(midi.read(), false);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
EXPECT_EQ(midi.read(), false);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
EXPECT_EQ(midi.read(), true);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);

EXPECT_EQ(midi.read(), false);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
EXPECT_EQ(midi.read(), false);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
EXPECT_EQ(midi.read(), true);
EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
}

END_UNNAMED_NAMESPACE