MIDI Scripter – advanced randomness

This is a simple Script for Logic Pro X to randomize Notes.

MIDI Scripter Demo


/*
		Probability Phrase.pst
		
		This script uses probability to determine if an incoming NoteOn event 
		should be sent. (It is based on "Probability Gate").
		
		The probability can be set via the "Probability" PluginParameter.
		
		If one note is played also the next notes will be sent. Use 
		the "Phraselength" PluginParameter to set the amount of notes. That
		results in a nice phrase instead of a single note.
		
		All notes are stored in an Array as soon as they are played. 
		With a probability the note played will be replaced by a random 
		note from the array. Use the PluginParameter "Note Variance" to 
		set the probability.
		
*/

// Set this to TRUE to receive a lot of information :)
const DEBUG = true;

var PROBABILITY = 50;
var PHRASELENGTH = 3;
var NOTEVARIANCE = 10;
var PHRASECOUNT = PHRASELENGTH;
var NOTES = []



function HandleMIDI(event) {

    if (event instanceof NoteOn) {

        if (DEBUG) {
            Trace("\r\nNoteOn Event recognized.");
        }

        if (NOTES.indexOf(event.pitch) === -1) {
            NOTES.push(event.pitch);
            Trace("New Note stored: " + MIDI.noteName(event.pitch));
        }

        if (eventChangeNoteValue()) {
            var newnote = Math.floor(Math.random() * NOTES.length);
            Trace("Note changed: " + MIDI.noteName(event.pitch) + " to " + MIDI.noteName(NOTES[newnote])+".");
            event.pitch = NOTES[newnote];

        }

        if (PHRASECOUNT > 0) {
            event.send();
            PHRASECOUNT = PHRASECOUNT - 1;
            if (DEBUG) {
                Trace("Note sent from phrase: " + MIDI.noteName(event.pitch) + ". Notes left in phrase: " + PHRASECOUNT+".");
            }
        } else if (eventShouldSend()) {
            event.send();
            PHRASECOUNT = PHRASELENGTH;
            if (DEBUG) {
                Trace("Note sent (new): " + MIDI.noteName(event.pitch) + ". Phraselength set to " + PHRASELENGTH+".");
            }
        } else {
            if (DEBUG) {
                Trace("Note dismissed.");
            }
        }
    } else {
        event.send();
    }
}

function ParameterChanged(param, value) {
    if (param === 0) {
        PROBABILITY = value;
    }
    if (param === 1) {
        PHRASELENGTH = value;
    }
    if (param === 2) {
        NOTEVARIANCE = value;
    }
}

function eventChangeNoteValue() {
    return (Math.ceil(Math.random() * 100) <= NOTEVARIANCE) ? true : false;
}

function eventShouldSend() {
    return (Math.ceil(Math.random() * 100) <= PROBABILITY) ? true : false;
}

var PluginParameters = [{
        name: "Probability",
        type: "linear",
        minValue: 0,
        maxValue: 100,
        numberOfSteps: 100,
        defaultValue: 50,
        unit: "%"
    },
    {
        name: "Phraselength",
        type: "linear",
        minValue: 1,
        maxValue: 10,
        numberOfSteps: 9,
        defaultValue: 3,
        unit: ""
    },
    {
        name: "Note Variance",
        type: "linear",
        minValue: 0,
        maxValue: 100,
        numberOfSteps: 100,
        defaultValue: 10,
        unit: "%"
    }
];

Leave a Comment