Skip to content

Commit 461fbf5

Browse files
committed
#30 Cleanup MIDI channels matching.
1 parent 415a81f commit 461fbf5

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

Source/PluginProcessor.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <chrono>
2121

22+
#include "aeolus/globals.h"
2223
#include "PluginProcessor.h"
2324
#include "PluginEditor.h"
2425

@@ -224,9 +225,9 @@ void AeolusAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer, juce::
224225
// when they first compile a plugin, but obviously you don't need to keep
225226
// this code if your algorithm always overwrites all the output channels.
226227
for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
227-
buffer.clear (i, 0, buffer.getNumSamples());
228+
buffer.clear(i, 0, buffer.getNumSamples());
228229

229-
processMidi (midiMessages);
230+
processMidi(midiMessages);
230231

231232
#if AEOLUS_MULTIBUS_OUTPUT
232233

@@ -274,9 +275,7 @@ void AeolusAudioProcessor::processMidi(juce::MidiBuffer& midiMessages)
274275

275276
const int mask{ _engine.getMIDIControlChannelsMask() };
276277

277-
if ((ch == 0 || (mask & (ch - 1)) != 0)
278-
&& msg.isController()) {
279-
278+
if (aeolus::midi::matchChannelToMask(mask, ch) && msg.isController()) {
280279
int cc = msg.getControllerNumber();
281280
const float value = float(msg.getControllerValue()) / 127.0f;
282281

Source/aeolus/division.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//
1818
// ----------------------------------------------------------------------------
1919

20+
#include "globals.h"
2021
#include "division.h"
2122
#include "engine.h"
2223

@@ -317,10 +318,8 @@ void Division::getAvailableRange(int& minNote, int& maxNote) const noexcept
317318

318319
bool Division::isForMIDIChannel(int channel) const noexcept
319320
{
320-
const int mask{_midiChannelsMask.load() };
321-
322-
return channel == 0 // broadcast message
323-
|| (mask & (1 << (channel - 1))) != 0;
321+
const int mask{ _midiChannelsMask.load() };
322+
return midi::matchChannelToMask(mask, channel);
324323
}
325324

326325
void Division::setTremulantEnabled(bool ena) noexcept

Source/aeolus/engine.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,8 @@ void Engine::process(AudioBuffer<float>& out, bool isNonRealtime)
535535

536536
void Engine::processMIDIMessage(const MidiMessage& message)
537537
{
538-
const int mask = getMIDIControlChannelsMask();
539-
540538
// Process global CCs
541-
if (message.getChannel() == 0 || (mask & (message.getChannel() - 1)) != 0) {
539+
if (midi::matchChannelToMask(getMIDIControlChannelsMask(), message.getChannel())) {
542540
processControlMIDIMessage(message);
543541
}
544542

@@ -557,15 +555,13 @@ void Engine::noteOn(int note, int midiChannel)
557555
{
558556
clearDivisionsTriggerFlag();
559557

560-
const int mask{ getMIDIControlChannelsMask() };
561-
562558
bool handled{ false };
563559

564560
// Handle key switches
565561
// @note If a key switch falls within the playable range we need to make
566562
// sure we don't process corresponding note-on event, otherwise
567563
// navigating the sequencer will create a spurious sounds.
568-
if (midiChannel == 0 || (mask & (midiChannel - 1)) != 0) {
564+
if (midi::matchChannelToMask(getMIDIControlChannelsMask(), midiChannel)) {
569565
if (isKeySwitchBackward(note)) {
570566
_sequencer->stepBackward();
571567
handled = true;

Source/aeolus/globals.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,24 @@ float exp2ap(float x)
5353

5454
} // namespace math
5555

56+
//==============================================================================
57+
58+
namespace midi {
59+
60+
int channelToMask(int channel)
61+
{
62+
// Zero means any MIDI channel.
63+
if (channel <= 0)
64+
return (1 << 16) - 1;
65+
66+
return 1 << (channel - 1);
67+
}
68+
69+
bool matchChannelToMask(int mask, int channel)
70+
{
71+
return (mask & channelToMask(channel)) != 0;
72+
}
73+
74+
} // namespace midi
75+
5676
AEOLUS_NAMESPACE_END

Source/aeolus/globals.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,21 @@ struct Cos<B, A, double>
204204
constexpr static double value = SinCosSeries<1, 33, B, A>::value;
205205
};
206206

207-
//----------------------------------------------------------
208-
209207
template <typename T>
210208
constexpr bool isPowerOfTwo(T v)
211209
{
212210
return (v & (v - 1)) == 0;
213211
}
214212

215-
216213
} // namespace math
217214

215+
//----------------------------------------------------------
216+
217+
namespace midi {
218+
219+
int channelToMask(int channel);
220+
bool matchChannelToMask(int mask, int channel);
221+
222+
} // namespace midi
223+
218224
AEOLUS_NAMESPACE_END

0 commit comments

Comments
 (0)