Badger
Badger

Reputation: 1

When I add this line JS code breaks. Apple Mainstage Scripter Plugin

Can anyone spot the error in this Javascript? (It's for the Apple Mainstage scripter plugin). I'm trying to transpose notes down an octave that fall below a certain "fold point" note.

When I add the "event.pitch -= 12;" line the next else statement is not allowed. I'm wondering if this needs to be wrapped in a function somehow. I'm new to coding and have mostly used lua but the scripting plugin in Mainstage uses Javascript. Many thanks for your time.

 // transpose notes below fold point


  var activeNotes = [];


function HandleMIDI(event)

{

if (event instanceof NoteOn) {

    if (event.pitch < GetParameter('Fold Point'))

    event.pitch -= 12; //        when I add this line the 'else' two lines below is not allowed

        event.send();

    else {

        activeNotes.push(event);

        event.send()

    }

}

else if (event instanceof NoteOff) {

    for (i=0; i < activeNotes.length; i++) {

        if (event.pitch == activeNotes[i].pitch) {

            event.send();

            activeNotes.splice(i,1);

            break;

        }

    }

}

else { // pass non-note events through

    event.send();

}

  }


  var PluginParameters = [


{   name:'Fold Point', type:'lin',

    minValue:0, maxValue:127, numberOfSteps:127, defaultValue:30}

  ];

Upvotes: 0

Views: 103

Answers (1)

Jason Smith
Jason Smith

Reputation: 1209

You need to add curly braces when the if statement covers more than one line. Like this:

if (event instanceof NoteOn) {
  if (event.pitch < GetParameter('Fold Point')) {
    event.pitch -= 12; //        when I add this line the 'else' two lines below is not allowed
    event.send();
  } else {
    activeNotes.push(event);
    event.send()
  }
}

Notice that I put a curly brace after the second "if" and before the "else".

Upvotes: 1

Related Questions