// Array to store active notes for latch mode reg activeNotes = []; // Variable to toggle latch mode reg isLatchEnabled = false; //To limit the number of last notes to latch const var lastEventsLimit = 2 reg activeNotes = []; // Variable to toggle latch mode reg isLatchEnabled = false; const var latchToggle = Content.getComponent("LatchToggle"); // Function to reset latched notes (e.g., on toggle off) inline function resetLatchedNotes() { if ( !activeNotes.length ) { return; } for ( n=0 ; n < activeNotes.length ; n++) { Synth.noteOffByEventId(Synth.playNote(activeNotes[n],1)); }; activeNotes.clear(); } inline function onLatchToggleControl(component, value) { if (value) { latchToggle.set("text", "Latch On"); isLatchEnabled = true; } else { latchToggle.set("text", "Latch Off"); isLatchEnabled = false; resetLatchedNotes(); } }; Content.getComponent("LatchToggle").setControlCallback(onLatchToggleControl); // Function to handle Note On events function onNoteOn() { if (isLatchEnabled) { // Check if the note is already latched if (activeNotes.length > lastEventsLimit ) { //Synth.noteOffByEventId(Synth.playNote(activeNotes[0],1)); resetLatchedNotes(); activeNotes = []; // Add the note to the active notes array activeNotes.push(Message.getNoteNumber()); } else { activeNotes.push(Message.getNoteNumber()); } } } // Function to handle Note Off events function onNoteOff() { if (isLatchEnabled) Message.ignoreEvent(true); }