Synth Event Handler - spessasus/spessasynth_lib GitHub Wiki

The Synthesizer Event Handler

The synthesizer supports event handling. For example, the MIDI Keyboard in the demo uses handling to visualize key-presses.

It is accessible via the synth.eventHandler property.

Table of contents

Managing the events

Adding event listener

synth.eventHandler.addEvent(name, id, callback);
  • name - the type of the event. refer to the table below.
  • id - unique id for the event listener. Can be anything, as long as it's unique.
  • callback. a function that gets called on the event. Callback takes an object argument. The properties depend on the event type. Refer to the table below.

Example:

// log every note played
synth.eventHandler.addEvent("noteon", "note-on-listener", data => {
    console.log(`Note ${data.midiNote} played for channel ${data.channel} with velocity ${data.velocity}.`)
})

Removing event listener

synth.eventHandler.removeEvent(name, id);
  • name - the type of the event.
  • id - the unique id of the event you wish to remove.

Example:

// remove the listener we set above
synth.eventHandler.removeEvent("noteon", "note-on-listener");

Delaying the event system

If you need to delay the events (for example, to sync up with something), you can use the timeDelay property.

synth.eventHandler.timeDelay = 5;

The delay time is specified in seconds. Set to 0 to disable (instant callback). Default is 0.

⚠️ **GitHub.com Fallback** ⚠️