Frank Faruk Logo

Creating a MIDI Note Range Filter with a Probability Gate

Shaping MIDI Data with Precision and Randomness

Working on music production can sometimes involve shaping MIDI data to fit specific creative needs. Today, we're going to take a look at a small but powerful MIDI script designed to filter out notes based on pitch range while also introducing an element of probability to note playback. This script was built in collaboration with Frank Faruk Ceviz & ChatGPT, and it offers a practical way to add a human touch of randomness to your MIDI sequences. Let's dive into what it does and how it works.

Overview

The script is a MIDI Note Range Filter with a Probability Gate. What does that mean? Essentially, it allows you to control which notes are allowed to pass through based on their pitch, while also giving you the option to apply a probability setting that determines whether the filtered notes will actually be sent through.

This combination is particularly handy when you want to limit the note range that an instrument plays, but also want to introduce some variability for more dynamic performances. Imagine applying this to a drum kit, where only certain drums are triggered, and only some of the time—this script can help you achieve that level of nuanced control.

Breaking Down the Script

The script is built using JavaScript, and it has three main parameters for user control:

  1. High Note: This defines the highest MIDI note that will be allowed to pass through. Notes above this value will be filtered out.
  2. Low Note: This parameter sets the lowest MIDI note allowed to pass. Any notes below this threshold will be removed.
  3. Probability: This is where the fun happens. This setting allows you to determine, in percentage terms, how likely it is for a note within the specified range to actually be sent. A value of 100% means all notes in the range will pass, whereas a lower value means that fewer notes will make it through, adding an element of randomness.

Code

Go to the MIDI FX slot on the Software Instrument Track you created. Click on it, then select Scripter from the MIDI FX options. The Scripter plugin allows you to add and customize JavaScript-based MIDI effects.



        /* MIDI Note Range Filter with Probability Gate
Built with Frank Faruk Ceviz & ChatGPT */

// Define MIDI plug-in controls
var PluginParameters = [
  {
    name: "High Note",
    type: "lin",
    minValue: 0,
    maxValue: 127,
    numberOfSteps: 127,
    defaultValue: 127
  },
  {
    name: "Low Note",
    type: "lin",
    minValue: 0,
    maxValue: 127,
    numberOfSteps: 127,
    defaultValue: 0
  },
  {
    name: "Probability",
    type: "linear",
    minValue: 0,
    maxValue: 100,
    numberOfSteps: 100,
    defaultValue: 50,
    unit: "%"
  }
];

var probability = 50;

function HandleMIDI(event) {
  // Get the user-set low and high note values from the sliders
  var lowNote = Math.floor(GetParameter("Low Note"));
  var highNote = Math.floor(GetParameter("High Note"));

  // Check if the event is a note on or note off event
  if (event instanceof NoteOn || event instanceof NoteOff) {
    // Get the MIDI note number of the event
    var noteNumber = event.pitch;

    // Check if the note is outside the user-set range
    if (noteNumber < lowNote || noteNumber > highNote) {
      // If so, send an "all notes off" message to stop the note from playing
      MIDI.allNotesOff();

      // And don't pass the event through
      return;
    }
  }

  // Check if the event should be sent based on probability
  if (eventShouldSend()) {
    // Send the event
    event.send();
  }
}

function ParameterChanged(param, value) {
  if (param === 2) {
    probability = value;
  }
}

function eventShouldSend() {
  return (Math.ceil(Math.random() * 100) <= probability);
}

// Pass events through and filter out notes outside the user-set range