Logic Pro X: Use the JavaScript Event object

Use the JavaScript Event object

When the HandleMIDI function is called, an Event object represents one MIDI event and implements several methods you can call in your script.

The Event object is not instantiated directly, but is a prototype for the following event-specific methods, properties, and types.

Tip: You can use the JavaScript “new” keyword to generate a new instance of an Event object of any type.

Event methods

  • Event.send(): Send the event.

  • Event.sendAfterMilliseconds(number ms): Send the event after the specified value has elapsed (can be an integer or a floating point number).

  • Event.sendAtBeat(number beat): Send the event at a specific beat (floating point number) in the host timeline.

  • Event.sendAfterBeats(number beat): As above, but uses the beat value as a delay in beats from the current position.

  • Event.trace(): Print the event to the plug-in console. See Use the Trace object.

  • Event.toString(): Returns a String representation of the event.

Event properties

  • Event.toarticulationID(integer number): Sets the articulation ID from 0–254.

  • Event.channel(number): Set MIDI channel 1 to 16.

  • Event.beatPos: Retrieves the event’s exact beat position.

Event types

The Event object is a prototype for the following event types. All event types inherit the methods and channel properties described above.

The event types and their properties are passed to HandleMIDI as follows:

  • NoteOn.pitch(integer number): Pitch from 1–127.

  • NoteOn.velocity(integer number): Velocity from 0–127. A velocity value of 0 is interpreted as a note off event, not a note on.

  • NoteOff.pitch(integer number): Pitch from 1–127.

  • NoteOff.velocity(integer number): Velocity from 0–127.

  • PolyPressure.pitch(integer number): Pitch from 1–127.

  • PolyPressure.value(integer number): Define a pressure value from 0–127.

  • ControlChange.number(integer number): Controller number from 0–127.

  • ControlChange.value(integer number): Controller value from 0–127.

  • ProgramChange.number(integer number): Program change number from 0–127.

  • ChannelPressure.value(integer number): Aftertouch value from 0–127.

  • PitchBend.value(integer number): 14-bit pitch bend value from -8192–8191. A value of 0 is center.

  • TargetEvent.target(string): Create user definable MIDI CC messages or control plug-in parameters.

  • TargetEvent.value(float): Sets the target value.

Load the corresponding Tutorial setting to view the script in the Script Editor. This will help you to understand the syntax structure and layout of code and comments. See Use the Script Editor.

Tutorial script 7: Event Creation

This example replaces every received MIDI event with a modulation control change message.

  • Text following /* shows comments that explain the JavaScript code.

    Tip: You can use the JavaScript “new” keyword to generate a new instance of an Event object of any type.

    function HandleMIDI() { var cc = new ControlChange; /* make a new control change message */ cc.number = 1; /* set it to controller 1 (modulation) */ cc.value = 100; /* set the value */ cc.send(); /* send the event */ cc.trace(); /* print the event to the console */ }

Tutorial script 8: Event Modification

This example replaces every received MIDI event with a C3 note on/off. The example also uses the NeedsTimingInfo variable. See Use the JavaScript TimingInfo object.

  • Text following /* shows comments that explain the JavaScript code.

    Tip: You can use the JavaScript “new” keyword to generate a new instance of an Event object of any type.

    var NeedsTimingInfo = true; /* needed for .sendAfterBeats() to work */ function HandleMIDI() { var on = new NoteOn; /* make a new note on */ on.pitch = 60; /* set its pitch to C3 */ on.send(); /* send the note */ var off = new NoteOff(on); /* make a note off using the note on to initialize its pitch value (to C3) */ off.sendAfterBeats(1); /* send a note off one beat later */ }

Tutorial script 15: Control Plug-ins

You can create user-definable MIDI CC messages, or you can control plug-in parameters. TargetEvent reads the parameter to be modified from a menu where the user can select a destination MIDI CC. Alternately, you can use the Learn Plug-In Parameter feature to assign any plug-in parameter inserted after (below) Scripter in the same channel strip. The chosen destination is saved with the plug-in setting.

  • Text following /* shows comments that explain the JavaScript code.

    Tip: You can use the JavaScript “new” keyword to generate a new instance of an Event object of any type.

    TargetEvent properties: TargetEvent.target(string) /* Name of target menu entry */ TargetEvent.value(float) /* Value of set Target from 0.0 to 1.0 */

    The code shown below controls any plug-in parameter with the modwheel. To test the function in Tutorial script 15, insert any plug-in or software instrument on the same channel and run the script. Choose Learn plug-in parameter in the menu, then click any plug-in parameter to control it with the modwheel.

    To create a menu for the modwheel target, name the menu entry and set it to a “target” type.

    var PluginParameters = [ /* parameter 0 */ { name:"Modwheel Target", type:"target" }];

    HandleMIDI is called every time Scripter receives a MIDI event in Tutorial script 15. The code shown below remaps the modulation wheel to the target chosen in the menu.

    function HandleMIDI(incomingEvent) { /* remap modulation to target selected in menu and check for incoming CC event with number 1 (Modwheel) */ if ((incomingEvent instanceof ControlChange) && (incomingEvent.number == 1)) { var newEvent = new TargetEvent(); /* create new Target event */ newEvent.target = "Modwheel Target"; /* name the menu entry to be used by this event */ newEvent.value = incomingEvent.value / 127; /* rescale from 0..127 to 0.0...1.0 */ newEvent.send(); /* send the event */ } else { /* send all other events */ incomingEvent.send(); }; };
Published Date: Aug 9, 2019
Helpful?