SCRIPTING API β

Updated for version 0.1.9

SCRIPTING API βLua Functionsbase ⇢table ⇢math ⇢string ⇢Global FunctionsUtililty FunctionsMessage FunctionsMIDI MessagesSimple OSC MessagesComplex OSC MessagesObjectsControlFieldsFunctionsCallback FunctionsRectangleFieldsConstructor functionsFunctionsColorFieldsConstructor functionsOperatorsStatic functionsVector typesFieldsConstructor functionsFunctionsOperatorsEnumerationsControlTypeShapeOutlineStyleButtonTypeRadioTypeResponseOrientationAlignHAlignVPointerStatePointerPriorityValueTypeValueFieldValuePropertyCursorDisplayMIDIMessageTypeConstantsColorsControl Properties and ValuesCommonPropertiesValuesBOXPropertiesBUTTONPropertiesValuesLABELPropertiesValuesTEXTPropertiesValuesFADERPropertiesValuesXYPropertiesValuesRADIALPropertiesValuesENCODERPropertiesValuesRADARPropertiesValuesRADIOPropertiesValuesPAGERPropertiesValuesExamplesControl Callback FunctionsSending MIDI MessagesSending OSC MessagesDetect Control 'Double-tap'Send Periodic MessageSend Accelerometer Data


TouchOSC's scripting layer is based on the Lua 5.1 language and virtual machine with custom additions and modifications.

Scripts can be added to all controls in a document and at the document root level. Each control's script will be executed in its own Lua context.

 

 

Lua Functions

The following Lua standard library functions are available.

 

base

Only the following Lua base library functions are available:

 

table

All standard Lua table library functions are available plus the following additions:

Returns a new sequential table created from the elements provided.

Same as the Lua base library function unpack.

 

math

All standard Lua math library functions are available plus the following additions:

Returns min(max(x, minVal), maxVal) where x is the first parameter and minVal and maxVal the second and third parameters.

 

string

All standard Lua string library functions are available.

 

 

Global Functions

TouchOSC provides the following global functions.

 

Utililty Functions

Returns a table with the application's version as major minor patch build number values stored in sequential order.

Returns the number of milliseconds since application start.

Returns a table with the current local date's year month day tzd number values stored in sequential order. tzd refers to the timezone differential in seconds.

Returns a table with the current local time's hour minute second millisecond number values stored in sequential order.

Returns true if the host device provides an accelerometer sensor, false otherwise.

Returns a list of three number values that are sampled from the host device's accelerometer sensor. If no accelerometer sensor is available, the values will be all zero.

Returns true if the host device provides a gyroscope sensor, false otherwise.

Returns a list of three number values that are sampled from the host device's gyroscope sensor. If no gyroscope sensor is available, the values will be all zero.

Returns the current battery charge level as a number ranging from 0.0 to 1.0 on mobile devices, 1.0 otherwise.

Returns a number that is the 32-bit integer representation created from the four byte number values supplied.

Returns a number that is the 32-bit floating point representation created from the four byte number values supplied.

 

Message Functions

TouchOSC provides the following functions to send MIDI and OSC messages.

 

MIDI Messages

Send a MIDI message on one or multiple configured connections.

The first argument table is a list of byte values that make up the MIDI message.

The optional second argument table is a list of boolean values that specify which connections to send the message on. If the argument is omitted, the default is to broadcast the message on all configured connections. If the table has less than five elements, the omitted elements default to false.

See Sending MIDI Messages for example code.

 

Simple OSC Messages

Send an OSC message on one or multiple configured connections.

string is the path of the OSC message to be sent.

The optional argument values ... will be auto-converted to boolean, float or string OSC types and added to the OSC message as arguments.

Note that argument values are never auto-converted to integer OSC types as scripts do not treat floating point and integer numbers as separate types. Use the complex OSC message send function instead.

The optional last argument table is a list of boolean values that specify which connections to send the message on. If the argument is omitted, the default is to broadcast the message on all configured connections. If the table has less than five elements, the omitted elements default to false.

See Sending OSC Messages for example code.

 

Complex OSC Messages

