Events and callbacks
<< Click to Display Table of Contents >> Navigation: Introduction > Events and callbacks |
Generally, GPScript is event driven. It doesn't do anything until something "happens" such as your playing a key, turning a knob, switching to another rackspace or to a different variation. A certain amount of time passing can also count as something "happening". All of these examples are known as events and when you write code that can respond to an event, that code is called a callback. GPScript can currently handle the following events:
•Initialization
•Rack activation
•Rack deactivation
•Variation change
•MIDI events (notes, CC values, aftertouch, etc.)
•Widget changes
•Plugin parameter changes
•LFOs (also known as Time Generators)
•OSC Messages
•Keystrokes (Gig file script only)
A simple example
var
pi : double
initialization
pi = 3.14159
Print(pi)
end
So, what's going on here? Well, first we declared a variable called "pi" and whose value must be a floating-point number (double). In GPScript all variables must be declared, and they must have a type. The type of a variable defines the kinds of values that the variable can represent and also determines what kinds of operations can be applied to it. So called strongly typed languages make it easier to prevent certain kinds of common bugs.
The keyword initialization defines the beginning of a callback that will be executed just once when a rackspace is initially loaded. In this example we simply assign a value to the variable and "print" its value. In the context of Gig Performer, printing means to show the item in a special window called the Script Logger. The keyword end completes the definition of this callback.
A more interesting example
// Declare a global variable
var A800 : MidiInBlock // A800 is a named block in Gig Performer
// Callback when a note event (on or off) is received
On NoteEvent (m : NoteMessage) From A800
SendNow(A800, m) // Send the note to the A800 MIDI In block
SendNow(A800, WithTranspose(m, 5)) // Also send a transposed note
End
Like before, the first thing we're doing is declaring a variable. But unlike the example above that just used a floating-point number, the variable A800 is declared with the type MidiInBlock. That type represents a Gig Performer MIDI In plugin block. Consequently, the name of the variable, in this case, A800, is significant. For this script to work there must be a MIDI In block in your rackspace and its scripting name, more properly known as a handle, must be A800. So in this example, we are binding a variable name to an entity in Gig Performer.
Note the handle name on the upper left and the {S} on the upper right, the latter indicating that the handle is available for use in GPScript.
Next, we define a callback. A callback is a block of code that will run automatically when the event associated with that callback occurs. The callback above responds to MIDI note events (both NoteOn and NoteOff events) that arrive at the MIDI In block named A800. There are other callbacks that respond to other MIDI events such as Control Change events or Aftertouch. The actual message itself is accessed through the incoming parameter argument m which has the type NoteMessage.
Once you define a callback for a MIDI event you have full responsibility for that event. GPScript will not automatically send the MIDI event back to the MIDI In block which means that the MIDI In block will not forward the event to whatever synth plugins are connected to it. So if you want the incoming note to be played, you have to explicitly send it out, which we do with the statement SendNow(A800, m). Immediately afterwards, we send the note again, after transposing it by 5 semitones. Voila, instant chords.