Free Wah Auto-Engage Script for Plug’n Script

When controlling a virtual Wah pedal with an expression pedal via MIDI, it is convenient to have a built-in tip switch to activate the Wah effect when pushing the pedal.

However what if all you have is a regular expression pedal without an extra tip switch? Call Plug’n Script to the rescue! Whether you are playing guitar, bass or keyboards (or whatever instrument you want to use a Wah pedal for), you will want to try this script!

Here is an auto-engage script that will automatically send an extra MIDI CC event to enable the effect when the pedal moves above a threshold, and another CC event to disable it when the pedal stays under the threshold for enough time.

/** \file
* auto engage MIDI CC
*/

#include "./library/Midi.hxx"

// metadata
string name="MIDI Auto-Engage";
string description="Create MIDI CC on/off from moving pedal events";

// parameters
array<string> inputParametersNames={"Channel", "Source CC","Dest CC","Threshold","Delay"};
array<double> inputParameters(inputParametersNames.length);

array<double> inputParametersMin={0,1,1,1,0};
array<double> inputParametersMax={16,127,127,127,2000};
array<double> inputParametersDefault={0,1,7,4,500};
array<int> inputParametersSteps={17,128,128,128,128};
array<string> inputParametersFormats={".0", ".0", ".0" , ".0" ,".0"};
array<string> inputParametersUnits = { "", "", "" , "" ,"ms" };


double pauseTimeInSamples = 0;
bool enabled = false;
uint8 lastCCvalue=0;

// local variables
MidiEvent tempEvent; ///< defining temp object in the script to avoid allocations in time-critical processBlock function

/* per-block processing function: called for every block with updated parameters values.
*
*/
void processBlock(BlockData& data)
{
    // reading parameters
    uint8 sourceChannel = int8(inputParameters[0]+.5);
    uint8 sourceCC = int8(inputParameters[1] + .5);
    uint8 destCC = int8(inputParameters[2]+.5);
    uint8 threshold = int8(inputParameters[3] + .5);

    // iterating on MIDI events
    for(uint i=0;i<data.inputMidiEvents.length;i++)
    {
        // check MIDI events from incoming channel
        uint8 eventChannel=MidiEventUtils::getChannel(data.inputMidiEvents[i]);
        if((eventChannel==sourceChannel || sourceChannel==0) 
        && (MidiEventUtils::getType(data.inputMidiEvents[i])==kMidiControlChange) 
        && MidiEventUtils::getCCNumber(data.inputMidiEvents[i])==sourceCC)
        {
            // received MIDI CC => no pause
            pauseTimeInSamples = 0;

            // update last cc value, and create ON event if not enabled yet and value above threshold
            tempEvent = data.inputMidiEvents[i];
            lastCCvalue = MidiEventUtils::getCCValue(tempEvent);
            if (!enabled && lastCCvalue >= threshold)
            {
                enabled = true;
                MidiEventUtils::setCCNumber(tempEvent, destCC);
                MidiEventUtils::setCCValue(tempEvent, 127);
                data.outputMidiEvents.push(tempEvent);
            }
         }
    }

    // below threshold?
    if (lastCCvalue < threshold)
    {
        // send off event if paused since enough time
        if (enabled && (pauseTimeInSamples >= inputParameters[4]*.001 * sampleRate))
        {
            enabled = false;
            tempEvent.timeStamp = 0;
            MidiEventUtils::setCCNumber(tempEvent, destCC);
            MidiEventUtils::setCCValue(tempEvent, 0);
            data.outputMidiEvents.push(tempEvent);
        }

        // update pause time
        pauseTimeInSamples += data.samplesToProcess;
    }
}

Copy and paste this script into a text file, save it with the cxx extension into the Plug’n Script’s user scripts folder, and load it into Plug’n Script. You can also get it here on our GitHub scripts repository.

You can choose the MIDI channel and the source CC number (the one used by the expression pedal), as well as the destination CC number (for the enable/disable event). Threshold and delay time let you choose the time to wait before disabling as well as the position where the pedal should be to disable the effect (usually a small value so that the effect gets disabled when the Wah is down).

Enjoy!

>discuss this topic in the forum

Leave a Reply

Your email address will not be published. Required fields are marked *