654 lines
20 KiB
JavaScript
Executable File
654 lines
20 KiB
JavaScript
Executable File
// Generated by CoffeeScript 1.7.1
|
|
(function() {
|
|
var IsArray, StrictError, binpack, getArrayArg, isOscBundleBuffer, mapBundleList, oscTypeCodes, padding, toOscTypeAndArgs,
|
|
__hasProp = {}.hasOwnProperty;
|
|
|
|
binpack = require("binpack");
|
|
|
|
exports.concat = function(buffers) {
|
|
var buffer, copyTo, destBuffer, sumLength, _i, _j, _k, _len, _len1, _len2;
|
|
if (!IsArray(buffers)) {
|
|
throw new Error("concat must take an array of buffers");
|
|
}
|
|
for (_i = 0, _len = buffers.length; _i < _len; _i++) {
|
|
buffer = buffers[_i];
|
|
if (!Buffer.isBuffer(buffer)) {
|
|
throw new Error("concat must take an array of buffers");
|
|
}
|
|
}
|
|
sumLength = 0;
|
|
for (_j = 0, _len1 = buffers.length; _j < _len1; _j++) {
|
|
buffer = buffers[_j];
|
|
sumLength += buffer.length;
|
|
}
|
|
destBuffer = new Buffer(sumLength);
|
|
copyTo = 0;
|
|
for (_k = 0, _len2 = buffers.length; _k < _len2; _k++) {
|
|
buffer = buffers[_k];
|
|
buffer.copy(destBuffer, copyTo);
|
|
copyTo += buffer.length;
|
|
}
|
|
return destBuffer;
|
|
};
|
|
|
|
exports.toOscString = function(str, strict) {
|
|
var i, nullIndex, _i, _ref;
|
|
if (!(typeof str === "string")) {
|
|
throw new Error("can't pack a non-string into an osc-string");
|
|
}
|
|
nullIndex = str.indexOf("\u0000");
|
|
if (nullIndex !== -1 && strict) {
|
|
throw StrictError("Can't pack an osc-string that contains NULL characters");
|
|
}
|
|
if (nullIndex !== -1) {
|
|
str = str.slice(0, nullIndex);
|
|
}
|
|
for (i = _i = 0, _ref = padding(str); 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
|
|
str += "\u0000";
|
|
}
|
|
return new Buffer(str);
|
|
};
|
|
|
|
exports.splitOscString = function(buffer, strict) {
|
|
var i, nullIndex, rawStr, rest, splitPoint, str, _i, _ref;
|
|
if (!Buffer.isBuffer(buffer)) {
|
|
throw StrictError("Can't split something that isn't a buffer");
|
|
}
|
|
rawStr = buffer.toString("utf8");
|
|
nullIndex = rawStr.indexOf("\u0000");
|
|
if (nullIndex === -1) {
|
|
if (strict) {
|
|
throw new Error("All osc-strings must contain a null character");
|
|
}
|
|
return {
|
|
string: rawStr,
|
|
rest: new Buffer(0)
|
|
};
|
|
}
|
|
str = rawStr.slice(0, nullIndex);
|
|
splitPoint = Buffer.byteLength(str) + padding(str);
|
|
if (strict && splitPoint > buffer.length) {
|
|
throw StrictError("Not enough padding for osc-string");
|
|
}
|
|
if (strict) {
|
|
for (i = _i = _ref = Buffer.byteLength(str); _ref <= splitPoint ? _i < splitPoint : _i > splitPoint; i = _ref <= splitPoint ? ++_i : --_i) {
|
|
if (buffer[i] !== 0) {
|
|
throw StrictError("Not enough or incorrect padding for osc-string");
|
|
}
|
|
}
|
|
}
|
|
rest = buffer.slice(splitPoint, buffer.length);
|
|
return {
|
|
string: str,
|
|
rest: rest
|
|
};
|
|
};
|
|
|
|
exports.splitInteger = function(buffer, type) {
|
|
var bytes, num, rest, value;
|
|
if (type == null) {
|
|
type = "Int32";
|
|
}
|
|
bytes = (binpack["pack" + type](0)).length;
|
|
if (buffer.length < bytes) {
|
|
throw new Error("buffer is not big enough for integer type");
|
|
}
|
|
num = 0;
|
|
value = binpack["unpack" + type](buffer.slice(0, bytes), "big");
|
|
rest = buffer.slice(bytes, buffer.length);
|
|
return {
|
|
integer: value,
|
|
rest: rest
|
|
};
|
|
};
|
|
|
|
exports.toIntegerBuffer = function(number, type) {
|
|
if (type == null) {
|
|
type = "Int32";
|
|
}
|
|
if (typeof number !== "number") {
|
|
throw new Error("cannot pack a non-number into an integer buffer");
|
|
}
|
|
return binpack["pack" + type](number, "big");
|
|
};
|
|
|
|
oscTypeCodes = {
|
|
s: {
|
|
representation: "string",
|
|
split: function(buffer, strict) {
|
|
var split;
|
|
split = exports.splitOscString(buffer, strict);
|
|
return {
|
|
value: split.string,
|
|
rest: split.rest
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
if (typeof value !== "string") {
|
|
throw new Error("expected string");
|
|
}
|
|
return exports.toOscString(value, strict);
|
|
}
|
|
},
|
|
i: {
|
|
representation: "integer",
|
|
split: function(buffer, strict) {
|
|
var split;
|
|
split = exports.splitInteger(buffer);
|
|
return {
|
|
value: split.integer,
|
|
rest: split.rest
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
if (typeof value !== "number") {
|
|
throw new Error("expected number");
|
|
}
|
|
return exports.toIntegerBuffer(value);
|
|
}
|
|
},
|
|
t: {
|
|
representation: "timetag",
|
|
split: function(buffer, strict) {
|
|
var split;
|
|
split = exports.splitInteger(buffer, "UInt64");
|
|
return {
|
|
value: split.integer,
|
|
rest: split.rest
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
if (typeof value !== "number") {
|
|
throw new Error("expected number");
|
|
}
|
|
return exports.toIntegerBuffer(value, "UInt64");
|
|
}
|
|
},
|
|
f: {
|
|
representation: "float",
|
|
split: function(buffer, strict) {
|
|
return {
|
|
value: binpack.unpackFloat32(buffer.slice(0, 4), "big"),
|
|
rest: buffer.slice(4, buffer.length)
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
if (typeof value !== "number") {
|
|
throw new Error("expected number");
|
|
}
|
|
return binpack.packFloat32(value, "big");
|
|
}
|
|
},
|
|
d: {
|
|
representation: "double",
|
|
split: function(buffer, strict) {
|
|
return {
|
|
value: binpack.unpackFloat64(buffer.slice(0, 8), "big"),
|
|
rest: buffer.slice(8, buffer.length)
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
if (typeof value !== "number") {
|
|
throw new Error("expected number");
|
|
}
|
|
return binpack.packFloat64(value, "big");
|
|
}
|
|
},
|
|
b: {
|
|
representation: "blob",
|
|
split: function(buffer, strict) {
|
|
var length, _ref;
|
|
_ref = exports.splitInteger(buffer), length = _ref.integer, buffer = _ref.rest;
|
|
return {
|
|
value: buffer.slice(0, length),
|
|
rest: buffer.slice(length, buffer.length)
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
var size;
|
|
if (!Buffer.isBuffer(value)) {
|
|
throw new Error("expected node.js Buffer");
|
|
}
|
|
size = exports.toIntegerBuffer(value.length);
|
|
return exports.concat([size, value]);
|
|
}
|
|
},
|
|
T: {
|
|
representation: "true",
|
|
split: function(buffer, strict) {
|
|
return {
|
|
rest: buffer,
|
|
value: true
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
if (!value && strict) {
|
|
throw new Error("true must be true");
|
|
}
|
|
return new Buffer(0);
|
|
}
|
|
},
|
|
F: {
|
|
representation: "false",
|
|
split: function(buffer, strict) {
|
|
return {
|
|
rest: buffer,
|
|
value: false
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
if (value && strict) {
|
|
throw new Error("false must be false");
|
|
}
|
|
return new Buffer(0);
|
|
}
|
|
},
|
|
N: {
|
|
representation: "null",
|
|
split: function(buffer, strict) {
|
|
return {
|
|
rest: buffer,
|
|
value: null
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
if (value && strict) {
|
|
throw new Error("null must be false");
|
|
}
|
|
return new Buffer(0);
|
|
}
|
|
},
|
|
I: {
|
|
representation: "bang",
|
|
split: function(buffer, strict) {
|
|
return {
|
|
rest: buffer,
|
|
value: "bang"
|
|
};
|
|
},
|
|
toArg: function(value, strict) {
|
|
return new Buffer(0);
|
|
}
|
|
}
|
|
};
|
|
|
|
exports.oscTypeCodeToTypeString = function(code) {
|
|
var _ref;
|
|
return (_ref = oscTypeCodes[code]) != null ? _ref.representation : void 0;
|
|
};
|
|
|
|
exports.typeStringToOscTypeCode = function(rep) {
|
|
var code, str;
|
|
for (code in oscTypeCodes) {
|
|
if (!__hasProp.call(oscTypeCodes, code)) continue;
|
|
str = oscTypeCodes[code].representation;
|
|
if (str === rep) {
|
|
return code;
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
exports.argToTypeCode = function(arg, strict) {
|
|
var code, value;
|
|
if (((arg != null ? arg.type : void 0) != null) && (typeof arg.type === 'string') && ((code = exports.typeStringToOscTypeCode(arg.type)) != null)) {
|
|
return code;
|
|
}
|
|
value = (arg != null ? arg.value : void 0) != null ? arg.value : arg;
|
|
if (strict && (value == null)) {
|
|
throw new Error('Argument has no value');
|
|
}
|
|
if (typeof value === 'string') {
|
|
return 's';
|
|
}
|
|
if (typeof value === 'number') {
|
|
return 'f';
|
|
}
|
|
if (Buffer.isBuffer(value)) {
|
|
return 'b';
|
|
}
|
|
if (typeof value === 'boolean') {
|
|
if (value) {
|
|
return 'T';
|
|
} else {
|
|
return 'F';
|
|
}
|
|
}
|
|
if (value === null) {
|
|
return 'N';
|
|
}
|
|
throw new Error("I don't know what type this is supposed to be.");
|
|
};
|
|
|
|
exports.splitOscArgument = function(buffer, type, strict) {
|
|
var osctype;
|
|
osctype = exports.typeStringToOscTypeCode(type);
|
|
if (osctype != null) {
|
|
return oscTypeCodes[osctype].split(buffer, strict);
|
|
} else {
|
|
throw new Error("I don't understand how I'm supposed to unpack " + type);
|
|
}
|
|
};
|
|
|
|
exports.toOscArgument = function(value, type, strict) {
|
|
var osctype;
|
|
osctype = exports.typeStringToOscTypeCode(type);
|
|
if (osctype != null) {
|
|
return oscTypeCodes[osctype].toArg(value, strict);
|
|
} else {
|
|
throw new Error("I don't know how to pack " + type);
|
|
}
|
|
};
|
|
|
|
exports.fromOscMessage = function(buffer, strict) {
|
|
var address, arg, args, arrayStack, built, type, typeString, types, _i, _len, _ref, _ref1;
|
|
_ref = exports.splitOscString(buffer, strict), address = _ref.string, buffer = _ref.rest;
|
|
if (strict && address[0] !== '/') {
|
|
throw StrictError('addresses must start with /');
|
|
}
|
|
if (!buffer.length) {
|
|
return {
|
|
address: address,
|
|
args: []
|
|
};
|
|
}
|
|
_ref1 = exports.splitOscString(buffer, strict), types = _ref1.string, buffer = _ref1.rest;
|
|
if (types[0] !== ',') {
|
|
if (strict) {
|
|
throw StrictError('Argument lists must begin with ,');
|
|
}
|
|
return {
|
|
address: address,
|
|
args: []
|
|
};
|
|
}
|
|
types = types.slice(1, +types.length + 1 || 9e9);
|
|
args = [];
|
|
arrayStack = [args];
|
|
for (_i = 0, _len = types.length; _i < _len; _i++) {
|
|
type = types[_i];
|
|
if (type === '[') {
|
|
arrayStack.push([]);
|
|
continue;
|
|
}
|
|
if (type === ']') {
|
|
if (arrayStack.length <= 1) {
|
|
if (strict) {
|
|
throw new StrictError("Mismatched ']' character.");
|
|
}
|
|
} else {
|
|
built = arrayStack.pop();
|
|
arrayStack[arrayStack.length - 1].push({
|
|
type: 'array',
|
|
value: built
|
|
});
|
|
}
|
|
continue;
|
|
}
|
|
typeString = exports.oscTypeCodeToTypeString(type);
|
|
if (typeString == null) {
|
|
throw new Error("I don't understand the argument code " + type);
|
|
}
|
|
arg = exports.splitOscArgument(buffer, typeString, strict);
|
|
if (arg != null) {
|
|
buffer = arg.rest;
|
|
}
|
|
arrayStack[arrayStack.length - 1].push({
|
|
type: typeString,
|
|
value: arg != null ? arg.value : void 0
|
|
});
|
|
}
|
|
if (arrayStack.length !== 1 && strict) {
|
|
throw new StrictError("Mismatched '[' character");
|
|
}
|
|
return {
|
|
address: address,
|
|
args: args,
|
|
oscType: "message"
|
|
};
|
|
};
|
|
|
|
exports.fromOscBundle = function(buffer, strict) {
|
|
var bundleTag, convertedElems, timetag, _ref, _ref1;
|
|
_ref = exports.splitOscString(buffer, strict), bundleTag = _ref.string, buffer = _ref.rest;
|
|
if (bundleTag !== "\#bundle") {
|
|
throw new Error("osc-bundles must begin with \#bundle");
|
|
}
|
|
_ref1 = exports.splitInteger(buffer, "UInt64"), timetag = _ref1.integer, buffer = _ref1.rest;
|
|
convertedElems = mapBundleList(buffer, function(buffer) {
|
|
return exports.fromOscPacket(buffer, strict);
|
|
});
|
|
return {
|
|
timetag: timetag,
|
|
elements: convertedElems,
|
|
oscType: "bundle"
|
|
};
|
|
};
|
|
|
|
exports.fromOscPacket = function(buffer, strict) {
|
|
if (isOscBundleBuffer(buffer, strict)) {
|
|
return exports.fromOscBundle(buffer, strict);
|
|
} else {
|
|
return exports.fromOscMessage(buffer, strict);
|
|
}
|
|
};
|
|
|
|
getArrayArg = function(arg) {
|
|
if (IsArray(arg)) {
|
|
return arg;
|
|
} else if (((arg != null ? arg.type : void 0) === "array") && (IsArray(arg != null ? arg.value : void 0))) {
|
|
return arg.value;
|
|
} else if ((arg != null) && (arg.type == null) && (IsArray(arg.value))) {
|
|
return arg.value;
|
|
} else {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
toOscTypeAndArgs = function(argList, strict) {
|
|
var arg, buff, oscargs, osctype, thisArgs, thisType, typeCode, value, _i, _len, _ref;
|
|
osctype = "";
|
|
oscargs = [];
|
|
for (_i = 0, _len = argList.length; _i < _len; _i++) {
|
|
arg = argList[_i];
|
|
if ((getArrayArg(arg)) != null) {
|
|
_ref = toOscTypeAndArgs(getArrayArg(arg), strict), thisType = _ref[0], thisArgs = _ref[1];
|
|
osctype += "[" + thisType + "]";
|
|
oscargs = oscargs.concat(thisArgs);
|
|
continue;
|
|
}
|
|
typeCode = exports.argToTypeCode(arg, strict);
|
|
if (typeCode != null) {
|
|
value = arg != null ? arg.value : void 0;
|
|
if (value === void 0) {
|
|
value = arg;
|
|
}
|
|
buff = exports.toOscArgument(value, exports.oscTypeCodeToTypeString(typeCode), strict);
|
|
if (buff != null) {
|
|
oscargs.push(buff);
|
|
osctype += typeCode;
|
|
}
|
|
}
|
|
}
|
|
return [osctype, oscargs];
|
|
};
|
|
|
|
exports.toOscMessage = function(message, strict) {
|
|
var address, allArgs, args, old_arg, oscaddr, oscargs, osctype, _ref;
|
|
address = (message != null ? message.address : void 0) != null ? message.address : message;
|
|
if (typeof address !== "string") {
|
|
throw new Error("message must contain an address");
|
|
}
|
|
args = message != null ? message.args : void 0;
|
|
if (args === void 0) {
|
|
args = [];
|
|
}
|
|
if (!IsArray(args)) {
|
|
old_arg = args;
|
|
args = [];
|
|
args[0] = old_arg;
|
|
}
|
|
oscaddr = exports.toOscString(address, strict);
|
|
_ref = toOscTypeAndArgs(args, strict), osctype = _ref[0], oscargs = _ref[1];
|
|
osctype = "," + osctype;
|
|
allArgs = exports.concat(oscargs);
|
|
osctype = exports.toOscString(osctype);
|
|
return exports.concat([oscaddr, osctype, allArgs]);
|
|
};
|
|
|
|
exports.toOscBundle = function(bundle, strict) {
|
|
var allElems, buff, e, elem, elements, elemstr, oscBundleTag, oscElems, oscTimeTag, size, timetag, _i, _len, _ref, _ref1;
|
|
if (strict && ((bundle != null ? bundle.timetag : void 0) == null)) {
|
|
throw StrictError("bundles must have timetags.");
|
|
}
|
|
timetag = (_ref = bundle != null ? bundle.timetag : void 0) != null ? _ref : 0;
|
|
elements = (_ref1 = bundle != null ? bundle.elements : void 0) != null ? _ref1 : [];
|
|
if (!IsArray(elements)) {
|
|
elemstr = elements;
|
|
elements = [];
|
|
elements.push(elemstr);
|
|
}
|
|
oscBundleTag = exports.toOscString("\#bundle");
|
|
oscTimeTag = exports.toIntegerBuffer(timetag, "UInt64");
|
|
oscElems = [];
|
|
for (_i = 0, _len = elements.length; _i < _len; _i++) {
|
|
elem = elements[_i];
|
|
try {
|
|
buff = exports.toOscPacket(elem, strict);
|
|
size = exports.toIntegerBuffer(buff.length);
|
|
oscElems.push(exports.concat([size, buff]));
|
|
} catch (_error) {
|
|
e = _error;
|
|
null;
|
|
}
|
|
}
|
|
allElems = exports.concat(oscElems);
|
|
return exports.concat([oscBundleTag, oscTimeTag, allElems]);
|
|
};
|
|
|
|
exports.toOscPacket = function(bundleOrMessage, strict) {
|
|
if ((bundleOrMessage != null ? bundleOrMessage.oscType : void 0) != null) {
|
|
if (bundleOrMessage.oscType === "bundle") {
|
|
return exports.toOscBundle(bundleOrMessage, strict);
|
|
}
|
|
return exports.toOscMessage(bundleOrMessage, strict);
|
|
}
|
|
if (((bundleOrMessage != null ? bundleOrMessage.timetag : void 0) != null) || ((bundleOrMessage != null ? bundleOrMessage.elements : void 0) != null)) {
|
|
return exports.toOscBundle(bundleOrMessage, strict);
|
|
}
|
|
return exports.toOscMessage(bundleOrMessage, strict);
|
|
};
|
|
|
|
exports.applyMessageTranformerToBundle = function(transform) {
|
|
return function(buffer) {
|
|
var bundleTagBuffer, copyIndex, elem, elems, lengthBuff, outBuffer, string, timetagBuffer, totalLength, _i, _j, _len, _len1, _ref;
|
|
_ref = exports.splitOscString(buffer), string = _ref.string, buffer = _ref.rest;
|
|
if (string !== "\#bundle") {
|
|
throw new Error("osc-bundles must begin with \#bundle");
|
|
}
|
|
bundleTagBuffer = exports.toOscString(string);
|
|
timetagBuffer = buffer.slice(0, 8);
|
|
buffer = buffer.slice(8, buffer.length);
|
|
elems = mapBundleList(buffer, function(buffer) {
|
|
return exports.applyTransform(buffer, transform, exports.applyMessageTranformerToBundle(transform));
|
|
});
|
|
totalLength = bundleTagBuffer.length + timetagBuffer.length;
|
|
for (_i = 0, _len = elems.length; _i < _len; _i++) {
|
|
elem = elems[_i];
|
|
totalLength += 4 + elem.length;
|
|
}
|
|
outBuffer = new Buffer(totalLength);
|
|
bundleTagBuffer.copy(outBuffer, 0);
|
|
timetagBuffer.copy(outBuffer, bundleTagBuffer.length);
|
|
copyIndex = bundleTagBuffer.length + timetagBuffer.length;
|
|
for (_j = 0, _len1 = elems.length; _j < _len1; _j++) {
|
|
elem = elems[_j];
|
|
lengthBuff = exports.toIntegerBuffer(elem.length);
|
|
lengthBuff.copy(outBuffer, copyIndex);
|
|
copyIndex += 4;
|
|
elem.copy(outBuffer, copyIndex);
|
|
copyIndex += elem.length;
|
|
}
|
|
return outBuffer;
|
|
};
|
|
};
|
|
|
|
exports.applyTransform = function(buffer, mTransform, bundleTransform) {
|
|
if (bundleTransform == null) {
|
|
bundleTransform = exports.applyMessageTranformerToBundle(mTransform);
|
|
}
|
|
if (isOscBundleBuffer(buffer)) {
|
|
return bundleTransform(buffer);
|
|
} else {
|
|
return mTransform(buffer);
|
|
}
|
|
};
|
|
|
|
exports.addressTransform = function(transform) {
|
|
return function(buffer) {
|
|
var rest, string, _ref;
|
|
_ref = exports.splitOscString(buffer), string = _ref.string, rest = _ref.rest;
|
|
string = transform(string);
|
|
return exports.concat([exports.toOscString(string), rest]);
|
|
};
|
|
};
|
|
|
|
exports.messageTransform = function(transform) {
|
|
return function(buffer) {
|
|
var message;
|
|
message = exports.fromOscMessage(buffer);
|
|
return exports.toOscMessage(transform(message));
|
|
};
|
|
};
|
|
|
|
IsArray = Array.isArray;
|
|
|
|
StrictError = function(str) {
|
|
return new Error("Strict Error: " + str);
|
|
};
|
|
|
|
padding = function(str) {
|
|
var bufflength;
|
|
bufflength = Buffer.byteLength(str);
|
|
return 4 - (bufflength % 4);
|
|
};
|
|
|
|
isOscBundleBuffer = function(buffer, strict) {
|
|
var string;
|
|
string = exports.splitOscString(buffer, strict).string;
|
|
return string === "\#bundle";
|
|
};
|
|
|
|
mapBundleList = function(buffer, func) {
|
|
var e, elem, elems, nonNullElems, size, thisElemBuffer, _i, _len;
|
|
elems = (function() {
|
|
var _ref, _results;
|
|
_results = [];
|
|
while (buffer.length) {
|
|
_ref = exports.splitInteger(buffer), size = _ref.integer, buffer = _ref.rest;
|
|
if (size > buffer.length) {
|
|
throw new Error("Invalid bundle list: size of element is bigger than buffer");
|
|
}
|
|
thisElemBuffer = buffer.slice(0, size);
|
|
buffer = buffer.slice(size, buffer.length);
|
|
try {
|
|
_results.push(func(thisElemBuffer));
|
|
} catch (_error) {
|
|
e = _error;
|
|
_results.push(null);
|
|
}
|
|
}
|
|
return _results;
|
|
})();
|
|
nonNullElems = [];
|
|
for (_i = 0, _len = elems.length; _i < _len; _i++) {
|
|
elem = elems[_i];
|
|
if (elem != null) {
|
|
nonNullElems.push(elem);
|
|
}
|
|
}
|
|
return nonNullElems;
|
|
};
|
|
|
|
}).call(this);
|