Pilcrow Records

Scripter Javascript Tutorial

javascriptmusiclogicscripterbookexcerpttutorial

67 Looping Through Arrays

In the following example, an array is used to store which notes are currently being played in the track. A loop is used to remove notes as NoteOff events are captured. Like the larger example from before, this gets into a function—HandleMIDI()—to provide context for what is happening within the loop itself and the example is a fairly common task when working in Scripter. The actual loop is in lines 10–16:

var activeNotes = [];

function HandleMIDI(event) {

	if ( event instanceof NoteOn ) {
		activeNotes.push(event);
		
	} else if ( event instanceof NoteOff ) {
		var length = activeNotes.length;
		for ( var thisNote = 0; thisNote < length; thisNote++ ) {
			var note = activeNotes[thisNote];
			if ( note.pitch == event.pitch ) {
				activeNotes.splice( thisNote, 1 );
				break;
			}
		}
	}
	
	Trace( activeNotes.length );
}

In the above example:

Active notes example workflow.