SuperCollider CLASSES

OSCdef

OSC response reference definition

Description

OSCdef provides a global reference to the functionality of its superclass OSCFunc. Essentially it stores itself at a key within a global dictionary, allowing replacement at any time. Most methods are inherited from its superclass.

NOTE: Use of the older classes OSCresponder, OSCresponderNode, and OSCpathResponder can be unsafe, since responders in user and class code can override each other unintentionally. OSCFunc and OSCdef are faster, safer, have more logical argument order, and support pattern matching. Thus they are the recommended way of registering for incoming OSC messages as of SC 3.5. (See below for an example demonstrating OSCpathResponder-type arg matching.)

Class Methods

*all

Get the global dictionary of all OSCdefs.

Returns:

*new (key, func, path, srcID, recvPort, argTemplate, dispatcher)

Create a new, enabled OSCdef. If an OSCdef already exists at this key, its parameters will be replaced with the ones provided (args for which nil is passed will use the old values).

Arguments:

key

The key at which to store this OSCdef in the global collection. Generally this will be a Symbol.

func

A Function or similar object which will respond to the incoming message. When evaluated it will be passed the arguments msg, time, addr, and recvPort, corresponding to the message as an Array in the form [OSCAddress, other args], the time that the message was sent (plus the latency if the message was in a bundle), a NetAddr corresponding to the IP address of the sender, and an Integer corresponding to the port on which the message was received.

path

A Symbol indicating the path of the OSC address of this object. Note that OSCdef demands OSC compliant addresses. If the path does not begin with a / one will be added automatically.

srcID

An optional instance of NetAddr indicating the IP address of the sender. If set this object will only respond to messages from that source.

recvPort

An optional Integer indicating the port on which messages will be received. If set this object will only respond to message received on that port. This method calls Main: -openUDPPort to ensure that the port is opened.

argTemplate

An optional Array composed of instances of Integer or Function (or objects which respond to the method Methods: matchItem) used to match the arguments of an incoming OSC message. If a Function, it will be evaluated with the corresponding message arg as an argument, and should return a Boolean indicating whether the argument matches and this OSCdef should respond (providing all other arguments match). Template values of nil will match any incoming argument value.

dispatcher

An optional instance of an appropriate subclass of AbstractDispatcher. This can be used to allow for customised dispatching. Normally this should not be needed.

Returns:

An instance of OSCdef.

*newMatching (key, func, path, srcID, recvPort, argTemplate)

A convenience method to create a new, enabled OSCdef whose dispatcher will perform pattern matching on incoming OSC messages to see if their address patterns match this object's path.

Arguments:

key

The key at which to store this OSCdef in the global collection. Generally this will be a Symbol.

func

A Function or similar object which will respond to the incoming message. When evaluated it will be passed the arguments msg, time, addr, and recvPort, corresponding to the message as an Array [OSCAddress, other args], the time that the message was sent, a NetAddr corresponding to the IP address of the sender, and an Integer corresponding to the port on which the message was received.

path

A Symbol indicating the path of the OSC address of this object. Note that OSCdef demands OSC compliant addresses. If the path does not begin with a / one will be added automatically. Pattern matching will be applied to any incoming messages to see if they match this address. Note that according to the OSC spec, regular expression wildcards are only permitted in the incoming message's address pattern. Thus path should not contain wildcards. For more details on OSC pattern matching, see http://opensoundcontrol.org/spec-1_0

srcID

An optional instance of NetAddr indicating the IP address of the sender. If set this object will only respond to messages from that source.

recvPort

An optional Integer indicating the port on which messages will be received.

argTemplate

An optional Array composed of instances of Integer or Function (or objects which respond to the method Methods: matchItem) used to match the arguments of an incoming OSC message. If a Function, it will be evaluated with the corresponding message arg as an argument, and should return a Boolean indicating whether the argument matches and this OSCFunc should respond (providing all other arguments match). Template values of nil will match any incoming argument value.

Returns:

An instance of OSCdef.

*freeAll

Clears and deactivates all OSCdefs from the global collection.

Inherited class methods

Instance Methods

-key

Get this OSCdef's key.

Returns:

Usually a Symbol.

-free

Clears this OSCdef from the global collection and deactivates it.

Inherited instance methods

Examples

n = NetAddr("127.0.0.1", 57120); // local machine

OSCdef(\test, {|msg, time, addr, recvPort| \unmatching.postln}, '/chat', n); // def style
OSCdef.newMatching(\test2, {|msg, time, addr, recvPort| \matching.postln}, '/chat', n); // path matching
OSCdef(\test3, {|msg, time, addr, recvPort| \oneShot.postln}, '/chat', n).oneShot; // once only


m = NetAddr("127.0.0.1", 57120); // loopback

m.sendMsg("/chat", "Hello App 1");
m.sendMsg("/chat", "Hello App 1"); // oneshot gone
m.sendMsg("/ch?t", "Hello App 1");
m.sendMsg("/*", "Hello App 1");
m.sendMsg("/chit", "Hello App 1"); // nothing

// Introspection

AbstractResponderFunc.allFuncProxies
AbstractResponderFunc.allEnabled
OSCdef(\test).disable;
AbstractResponderFunc.allDisabled

// change funcs
OSCdef(\test).enable;
OSCdef(\test, {|msg, time, addr, recvPort| 'Changed Unmatching'.postln}, '/chat', n); // replace at key \test
m.sendMsg("/chat", "Hello App 1");
OSCdef(\test).add(f = {\foo.postln}); // add another func
m.sendMsg("/chat", "Hello App 1");
OSCdef(\test).clear; // remove all functions
m.sendMsg("/chat", "Hello App 1");
OSCdef(\test).free;  // unregister OSCdef


//////// Use an argTemplate for finer grained matching

s.boot;
x = Synth(\default);
OSCdef(\watchForXEnd, { 'ended!'.postln }, '/n_end', s.addr, nil, [x.nodeID]).oneShot;
x.release(3);

// Args for which nil is passed will use the old values. While this facilitates swapping only parts of an OSCdef, such as the function, it also means you will have to free and recreate an OSCdef to remove parts.

OSCdef(\argtest, {|msg ... args| msg[1].postln}, '/hey', argTemplate: ['you']);
m.sendMsg("/hey", "you"); // oscdef will respond
m.sendMsg("/hey", "there"); // oscdef will not respond, filtered out by argTemplate

OSCdef(\argtest, argTemplate: nil); // this has no effect
m.sendMsg("/hey", "you"); // oscdef will respond
m.sendMsg("/hey", "there"); // oscdef will not respond, still filtered out by argTemplate

OSCdef(\argtest).free; // in order to get rid of the argTemplate, free the def...
OSCdef(\argtest, {|msg ... args| msg[1].postln}, '/hey'); // ... and recreate it.
m.sendMsg("/hey", "you"); // oscdef will respond
m.sendMsg("/hey", "there"); // oscdef will respond

Timing in incoming bundles

The time argument indicates the time the message was sent plus, if given, the latency added to the bundle:

(
OSCdef(\x, { |msg, time|
    "reception time: %\nscheduling time: %\ndelta: %\n\n".postf(Main.elapsedTime, time, time - Main.elapsedTime)
}, \time);

n = NetAddr("127.0.0.1", 57120);
)

(
n.sendBundle(0.0, [\time]);
n.sendBundle(1.0, [\time]);
)