Post by nic on May 8, 2019 6:31:14 GMT
In this tutorial, I will take you through creating a simple MIDI FX unit step by step. We will create a channel switcher where the user can choose a MIDI channel and all MIDI events will be remapped to the channel as they pass through. In addition we will add a toggle button to disable the remapping (ie. a bypass)
Here is what the UI will look like:

First, we will configure the look and feel of the controls box to match the above. We will use the first control (array variable Q0) to select the channel using a slider ranging from 1 to 16, and the second (Q1) is a toggle button to engage/disengage the channel remapping. However, let's also alias those two array variables in our code to make that more obvious while we are at it. I recommend you place aliases and UI setup inside an 'if load' block at the top of your script. - this is more efficient when processing MIDI events.
# simple channel switcher
if load
# give controls q0 and q1 meaningful names
alias q0 ChannelSlider
alias q1 EngageButton
# configure our two controls
set ChannelSlider Channel $1 $16 +slider
set EngageButton Engage +toggle
# set the default values for the two
# controls and preserve them
assign ChannelSlider = 1 +preserve
assign EngageButton = 1 +preserve
end
The last two lines above configure the controls with default MIDI channel of 1 and the switcher engaged when the script is loaded. However, I have added the '+preserve' flag which means that if the user changes the slider or button and then saves that into a preset, then the next time that loads, the channel/button will not be restored to the defaults we put in our script, but to the values that were in place when it was saved.
Now, the above script will run but doesn't actually do any remapping. We now need to add the MIDI processing logic - we want to remap all channelised MIDI events to the selected channel but only if the Engage button is on. Outside of the 'if load' is where we add this:
# event processing logic
if MT != F0
if EngageButton == 1
math M0 = MT + ChannelSlider
math M0 = M0 - 1
end
end
The first if statement makes sure that the message type (MT) is a channelised event (ie. not a sysex/realtime event). The second if statement makes sure the Engage button is on.
There are two steps to rewriting the channel. The first math statement takes the current message type and adds the current value of the slider and puts that into the first byte of the current MIDI message (M0). In MIDI messages, the channels are numbered from 0 to F (0 to 15) but our channel slider range is 1 to 16; we therefore need to subtract 1 from the first byte to align correctly.
And there you have it, a working channel switcher. Here is the complete code:
Here is what the UI will look like:

First, we will configure the look and feel of the controls box to match the above. We will use the first control (array variable Q0) to select the channel using a slider ranging from 1 to 16, and the second (Q1) is a toggle button to engage/disengage the channel remapping. However, let's also alias those two array variables in our code to make that more obvious while we are at it. I recommend you place aliases and UI setup inside an 'if load' block at the top of your script. - this is more efficient when processing MIDI events.
# simple channel switcher
if load
# give controls q0 and q1 meaningful names
alias q0 ChannelSlider
alias q1 EngageButton
# configure our two controls
set ChannelSlider Channel $1 $16 +slider
set EngageButton Engage +toggle
# set the default values for the two
# controls and preserve them
assign ChannelSlider = 1 +preserve
assign EngageButton = 1 +preserve
end
The last two lines above configure the controls with default MIDI channel of 1 and the switcher engaged when the script is loaded. However, I have added the '+preserve' flag which means that if the user changes the slider or button and then saves that into a preset, then the next time that loads, the channel/button will not be restored to the defaults we put in our script, but to the values that were in place when it was saved.
Now, the above script will run but doesn't actually do any remapping. We now need to add the MIDI processing logic - we want to remap all channelised MIDI events to the selected channel but only if the Engage button is on. Outside of the 'if load' is where we add this:
# event processing logic
if MT != F0
if EngageButton == 1
math M0 = MT + ChannelSlider
math M0 = M0 - 1
end
end
The first if statement makes sure that the message type (MT) is a channelised event (ie. not a sysex/realtime event). The second if statement makes sure the Engage button is on.
There are two steps to rewriting the channel. The first math statement takes the current message type and adds the current value of the slider and puts that into the first byte of the current MIDI message (M0). In MIDI messages, the channels are numbered from 0 to F (0 to 15) but our channel slider range is 1 to 16; we therefore need to subtract 1 from the first byte to align correctly.
And there you have it, a working channel switcher. Here is the complete code:
# simple channel switcher
if load
# give controls q0 and q1 meaningful names
alias q0 ChannelSlider
alias q1 EngageButton
# configure our two controls
set ChannelSlider Channel $1 $16 +slider
set EngageButton Engage +toggle
# set the default values for the two
# controls and preserve them
assign ChannelSlider = 1 +preserve
assign EngageButton = 1 +preserve
end
# event processing logic
if MT != F0
if EngageButton == 1
math M0 = MT + ChannelSlider
math M0 = M0 - 1
end
end