Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
MIDI_Pipes-Filter.ino

MIDI_Pipes-Filter

Filtering and modifying MIDI messages that travel through a MIDI_Pipe.

This example demonstrates how to create custom MIDI pipes that can filter out and modify the messages that travel through it. It uses a debug MIDI interface for demonstration purposes, and loops back only MIDI note and real-time messages, messages of any other type are dropped. Additionally, all MIDI note messages are transposed down an octave, and their channel is set to 5.

To try it out, open the Serial monitor (at 115200 baud) and type 98 3C 7F (Note On, channel 9, C4, full velocity). When you press enter, you'll get back the following message (note how the channel and pitch changed):

Note On Channel: 5 Data 1: 0x30 Data 2: 0x7F
A type-safe class for MIDI channels.
Definition Channel.hpp:13

If you try to send a Pitch Bend message (for example, E0 12 34), you'll get no response, because the code filtered it out.

Boards: 🛈
AVR, AVR USB, Nano Every, Due, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, ESP32, Teensy 3.x
// A unidirectional pipe that connects a MIDI source to a MIDI sink.
// Messages coming from the source are handled by the `mapForwardMIDI()`
// function, where they can be modified or filtered out, and then sent
// to the MIDI sink by calling the `sourceMIDItoSink()` function.
struct MyMIDIFilter : MIDI_Pipe {
// Pass on only Note On/Off messages, changing the channel to 5 and
// transposing all notes an octave down.
void mapForwardMIDI(ChannelMessage msg) override {
switch (msg.getMessageType()) {
case MIDIMessageType::NoteOff: // fallthrough
if (msg.data1 >= 12)
msg.data1 -= 12;
break;
default: break;
}
}
// Do not pass on System Exclusive messages.
void mapForwardMIDI(SysExMessage) override {}
// Do not pass on System Common messages.
void mapForwardMIDI(SysCommonMessage) override {}
// Pass on Real-Time messages without changes.
void mapForwardMIDI(RealTimeMessage msg) override {
}
};
MyMIDIFilter filter;
void setup() {
// Loop back the MIDI input to the MIDI output, through the filter:
// (MIDI input) -> (MIDI filter) -> (MIDI output)
// (source) -> (pipe) -> (sink)
midi >> filter >> midi;
midi.begin();
}
void loop() {
midi.update();
}
constexpr Channel Channel_5
Definition Channel.hpp:122
The main header file that includes all Control-Surface header files.
@ NoteOn
Note On Channel Voice message (3B).
@ NoteOff
Note Off Channel Voice message (3B).
Class that routes MIDI messages from a MIDI_Source to a MIDI_Sink.
void sourceMIDItoSink(Message msg)
Send the given MIDI message to the sink of this pipe.
virtual void mapForwardMIDI(ChannelMessage msg)
Function that maps, edits or filters MIDI messages, and then forwards them to the sink of the pipe.
void begin() override
Start the Serial interface at the predefined baud rate.
A class for debug MIDI interfaces sending and receiving human-readable MIDI messages over the USB CDC...
void setChannel(Channel channel)
Set the MIDI channel of the message.
MIDIMessageType getMessageType() const
Get the MIDI message type.
uint8_t data1
First MIDI data byte.