From 2fec41ecfe56160589b475c9be27b028364daa54 Mon Sep 17 00:00:00 2001 From: Francois Best Date: Tue, 31 Aug 2021 15:23:54 +0200 Subject: [PATCH] chore: Add example --- examples/ThruFilterMap/ThruFilterMap.ino | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 examples/ThruFilterMap/ThruFilterMap.ino diff --git a/examples/ThruFilterMap/ThruFilterMap.ino b/examples/ThruFilterMap/ThruFilterMap.ino new file mode 100644 index 0000000..be04679 --- /dev/null +++ b/examples/ThruFilterMap/ThruFilterMap.ino @@ -0,0 +1,52 @@ +#include + +using Message = midi::Message; + +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 Message& message) +{ + if (message.type == midi::NoteOn) + { + // Only forward NoteOn messages + return true; + } + return false; +} + +Message map(const Message& message) +{ + // Make a copy of the message + Message 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(); +}