Sends an OSC message on one or multiple configured connections.

The first argument table is a list that represents the OSC message to be sent, where the first element is the path string of the message, and the second element is a list of argument tables with tag and value keys for each argument:

Each argument value will be converted to an OSC type according to the type tag string provided:

TagOSC Type
TBoolean true
FBoolean false
NNil
IInfinitum
iint32
ffloat32
sstring
bblob

If the tag key is omitted, the value will be auto-converted the same way as when sending simple OSC messages.

The T F N and I types do not need a value to be specified.

The b OSC blob type expects the value to be a list of byte values making up the blob data.

The optional second argument table is a list of boolean values that specify which connections to send the message on. If the argument is omitted, the default is to broadcast the message on all configured connections. If the table has less than five elements, the omitted elements default to false.

See Sending OSC Messages for example code.

 

 

Objects

TouchOSC defines the following objects to represent its internal and native types.

 

Control

A control object represents a reference to a single control contained within a TouchOSC document.

Using this reference most of a control's properties and values can be queried and set. From within a control's script, the self reference can be used to refer to the control's own fields and functions.

Each control is assigned a unique ID on creation that remains unchanged during and between application runs, document save/load and editor network transfer.

All controls live in a document tree hierarchy starting at the document root. All controls except for the root have a reference to a parent control, and some control types are containers for child controls.

During compilation each script will be checked for the definitions of any of the callback functions listed below, which serve as the main customization points during the various stages of TouchOSC's processing of an application frame.

 

Fields

A unique ID string, constant for a control's lifetime, between document load/save and during editor network transmission.

Control type numeric constant, one of the ControlType enumeration values.

The control's position in its parent list of child controls, 1-n for regular controls, 0 for the document root.

A reference to the control's parent Control object, or nil for the document root.

A reference to TouchOSC's native container object for a list of the control's child Control objects. The container can be indexed by control name string or index number. Control names are user assignable and not unique.

UsageDescription
control.children.name control.children[name]Returns the first child control with name name or nil if none is found. Indexing by name is equivalent to calling control:findByName(name, false).
control.children[1-n]Returns the child control at index 1-n or nil if none is found.
#control.childrenReturns the number of child controls.

 

A reference to TouchOSC's native container object for a list of the control's properties with no guaranteed ordering. The container can be indexed by property name string or index number. Property names are unique.

UsageDescription
control.properties.name control.properties[name]Returns the current value of the property with name name or nil if none is found.
control.properties[1-n]Returns the current value of the property at index 1-n or nil if none is found.
control.properties.keysReturns a table of all property names in the list.
#control.propertiesReturns the number of properties in the list.

NOTE For convenience, indexing a control reference directly using control.name or control[name], where name is not one of the field or function names listed here, will implicitly index the control's property list with control.properties[name].

Therefore control.color and control.properties.color will refer to the same property value.

See Control Properties and Values for a list of possible properties for each control type.

 

A reference to TouchOSC's native container object for a list of the control's values with no guaranteed ordering. The container can be indexed by value name string or index number. Value names are unique.

UsageDescription
control.values.name control.values[name]Returns the current value of the control value with name name or nil if none is found.
control.values[1-n]Returns the current value of the value at index 1-n or nil if none is found.
control.values.keysReturns a table of all value names in the list.
#control.valuesReturns the number of values. in the list.

See Control Properties and Values for a list of possible values for each control type.

 

A list containing one table for each pointer currently associated with the control during the current frame with the following table keys per pointer:

KeyDescription
IDThe numeric ID of the pointer. Constant during the pointers lifetime.
xThe x position of the pointer.
yThe y position of the pointer.
stateThe current state of the pointer, one of the possible values of the PointerState enumeration.
createdThe time the pointer event began, in milliseconds as returned by the getMillis global function.
modifiedThe time of the last modification of this pointer, in milliseconds as returned by the getMillis global function.

Each pointer progresses through the states in the PointerState enumeration during its lifetime:

 

Functions

Returns a value of the control value with name string or nil if none is found.

The parameter field can be one of the possible values of the ValueField enumeration and determines which value is returned:

