HISE | MIDI Latch last notes

PHOTO EMBED

Thu Dec 12 2024 10:25:17 GMT+0000 (Coordinated Universal Time)

Saved by @swina #javascript

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