MIDI scripter dropping events after x amount of loops

hello, my drum riff script seems to work up until the riff is repeated (i think around) 32 times. then some notes like snare... start to dissappear. if i hit the global stop and play again it works. also if i reload the script it doesn't help which makes me think... but come up with the road to nowhere. any ideas?

^^^ check out full script in additional text above

const riffs = [
['11th Hour',[[1,49,114,1,0],[0,49,0,1.25,0],[1,46,89,1.501,20],[1,36,101,1.748,0],…],
['Black Veil',…
];


var riffData = riffs[0][1];
var lastStep = 0;
var isRandom = true;
var randomRiffPlays = 0;
var repeats = 0;

var NeedsTimingInfo = true;
var ResetParameterDefaults = true;


function ParameterChanged(index, value)
 {
    var name = PluginParameters[index].name;
    Trace("parameterChanged($index:"+index+" ["+name+"], $value:"+value+")");
    if (name == "Repeats")
     {
        repeats = value;
        if (value < randomRiffPlays && isRandom)
         {
            nextRandomRiff();
         }
     }
    if (name == 'Riff')
     {
        if (value == 0)
         {
            isRandom = true;
            nextRandomRiff();
         }
        else
         {
            isRandom = false;
            // account for 'random' as first value.
            setRiff(value - 1);
         }
     }
 }

function nextRandomRiff()
 {
    randomRiffPlays = 0;
    setRiff(Math.floor(Math.random() * riffs.length));
 }

function setRiff(riff)
 {
    Trace("Riff Changed To: "+riff+": "+riffs[riff][0]);
    riffData = riffs[riff][1];
    SetParameter('RiffName', riff);
    MIDI.allNotesOff(); 
 }

function ProcessMIDI()
 {
    var musicInfo = GetTimingInfo();

    if (musicInfo.playing)
     {
        var blockStartBeat = musicInfo.blockStartBeat;
        var blockEndBeat = musicInfo.blockEndBeat;
        var riffStart = blockStartBeat - (blockStartBeat % 32);
        var length = riffData.length;
        var i = 0;
        for (i; i < length; i++)
         {
            var data = riffData[i];
            var pos = data[3] + riffStart;
            // its before the process block
            if (pos < blockStartBeat)
             {
                continue;
             }
            // its after the process block
            if (pos > blockEndBeat)     
             {
                break;
             }
            //Trace("["+data[0]+","+data[1]+","+data[2]+","+data[3]+","+data[4]+"] ");
            if (data[0] == 1)
             {
                note = new NoteOn();
             }
            else
             {
                note = new NoteOff();
             }
            note.pitch = data[1];
            note.velocity = data[2];
            note.articulationID = data[4];
            note.sendAtBeat(pos);

            if (isRandom == true && lastStep > i)
             { // check if next random riff should be set
                if (randomRiffPlays >= repeats)
                 {
                    nextRandomRiff();
                    length = riffData.length;
                 }
                else
                 {
                    randomRiffPlays++;
                 }
                lastStep = 0;
                break;
             }
            lastStep = i;
            // no more riff events but might still be processing so begin next riff
            if (i + 1 >= length)
             {
                riffStart+= 32;
                i = 0;
             }
         }
     }
 }

//define the UI -----------------------------------------------------------------

var PluginParameters = [];

//add a number of riff repeats
PluginParameters.push({
    name: "Repeats",
    type: "linear",
    minValue: 0,
    maxValue: 8,
    numberOfSteps: 8,
    defaultValue: 1,
    unit: "x"
 });

// add select riff control
PluginParameters.push(
 {
    name: "Riff",
    type: "menu",
    valueStrings: [
           "Random",
        riffs[0][0],
        riffs[1][0],
        riffs[2][0],
        riffs[3][0],
        riffs[4][0],
        riffs[5][0],
        riffs[6][0],
        riffs[7][0]
    ],
    defaultValue: 0
 });
PluginParameters.push(
 {
    name: "RiffName",
    type: "menu",
    valueStrings: [
        riffs[0][0],
        riffs[1][0],
        riffs[2][0],
        riffs[3][0],
        riffs[4][0],
        riffs[5][0],
        riffs[6][0],
        riffs[7][0]
    ],
    defaultValue: 0,
    disableAutomation: true
 });

Posted on Dec 11, 2022 7:11 AM

Reply
2 replies

Dec 11, 2022 8:54 AM in response to Belfie

I just did a trace of all sent events and apparently the riffs are identical every loop... but it's obvious when listening that it drops up to half of the events..


glitch... my iMac, macOS, mainstage???


can anyone test it on their machine


if you paste the script into a plugin on a software drum kit channel and select a riff in the scripter UI (i use "black veil") then hit play, you'll notice at the 20th loop (works any tempo so u can ramp it up to get there faster) that some notes don't get played.. more drop off every loop

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

MIDI scripter dropping events after x amount of loops

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.