Invoking the function with field parameter ValueField.CURRENT is equivalent to referencing control.values.name.

See Control Properties and Values for a list of possible values for each control type.

 

Returns the value of the property property of the control value with name string or nil if none is found.

The parameter property can be one of the possible values of the ValueProperty enumeration and determines which property value is returned:

See Control Properties and Values for a list of possible values for each control type.

 

Set the value of the property property of the control value with name string.

The parameter property can be one of the possible values of the ValueProperty enumeration with the exception of ValueProperty.TYPE and determines which property value is set.

See the control:getValueProperty function above for a description of the possible value properties.

See Control Properties and Values for a list of possible values for each control type.

 

Invokes the onReceiveNotify callback function on another control.

The parameter string and an optional parameter value will be copied to the receiving control's Lua context and passed to the onReceiveNotify callback function, only if that callback function is defined in the receiving control's script. Calling the function on self has no effect.

The optional parameter value can be of any Lua type except for function, userdata or thread.

 

Returns the child Control object with ID string or nil if none is found. The optional boolean parameter determines if the search will be recursive and descend the child control hierarchy, defaults to false.

 

Returns the first child Control object with name string or nil if none is found. The optional boolean parameter determines if the search will be recursive and descend the child control hierarchy, defaults to false.

 

Returns a list of child Control objects with name string or an empty list if none are found. The optional boolean parameter determines if the search will be recursive and descend the child control hierarchy, defaults to false.

 

Callback Functions

If any of the following functions are defined in a control's script, these callback functions are invoked during the various stages of processing of an application frame.

When considering a script function for registration as a callback, the parameter declarations are optional and the function will be called regardless of the parameters being omitted or not.

See Control Callback Functions for example implementations.

 

Called once per application frame after all processing of user input and received messages has completed.

 

Called after any of the control's values has changed, once for each changed value, and before any further processing as a result of the change.

The parameter string is the name of the value that has changed. It is valid to set the changed value again from inside the callback, but note that the callback will not be invoked again as a result.

Returning a value that evaluates to true from this callback will end any further processing TouchOSC would normally do as a result of the change (ie sending of messages).

 

Called after processing of user input is complete and all active pointers (mouse cursor or touch input) have been mapped and assigned to any controls, and before any further processing of the pointer state and internal control behavior in response to the pointer input is evaluated.

Will only be invoked if there are any pointers associated with the control during the current frame.

The table passed as parameter to the callback contains a list of one or more pointers that have been selected as the significant event input according to the control's configuration and do not necessarily include all pointers currently associated with the control.

For example, a button type control will commonly only be interested in a single significant touch input, which will be selected by the application and passed to the control for processing based on the control's configuration.

To access all pointers currently associated with a control access the control.pointers field.

