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
});