12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117 |
- var osc = osc || {};
- (function () {
- "use strict";
- osc.SECS_70YRS = 2208988800;
- osc.TWO_32 = 4294967296;
- osc.defaults = {
- metadata: false,
- unpackSingleArgs: true
- };
-
- osc.isCommonJS = typeof module !== "undefined" && module.exports ? true : false;
-
- osc.isNode = osc.isCommonJS && typeof window === "undefined";
-
- osc.isElectron = typeof process !== "undefined" &&
- process.versions && process.versions.electron ? true : false;
-
- osc.isBufferEnv = osc.isNode || osc.isElectron;
-
- osc.isArray = function (obj) {
- return obj && Object.prototype.toString.call(obj) === "[object Array]";
- };
-
- osc.isTypedArrayView = function (obj) {
- return obj.buffer && obj.buffer instanceof ArrayBuffer;
- };
-
- osc.isBuffer = function (obj) {
- return osc.isBufferEnv && obj instanceof Buffer;
- };
-
- osc.Long = typeof Long !== "undefined" ? Long :
- osc.isNode ? require("long") : undefined;
-
-
- osc.TextDecoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf-8") :
- typeof util !== "undefined" && typeof (util.TextDecoder !== "undefined") ? new util.TextDecoder("utf-8") : undefined;
- osc.TextEncoder = typeof TextEncoder !== "undefined" ? new TextEncoder("utf-8") :
- typeof util !== "undefined" && typeof (util.TextEncoder !== "undefined") ? new util.TextEncoder("utf-8") : undefined;
-
-
- 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);
- };
-
-
- 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));
- }
-
-
-
- return new Uint8Array(buf);
- };
-
-
- osc.nativeBuffer = function (obj) {
- if (osc.isBufferEnv) {
- return osc.isBuffer(obj) ? obj :
- Buffer.from(obj.buffer ? obj : new Uint8Array(obj));
- }
- return osc.isTypedArrayView(obj) ? obj : new Uint8Array(obj);
- };
-
- 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;
- };
-
- 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;
- }
- }
-
- idx = (idx + 3) & ~0x03;
- offsetState.idx = idx;
- var decoder = osc.isBufferEnv ? osc.readString.withBuffer :
- osc.TextDecoder ? osc.readString.withTextDecoder : osc.readString.raw;
- return decoder(charCodes);
- };
- osc.readString.raw = function (charCodes) {
-
-
- var str = "";
- var sliceSize = 10000;
-
-
- for (var i = 0; i < charCodes.length; i += sliceSize) {
- str += String.fromCharCode.apply(null, charCodes.slice(i, i + sliceSize));
- }
- return str;
- };
- osc.readString.withTextDecoder = function (charCodes) {
- var data = new Int8Array(charCodes);
- return osc.TextDecoder.decode(data);
- };
- osc.readString.withBuffer = function (charCodes) {
- return Buffer.from(charCodes).toString("utf-8");
- };
-
- osc.writeString = function (str) {
- var terminated = str + "\u0000",
- len = terminated.length,
- paddedLen = (len + 3) & ~0x03,
- arr = new Uint8Array(paddedLen);
- var encoder = osc.isBufferEnv ? osc.writeString.withBuffer :
- osc.TextEncoder ? osc.writeString.withTextEncoder : null,
- encodedStr;
- if (encoder) {
- encodedStr = encoder(terminated);
- }
- for (var i = 0; i < terminated.length; i++) {
- var charCode = encoder ? encodedStr[i] : terminated.charCodeAt(i);
- arr[i] = charCode;
- }
- return arr;
- };
- osc.writeString.withTextEncoder = function (str) {
- return osc.TextEncoder.encode(str);
- };
- osc.writeString.withBuffer = function (str) {
- return Buffer.from(str);
- };
-
- osc.readPrimitive = function (dv, readerName, numBytes, offsetState) {
- var val = dv[readerName](offsetState.idx, false);
- offsetState.idx += numBytes;
- return val;
- };
-
- 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;
- };
-
- osc.readInt32 = function (dv, offsetState) {
- return osc.readPrimitive(dv, "getInt32", 4, offsetState);
- };
-
- osc.writeInt32 = function (val, dv, offset) {
- return osc.writePrimitive(val, dv, "setInt32", 4, offset);
- };
-
- osc.readInt64 = function (dv, offsetState) {
- var high = osc.readPrimitive(dv, "getInt32", 4, offsetState),
- low = osc.readPrimitive(dv, "getInt32", 4, offsetState);
- if (osc.Long) {
- return new osc.Long(low, high);
- } else {
- return {
- high: high,
- low: low,
- unsigned: false
- };
- }
- };
-
- 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;
- };
-
- osc.readFloat32 = function (dv, offsetState) {
- return osc.readPrimitive(dv, "getFloat32", 4, offsetState);
- };
-
- osc.writeFloat32 = function (val, dv, offset) {
- return osc.writePrimitive(val, dv, "setFloat32", 4, offset);
- };
-
- osc.readFloat64 = function (dv, offsetState) {
- return osc.readPrimitive(dv, "getFloat64", 8, offsetState);
- };
-
- osc.writeFloat64 = function (val, dv, offset) {
- return osc.writePrimitive(val, dv, "setFloat64", 8, offset);
- };
-
- osc.readChar32 = function (dv, offsetState) {
- var charCode = osc.readPrimitive(dv, "getUint32", 4, offsetState);
- return String.fromCharCode(charCode);
- };
-
- 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);
- };
-
- 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;
- };
-
- osc.writeBlob = function (data) {
- data = osc.byteArray(data);
- var len = data.byteLength,
- paddedLen = (len + 3) & ~0x03,
- offset = 4,
- blobLen = paddedLen + offset,
- arr = new Uint8Array(blobLen),
- dv = new DataView(arr.buffer);
-
- osc.writeInt32(len, dv);
-
-
- arr.set(data, offset);
- return arr;
- };
-
- osc.readMIDIBytes = function (dv, offsetState) {
- var midi = new Uint8Array(dv.buffer, offsetState.idx, 4);
- offsetState.idx += 4;
- return midi;
- };
-
- osc.writeMIDIBytes = function (bytes) {
- bytes = osc.byteArray(bytes);
- var arr = new Uint8Array(4);
- arr.set(bytes);
- return arr;
- };
-
- 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
- };
- };
-
- osc.writeColor = function (color) {
- var alpha = Math.round(color.a * 255),
- arr = new Uint8Array([color.r, color.g, color.b, alpha]);
- return arr;
- };
-
- osc.readTrue = function () {
- return true;
- };
-
- osc.readFalse = function () {
- return false;
- };
-
- osc.readNull = function () {
- return null;
- };
-
- osc.readImpulse = function () {
- return 1.0;
- };
-
- 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
- };
- };
-
- osc.writeTimeTag = function (timeTag) {
- var raw = timeTag.raw ? timeTag.raw : osc.jsToNTPTime(timeTag.native),
- arr = new Uint8Array(8),
- dv = new DataView(arr.buffer);
- osc.writeInt32(raw[0], dv, 0);
- osc.writeInt32(raw[1], dv, 4);
- return arr;
- };
-
- 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]
- };
- };
-
- 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];
- };
-
- osc.readArguments = function (dv, options, offsetState) {
- var typeTagString = osc.readString(dv, offsetState);
- if (typeTagString.indexOf(",") !== 0) {
-
-
-
-
- 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;
- };
-
- 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;
- };
-
- 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;
- };
-
- osc.writeArguments = function (args, options) {
- var argCollection = osc.collectArguments(args, options);
- return osc.joinParts(argCollection);
- };
-
- 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;
- };
-
- 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;
- };
-
- 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;
- };
-
- 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);
- };
-
- 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
- };
- };
-
- 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);
- };
-
- 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;
- };
-
- osc.readBundle = function (dv, options, offsetState) {
- return osc.readPacket(dv, options, offsetState);
- };
-
- 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;
- };
-
- 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;
- };
-
- 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
- };
- };
-
- 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);
- };
-
- 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));
- }
- };
-
- 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"
- },
-
- };
-
- osc.inferTypeForArgument = function (arg) {
- var type = typeof arg;
-
- 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));
- };
-
- 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) {
-
- msgArg = arg;
- } else if (osc.isArray(arg)) {
-
-
- 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;
- }
- }());
- ;
- !function(t,i){"object"==typeof exports&&"object"==typeof module?module.exports=i():"function"==typeof define&&define.amd?define([],i):"object"==typeof exports?exports.Long=i():t.Long=i()}("undefined"!=typeof self?self:this,function(){return function(t){function i(e){if(n[e])return n[e].exports;var r=n[e]={i:e,l:!1,exports:{}};return t[e].call(r.exports,r,r.exports,i),r.l=!0,r.exports}var n={};return i.m=t,i.c=n,i.d=function(t,n,e){i.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:e})},i.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(n,"a",n),n},i.o=function(t,i){return Object.prototype.hasOwnProperty.call(t,i)},i.p="",i(i.s=0)}([function(t,i){function n(t,i,n){this.low=0|t,this.high=0|i,this.unsigned=!!n}function e(t){return!0===(t&&t.__isLong__)}function r(t,i){var n,e,r;return i?(t>>>=0,(r=0<=t&&t<256)&&(e=l[t])?e:(n=h(t,(0|t)<0?-1:0,!0),r&&(l[t]=n),n)):(t|=0,(r=-128<=t&&t<128)&&(e=f[t])?e:(n=h(t,t<0?-1:0,!1),r&&(f[t]=n),n))}function s(t,i){if(isNaN(t))return i?p:m;if(i){if(t<0)return p;if(t>=c)return q}else{if(t<=-v)return _;if(t+1>=v)return E}return t<0?s(-t,i).neg():h(t%d|0,t/d|0,i)}function h(t,i,e){return new n(t,i,e)}function u(t,i,n){if(0===t.length)throw Error("empty string");if("NaN"===t||"Infinity"===t||"+Infinity"===t||"-Infinity"===t)return m;if("number"==typeof i?(n=i,i=!1):i=!!i,(n=n||10)<2||36<n)throw RangeError("radix");var e;if((e=t.indexOf("-"))>0)throw Error("interior hyphen");if(0===e)return u(t.substring(1),i,n).neg();for(var r=s(a(n,8)),h=m,o=0;o<t.length;o+=8){var g=Math.min(8,t.length-o),f=parseInt(t.substring(o,o+g),n);if(g<8){var l=s(a(n,g));h=h.mul(l).add(s(f))}else h=h.mul(r),h=h.add(s(f))}return h.unsigned=i,h}function o(t,i){return"number"==typeof t?s(t,i):"string"==typeof t?u(t,i):h(t.low,t.high,"boolean"==typeof i?i:t.unsigned)}t.exports=n;var g=null;try{g=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch(t){}n.prototype.__isLong__,Object.defineProperty(n.prototype,"__isLong__",{value:!0}),n.isLong=e;var f={},l={};n.fromInt=r,n.fromNumber=s,n.fromBits=h;var a=Math.pow;n.fromString=u,n.fromValue=o;var d=4294967296,c=d*d,v=c/2,w=r(1<<24),m=r(0);n.ZERO=m;var p=r(0,!0);n.UZERO=p;var y=r(1);n.ONE=y;var b=r(1,!0);n.UONE=b;var N=r(-1);n.NEG_ONE=N;var E=h(-1,2147483647,!1);n.MAX_VALUE=E;var q=h(-1,-1,!0);n.MAX_UNSIGNED_VALUE=q;var _=h(0,-2147483648,!1);n.MIN_VALUE=_;var B=n.prototype;B.toInt=function(){return this.unsigned?this.low>>>0:this.low},B.toNumber=function(){return this.unsigned?(this.high>>>0)*d+(this.low>>>0):this.high*d+(this.low>>>0)},B.toString=function(t){if((t=t||10)<2||36<t)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative()){if(this.eq(_)){var i=s(t),n=this.div(i),e=n.mul(i).sub(this);return n.toString(t)+e.toInt().toString(t)}return"-"+this.neg().toString(t)}for(var r=s(a(t,6),this.unsigned),h=this,u="";;){var o=h.div(r),g=h.sub(o.mul(r)).toInt()>>>0,f=g.toString(t);if(h=o,h.isZero())return f+u;for(;f.length<6;)f="0"+f;u=""+f+u}},B.getHighBits=function(){return this.high},B.getHighBitsUnsigned=function(){return this.high>>>0},B.getLowBits=function(){return this.low},B.getLowBitsUnsigned=function(){return this.low>>>0},B.getNumBitsAbs=function(){if(this.isNegative())return this.eq(_)?64:this.neg().getNumBitsAbs();for(var t=0!=this.high?this.high:this.low,i=31;i>0&&0==(t&1<<i);i--);return 0!=this.high?i+33:i+1},B.isZero=function(){return 0===this.high&&0===this.low},B.eqz=B.isZero,B.isNegative=function(){return!this.unsigned&&this.high<0},B.isPositive=function(){return this.unsigned||this.high>=0},B.isOdd=function(){return 1==(1&this.low)},B.isEven=function(){return 0==(1&this.low)},B.equals=function(t){return e(t)||(t=o(t)),(this.unsigned===t.unsigned||this.high>>>31!=1||t.high>>>31!=1)&&(this.high===t.high&&this.low===t.low)},B.eq=B.equals,B.notEquals=function(t){return!this.eq(t)},B.neq=B.notEquals,B.ne=B.notEquals,B.lessThan=function(t){return this.comp(t)<0},B.lt=B.lessThan,B.lessThanOrEqual=function(t){return this.comp(t)<=0},B.lte=B.lessThanOrEqual,B.le=B.lessThanOrEqual,B.greaterThan=function(t){return this.comp(t)>0},B.gt=B.greaterThan,B.greaterThanOrEqual=function(t){return this.comp(t)>=0},B.gte=B.greaterThanOrEqual,B.ge=B.greaterThanOrEqual,B.compare=function(t){if(e(t)||(t=o(t)),this.eq(t))return 0;var i=this.isNegative(),n=t.isNegative();return i&&!n?-1:!i&&n?1:this.unsigned?t.high>>>0>this.high>>>0||t.high===this.high&&t.low>>>0>this.low>>>0?-1:1:this.sub(t).isNegative()?-1:1},B.comp=B.compare,B.negate=function(){return!this.unsigned&&this.eq(_)?_:this.not().add(y)},B.neg=B.negate,B.add=function(t){e(t)||(t=o(t));var i=this.high>>>16,n=65535&this.high,r=this.low>>>16,s=65535&this.low,u=t.high>>>16,g=65535&t.high,f=t.low>>>16,l=65535&t.low,a=0,d=0,c=0,v=0;return v+=s+l,c+=v>>>16,v&=65535,c+=r+f,d+=c>>>16,c&=65535,d+=n+g,a+=d>>>16,d&=65535,a+=i+u,a&=65535,h(c<<16|v,a<<16|d,this.unsigned)},B.subtract=function(t){return e(t)||(t=o(t)),this.add(t.neg())},B.sub=B.subtract,B.multiply=function(t){if(this.isZero())return m;if(e(t)||(t=o(t)),g){return h(g.mul(this.low,this.high,t.low,t.high),g.get_high(),this.unsigned)}if(t.isZero())return m;if(this.eq(_))return t.isOdd()?_:m;if(t.eq(_))return this.isOdd()?_:m;if(this.isNegative())return t.isNegative()?this.neg().mul(t.neg()):this.neg().mul(t).neg();if(t.isNegative())return this.mul(t.neg()).neg();if(this.lt(w)&&t.lt(w))return s(this.toNumber()*t.toNumber(),this.unsigned);var i=this.high>>>16,n=65535&this.high,r=this.low>>>16,u=65535&this.low,f=t.high>>>16,l=65535&t.high,a=t.low>>>16,d=65535&t.low,c=0,v=0,p=0,y=0;return y+=u*d,p+=y>>>16,y&=65535,p+=r*d,v+=p>>>16,p&=65535,p+=u*a,v+=p>>>16,p&=65535,v+=n*d,c+=v>>>16,v&=65535,v+=r*a,c+=v>>>16,v&=65535,v+=u*l,c+=v>>>16,v&=65535,c+=i*d+n*a+r*l+u*f,c&=65535,h(p<<16|y,c<<16|v,this.unsigned)},B.mul=B.multiply,B.divide=function(t){if(e(t)||(t=o(t)),t.isZero())throw Error("division by zero");if(g){if(!this.unsigned&&-2147483648===this.high&&-1===t.low&&-1===t.high)return this;return h((this.unsigned?g.div_u:g.div_s)(this.low,this.high,t.low,t.high),g.get_high(),this.unsigned)}if(this.isZero())return this.unsigned?p:m;var i,n,r;if(this.unsigned){if(t.unsigned||(t=t.toUnsigned()),t.gt(this))return p;if(t.gt(this.shru(1)))return b;r=p}else{if(this.eq(_)){if(t.eq(y)||t.eq(N))return _;if(t.eq(_))return y;return i=this.shr(1).div(t).shl(1),i.eq(m)?t.isNegative()?y:N:(n=this.sub(t.mul(i)),r=i.add(n.div(t)))}if(t.eq(_))return this.unsigned?p:m;if(this.isNegative())return t.isNegative()?this.neg().div(t.neg()):this.neg().div(t).neg();if(t.isNegative())return this.div(t.neg()).neg();r=m}for(n=this;n.gte(t);){i=Math.max(1,Math.floor(n.toNumber()/t.toNumber()));for(var u=Math.ceil(Math.log(i)/Math.LN2),f=u<=48?1:a(2,u-48),l=s(i),d=l.mul(t);d.isNegative()||d.gt(n);)i-=f,l=s(i,this.unsigned),d=l.mul(t);l.isZero()&&(l=y),r=r.add(l),n=n.sub(d)}return r},B.div=B.divide,B.modulo=function(t){if(e(t)||(t=o(t)),g){return h((this.unsigned?g.rem_u:g.rem_s)(this.low,this.high,t.low,t.high),g.get_high(),this.unsigned)}return this.sub(this.div(t).mul(t))},B.mod=B.modulo,B.rem=B.modulo,B.not=function(){return h(~this.low,~this.high,this.unsigned)},B.and=function(t){return e(t)||(t=o(t)),h(this.low&t.low,this.high&t.high,this.unsigned)},B.or=function(t){return e(t)||(t=o(t)),h(this.low|t.low,this.high|t.high,this.unsigned)},B.xor=function(t){return e(t)||(t=o(t)),h(this.low^t.low,this.high^t.high,this.unsigned)},B.shiftLeft=function(t){return e(t)&&(t=t.toInt()),0==(t&=63)?this:t<32?h(this.low<<t,this.high<<t|this.low>>>32-t,this.unsigned):h(0,this.low<<t-32,this.unsigned)},B.shl=B.shiftLeft,B.shiftRight=function(t){return e(t)&&(t=t.toInt()),0==(t&=63)?this:t<32?h(this.low>>>t|this.high<<32-t,this.high>>t,this.unsigned):h(this.high>>t-32,this.high>=0?0:-1,this.unsigned)},B.shr=B.shiftRight,B.shiftRightUnsigned=function(t){if(e(t)&&(t=t.toInt()),0===(t&=63))return this;var i=this.high;if(t<32){return h(this.low>>>t|i<<32-t,i>>>t,this.unsigned)}return 32===t?h(i,0,this.unsigned):h(i>>>t-32,0,this.unsigned)},B.shru=B.shiftRightUnsigned,B.shr_u=B.shiftRightUnsigned,B.toSigned=function(){return this.unsigned?h(this.low,this.high,!1):this},B.toUnsigned=function(){return this.unsigned?this:h(this.low,this.high,!0)},B.toBytes=function(t){return t?this.toBytesLE():this.toBytesBE()},B.toBytesLE=function(){var t=this.high,i=this.low;return[255&i,i>>>8&255,i>>>16&255,i>>>24,255&t,t>>>8&255,t>>>16&255,t>>>24]},B.toBytesBE=function(){var t=this.high,i=this.low;return[t>>>24,t>>>16&255,t>>>8&255,255&t,i>>>24,i>>>16&255,i>>>8&255,255&i]},n.fromBytes=function(t,i,e){return e?n.fromBytesLE(t,i):n.fromBytesBE(t,i)},n.fromBytesLE=function(t,i){return new n(t[0]|t[1]<<8|t[2]<<16|t[3]<<24,t[4]|t[5]<<8|t[6]<<16|t[7]<<24,i)},n.fromBytesBE=function(t,i){return new n(t[4]<<24|t[5]<<16|t[6]<<8|t[7],t[0]<<24|t[1]<<16|t[2]<<8|t[3],i)}}])});
- (function (root, factory) {
- "use strict";
- if (typeof exports === "object") {
-
- root.slip = exports;
- factory(exports);
- } else if (typeof define === "function" && define.amd) {
-
- define(["exports"], function (exports) {
- root.slip = exports;
- return (root.slip, factory(exports));
- });
- } else {
-
- 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.encode = function (data, o) {
- o = o || {};
- o.bufferPadding = o.bufferPadding || 4;
- 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++) {
-
- 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);
- };
-
- slip.Decoder = function (o) {
- o = typeof o !== "function" ? o || {} : {
- onMessage: o
- };
- this.maxMessageSize = o.maxMessageSize || 10485760;
- this.bufferSize = o.bufferSize || 1024;
- 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;
-
- 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.");
- }
-
- this.msgBufferIdx = 0;
- this.escape = false;
- };
-
- 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;
- };
-
- p.handleEnd = function () {
- if (this.msgBufferIdx === 0) {
- return;
- }
- var msg = slip.sliceByteArray(this.msgBuffer, 0, this.msgBufferIdx);
- if (this.onMessage) {
- this.onMessage(msg);
- }
-
- this.msgBufferIdx = 0;
- return msg;
- };
- return slip;
- }));
- ;
- ;(function (exports) {
- 'use strict';
-
- function EventEmitter() {}
-
- var proto = EventEmitter.prototype;
- var originalGlobalValue = exports.EventEmitter;
-
- function indexOfListener(listeners, listener) {
- var i = listeners.length;
- while (i--) {
- if (listeners[i].listener === listener) {
- return i;
- }
- }
- return -1;
- }
-
- function alias(name) {
- return function aliasClosure() {
- return this[name].apply(this, arguments);
- };
- }
-
- proto.getListeners = function getListeners(evt) {
- var events = this._getEvents();
- var response;
- var key;
-
-
- 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;
- };
-
- proto.flattenListeners = function flattenListeners(listeners) {
- var flatListeners = [];
- var i;
- for (i = 0; i < listeners.length; i += 1) {
- flatListeners.push(listeners[i].listener);
- }
- return flatListeners;
- };
-
- proto.getListenersAsObject = function getListenersAsObject(evt) {
- var listeners = this.getListeners(evt);
- var response;
- if (listeners instanceof Array) {
- response = {};
- response[evt] = listeners;
- }
- return response || listeners;
- };
- function isValidListener (listener) {
- if (typeof listener === 'function' || listener instanceof RegExp) {
- return true
- } else if (listener && typeof listener === 'object') {
- return isValidListener(listener.listener)
- } else {
- return false
- }
- }
-
- proto.addListener = function addListener(evt, listener) {
- if (!isValidListener(listener)) {
- throw new TypeError('listener must be a function');
- }
- 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;
- };
-
- proto.on = alias('addListener');
-
- proto.addOnceListener = function addOnceListener(evt, listener) {
- return this.addListener(evt, {
- listener: listener,
- once: true
- });
- };
-
- proto.once = alias('addOnceListener');
-
- proto.defineEvent = function defineEvent(evt) {
- this.getListeners(evt);
- return this;
- };
-
- proto.defineEvents = function defineEvents(evts) {
- for (var i = 0; i < evts.length; i += 1) {
- this.defineEvent(evts[i]);
- }
- return this;
- };
-
- 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;
- };
-
- proto.off = alias('removeListener');
-
- proto.addListeners = function addListeners(evt, listeners) {
-
- return this.manipulateListeners(false, evt, listeners);
- };
-
- proto.removeListeners = function removeListeners(evt, listeners) {
-
- return this.manipulateListeners(true, evt, listeners);
- };
-
- 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 (typeof evt === 'object' && !(evt instanceof RegExp)) {
- for (i in evt) {
- if (evt.hasOwnProperty(i) && (value = evt[i])) {
-
- if (typeof value === 'function') {
- single.call(this, i, value);
- }
- else {
-
- multiple.call(this, i, value);
- }
- }
- }
- }
- else {
-
-
-
- i = listeners.length;
- while (i--) {
- single.call(this, evt, listeners[i]);
- }
- }
- return this;
- };
-
- proto.removeEvent = function removeEvent(evt) {
- var type = typeof evt;
- var events = this._getEvents();
- var key;
-
- if (type === 'string') {
-
- delete events[evt];
- }
- else if (evt instanceof RegExp) {
-
- for (key in events) {
- if (events.hasOwnProperty(key) && evt.test(key)) {
- delete events[key];
- }
- }
- }
- else {
-
- delete this._events;
- }
- return this;
- };
-
- proto.removeAllListeners = alias('removeEvent');
-
- 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);
- for (i = 0; i < listeners.length; i++) {
-
-
- 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;
- };
-
- proto.trigger = alias('emitEvent');
-
- proto.emit = function emit(evt) {
- var args = Array.prototype.slice.call(arguments, 1);
- return this.emitEvent(evt, args);
- };
-
- proto.setOnceReturnValue = function setOnceReturnValue(value) {
- this._onceReturnValue = value;
- return this;
- };
-
- proto._getOnceReturnValue = function _getOnceReturnValue() {
- if (this.hasOwnProperty('_onceReturnValue')) {
- return this._onceReturnValue;
- }
- else {
- return true;
- }
- };
-
- proto._getEvents = function _getEvents() {
- return this._events || (this._events = {});
- };
-
- EventEmitter.noConflict = function noConflict() {
- exports.EventEmitter = originalGlobalValue;
- return EventEmitter;
- };
-
- if (typeof define === 'function' && define.amd) {
- define(function () {
- return EventEmitter;
- });
- }
- else if (typeof module === 'object' && module.exports){
- module.exports = EventEmitter;
- }
- else {
- exports.EventEmitter = EventEmitter;
- }
- }(typeof window !== 'undefined' ? window : this || {}));
- ;
- var osc = osc || require("./osc.js"),
- slip = slip || require("slip"),
- EventEmitter = EventEmitter || require("events").EventEmitter;
- (function () {
- "use strict";
- osc.supportsSerial = false;
-
- osc.firePacketEvents = function (port, packet, timeTag, packetInfo) {
- if (packet.address) {
- port.emit("message", packet, timeTag, packetInfo);
- } else {
- osc.fireBundleEvents(port, packet, timeTag, packetInfo);
- }
- };
-
- 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.fireClosedPortSendError = function (port, msg) {
- msg = msg || "Can't send packets on a closed osc.Port object. Please open (or reopen) this Port by calling open().";
- port.emit("error", msg);
- };
- 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) {
-
-
- 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) {
-
-
- 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) {
-
- this.decoder.decode(data, packetInfo);
- };
-
- 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
- };
- };
-
- 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);
- };
-
- osc.stopRelaying = function (from, relaySpec) {
- from.removeListener(relaySpec.eventName, relaySpec.listener);
- };
-
- 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);
-
-
-
-
- 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 (typeof module !== "undefined" && module.exports) {
- module.exports = osc;
- }
- }());
- ;
- var osc = osc || require("./osc.js");
- (function () {
- "use strict";
- osc.WebSocket = typeof WebSocket !== "undefined" ? WebSocket : require ("ws");
- 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) {
- osc.WebSocketPort.setupSocketForBinary(this.socket);
- 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.readyState > 1) {
- this.socket = new osc.WebSocket(this.options.url);
- }
- osc.WebSocketPort.setupSocketForBinary(this.socket);
- var that = this;
- this.socket.onopen = function () {
- that.emit("open", that.socket);
- };
- this.socket.onerror = function (err) {
- that.emit("error", err);
- };
- };
- p.listen = function () {
- var that = this;
- this.socket.onmessage = function (e) {
- that.emit("data", e.data, e);
- };
- this.socket.onclose = function (e) {
- that.emit("close", e);
- };
- that.emit("ready");
- };
- p.sendRaw = function (encoded) {
- if (!this.socket || this.socket.readyState !== 1) {
- osc.fireClosedPortSendError(this);
- return;
- }
- this.socket.send(encoded);
- };
- p.close = function (code, reason) {
- this.socket.close(code, reason);
- };
- osc.WebSocketPort.setupSocketForBinary = function (socket) {
- socket.binaryType = osc.isNode ? "nodebuffer" : "arraybuffer";
- };
- }());
|