Returning a value that evaluates to true from this callback will end any further processing TouchOSC would normally do for the current control as a result of the input (ie changing a control's values).

For a description of the pointer table format and pointer states see the control.pointers field.

 

Called after receiving a MIDI message and determining that the control should be a receiver of the message according to the routing table, and before any further evaluation or processing of potential changes to a control's values or properties.

Returning a value that evaluates to true from this callback will end any further processing TouchOSC would normally do for the current control as a result of receiving the message (ie changing a control's values or properties).

NOTE If it is defined, the document root's onReceiveMIDI callback function will always be invoked first, and if a value that evaluates to true is returned from the callback, processing of the message will end, it will not be passed along to any other controls in the routing table and no further callbacks will be invoked.

For the format of the message and connections parameters see the sendMIDI function.

 

Called after receiving an OSC message and determining that the control should be a receiver of the message according to the routing table, and before any further evaluation or processing of potential changes to a control's values or properties.

Returning a value that evaluates to true from this callback will end any further processing TouchOSC would normally do for the current control as a result of receiving the message (ie changing a control's values or properties).

NOTE If it is defined, the document root's onReceiveOSC callback function will always be invoked first, and if a value that evaluates to true is returned from the callback, processing of the message will end, it will not be passed along to any other controls in the routing table and no further callbacks will be invoked.

For the format of the message and connections parameters see the sendOSC function for complex messages.

 

Called as a result of the control's notify function begin called by another control.

The parameters string and an optional value will be copied from the calling control's Lua context to the receiving control's Lua context and passed as parameters to the callback function.

Please note that because the parameter values have to be copied between Lua execution contexts, and because this introduces some overhead, it is advisable not to invoke the notify function from inside the update function every frame but only in response to events such as received messages, changed pointer input or value changes.

 

 

Rectangle

A rectangle object native to TouchOSC. Will be returned and can be passed anywhere a rectangle is required.

 

Fields

The x position of the rectangle.

The y position of the rectangle.

The width of the rectangle.

The height of the rectangle.

 

Constructor functions

Returns a new rectangle with

  1. position and size set to (0,0).
  2. position and size copied from another Rectangle object.
  3. position set to (0,0) and size set to the two numbers.
  4. position set to the first pair of numbers and size set to the second two numbers.

 

Functions

Tests if the point at position (number, number) is contained within the rectangle and returns a boolean value.

 

 

Color

A color object native to TouchOSC. Will be returned and can be passed anywhere a color is required. Color components are stored as floating point values ranging from 0.0 to 1.0.

 

Fields

The red component of the color.

The green component of the color.

The blue component of the color.

The alpha component of the color.

 

Constructor functions

Returns a new color with

  1. all components initialized with 0.0.
  2. all components copied from another Color object.
  3. all components initialized with number.
  4. rgb components initialized with the first number, the a component initialized with the second number.
  5. rgb components initialized with the three numbers, the a component initialized with 1.0
  6. rgba components initialized with the four numbers provided.

 

Operators

All operators operate component-wise and return a new color object.

 

Static functions

Returns a hexadecimal string representation of the color in the format RRGGBBAA.

Returns a color object created from the hexadecimal string representation. The string can be in one of the following formats: RRGGBBAA, RRGGBB, GGAA, GG, with the latter two forms creating a grayscale color from the GG value.

 

 

Vector types

TouchOSC provides 2,3 and 4 component vector types as Vec2 Vec3 Vec4 objects.

 

Fields

The x component of the vector.

The y component of the vector.

The z component of the vector.

The w component of the vector.

 

Constructor functions

Returns a new vector with

  1. all components initialized with 0.0.
  2. all components copied from another vector object.
  3. all components initialized with number.
  4. each component initialized with the numbers provided.

 

Functions

Returns the length of the vector

Returns a new vector that is the normalized vector

 

Operators

All operators operate component-wise and return a new vector object.

 

Enumerations

 

ControlType

Possible values for a Control object's type field.

 

Shape

Possible values for a Control object's shape property.

 

OutlineStyle

Possible values for a Control object's outlineStyle property.

 

ButtonType

Possible values for a Control object's buttonType property.

 

RadioType

Possible values for a Control object's radioType property.

 

Response

Possible values for a Control object's response property.

 

Orientation

Possible values for a Control object's orientation property.

 

AlignH

Possible values for a Control object's textAlignH property.

 

AlignV

Possible values for a Control object's textAlignV property.

 

PointerState

 

PointerPriority

 

ValueType

 

ValueField

 

ValueProperty

 

CursorDisplay

 

MIDIMessageType

 

 

Constants

 

Colors

 

 

Control Properties and Values

 

Common

Properties and values that are common to all controls, independent of their type. Not all control types will utilize the value of these properties.

Properties

NameDescription
nameA user-editable string.
frameA Rectangle object.
colorA Color object.
visibleA boolean value.
interactiveA boolean value.
backgroundA boolean value.
outlineA boolean value.
outlineStyleOne of the possible values of the OutlineStyle enumeration.
grabFocusA boolean value.
pointerPriorityOne of the possible values of the PointerPriority enumeration.
cornerAn integer number value ranging from 0 to 10
orientationOne of the possible values of the Orientation enumeration.
scriptA string value. The control's script source code.

Values

NameDescription
touchA boolean value. true if any pointers are associated with the control in the current frame, false otherwise. For a control to be able to be associated with a pointer, its visible and interactive properties have to both be true

 

BOX

Properties

NameDescription
shapeOne of the possible values of the Shape enumeration

 

BUTTON

Properties

NameType
shapeOne of the possible values of the Shape enumeration.
buttonTypeOne of the possible values of the ButtonType enumeration.
pressA boolean value.
releaseA boolean value.
valuePositionA boolean value.

Values

NameDescription
xA floating point value ranging from 0.0 to 1.0.

 

LABEL

Properties

NameType
textSizeAn integer value.
textLengthAn integer value.
textAlignHOne of the possible values of the AlignH enumeration.
textAlignVOne of the possible values of the AlignV enumeration.
textColorA Color object.
textClipA boolean value.

Values

NameDescription
textA string value.

 

TEXT

Properties

NameType
textSizeAn integer value.
textColorA Color object.

Values

NameDescription
textA string value.

 

FADER

Properties

NameType
cursorA boolean value.
cursorDisplayOne of the possible values of the CursorDisplay enumeration.
barA boolean value.
barDisplayOne of the possible values of the CursorDisplay enumeration.
centeredA boolean value.
responseOne of the possible values of the Response enumeration.
responseFactorAn integer value ranging from 1 to 100.
gridA boolean value.
gridStepsAn integer value.

Values

NameDescription
xA floating point value ranging from 0.0 to 1.0.

 

XY

Properties

NameType
cursorA boolean value.
cursorDisplayOne of the possible values of the CursorDisplay enumeration.
linesA boolean value.
linesDisplayOne of the possible values of the CursorDisplay enumeration.
lockXA boolean value.
lockYA boolean value.
responseOne of the possible values of the Response enumeration.
responseFactorAn integer value ranging from 1 to 100.
gridXA boolean value.
gridYA boolean value.
gridStepsXAn integer value.
gridStepsYAn integer value.

Values

NameDescription
xA floating point value ranging from 0.0 to 1.0.
yA floating point value ranging from 0.0 to 1.0.

 

RADIAL

Properties

NameType
invertedA boolean value.
centeredA boolean value.
responseOne of the possible values of the Response enumeration.
responseFactorAn integer value ranging from 1 to 100.
gridA boolean value.
gridStepsAn integer value.

Values

NameDescription
xA floating point value ranging from 0.0 to 1.0.

 

ENCODER

Properties

NameType
cursorA boolean value.
cursorDisplayOne of the possible values of the CursorDisplay enumeration.
responseOne of the possible values of the Response enumeration.
responseFactorAn integer value ranging from 1 to 100.
gridA boolean value.
gridStepsAn integer value.

Values

NameDescription
xA floating point value ranging from 0.0 to 1.0.
yA floating point value ranging from 0.0 to 1.0.

 

RADAR

Properties

NameType
cursorA boolean value.
cursorDisplayOne of the possible values of the CursorDisplay enumeration.
linesA boolean value.
linesDisplayOne of the possible values of the CursorDisplay enumeration.
lockXA boolean value.
lockYA boolean value.
gridXA boolean value.
gridYA boolean value.
gridStepsXAn integer value.
gridStepsYAn integer value.

Values

NameDescription
xA floating point value ranging from 0.0 to 1.0.
yA floating point value ranging from 0.0 to 1.0.

 

RADIO

Properties

NameType
stepsAn integer value.
radioTypeOne of the possible values of the RadioType enumeration.

Values

NameDescription
xAn integer value ranging from 0 to the value of the steps property minus one.

 

PAGER

Properties

NameType
tabbarA boolean value.
tabbarSizeAn integer value ranging from 10 to the value of the 300.
tabbarDoubleTapA boolean value.
tabLabelsA boolean value.
textSizeOffAn integer value.
textSizeOnAn integer value.

Values

NameDescription
pageAn integer value ranging from 0 to the number of pages minus one.

 

 

Examples

 

Control Callback Functions

 

Sending MIDI Messages

 

Sending OSC Messages

 

Detect Control 'Double-tap'

 

Send Periodic Message

 

Send Accelerometer Data