/*! osc.js 2.0.3, Copyright 2016 Colin Clark | github.com/colinbdclark/osc.js */ /* * osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js * * Copyright 2014-2015, Colin Clark * Licensed under the MIT and GPL 3 licenses. */ /* global require, module, Buffer, dcodeIO */ var osc = osc || {}; (function () { "use strict"; osc.SECS_70YRS = 2208988800; osc.TWO_32 = 4294967296; osc.defaults = { metadata: false, unpackSingleArgs: true }; // A flag to tell us if we're in a Node.js-compatible environment with Buffers, // which we will assume are faster. // Unsupported, non-API property. osc.isBufferEnv = typeof Buffer !== "undefined"; // Unsupported, non-API property. osc.isCommonJS = typeof module !== "undefined" && module.exports; // Unsupported, non-API property. osc.isNode = osc.isCommonJS && typeof window === "undefined"; // Unsupported, non-API function. osc.isArray = function (obj) { return obj && Object.prototype.toString.call(obj) === "[object Array]"; }; // Unsupported, non-API function osc.isTypedArrayView = function (obj) { return obj.buffer && obj.buffer instanceof ArrayBuffer; }; // Unsupported, non-API function osc.isBuffer = function (obj) { return osc.isBufferEnv && obj instanceof Buffer; }; // Private instance of the optional Long dependency. var Long = typeof dcodeIO !== "undefined" ? dcodeIO.Long : typeof Long !== "undefined" ? Long : osc.isNode ? require("long") : undefined; /** * Wraps the specified object in a DataView. * * @param {Array-like} obj the object to wrap in a DataView instance * @return {DataView} the DataView object */ // Unsupported, non-API function. osc.dataView = function (obj, offset, length) { if (obj.buffer) { return new DataView(obj.buffer, offset, length); } if (obj instanceof ArrayBuffer) { return new DataView(obj, offset, length); } return new DataView(new Uint8Array(obj), offset, length); }; /** * Takes an ArrayBuffer, TypedArray, DataView, Buffer, or array-like object * and returns a Uint8Array view of it. * * Throws an error if the object isn't suitably array-like. * * @param {Array-like or Array-wrapping} obj an array-like or array-wrapping object * @returns {Uint8Array} a typed array of octets */ // Unsupported, non-API function. osc.byteArray = function (obj) { if (obj instanceof Uint8Array) { return obj; } var buf = obj.buffer ? obj.buffer : obj; if (!(buf instanceof ArrayBuffer) && (typeof buf.length === "undefined" || typeof buf === "string")) { throw new Error("Can't wrap a non-array-like object as Uint8Array. Object was: " + JSON.stringify(obj, null, 2)); } // TODO gh-39: This is a potentially unsafe algorithm; // if we're getting anything other than a TypedArrayView (such as a DataView), // we really need to determine the range of the view it is viewing. return new Uint8Array(buf); }; /** * Takes an ArrayBuffer, TypedArray, DataView, or array-like object * and returns a native buffer object * (i.e. in Node.js, a Buffer object and in the browser, a Uint8Array). * * Throws an error if the object isn't suitably array-like. * * @param {Array-like or Array-wrapping} obj an array-like or array-wrapping object * @returns {Buffer|Uint8Array} a buffer object */ // Unsupported, non-API function. osc.nativeBuffer = function (obj) { if (osc.isBufferEnv && osc.isNode) { return osc.isBuffer(obj) ? obj : new Buffer(obj.buffer ? obj : new Uint8Array(obj)); } return osc.isTypedArrayView(obj) ? obj : new Uint8Array(obj); }; // Unsupported, non-API function osc.copyByteArray = function (source, target, offset) { if (osc.isTypedArrayView(source) && osc.isTypedArrayView(target)) { target.set(source, offset); } else { var start = offset === undefined ? 0 : offset, len = Math.min(target.length - offset, source.length); for (var i = 0, j = start; i < len; i++, j++) { target[j] = source[i]; } } return target; }; /** * Reads an OSC-formatted string. * * @param {DataView} dv a DataView containing the raw bytes of the OSC string * @param {Object} offsetState an offsetState object used to store the current offset index * @return {String} the JavaScript String that was read */ osc.readString = function (dv, offsetState) { var charCodes = [], idx = offsetState.idx; for (; idx < dv.byteLength; idx++) { var charCode = dv.getUint8(idx); if (charCode !== 0) { charCodes.push(charCode); } else { idx++; break; } } // Round to the nearest 4-byte block. idx = (idx + 3) & ~0x03; offsetState.idx = idx; return String.fromCharCode.apply(null, charCodes); }; /** * Writes a JavaScript string as an OSC-formatted string. * * @param {String} str the string to write * @return {Uint8Array} a buffer containing the OSC-formatted string */ osc.writeString = function (str) { var terminated = str + "\u0000", len = terminated.length, paddedLen = (len + 3) & ~0x03, arr = new Uint8Array(paddedLen); for (var i = 0; i < terminated.length; i++) { var charCode = terminated.charCodeAt(i); arr[i] = charCode; } return arr; }; // Unsupported, non-API function. osc.readPrimitive = function (dv, readerName, numBytes, offsetState) { var val = dv[readerName](offsetState.idx, false); offsetState.idx += numBytes; return val; }; // Unsupported, non-API function. osc.writePrimitive = function (val, dv, writerName, numBytes, offset) { offset = offset === undefined ? 0 : offset; var arr; if (!dv) { arr = new Uint8Array(numBytes); dv = new DataView(arr.buffer); } else { arr = new Uint8Array(dv.buffer); } dv[writerName](offset, val, false); return arr; }; /** * Reads an OSC int32 ("i") value. * * @param {DataView} dv a DataView containing the raw bytes * @param {Object} offsetState an offsetState object used to store the current offset index into dv * @return {Number} the number that was read */ osc.readInt32 = function (dv, offsetState) { return osc.readPrimitive(dv, "getInt32", 4, offsetState); }; /** * Writes an OSC int32 ("i") value. * * @param {Number} val the number to write * @param {DataView} [dv] a DataView instance to write the number into * @param {Number} [offset] an offset into dv */ osc.writeInt32 = function (val, dv, offset) { return osc.writePrimitive(val, dv, "setInt32", 4, offset); }; /** * Reads an OSC int64 ("h") value. * * @param {DataView} dv a DataView containing the raw bytes * @param {Object} offsetState an offsetState object used to store the current offset index into dv * @return {Number} the number that was read */ osc.readInt64 = function (dv, offsetState) { var high = osc.readPrimitive(dv, "getInt32", 4, offsetState), low = osc.readPrimitive(dv, "getInt32", 4, offsetState); if (Long) { return new Long(low, high); } else { return { high: high, low: low, unsigned: false }; } }; /** * Writes an OSC int64 ("h") value. * * @param {Number} val the number to write * @param {DataView} [dv] a DataView instance to write the number into * @param {Number} [offset] an offset into dv */ osc.writeInt64 = function (val, dv, offset) { var arr = new Uint8Array(8); arr.set(osc.writePrimitive(val.high, dv, "setInt32", 4, offset), 0); arr.set(osc.writePrimitive(val.low, dv, "setInt32", 4, offset + 4), 4); return arr; }; /** * Reads an OSC float32 ("f") value. * * @param {DataView} dv a DataView containing the raw bytes * @param {Object} offsetState an offsetState object used to store the current offset index into dv * @return {Number} the number that was read */ osc.readFloat32 = function (dv, offsetState) { return osc.readPrimitive(dv, "getFloat32", 4, offsetState); }; /** * Writes an OSC float32 ("f") value. * * @param {Number} val the number to write * @param {DataView} [dv] a DataView instance to write the number into * @param {Number} [offset] an offset into dv */ osc.writeFloat32 = function (val, dv, offset) { return osc.writePrimitive(val, dv, "setFloat32", 4, offset); }; /** * Reads an OSC float64 ("d") value. * * @param {DataView} dv a DataView containing the raw bytes * @param {Object} offsetState an offsetState object used to store the current offset index into dv * @return {Number} the number that was read */ osc.readFloat64 = function (dv, offsetState) { return osc.readPrimitive(dv, "getFloat64", 8, offsetState); }; /** * Writes an OSC float64 ("d") value. * * @param {Number} val the number to write * @param {DataView} [dv] a DataView instance to write the number into * @param {Number} [offset] an offset into dv */ osc.writeFloat64 = function (val, dv, offset) { return osc.writePrimitive(val, dv, "setFloat64", 8, offset); }; /** * Reads an OSC 32-bit ASCII character ("c") value. * * @param {DataView} dv a DataView containing the raw bytes * @param {Object} offsetState an offsetState object used to store the current offset index into dv * @return {String} a string containing the read character */ osc.readChar32 = function (dv, offsetState) { var charCode = osc.readPrimitive(dv, "getUint32", 4, offsetState); return String.fromCharCode(charCode); }; /** * Writes an OSC 32-bit ASCII character ("c") value. * * @param {String} str the string from which the first character will be written * @param {DataView} [dv] a DataView instance to write the character into * @param {Number} [offset] an offset into dv * @return {String} a string containing the read character */ osc.writeChar32 = function (str, dv, offset) { var charCode = str.charCodeAt(0); if (charCode === undefined || charCode < -1) { return undefined; } return osc.writePrimitive(charCode, dv, "setUint32", 4, offset); }; /** * Reads an OSC blob ("b") (i.e. a Uint8Array). * * @param {DataView} dv a DataView instance to read from * @param {Object} offsetState an offsetState object used to store the current offset index into dv * @return {Uint8Array} the data that was read */ osc.readBlob = function (dv, offsetState) { var len = osc.readInt32(dv, offsetState), paddedLen = (len + 3) & ~0x03, blob = new Uint8Array(dv.buffer, offsetState.idx, len); offsetState.idx += paddedLen; return blob; }; /** * Writes a raw collection of bytes to a new ArrayBuffer. * * @param {Array-like} data a collection of octets * @return {ArrayBuffer} a buffer containing the OSC-formatted blob */ osc.writeBlob = function (data) { data = osc.byteArray(data); var len = data.byteLength, paddedLen = (len + 3) & ~0x03, offset = 4, // Extra 4 bytes is for the size. blobLen = paddedLen + offset, arr = new Uint8Array(blobLen), dv = new DataView(arr.buffer); // Write the size. osc.writeInt32(len, dv); // Since we're writing to a real ArrayBuffer, // we don't need to pad the remaining bytes. arr.set(data, offset); return arr; }; /** * Reads an OSC 4-byte MIDI message. * * @param {DataView} dv the DataView instance to read from * @param {Object} offsetState an offsetState object used to store the current offset index into dv * @return {Uint8Array} an array containing (in order) the port ID, status, data1 and data1 bytes */ osc.readMIDIBytes = function (dv, offsetState) { var midi = new Uint8Array(dv.buffer, offsetState.idx, 4); offsetState.idx += 4; return midi; }; /** * Writes an OSC 4-byte MIDI message. * * @param {Array-like} bytes a 4-element array consisting of the port ID, status, data1 and data1 bytes * @return {Uint8Array} the written message */ osc.writeMIDIBytes = function (bytes) { bytes = osc.byteArray(bytes); var arr = new Uint8Array(4); arr.set(bytes); return arr; }; /** * Reads an OSC RGBA colour value. * * @param {DataView} dv the DataView instance to read from * @param {Object} offsetState an offsetState object used to store the current offset index into dv * @return {Object} a colour object containing r, g, b, and a properties */ osc.readColor = function (dv, offsetState) { var bytes = new Uint8Array(dv.buffer, offsetState.idx, 4), alpha = bytes[3] / 255; offsetState.idx += 4; return { r: bytes[0], g: bytes[1], b: bytes[2], a: alpha }; }; /** * Writes an OSC RGBA colour value. * * @param {Object} color a colour object containing r, g, b, and a properties * @return {Uint8Array} a byte array containing the written color */ osc.writeColor = function (color) { var alpha = Math.round(color.a * 255), arr = new Uint8Array([color.r, color.g, color.b, alpha]); return arr; }; /** * Reads an OSC true ("T") value by directly returning the JavaScript Boolean "true". */ osc.readTrue = function () { return true; }; /** * Reads an OSC false ("F") value by directly returning the JavaScript Boolean "false". */ osc.readFalse = function () { return false; }; /** * Reads an OSC nil ("N") value by directly returning the JavaScript "null" value. */ osc.readNull = function () { return null; }; /** * Reads an OSC impulse/bang/infinitum ("I") value by directly returning 1.0. */ osc.readImpulse = function () { return 1.0; }; /** * Reads an OSC time tag ("t"). * * @param {DataView} dv the DataView instance to read from * @param {Object} offsetState an offset state object containing the current index into dv * @param {Object} a time tag object containing both the raw NTP as well as the converted native (i.e. JS/UNIX) time */ osc.readTimeTag = function (dv, offsetState) { var secs1900 = osc.readPrimitive(dv, "getUint32", 4, offsetState), frac = osc.readPrimitive(dv, "getUint32", 4, offsetState), native = (secs1900 === 0 && frac === 1) ? Date.now() : osc.ntpToJSTime(secs1900, frac); return { raw: [secs1900, frac], native: native }; }; /** * Writes an OSC time tag ("t"). * * Takes, as its argument, a time tag object containing either a "raw" or "native property." * The raw timestamp must conform to the NTP standard representation, consisting of two unsigned int32 * values. The first represents the number of seconds since January 1, 1900; the second, fractions of a second. * "Native" JavaScript timestamps are specified as a Number representing milliseconds since January 1, 1970. * * @param {Object} timeTag time tag object containing either a native JS timestamp (in ms) or a NTP timestamp pair * @return {Uint8Array} raw bytes for the written time tag */ osc.writeTimeTag = function (timeTag) { var raw = timeTag.raw ? timeTag.raw : osc.jsToNTPTime(timeTag.native), arr = new Uint8Array(8), // Two Unit32s. dv = new DataView(arr.buffer); osc.writeInt32(raw[0], dv, 0); osc.writeInt32(raw[1], dv, 4); return arr; }; /** * Produces a time tag containing a raw NTP timestamp * relative to now by the specified number of seconds. * * @param {Number} secs the number of seconds relative to now (i.e. + for the future, - for the past) * @param {Number} now the number of milliseconds since epoch to use as the current time. Defaults to Date.now() * @return {Object} the time tag */ osc.timeTag = function (secs, now) { secs = secs || 0; now = now || Date.now(); var nowSecs = now / 1000, nowWhole = Math.floor(nowSecs), nowFracs = nowSecs - nowWhole, secsWhole = Math.floor(secs), secsFracs = secs - secsWhole, fracs = nowFracs + secsFracs; if (fracs > 1) { var fracsWhole = Math.floor(fracs), fracsFracs = fracs - fracsWhole; secsWhole += fracsWhole; fracs = fracsFracs; } var ntpSecs = nowWhole + secsWhole + osc.SECS_70YRS, ntpFracs = Math.round(osc.TWO_32 * fracs); return { raw: [ntpSecs, ntpFracs] }; }; /** * Converts OSC's standard time tag representation (which is the NTP format) * into the JavaScript/UNIX format in milliseconds. * * @param {Number} secs1900 the number of seconds since 1900 * @param {Number} frac the number of fractions of a second (between 0 and 2^32) * @return {Number} a JavaScript-compatible timestamp in milliseconds */ osc.ntpToJSTime = function (secs1900, frac) { var secs1970 = secs1900 - osc.SECS_70YRS, decimals = frac / osc.TWO_32, msTime = (secs1970 + decimals) * 1000; return msTime; }; osc.jsToNTPTime = function (jsTime) { var secs = jsTime / 1000, secsWhole = Math.floor(secs), secsFrac = secs - secsWhole, ntpSecs = secsWhole + osc.SECS_70YRS, ntpFracs = Math.round(osc.TWO_32 * secsFrac); return [ntpSecs, ntpFracs]; }; /** * Reads the argument portion of an OSC message. * * @param {DataView} dv a DataView instance to read from * @param {Object} offsetState the offsetState object that stores the current offset into dv * @param {Oobject} [options] read options * @return {Array} an array of the OSC arguments that were read */ osc.readArguments = function (dv, options, offsetState) { var typeTagString = osc.readString(dv, offsetState); if (typeTagString.indexOf(",") !== 0) { // Despite what the OSC 1.0 spec says, // it just doesn't make sense to handle messages without type tags. // scsynth appears to read such messages as if they have a single // Uint8 argument. sclang throws an error if the type tag is omitted. throw new Error("A malformed type tag string was found while reading " + "the arguments of an OSC message. String was: " + typeTagString, " at offset: " + offsetState.idx); } var argTypes = typeTagString.substring(1).split(""), args = []; osc.readArgumentsIntoArray(args, argTypes, typeTagString, dv, options, offsetState); return args; }; // Unsupported, non-API function. osc.readArgument = function (argType, typeTagString, dv, options, offsetState) { var typeSpec = osc.argumentTypes[argType]; if (!typeSpec) { throw new Error("'" + argType + "' is not a valid OSC type tag. Type tag string was: " + typeTagString); } var argReader = typeSpec.reader, arg = osc[argReader](dv, offsetState); if (options.metadata) { arg = { type: argType, value: arg }; } return arg; }; // Unsupported, non-API function. osc.readArgumentsIntoArray = function (arr, argTypes, typeTagString, dv, options, offsetState) { var i = 0; while (i < argTypes.length) { var argType = argTypes[i], arg; if (argType === "[") { var fromArrayOpen = argTypes.slice(i + 1), endArrayIdx = fromArrayOpen.indexOf("]"); if (endArrayIdx < 0) { throw new Error("Invalid argument type tag: an open array type tag ('[') was found " + "without a matching close array tag ('[]'). Type tag was: " + typeTagString); } var typesInArray = fromArrayOpen.slice(0, endArrayIdx); arg = osc.readArgumentsIntoArray([], typesInArray, typeTagString, dv, options, offsetState); i += endArrayIdx + 2; } else { arg = osc.readArgument(argType, typeTagString, dv, options, offsetState); i++; } arr.push(arg); } return arr; }; /** * Writes the specified arguments. * * @param {Array} args an array of arguments * @param {Object} options options for writing * @return {Uint8Array} a buffer containing the OSC-formatted argument type tag and values */ osc.writeArguments = function (args, options) { var argCollection = osc.collectArguments(args, options); return osc.joinParts(argCollection); }; // Unsupported, non-API function. osc.joinParts = function (dataCollection) { var buf = new Uint8Array(dataCollection.byteLength), parts = dataCollection.parts, offset = 0; for (var i = 0; i < parts.length; i++) { var part = parts[i]; osc.copyByteArray(part, buf, offset); offset += part.length; } return buf; }; // Unsupported, non-API function. osc.addDataPart = function (dataPart, dataCollection) { dataCollection.parts.push(dataPart); dataCollection.byteLength += dataPart.length; }; osc.writeArrayArguments = function (args, dataCollection) { var typeTag = "["; for (var i = 0; i < args.length; i++) { var arg = args[i]; typeTag += osc.writeArgument(arg, dataCollection); } typeTag += "]"; return typeTag; }; osc.writeArgument = function (arg, dataCollection) { if (osc.isArray(arg)) { return osc.writeArrayArguments(arg, dataCollection); } var type = arg.type, writer = osc.argumentTypes[type].writer; if (writer) { var data = osc[writer](arg.value); osc.addDataPart(data, dataCollection); } return arg.type; }; // Unsupported, non-API function. osc.collectArguments = function (args, options, dataCollection) { if (!osc.isArray(args)) { args = typeof args === "undefined" ? [] : [args]; } dataCollection = dataCollection || { byteLength: 0, parts: [] }; if (!options.metadata) { args = osc.annotateArguments(args); } var typeTagString = ",", currPartIdx = dataCollection.parts.length; for (var i = 0; i < args.length; i++) { var arg = args[i]; typeTagString += osc.writeArgument(arg, dataCollection); } var typeData = osc.writeString(typeTagString); dataCollection.byteLength += typeData.byteLength; dataCollection.parts.splice(currPartIdx, 0, typeData); return dataCollection; }; /** * Reads an OSC message. * * @param {Array-like} data an array of bytes to read from * @param {Object} [options] read options * @param {Object} [offsetState] an offsetState object that stores the current offset into dv * @return {Object} the OSC message, formatted as a JavaScript object containing "address" and "args" properties */ osc.readMessage = function (data, options, offsetState) { options = options || osc.defaults; var dv = osc.dataView(data, data.byteOffset, data.byteLength); offsetState = offsetState || { idx: 0 }; var address = osc.readString(dv, offsetState); return osc.readMessageContents(address, dv, options, offsetState); }; // Unsupported, non-API function. osc.readMessageContents = function (address, dv, options, offsetState) { if (address.indexOf("/") !== 0) { throw new Error("A malformed OSC address was found while reading " + "an OSC message. String was: " + address); } var args = osc.readArguments(dv, options, offsetState); return { address: address, args: args.length === 1 && options.unpackSingleArgs ? args[0] : args }; }; // Unsupported, non-API function. osc.collectMessageParts = function (msg, options, dataCollection) { dataCollection = dataCollection || { byteLength: 0, parts: [] }; osc.addDataPart(osc.writeString(msg.address), dataCollection); return osc.collectArguments(msg.args, options, dataCollection); }; /** * Writes an OSC message. * * @param {Object} msg a message object containing "address" and "args" properties * @param {Object} [options] write options * @return {Uint8Array} an array of bytes containing the OSC message */ osc.writeMessage = function (msg, options) { options = options || osc.defaults; if (!osc.isValidMessage(msg)) { throw new Error("An OSC message must contain a valid address. Message was: " + JSON.stringify(msg, null, 2)); } var msgCollection = osc.collectMessageParts(msg, options); return osc.joinParts(msgCollection); }; osc.isValidMessage = function (msg) { return msg.address && msg.address.indexOf("/") === 0; }; /** * Reads an OSC bundle. * * @param {DataView} dv the DataView instance to read from * @param {Object} [options] read optoins * @param {Object} [offsetState] an offsetState object that stores the current offset into dv * @return {Object} the bundle or message object that was read */ osc.readBundle = function (dv, options, offsetState) { return osc.readPacket(dv, options, offsetState); }; // Unsupported, non-API function. osc.collectBundlePackets = function (bundle, options, dataCollection) { dataCollection = dataCollection || { byteLength: 0, parts: [] }; osc.addDataPart(osc.writeString("#bundle"), dataCollection); osc.addDataPart(osc.writeTimeTag(bundle.timeTag), dataCollection); for (var i = 0; i < bundle.packets.length; i++) { var packet = bundle.packets[i], collector = packet.address ? osc.collectMessageParts : osc.collectBundlePackets, packetCollection = collector(packet, options); dataCollection.byteLength += packetCollection.byteLength; osc.addDataPart(osc.writeInt32(packetCollection.byteLength), dataCollection); dataCollection.parts = dataCollection.parts.concat(packetCollection.parts); } return dataCollection; }; /** * Writes an OSC bundle. * * @param {Object} a bundle object containing "timeTag" and "packets" properties * @param {object} [options] write options * @return {Uint8Array} an array of bytes containing the message */ osc.writeBundle = function (bundle, options) { if (!osc.isValidBundle(bundle)) { throw new Error("An OSC bundle must contain 'timeTag' and 'packets' properties. " + "Bundle was: " + JSON.stringify(bundle, null, 2)); } options = options || osc.defaults; var bundleCollection = osc.collectBundlePackets(bundle, options); return osc.joinParts(bundleCollection); }; osc.isValidBundle = function (bundle) { return bundle.timeTag !== undefined && bundle.packets !== undefined; }; // Unsupported, non-API function. osc.readBundleContents = function (dv, options, offsetState, len) { var timeTag = osc.readTimeTag(dv, offsetState), packets = []; while (offsetState.idx < len) { var packetSize = osc.readInt32(dv, offsetState), packetLen = offsetState.idx + packetSize, packet = osc.readPacket(dv, options, offsetState, packetLen); packets.push(packet); } return { timeTag: timeTag, packets: packets }; }; /** * Reads an OSC packet, which may consist of either a bundle or a message. * * @param {Array-like} data an array of bytes to read from * @param {Object} [options] read options * @return {Object} a bundle or message object */ osc.readPacket = function (data, options, offsetState, len) { var dv = osc.dataView(data, data.byteOffset, data.byteLength); len = len === undefined ? dv.byteLength : len; offsetState = offsetState || { idx: 0 }; var header = osc.readString(dv, offsetState), firstChar = header[0]; if (firstChar === "#") { return osc.readBundleContents(dv, options, offsetState, len); } else if (firstChar === "/") { return osc.readMessageContents(header, dv, options, offsetState); } throw new Error("The header of an OSC packet didn't contain an OSC address or a #bundle string." + " Header was: " + header); }; /** * Writes an OSC packet, which may consist of either of a bundle or a message. * * @param {Object} a bundle or message object * @param {Object} [options] write options * @return {Uint8Array} an array of bytes containing the message */ osc.writePacket = function (packet, options) { if (osc.isValidMessage(packet)) { return osc.writeMessage(packet, options); } else if (osc.isValidBundle(packet)) { return osc.writeBundle(packet, options); } else { throw new Error("The specified packet was not recognized as a valid OSC message or bundle." + " Packet was: " + JSON.stringify(packet, null, 2)); } }; // Unsupported, non-API. osc.argumentTypes = { i: { reader: "readInt32", writer: "writeInt32" }, h: { reader: "readInt64", writer: "writeInt64" }, f: { reader: "readFloat32", writer: "writeFloat32" }, s: { reader: "readString", writer: "writeString" }, S: { reader: "readString", writer: "writeString" }, b: { reader: "readBlob", writer: "writeBlob" }, t: { reader: "readTimeTag", writer: "writeTimeTag" }, T: { reader: "readTrue" }, F: { reader: "readFalse" }, N: { reader: "readNull" }, I: { reader: "readImpulse" }, d: { reader: "readFloat64", writer: "writeFloat64" }, c: { reader: "readChar32", writer: "writeChar32" }, r: { reader: "readColor", writer: "writeColor" }, m: { reader: "readMIDIBytes", writer: "writeMIDIBytes" }, // [] are special cased within read/writeArguments() }; // Unsupported, non-API function. osc.inferTypeForArgument = function (arg) { var type = typeof arg; // TODO: This is freaking hideous. switch (type) { case "boolean": return arg ? "T" : "F"; case "string": return "s"; case "number": return "f"; case "undefined": return "N"; case "object": if (arg === null) { return "N"; } else if (arg instanceof Uint8Array || arg instanceof ArrayBuffer) { return "b"; } else if (typeof arg.high === "number" && typeof arg.low === "number") { return "h"; } break; } throw new Error("Can't infer OSC argument type for value: " + JSON.stringify(arg, null, 2)); }; // Unsupported, non-API function. osc.annotateArguments = function (args) { var annotated = []; for (var i = 0; i < args.length; i++) { var arg = args[i], msgArg; if (typeof (arg) === "object" && arg.type && arg.value !== undefined) { // We've got an explicitly typed argument. msgArg = arg; } else if (osc.isArray(arg)) { // We've got an array of arguments, // so they each need to be inferred and expanded. msgArg = osc.annotateArguments(arg); } else { var oscType = osc.inferTypeForArgument(arg); msgArg = { type: oscType, value: arg }; } annotated.push(msgArg); } return annotated; }; if (osc.isCommonJS) { module.exports = osc; } }()); ;/* Copyright 2013 Daniel Wirtz Copyright 2009 The Closure Library Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * @license long.js (c) 2013 Daniel Wirtz * Released under the Apache License, Version 2.0 * see: https://github.com/dcodeIO/long.js for details */ (function(global, factory) { /* AMD */ if (typeof define === 'function' && define["amd"]) define([], factory); /* CommonJS */ else if (typeof require === 'function' && typeof module === "object" && module && module["exports"]) module["exports"] = factory(); /* Global */ else (global["dcodeIO"] = global["dcodeIO"] || {})["Long"] = factory(); })(this, function() { "use strict"; /** * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers. * See the from* functions below for more convenient ways of constructing Longs. * @exports Long * @class A Long class for representing a 64 bit two's-complement integer value. * @param {number} low The low (signed) 32 bits of the long * @param {number} high The high (signed) 32 bits of the long * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed * @constructor */ function Long(low, high, unsigned) { /** * The low 32 bits as a signed value. * @type {number} * @expose */ this.low = low | 0; /** * The high 32 bits as a signed value. * @type {number} * @expose */ this.high = high | 0; /** * Whether unsigned or not. * @type {boolean} * @expose */ this.unsigned = !!unsigned; } // The internal representation of a long is the two given signed, 32-bit values. // We use 32-bit pieces because these are the size of integers on which // Javascript performs bit-operations. For operations like addition and // multiplication, we split each number into 16 bit pieces, which can easily be // multiplied within Javascript's floating-point representation without overflow // or change in sign. // // In the algorithms below, we frequently reduce the negative case to the // positive case by negating the input(s) and then post-processing the result. // Note that we must ALWAYS check specially whether those values are MIN_VALUE // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as // a positive number, it overflows back into a negative). Not handling this // case would often result in infinite recursion. // // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from* // methods on which they depend. /** * An indicator used to reliably determine if an object is a Long or not. * @type {boolean} * @const * @expose * @private */ Long.__isLong__; Object.defineProperty(Long.prototype, "__isLong__", { value: true, enumerable: false, configurable: false }); /** * @function * @param {*} obj Object * @returns {boolean} * @inner */ function isLong(obj) { return (obj && obj["__isLong__"]) === true; } /** * Tests if the specified object is a Long. * @function * @param {*} obj Object * @returns {boolean} * @expose */ Long.isLong = isLong; /** * A cache of the Long representations of small integer values. * @type {!Object} * @inner */ var INT_CACHE = {}; /** * A cache of the Long representations of small unsigned integer values. * @type {!Object} * @inner */ var UINT_CACHE = {}; /** * @param {number} value * @param {boolean=} unsigned * @returns {!Long} * @inner */ function fromInt(value, unsigned) { var obj, cachedObj, cache; if (unsigned) { value >>>= 0; if (cache = (0 <= value && value < 256)) { cachedObj = UINT_CACHE[value]; if (cachedObj) return cachedObj; } obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true); if (cache) UINT_CACHE[value] = obj; return obj; } else { value |= 0; if (cache = (-128 <= value && value < 128)) { cachedObj = INT_CACHE[value]; if (cachedObj) return cachedObj; } obj = fromBits(value, value < 0 ? -1 : 0, false); if (cache) INT_CACHE[value] = obj; return obj; } } /** * Returns a Long representing the given 32 bit integer value. * @function * @param {number} value The 32 bit integer in question * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed * @returns {!Long} The corresponding Long value * @expose */ Long.fromInt = fromInt; /** * @param {number} value * @param {boolean=} unsigned * @returns {!Long} * @inner */ function fromNumber(value, unsigned) { if (isNaN(value) || !isFinite(value)) return unsigned ? UZERO : ZERO; if (unsigned) { if (value < 0) return UZERO; if (value >= TWO_PWR_64_DBL) return MAX_UNSIGNED_VALUE; } else { if (value <= -TWO_PWR_63_DBL) return MIN_VALUE; if (value + 1 >= TWO_PWR_63_DBL) return MAX_VALUE; } if (value < 0) return fromNumber(-value, unsigned).neg(); return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned); } /** * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. * @function * @param {number} value The number in question * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed * @returns {!Long} The corresponding Long value * @expose */ Long.fromNumber = fromNumber; /** * @param {number} lowBits * @param {number} highBits * @param {boolean=} unsigned * @returns {!Long} * @inner */ function fromBits(lowBits, highBits, unsigned) { return new Long(lowBits, highBits, unsigned); } /** * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is * assumed to use 32 bits. * @function * @param {number} lowBits The low 32 bits * @param {number} highBits The high 32 bits * @param {boolean=} unsigned Whether unsigned or not, defaults to `false` for signed * @returns {!Long} The corresponding Long value * @expose */ Long.fromBits = fromBits; /** * @function * @param {number} base * @param {number} exponent * @returns {number} * @inner */ var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4) /** * @param {string} str * @param {(boolean|number)=} unsigned * @param {number=} radix * @returns {!Long} * @inner */ function fromString(str, unsigned, radix) { if (str.length === 0) throw Error('empty string'); if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") return ZERO; if (typeof unsigned === 'number') // For goog.math.long compatibility radix = unsigned, unsigned = false; radix = radix || 10; if (radix < 2 || 36 < radix) throw RangeError('radix'); var p; if ((p = str.indexOf('-')) > 0) throw Error('interior hyphen'); else if (p === 0) { return fromString(str.substring(1), unsigned, radix).neg(); } // Do several (8) digits each time through the loop, so as to // minimize the calls to the very expensive emulated div. var radixToPower = fromNumber(pow_dbl(radix, 8)); var result = ZERO; for (var i = 0; i < str.length; i += 8) { var size = Math.min(8, str.length - i), value = parseInt(str.substring(i, i + size), radix); if (size < 8) { var power = fromNumber(pow_dbl(radix, size)); result = result.mul(power).add(fromNumber(value)); } else { result = result.mul(radixToPower); result = result.add(fromNumber(value)); } } result.unsigned = unsigned; return result; } /** * Returns a Long representation of the given string, written using the specified radix. * @function * @param {string} str The textual representation of the Long * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to `false` for signed * @param {number=} radix The radix in which the text is written (2-36), defaults to 10 * @returns {!Long} The corresponding Long value * @expose */ Long.fromString = fromString; /** * @function * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val * @returns {!Long} * @inner */ function fromValue(val) { if (val /* is compatible */ instanceof Long) return val; if (typeof val === 'number') return fromNumber(val); if (typeof val === 'string') return fromString(val); // Throws for non-objects, converts non-instanceof Long: return fromBits(val.low, val.high, val.unsigned); } /** * Converts the specified value to a Long. * @function * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value * @returns {!Long} * @expose */ Long.fromValue = fromValue; // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be // no runtime penalty for these. /** * @type {number} * @const * @inner */ var TWO_PWR_16_DBL = 1 << 16; /** * @type {number} * @const * @inner */ var TWO_PWR_24_DBL = 1 << 24; /** * @type {number} * @const * @inner */ var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL; /** * @type {number} * @const * @inner */ var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL; /** * @type {number} * @const * @inner */ var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2; /** * @type {!Long} * @const * @inner */ var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL); /** * @type {!Long} * @inner */ var ZERO = fromInt(0); /** * Signed zero. * @type {!Long} * @expose */ Long.ZERO = ZERO; /** * @type {!Long} * @inner */ var UZERO = fromInt(0, true); /** * Unsigned zero. * @type {!Long} * @expose */ Long.UZERO = UZERO; /** * @type {!Long} * @inner */ var ONE = fromInt(1); /** * Signed one. * @type {!Long} * @expose */ Long.ONE = ONE; /** * @type {!Long} * @inner */ var UONE = fromInt(1, true); /** * Unsigned one. * @type {!Long} * @expose */ Long.UONE = UONE; /** * @type {!Long} * @inner */ var NEG_ONE = fromInt(-1); /** * Signed negative one. * @type {!Long} * @expose */ Long.NEG_ONE = NEG_ONE; /** * @type {!Long} * @inner */ var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false); /** * Maximum signed value. * @type {!Long} * @expose */ Long.MAX_VALUE = MAX_VALUE; /** * @type {!Long} * @inner */ var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true); /** * Maximum unsigned value. * @type {!Long} * @expose */ Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE; /** * @type {!Long} * @inner */ var MIN_VALUE = fromBits(0, 0x80000000|0, false); /** * Minimum signed value. * @type {!Long} * @expose */ Long.MIN_VALUE = MIN_VALUE; /** * @alias Long.prototype * @inner */ var LongPrototype = Long.prototype; /** * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. * @returns {number} * @expose */ LongPrototype.toInt = function toInt() { return this.unsigned ? this.low >>> 0 : this.low; }; /** * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). * @returns {number} * @expose */ LongPrototype.toNumber = function toNumber() { if (this.unsigned) return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0); return this.high * TWO_PWR_32_DBL + (this.low >>> 0); }; /** * Converts the Long to a string written in the specified radix. * @param {number=} radix Radix (2-36), defaults to 10 * @returns {string} * @override * @throws {RangeError} If `radix` is out of range * @expose */ LongPrototype.toString = function toString(radix) { radix = radix || 10; if (radix < 2 || 36 < radix) throw RangeError('radix'); if (this.isZero()) return '0'; if (this.isNegative()) { // Unsigned Longs are never negative if (this.eq(MIN_VALUE)) { // We need to change the Long value before it can be negated, so we remove // the bottom-most digit in this base and then recurse to do the rest. var radixLong = fromNumber(radix), div = this.div(radixLong), rem1 = div.mul(radixLong).sub(this); return div.toString(radix) + rem1.toInt().toString(radix); } else return '-' + this.neg().toString(radix); } // Do several (6) digits each time through the loop, so as to // minimize the calls to the very expensive emulated div. var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned), rem = this; var result = ''; while (true) { var remDiv = rem.div(radixToPower), intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0, digits = intval.toString(radix); rem = remDiv; if (rem.isZero()) return digits + result; else { while (digits.length < 6) digits = '0' + digits; result = '' + digits + result; } } }; /** * Gets the high 32 bits as a signed integer. * @returns {number} Signed high bits * @expose */ LongPrototype.getHighBits = function getHighBits() { return this.high; }; /** * Gets the high 32 bits as an unsigned integer. * @returns {number} Unsigned high bits * @expose */ LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() { return this.high >>> 0; }; /** * Gets the low 32 bits as a signed integer. * @returns {number} Signed low bits * @expose */ LongPrototype.getLowBits = function getLowBits() { return this.low; }; /** * Gets the low 32 bits as an unsigned integer. * @returns {number} Unsigned low bits * @expose */ LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() { return this.low >>> 0; }; /** * Gets the number of bits needed to represent the absolute value of this Long. * @returns {number} * @expose */ LongPrototype.getNumBitsAbs = function getNumBitsAbs() { if (this.isNegative()) // Unsigned Longs are never negative return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs(); var val = this.high != 0 ? this.high : this.low; for (var bit = 31; bit > 0; bit--) if ((val & (1 << bit)) != 0) break; return this.high != 0 ? bit + 33 : bit + 1; }; /** * Tests if this Long's value equals zero. * @returns {boolean} * @expose */ LongPrototype.isZero = function isZero() { return this.high === 0 && this.low === 0; }; /** * Tests if this Long's value is negative. * @returns {boolean} * @expose */ LongPrototype.isNegative = function isNegative() { return !this.unsigned && this.high < 0; }; /** * Tests if this Long's value is positive. * @returns {boolean} * @expose */ LongPrototype.isPositive = function isPositive() { return this.unsigned || this.high >= 0; }; /** * Tests if this Long's value is odd. * @returns {boolean} * @expose */ LongPrototype.isOdd = function isOdd() { return (this.low & 1) === 1; }; /** * Tests if this Long's value is even. * @returns {boolean} * @expose */ LongPrototype.isEven = function isEven() { return (this.low & 1) === 0; }; /** * Tests if this Long's value equals the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.equals = function equals(other) { if (!isLong(other)) other = fromValue(other); if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1) return false; return this.high === other.high && this.low === other.low; }; /** * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.eq = LongPrototype.equals; /** * Tests if this Long's value differs from the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.notEquals = function notEquals(other) { return !this.eq(/* validates */ other); }; /** * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.neq = LongPrototype.notEquals; /** * Tests if this Long's value is less than the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.lessThan = function lessThan(other) { return this.comp(/* validates */ other) < 0; }; /** * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.lt = LongPrototype.lessThan; /** * Tests if this Long's value is less than or equal the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) { return this.comp(/* validates */ other) <= 0; }; /** * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.lte = LongPrototype.lessThanOrEqual; /** * Tests if this Long's value is greater than the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.greaterThan = function greaterThan(other) { return this.comp(/* validates */ other) > 0; }; /** * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.gt = LongPrototype.greaterThan; /** * Tests if this Long's value is greater than or equal the specified's. * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) { return this.comp(/* validates */ other) >= 0; }; /** * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}. * @function * @param {!Long|number|string} other Other value * @returns {boolean} * @expose */ LongPrototype.gte = LongPrototype.greaterThanOrEqual; /** * Compares this Long's value with the specified's. * @param {!Long|number|string} other Other value * @returns {number} 0 if they are the same, 1 if the this is greater and -1 * if the given one is greater * @expose */ LongPrototype.compare = function compare(other) { if (!isLong(other)) other = fromValue(other); if (this.eq(other)) return 0; var thisNeg = this.isNegative(), otherNeg = other.isNegative(); if (thisNeg && !otherNeg) return -1; if (!thisNeg && otherNeg) return 1; // At this point the sign bits are the same if (!this.unsigned) return this.sub(other).isNegative() ? -1 : 1; // Both are positive if at least one is unsigned return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1; }; /** * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}. * @function * @param {!Long|number|string} other Other value * @returns {number} 0 if they are the same, 1 if the this is greater and -1 * if the given one is greater * @expose */ LongPrototype.comp = LongPrototype.compare; /** * Negates this Long's value. * @returns {!Long} Negated Long * @expose */ LongPrototype.negate = function negate() { if (!this.unsigned && this.eq(MIN_VALUE)) return MIN_VALUE; return this.not().add(ONE); }; /** * Negates this Long's value. This is an alias of {@link Long#negate}. * @function * @returns {!Long} Negated Long * @expose */ LongPrototype.neg = LongPrototype.negate; /** * Returns the sum of this and the specified Long. * @param {!Long|number|string} addend Addend * @returns {!Long} Sum * @expose */ LongPrototype.add = function add(addend) { if (!isLong(addend)) addend = fromValue(addend); // Divide each number into 4 chunks of 16 bits, and then sum the chunks. var a48 = this.high >>> 16; var a32 = this.high & 0xFFFF; var a16 = this.low >>> 16; var a00 = this.low & 0xFFFF; var b48 = addend.high >>> 16; var b32 = addend.high & 0xFFFF; var b16 = addend.low >>> 16; var b00 = addend.low & 0xFFFF; var c48 = 0, c32 = 0, c16 = 0, c00 = 0; c00 += a00 + b00; c16 += c00 >>> 16; c00 &= 0xFFFF; c16 += a16 + b16; c32 += c16 >>> 16; c16 &= 0xFFFF; c32 += a32 + b32; c48 += c32 >>> 16; c32 &= 0xFFFF; c48 += a48 + b48; c48 &= 0xFFFF; return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); }; /** * Returns the difference of this and the specified Long. * @param {!Long|number|string} subtrahend Subtrahend * @returns {!Long} Difference * @expose */ LongPrototype.subtract = function subtract(subtrahend) { if (!isLong(subtrahend)) subtrahend = fromValue(subtrahend); return this.add(subtrahend.neg()); }; /** * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}. * @function * @param {!Long|number|string} subtrahend Subtrahend * @returns {!Long} Difference * @expose */ LongPrototype.sub = LongPrototype.subtract; /** * Returns the product of this and the specified Long. * @param {!Long|number|string} multiplier Multiplier * @returns {!Long} Product * @expose */ LongPrototype.multiply = function multiply(multiplier) { if (this.isZero()) return ZERO; if (!isLong(multiplier)) multiplier = fromValue(multiplier); if (multiplier.isZero()) return ZERO; if (this.eq(MIN_VALUE)) return multiplier.isOdd() ? MIN_VALUE : ZERO; if (multiplier.eq(MIN_VALUE)) return this.isOdd() ? MIN_VALUE : ZERO; if (this.isNegative()) { if (multiplier.isNegative()) return this.neg().mul(multiplier.neg()); else return this.neg().mul(multiplier).neg(); } else if (multiplier.isNegative()) return this.mul(multiplier.neg()).neg(); // If both longs are small, use float multiplication if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24)) return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned); // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products. // We can skip products that would overflow. var a48 = this.high >>> 16; var a32 = this.high & 0xFFFF; var a16 = this.low >>> 16; var a00 = this.low & 0xFFFF; var b48 = multiplier.high >>> 16; var b32 = multiplier.high & 0xFFFF; var b16 = multiplier.low >>> 16; var b00 = multiplier.low & 0xFFFF; var c48 = 0, c32 = 0, c16 = 0, c00 = 0; c00 += a00 * b00; c16 += c00 >>> 16; c00 &= 0xFFFF; c16 += a16 * b00; c32 += c16 >>> 16; c16 &= 0xFFFF; c16 += a00 * b16; c32 += c16 >>> 16; c16 &= 0xFFFF; c32 += a32 * b00; c48 += c32 >>> 16; c32 &= 0xFFFF; c32 += a16 * b16; c48 += c32 >>> 16; c32 &= 0xFFFF; c32 += a00 * b32; c48 += c32 >>> 16; c32 &= 0xFFFF; c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; c48 &= 0xFFFF; return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned); }; /** * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}. * @function * @param {!Long|number|string} multiplier Multiplier * @returns {!Long} Product * @expose */ LongPrototype.mul = LongPrototype.multiply; /** * Returns this Long divided by the specified. * @param {!Long|number|string} divisor Divisor * @returns {!Long} Quotient * @expose */ LongPrototype.divide = function divide(divisor) { if (!isLong(divisor)) divisor = fromValue(divisor); if (divisor.isZero()) throw Error('division by zero'); if (this.isZero()) return this.unsigned ? UZERO : ZERO; var approx, rem, res; if (this.eq(MIN_VALUE)) { if (divisor.eq(ONE) || divisor.eq(NEG_ONE)) return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE else if (divisor.eq(MIN_VALUE)) return ONE; else { // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. var halfThis = this.shr(1); approx = halfThis.div(divisor).shl(1); if (approx.eq(ZERO)) { return divisor.isNegative() ? ONE : NEG_ONE; } else { rem = this.sub(divisor.mul(approx)); res = approx.add(rem.div(divisor)); return res; } } } else if (divisor.eq(MIN_VALUE)) return this.unsigned ? UZERO : ZERO; if (this.isNegative()) { if (divisor.isNegative()) return this.neg().div(divisor.neg()); return this.neg().div(divisor).neg(); } else if (divisor.isNegative()) return this.div(divisor.neg()).neg(); // Repeat the following until the remainder is less than other: find a // floating-point that approximates remainder / other *from below*, add this // into the result, and subtract it from the remainder. It is critical that // the approximate value is less than or equal to the real value so that the // remainder never becomes negative. res = ZERO; rem = this; while (rem.gte(divisor)) { // Approximate the result of division. This may be a little greater or // smaller than the actual value. approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber())); // We will tweak the approximate result by changing it in the 48-th digit or // the smallest non-fractional digit, whichever is larger. var log2 = Math.ceil(Math.log(approx) / Math.LN2), delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48), // Decrease the approximation until it is smaller than the remainder. Note // that if it is too large, the product overflows and is negative. approxRes = fromNumber(approx), approxRem = approxRes.mul(divisor); while (approxRem.isNegative() || approxRem.gt(rem)) { approx -= delta; approxRes = fromNumber(approx, this.unsigned); approxRem = approxRes.mul(divisor); } // We know the answer can't be zero... and actually, zero would cause // infinite recursion since we would make no progress. if (approxRes.isZero()) approxRes = ONE; res = res.add(approxRes); rem = rem.sub(approxRem); } return res; }; /** * Returns this Long divided by the specified. This is an alias of {@link Long#divide}. * @function * @param {!Long|number|string} divisor Divisor * @returns {!Long} Quotient * @expose */ LongPrototype.div = LongPrototype.divide; /** * Returns this Long modulo the specified. * @param {!Long|number|string} divisor Divisor * @returns {!Long} Remainder * @expose */ LongPrototype.modulo = function modulo(divisor) { if (!isLong(divisor)) divisor = fromValue(divisor); return this.sub(this.div(divisor).mul(divisor)); }; /** * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}. * @function * @param {!Long|number|string} divisor Divisor * @returns {!Long} Remainder * @expose */ LongPrototype.mod = LongPrototype.modulo; /** * Returns the bitwise NOT of this Long. * @returns {!Long} * @expose */ LongPrototype.not = function not() { return fromBits(~this.low, ~this.high, this.unsigned); }; /** * Returns the bitwise AND of this Long and the specified. * @param {!Long|number|string} other Other Long * @returns {!Long} * @expose */ LongPrototype.and = function and(other) { if (!isLong(other)) other = fromValue(other); return fromBits(this.low & other.low, this.high & other.high, this.unsigned); }; /** * Returns the bitwise OR of this Long and the specified. * @param {!Long|number|string} other Other Long * @returns {!Long} * @expose */ LongPrototype.or = function or(other) { if (!isLong(other)) other = fromValue(other); return fromBits(this.low | other.low, this.high | other.high, this.unsigned); }; /** * Returns the bitwise XOR of this Long and the given one. * @param {!Long|number|string} other Other Long * @returns {!Long} * @expose */ LongPrototype.xor = function xor(other) { if (!isLong(other)) other = fromValue(other); return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned); }; /** * Returns this Long with bits shifted to the left by the given amount. * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long * @expose */ LongPrototype.shiftLeft = function shiftLeft(numBits) { if (isLong(numBits)) numBits = numBits.toInt(); if ((numBits &= 63) === 0) return this; else if (numBits < 32) return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned); else return fromBits(0, this.low << (numBits - 32), this.unsigned); }; /** * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}. * @function * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long * @expose */ LongPrototype.shl = LongPrototype.shiftLeft; /** * Returns this Long with bits arithmetically shifted to the right by the given amount. * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long * @expose */ LongPrototype.shiftRight = function shiftRight(numBits) { if (isLong(numBits)) numBits = numBits.toInt(); if ((numBits &= 63) === 0) return this; else if (numBits < 32) return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned); else return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned); }; /** * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}. * @function * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long * @expose */ LongPrototype.shr = LongPrototype.shiftRight; /** * Returns this Long with bits logically shifted to the right by the given amount. * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long * @expose */ LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) { if (isLong(numBits)) numBits = numBits.toInt(); numBits &= 63; if (numBits === 0) return this; else { var high = this.high; if (numBits < 32) { var low = this.low; return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned); } else if (numBits === 32) return fromBits(high, 0, this.unsigned); else return fromBits(high >>> (numBits - 32), 0, this.unsigned); } }; /** * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}. * @function * @param {number|!Long} numBits Number of bits * @returns {!Long} Shifted Long * @expose */ LongPrototype.shru = LongPrototype.shiftRightUnsigned; /** * Converts this Long to signed. * @returns {!Long} Signed long * @expose */ LongPrototype.toSigned = function toSigned() { if (!this.unsigned) return this; return fromBits(this.low, this.high, false); }; /** * Converts this Long to unsigned. * @returns {!Long} Unsigned long * @expose */ LongPrototype.toUnsigned = function toUnsigned() { if (this.unsigned) return this; return fromBits(this.low, this.high, true); }; return Long; }); ;/* * slip.js: A plain JavaScript SLIP implementation that works in both the browser and Node.js * * Copyright 2014, Colin Clark * Licensed under the MIT and GPL 3 licenses. */ /*global exports, define*/ (function (root, factory) { "use strict"; if (typeof exports === "object") { // We're in a CommonJS-style loader. root.slip = exports; factory(exports); } else if (typeof define === "function" && define.amd) { // We're in an AMD-style loader. define(["exports"], function (exports) { root.slip = exports; return (root.slip, factory(exports)); }); } else { // Plain old browser. root.slip = {}; factory(root.slip); } }(this, function (exports) { "use strict"; var slip = exports; slip.END = 192; slip.ESC = 219; slip.ESC_END = 220; slip.ESC_ESC = 221; slip.byteArray = function (data, offset, length) { return data instanceof ArrayBuffer ? new Uint8Array(data, offset, length) : data; }; slip.expandByteArray = function (arr) { var expanded = new Uint8Array(arr.length * 2); expanded.set(arr); return expanded; }; slip.sliceByteArray = function (arr, start, end) { var sliced = arr.buffer.slice ? arr.buffer.slice(start, end) : arr.subarray(start, end); return new Uint8Array(sliced); }; /** * SLIP encodes a byte array. * * @param {Array-like} data a Uint8Array, Node.js Buffer, ArrayBuffer, or [] containing raw bytes * @param {Object} options encoder options * @return {Uint8Array} the encoded copy of the data */ slip.encode = function (data, o) { o = o || {}; o.bufferPadding = o.bufferPadding || 4; // Will be rounded to the nearest 4 bytes. data = slip.byteArray(data, o.offset, o.byteLength); var bufLen = (data.length + o.bufferPadding + 3) & ~0x03, encoded = new Uint8Array(bufLen), j = 1; encoded[0] = slip.END; for (var i = 0; i < data.length; i++) { // We always need enough space for two value bytes plus a trailing END. if (j > encoded.length - 3) { encoded = slip.expandByteArray(encoded); } var val = data[i]; if (val === slip.END) { encoded[j++] = slip.ESC; val = slip.ESC_END; } else if (val === slip.ESC) { encoded[j++] = slip.ESC; val = slip.ESC_ESC; } encoded[j++] = val; } encoded[j] = slip.END; return slip.sliceByteArray(encoded, 0, j + 1); }; /** * Creates a new SLIP Decoder. * @constructor * * @param {Function} onMessage a callback function that will be invoked when a message has been fully decoded * @param {Number} maxBufferSize the maximum size of a incoming message; larger messages will throw an error */ slip.Decoder = function (o) { o = typeof o !== "function" ? o || {} : { onMessage: o }; this.maxMessageSize = o.maxMessageSize || 10485760; // Defaults to 10 MB. this.bufferSize = o.bufferSize || 1024; // Message buffer defaults to 1 KB. this.msgBuffer = new Uint8Array(this.bufferSize); this.msgBufferIdx = 0; this.onMessage = o.onMessage; this.onError = o.onError; this.escape = false; }; var p = slip.Decoder.prototype; /** * Decodes a SLIP data packet. * The onMessage callback will be invoked when a complete message has been decoded. * * @param {Array-like} data an incoming stream of bytes */ p.decode = function (data) { data = slip.byteArray(data); var msg; for (var i = 0; i < data.length; i++) { var val = data[i]; if (this.escape) { if (val === slip.ESC_ESC) { val = slip.ESC; } else if (val === slip.ESC_END) { val = slip.END; } } else { if (val === slip.ESC) { this.escape = true; continue; } if (val === slip.END) { msg = this.handleEnd(); continue; } } var more = this.addByte(val); if (!more) { this.handleMessageMaxError(); } } return msg; }; p.handleMessageMaxError = function () { if (this.onError) { this.onError(this.msgBuffer.subarray(0), "The message is too large; the maximum message size is " + this.maxMessageSize / 1024 + "KB. Use a larger maxMessageSize if necessary."); } // Reset everything and carry on. this.msgBufferIdx = 0; this.escape = false; }; // Unsupported, non-API method. p.addByte = function (val) { if (this.msgBufferIdx > this.msgBuffer.length - 1) { this.msgBuffer = slip.expandByteArray(this.msgBuffer); } this.msgBuffer[this.msgBufferIdx++] = val; this.escape = false; return this.msgBuffer.length < this.maxMessageSize; }; // Unsupported, non-API method. p.handleEnd = function () { if (this.msgBufferIdx === 0) { return; // Toss opening END byte and carry on. } var msg = slip.sliceByteArray(this.msgBuffer, 0, this.msgBufferIdx); if (this.onMessage) { this.onMessage(msg); } // Clear our pointer into the message buffer. this.msgBufferIdx = 0; return msg; }; return slip; })); ;/*! * EventEmitter v4.2.11 - git.io/ee * Unlicense - http://unlicense.org/ * Oliver Caldwell - http://oli.me.uk/ * @preserve */ ;(function () { 'use strict'; /** * Class for managing events. * Can be extended to provide event functionality in other classes. * * @class EventEmitter Manages event registering and emitting. */ function EventEmitter() {} // Shortcuts to improve speed and size var proto = EventEmitter.prototype; var exports = this; var originalGlobalValue = exports.EventEmitter; /** * Finds the index of the listener for the event in its storage array. * * @param {Function[]} listeners Array of listeners to search through. * @param {Function} listener Method to look for. * @return {Number} Index of the specified listener, -1 if not found * @api private */ function indexOfListener(listeners, listener) { var i = listeners.length; while (i--) { if (listeners[i].listener === listener) { return i; } } return -1; } /** * Alias a method while keeping the context correct, to allow for overwriting of target method. * * @param {String} name The name of the target method. * @return {Function} The aliased method * @api private */ function alias(name) { return function aliasClosure() { return this[name].apply(this, arguments); }; } /** * Returns the listener array for the specified event. * Will initialise the event object and listener arrays if required. * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them. * Each property in the object response is an array of listener functions. * * @param {String|RegExp} evt Name of the event to return the listeners from. * @return {Function[]|Object} All listener functions for the event. */ proto.getListeners = function getListeners(evt) { var events = this._getEvents(); var response; var key; // Return a concatenated array of all matching events if // the selector is a regular expression. if (evt instanceof RegExp) { response = {}; for (key in events) { if (events.hasOwnProperty(key) && evt.test(key)) { response[key] = events[key]; } } } else { response = events[evt] || (events[evt] = []); } return response; }; /** * Takes a list of listener objects and flattens it into a list of listener functions. * * @param {Object[]} listeners Raw listener objects. * @return {Function[]} Just the listener functions. */ proto.flattenListeners = function flattenListeners(listeners) { var flatListeners = []; var i; for (i = 0; i < listeners.length; i += 1) { flatListeners.push(listeners[i].listener); } return flatListeners; }; /** * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful. * * @param {String|RegExp} evt Name of the event to return the listeners from. * @return {Object} All listener functions for an event in an object. */ proto.getListenersAsObject = function getListenersAsObject(evt) { var listeners = this.getListeners(evt); var response; if (listeners instanceof Array) { response = {}; response[evt] = listeners; } return response || listeners; }; /** * Adds a listener function to the specified event. * The listener will not be added if it is a duplicate. * If the listener returns true then it will be removed after it is called. * If you pass a regular expression as the event name then the listener will be added to all events that match it. * * @param {String|RegExp} evt Name of the event to attach the listener to. * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. * @return {Object} Current instance of EventEmitter for chaining. */ proto.addListener = function addListener(evt, listener) { var listeners = this.getListenersAsObject(evt); var listenerIsWrapped = typeof listener === 'object'; var key; for (key in listeners) { if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) { listeners[key].push(listenerIsWrapped ? listener : { listener: listener, once: false }); } } return this; }; /** * Alias of addListener */ proto.on = alias('addListener'); /** * Semi-alias of addListener. It will add a listener that will be * automatically removed after its first execution. * * @param {String|RegExp} evt Name of the event to attach the listener to. * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. * @return {Object} Current instance of EventEmitter for chaining. */ proto.addOnceListener = function addOnceListener(evt, listener) { return this.addListener(evt, { listener: listener, once: true }); }; /** * Alias of addOnceListener. */ proto.once = alias('addOnceListener'); /** * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad. * You need to tell it what event names should be matched by a regex. * * @param {String} evt Name of the event to create. * @return {Object} Current instance of EventEmitter for chaining. */ proto.defineEvent = function defineEvent(evt) { this.getListeners(evt); return this; }; /** * Uses defineEvent to define multiple events. * * @param {String[]} evts An array of event names to define. * @return {Object} Current instance of EventEmitter for chaining. */ proto.defineEvents = function defineEvents(evts) { for (var i = 0; i < evts.length; i += 1) { this.defineEvent(evts[i]); } return this; }; /** * Removes a listener function from the specified event. * When passed a regular expression as the event name, it will remove the listener from all events that match it. * * @param {String|RegExp} evt Name of the event to remove the listener from. * @param {Function} listener Method to remove from the event. * @return {Object} Current instance of EventEmitter for chaining. */ proto.removeListener = function removeListener(evt, listener) { var listeners = this.getListenersAsObject(evt); var index; var key; for (key in listeners) { if (listeners.hasOwnProperty(key)) { index = indexOfListener(listeners[key], listener); if (index !== -1) { listeners[key].splice(index, 1); } } } return this; }; /** * Alias of removeListener */ proto.off = alias('removeListener'); /** * Adds listeners in bulk using the manipulateListeners method. * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added. * You can also pass it a regular expression to add the array of listeners to all events that match it. * Yeah, this function does quite a bit. That's probably a bad thing. * * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once. * @param {Function[]} [listeners] An optional array of listener functions to add. * @return {Object} Current instance of EventEmitter for chaining. */ proto.addListeners = function addListeners(evt, listeners) { // Pass through to manipulateListeners return this.manipulateListeners(false, evt, listeners); }; /** * Removes listeners in bulk using the manipulateListeners method. * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. * You can also pass it an event name and an array of listeners to be removed. * You can also pass it a regular expression to remove the listeners from all events that match it. * * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once. * @param {Function[]} [listeners] An optional array of listener functions to remove. * @return {Object} Current instance of EventEmitter for chaining. */ proto.removeListeners = function removeListeners(evt, listeners) { // Pass through to manipulateListeners return this.manipulateListeners(true, evt, listeners); }; /** * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level. * The first argument will determine if the listeners are removed (true) or added (false). * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. * You can also pass it an event name and an array of listeners to be added/removed. * You can also pass it a regular expression to manipulate the listeners of all events that match it. * * @param {Boolean} remove True if you want to remove listeners, false if you want to add. * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once. * @param {Function[]} [listeners] An optional array of listener functions to add/remove. * @return {Object} Current instance of EventEmitter for chaining. */ proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) { var i; var value; var single = remove ? this.removeListener : this.addListener; var multiple = remove ? this.removeListeners : this.addListeners; // If evt is an object then pass each of its properties to this method if (typeof evt === 'object' && !(evt instanceof RegExp)) { for (i in evt) { if (evt.hasOwnProperty(i) && (value = evt[i])) { // Pass the single listener straight through to the singular method if (typeof value === 'function') { single.call(this, i, value); } else { // Otherwise pass back to the multiple function multiple.call(this, i, value); } } } } else { // So evt must be a string // And listeners must be an array of listeners // Loop over it and pass each one to the multiple method i = listeners.length; while (i--) { single.call(this, evt, listeners[i]); } } return this; }; /** * Removes all listeners from a specified event. * If you do not specify an event then all listeners will be removed. * That means every event will be emptied. * You can also pass a regex to remove all events that match it. * * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. * @return {Object} Current instance of EventEmitter for chaining. */ proto.removeEvent = function removeEvent(evt) { var type = typeof evt; var events = this._getEvents(); var key; // Remove different things depending on the state of evt if (type === 'string') { // Remove all listeners for the specified event delete events[evt]; } else if (evt instanceof RegExp) { // Remove all events matching the regex. for (key in events) { if (events.hasOwnProperty(key) && evt.test(key)) { delete events[key]; } } } else { // Remove all listeners in all events delete this._events; } return this; }; /** * Alias of removeEvent. * * Added to mirror the node API. */ proto.removeAllListeners = alias('removeEvent'); /** * Emits an event of your choice. * When emitted, every listener attached to that event will be executed. * If you pass the optional argument array then those arguments will be passed to every listener upon execution. * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately. * So they will not arrive within the array on the other side, they will be separate. * You can also pass a regular expression to emit to all events that match it. * * @param {String|RegExp} evt Name of the event to emit and execute listeners for. * @param {Array} [args] Optional array of arguments to be passed to each listener. * @return {Object} Current instance of EventEmitter for chaining. */ proto.emitEvent = function emitEvent(evt, args) { var listenersMap = this.getListenersAsObject(evt); var listeners; var listener; var i; var key; var response; for (key in listenersMap) { if (listenersMap.hasOwnProperty(key)) { listeners = listenersMap[key].slice(0); i = listeners.length; while (i--) { // If the listener returns true then it shall be removed from the event // The function is executed either with a basic call or an apply if there is an args array listener = listeners[i]; if (listener.once === true) { this.removeListener(evt, listener.listener); } response = listener.listener.apply(this, args || []); if (response === this._getOnceReturnValue()) { this.removeListener(evt, listener.listener); } } } } return this; }; /** * Alias of emitEvent */ proto.trigger = alias('emitEvent'); /** * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on. * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it. * * @param {String|RegExp} evt Name of the event to emit and execute listeners for. * @param {...*} Optional additional arguments to be passed to each listener. * @return {Object} Current instance of EventEmitter for chaining. */ proto.emit = function emit(evt) { var args = Array.prototype.slice.call(arguments, 1); return this.emitEvent(evt, args); }; /** * Sets the current value to check against when executing listeners. If a * listeners return value matches the one set here then it will be removed * after execution. This value defaults to true. * * @param {*} value The new value to check for when executing listeners. * @return {Object} Current instance of EventEmitter for chaining. */ proto.setOnceReturnValue = function setOnceReturnValue(value) { this._onceReturnValue = value; return this; }; /** * Fetches the current value to check against when executing listeners. If * the listeners return value matches this one then it should be removed * automatically. It will return true by default. * * @return {*|Boolean} The current value to check for or the default, true. * @api private */ proto._getOnceReturnValue = function _getOnceReturnValue() { if (this.hasOwnProperty('_onceReturnValue')) { return this._onceReturnValue; } else { return true; } }; /** * Fetches the events object and creates one if required. * * @return {Object} The events storage object. * @api private */ proto._getEvents = function _getEvents() { return this._events || (this._events = {}); }; /** * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version. * * @return {Function} Non conflicting EventEmitter class. */ EventEmitter.noConflict = function noConflict() { exports.EventEmitter = originalGlobalValue; return EventEmitter; }; // Expose the class either via AMD, CommonJS or the global object if (typeof define === 'function' && define.amd) { define(function () { return EventEmitter; }); } else if (typeof module === 'object' && module.exports){ module.exports = EventEmitter; } else { exports.EventEmitter = EventEmitter; } }.call(this)); ;/* * osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js * * Cross-platform base transport library for osc.js. * * Copyright 2014-2015, Colin Clark * Licensed under the MIT and GPL 3 licenses. */ /* global require, module */ var osc = osc || require("./osc.js"), slip = slip || require("slip"), EventEmitter = EventEmitter || require("events").EventEmitter; (function () { "use strict"; // Unsupported, non-API function. osc.firePacketEvents = function (port, packet, timeTag, packetInfo) { if (packet.address) { port.emit("message", packet, timeTag, packetInfo); } else { osc.fireBundleEvents(port, packet, timeTag, packetInfo); } }; // Unsupported, non-API function. osc.fireBundleEvents = function (port, bundle, timeTag, packetInfo) { port.emit("bundle", bundle, timeTag, packetInfo); for (var i = 0; i < bundle.packets.length; i++) { var packet = bundle.packets[i]; osc.firePacketEvents(port, packet, bundle.timeTag, packetInfo); } }; osc.Port = function (options) { this.options = options || {}; this.on("data", this.decodeOSC.bind(this)); }; var p = osc.Port.prototype = Object.create(EventEmitter.prototype); p.constructor = osc.Port; p.send = function (oscPacket) { var args = Array.prototype.slice.call(arguments), encoded = this.encodeOSC(oscPacket), buf = osc.nativeBuffer(encoded); args[0] = buf; this.sendRaw.apply(this, args); }; p.encodeOSC = function (packet) { // TODO gh-39: This is unsafe; we should only access the underlying // buffer within the range of its view. packet = packet.buffer ? packet.buffer : packet; var encoded; try { encoded = osc.writePacket(packet, this.options); } catch (err) { this.emit("error", err); } return encoded; }; p.decodeOSC = function (data, packetInfo) { data = osc.byteArray(data); this.emit("raw", data, packetInfo); try { var packet = osc.readPacket(data, this.options); this.emit("osc", packet, packetInfo); osc.firePacketEvents(this, packet, undefined, packetInfo); } catch (err) { this.emit("error", err); } }; osc.SLIPPort = function (options) { var that = this; var o = this.options = options || {}; o.useSLIP = o.useSLIP === undefined ? true : o.useSLIP; this.decoder = new slip.Decoder({ onMessage: this.decodeOSC.bind(this), onError: function (err) { that.emit("error", err); } }); var decodeHandler = o.useSLIP ? this.decodeSLIPData : this.decodeOSC; this.on("data", decodeHandler.bind(this)); }; p = osc.SLIPPort.prototype = Object.create(osc.Port.prototype); p.constructor = osc.SLIPPort; p.encodeOSC = function (packet) { // TODO gh-39: This is unsafe; we should only access the underlying // buffer within the range of its view. packet = packet.buffer ? packet.buffer : packet; var framed; try { var encoded = osc.writePacket(packet, this.options); framed = slip.encode(encoded); } catch (err) { this.emit("error", err); } return framed; }; p.decodeSLIPData = function (data, packetInfo) { // TODO: Get packetInfo through SLIP decoder. this.decoder.decode(data, packetInfo); }; // Unsupported, non-API function. osc.relay = function (from, to, eventName, sendFnName, transformFn, sendArgs) { eventName = eventName || "message"; sendFnName = sendFnName || "send"; transformFn = transformFn || function () {}; sendArgs = sendArgs ? [null].concat(sendArgs) : []; var listener = function (data) { sendArgs[0] = data; data = transformFn(data); to[sendFnName].apply(to, sendArgs); }; from.on(eventName, listener); return { eventName: eventName, listener: listener }; }; // Unsupported, non-API function. osc.relayPorts = function (from, to, o) { var eventName = o.raw ? "raw" : "osc", sendFnName = o.raw ? "sendRaw" : "send"; return osc.relay(from, to, eventName, sendFnName, o.transform); }; // Unsupported, non-API function. osc.stopRelaying = function (from, relaySpec) { from.removeListener(relaySpec.eventName, relaySpec.listener); }; /** * A Relay connects two sources of OSC data together, * relaying all OSC messages received by each port to the other. * @constructor * * @param {osc.Port} port1 the first port to relay * @param {osc.Port} port2 the second port to relay * @param {Object} options the configuration options for this relay */ osc.Relay = function (port1, port2, options) { var o = this.options = options || {}; o.raw = false; this.port1 = port1; this.port2 = port2; this.listen(); }; p = osc.Relay.prototype = Object.create(EventEmitter.prototype); p.constructor = osc.Relay; p.open = function () { this.port1.open(); this.port2.open(); }; p.listen = function () { if (this.port1Spec && this.port2Spec) { this.close(); } this.port1Spec = osc.relayPorts(this.port1, this.port2, this.options); this.port2Spec = osc.relayPorts(this.port2, this.port1, this.options); // Bind port close listeners to ensure that the relay // will stop forwarding messages if one of its ports close. // Users are still responsible for closing the underlying ports // if necessary. var closeListener = this.close.bind(this); this.port1.on("close", closeListener); this.port2.on("close", closeListener); }; p.close = function () { osc.stopRelaying(this.port1, this.port1Spec); osc.stopRelaying(this.port2, this.port2Spec); this.emit("close", this.port1, this.port2); }; // If we're in a require-compatible environment, export ourselves. if (typeof module !== "undefined" && module.exports) { module.exports = osc; } }()); ;/* * osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js * * Browser transports for osc.js * * Copyright 2014-2015, Colin Clark * Licensed under the MIT and GPL 3 licenses. */ /*global WebSocket*/ var osc = osc; (function () { "use strict"; osc.WebSocketPort = function (options) { osc.Port.call(this, options); this.on("open", this.listen.bind(this)); this.socket = options.socket; if (this.socket) { if (this.socket.readyState === 1) { this.emit("open", this.socket); } else { this.open(); } } }; var p = osc.WebSocketPort.prototype = Object.create(osc.Port.prototype); p.constructor = osc.WebSocketPort; p.open = function () { if (!this.socket) { this.socket = new WebSocket(this.options.url); } this.socket.binaryType = "arraybuffer"; var that = this; this.socket.onopen = function () { that.emit("open", that.socket); }; }; p.listen = function () { var that = this; this.socket.onmessage = function (e) { that.emit("data", e.data, e); }; this.socket.onerror = function (err) { that.emit("error", err); }; this.socket.onclose = function (e) { that.emit("close", e); }; that.emit("ready"); }; p.sendRaw = function (encoded) { if (!this.socket) { return; } this.socket.send(encoded); }; p.close = function (code, reason) { this.socket.close(code, reason); }; }()); ;/* * osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js * * Chrome App transports for osc.js * * Copyright 2014-2015, Colin Clark * Licensed under the MIT and GPL 3 licenses. */ /*global chrome*/ var osc = osc || {}; (function () { "use strict"; osc.listenToTransport = function (that, transport, idName) { transport.onReceive.addListener(function (e) { if (e[idName] === that[idName]) { that.emit("data", e.data, e); } }); transport.onReceiveError.addListener(function (err) { that.emit("error", err); }); that.emit("ready"); }; osc.emitNetworkError = function (that, resultCode) { that.emit("error", "There was an error while opening the UDP socket connection. Result code: " + resultCode); }; osc.SerialPort = function (options) { this.on("open", this.listen.bind(this)); osc.SLIPPort.call(this, options); this.connectionId = this.options.connectionId; if (this.connectionId) { this.emit("open", this.connectionId); } }; var p = osc.SerialPort.prototype = Object.create(osc.SLIPPort.prototype); p.constructor = osc.SerialPort; p.open = function () { var that = this, connectionOpts = { bitrate: that.options.bitrate }; chrome.serial.connect(this.options.devicePath, connectionOpts, function (info) { that.connectionId = info.connectionId; that.emit("open", info); }); }; p.listen = function () { osc.listenToTransport(this, chrome.serial, "connectionId"); }; p.sendRaw = function (encoded) { if (!this.connectionId) { return; } var that = this; // TODO gh-39: This is unsafe; we should only access the underlying // buffer within the range of its view. chrome.serial.send(this.connectionId, encoded.buffer, function (bytesSent, err) { if (err) { that.emit("error", err + ". Total bytes sent: " + bytesSent); } }); }; p.close = function () { if (this.connectionId) { var that = this; chrome.serial.disconnect(this.connectionId, function (result) { if (result) { that.emit("close"); } }); } }; osc.UDPPort = function (options) { osc.Port.call(this, options); var o = this.options; o.localAddress = o.localAddress || "127.0.0.1"; o.localPort = o.localPort !== undefined ? o.localPort : 57121; this.on("open", this.listen.bind(this)); this.socketId = o.socketId; if (this.socketId) { this.emit("open", 0); } }; p = osc.UDPPort.prototype = Object.create(osc.Port.prototype); p.constructor = osc.UDPPort; p.open = function () { if (this.socketId) { return; } var o = this.options, props = { persistent: o.persistent, name: o.name, bufferSize: o.bufferSize }, that = this; chrome.sockets.udp.create(props, function (info) { that.socketId = info.socketId; that.bindSocket(); }); }; p.bindSocket = function () { var that = this, o = this.options; if (o.broadcast !== undefined) { chrome.sockets.udp.setBroadcast(this.socketId, o.broadcast, function (resultCode) { if (resultCode < 0) { that.emit("error", new Error("An error occurred while setting the socket's broadcast flag. Result code: " + resultCode)); } }); } if (o.multicastTTL !== undefined) { chrome.sockets.udp.setMulticastTimeToLive(this.socketId, o.multicastTTL, function (resultCode) { if (resultCode < 0) { that.emit("error", new Error("An error occurred while setting the socket's multicast time to live flag. " + "Result code: " + resultCode)); } }); } chrome.sockets.udp.bind(this.socketId, o.localAddress, o.localPort, function (resultCode) { if (resultCode > 0) { osc.emitNetworkError(that, resultCode); return; } that.emit("open", resultCode); }); }; p.listen = function () { var o = this.options; osc.listenToTransport(this, chrome.sockets.udp, "socketId"); if (o.multicastMembership) { if (typeof o.multicastMembership === "string") { o.multicastMembership = [o.multicastMembership]; } o.multicastMembership.forEach(function (addr) { chrome.sockets.udp.joinGroup(this.socketId, addr, function (resultCode) { if (resultCode < 0) { this.emit("error", new Error( "There was an error while trying to join the multicast group " + addr + ". Result code: " + resultCode)); } }); }); } }; p.sendRaw = function (encoded, address, port) { if (!this.socketId) { return; } var o = this.options, that = this; address = address || o.remoteAddress; port = port !== undefined ? port : o.remotePort; // TODO gh-39: This is unsafe; we should only access the underlying // buffer within the range of its view. chrome.sockets.udp.send(this.socketId, encoded.buffer, address, port, function (info) { if (!info) { that.emit("error", "There was an unknown error while trying to send a UDP message. " + "Have you declared the appropriate udp send permissions " + "in your application's manifest file?"); } if (info.resultCode > 0) { osc.emitNetworkError(that, info.resultCode); } }); }; p.close = function () { if (this.socketId) { var that = this; chrome.sockets.udp.close(this.socketId, function () { that.emit("close"); }); } }; }());