1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
OSCpathDispatcher dispatches OSC commands to OSCpathResponder 's.
It is also an OSCMultiResponder, so it distributes a command to
any OSCresponderNode's assigned to that command.
path: an array of the indices in the OSC message that are the path values
*/
OSCpathDispatcher : OSCMultiResponder {
classvar <>cmdPathIndices;
var <>pathResponders;
var <>maxPathSize = 0;
*new { arg addr, cmdName, action, pathSize;
this.deprecated(thisMethod, OSCFunc.class.findMethod(\new));
^super.new(addr, cmdName, action).initPathSize(pathSize);
}
initPathSize { arg pathSize;
maxPathSize = pathSize;
pathResponders = Set.new;
}
value { arg time, msg;
var cmdPath, match, responder;
super.value(time, msg);
if (maxPathSize.notNil, {
cmdPath = [cmdName] ++ msg[1..maxPathSize];
responder = OSCpathResponder(addr, cmdPath);
match = pathResponders.findMatch(responder);
if (match.notNil, {
match.value(time, msg);
});
(maxPathSize - 1).do({ arg i;
responder.path.removeAt(responder.path.size - 1);
match = pathResponders.findMatch(responder);
if (match.notNil, {
match.value(time, msg);
});
});
});
}
addChild { arg responder;
var old;
old = pathResponders.findMatch(responder);
if(old.notNil,{ pathResponders.remove(old) });
pathResponders.add(responder);
if(responder.path.size > maxPathSize) { maxPathSize = responder.path.size };
}
removeChild { arg responder;
pathResponders.remove(responder);
if(responder.path.size == maxPathSize) {
maxPathSize = pathResponders.maxValue({ |resp| resp.path.size }) ? 0;
};
if(this.isEmpty) { this.remove };
}
isEmpty { ^(nodes.size + pathResponders.size) == 0 }
}
OSCpathResponder : OSCresponder {
var <>path;
var <>dispatcher;
*new { arg addr, cmdPath, action;
var cmdName, path;
this.deprecated(thisMethod, OSCFunc.class.findMethod(\new));
#cmdName ...path = cmdPath;
^super.new(addr, cmdName, action).path_(path);
}
findDispatcher {
var responder, match, pathIndices;
responder = OSCpathDispatcher(addr, cmdName, nil, path.size);
match = OSCresponder.all.findMatch(responder);
if(match.isNil, { ^responder.add });
if (match.class === OSCresponder, {
match.remove;
responder.nodes_([match]);
^responder.add
});
if (match.class === OSCMultiResponder, {
match.remove;
responder.nodes_(match.nodes);
^responder.add;
});
^match;
}
add {
dispatcher = this.findDispatcher;
dispatcher.addChild(this);
}
remove {
dispatcher.removeChild(this);
}
== { arg that;
^that respondsTo: \path and: { path == that.path }
}
hash { ^path.hash }
}
/*
(
var s, commandpath, response, aSynth, nodeID, triggerID;
s = Server.local;
s.boot;
triggerID = 1;
aSynth = {arg freq = 1, triggerID = 1; SendTrig.kr(SinOsc.kr(freq), triggerID, 666); }.play;
nodeID = aSynth.nodeID;
commandpath = ['/tr', nodeID, triggerID];
response = { arg time, responder, message; message.postln };
o = OSCpathResponder(s.addr, commandpath, response);
o.add;
)
o.remove
(
var s, commandpath, response, aSynth, nodeID, triggerID;
s = Server.local;
commandpath = ['/tr', nil, triggerID];
response = { arg time, responder, message; message.postln };
o = OSCpathResponder(s.addr, commandpath, response);
o.add;
)
*/
|