123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240 |
- var osc = osc || {};
- (function () {
- "use strict";
- osc.SECS_70YRS = 2208988800;
- osc.TWO_32 = 4294967296;
- osc.defaults = {
- metadata: false,
- unpackSingleArgs: true
- };
-
-
-
- osc.isBufferEnv = typeof Buffer !== "undefined";
-
- osc.isCommonJS = typeof module !== "undefined" && module.exports;
-
- osc.isNode = osc.isCommonJS && typeof window === "undefined";
-
- 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;
- };
-
- var Long = typeof dcodeIO !== "undefined" ? dcodeIO.Long :
- typeof Long !== "undefined" ? Long :
- osc.isNode ? require("long") : 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 && osc.isNode) {
- return osc.isBuffer(obj) ? obj : new Buffer(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;
- return String.fromCharCode.apply(null, charCodes);
- };
-
- 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;
- };
-
- 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 (Long) {
- return new 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(global, factory) {
- if (typeof define === 'function' && define["amd"])
- define([], factory);
- else if (typeof require === 'function' && typeof module === "object" && module && module["exports"])
- module["exports"] = factory();
- else
- (global["dcodeIO"] = global["dcodeIO"] || {})["Long"] = factory();
- })(this, function() {
- "use strict";
-
- function Long(low, high, unsigned) {
-
- this.low = low | 0;
-
- this.high = high | 0;
-
- this.unsigned = !!unsigned;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Long.__isLong__;
- Object.defineProperty(Long.prototype, "__isLong__", {
- value: true,
- enumerable: false,
- configurable: false
- });
-
- function isLong(obj) {
- return (obj && obj["__isLong__"]) === true;
- }
-
- Long.isLong = isLong;
-
- var INT_CACHE = {};
-
- var UINT_CACHE = {};
-
- 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;
- }
- }
-
- Long.fromInt = fromInt;
-
- 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);
- }
-
- Long.fromNumber = fromNumber;
-
- function fromBits(lowBits, highBits, unsigned) {
- return new Long(lowBits, highBits, unsigned);
- }
-
- Long.fromBits = fromBits;
-
- var pow_dbl = Math.pow;
-
- 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')
- 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();
- }
-
-
- 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;
- }
-
- Long.fromString = fromString;
-
- function fromValue(val) {
- if (val instanceof Long)
- return val;
- if (typeof val === 'number')
- return fromNumber(val);
- if (typeof val === 'string')
- return fromString(val);
-
- return fromBits(val.low, val.high, val.unsigned);
- }
-
- Long.fromValue = fromValue;
-
-
-
- var TWO_PWR_16_DBL = 1 << 16;
-
- var TWO_PWR_24_DBL = 1 << 24;
-
- var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
-
- var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
-
- var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
-
- var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL);
-
- var ZERO = fromInt(0);
-
- Long.ZERO = ZERO;
-
- var UZERO = fromInt(0, true);
-
- Long.UZERO = UZERO;
-
- var ONE = fromInt(1);
-
- Long.ONE = ONE;
-
- var UONE = fromInt(1, true);
-
- Long.UONE = UONE;
-
- var NEG_ONE = fromInt(-1);
-
- Long.NEG_ONE = NEG_ONE;
-
- var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false);
-
- Long.MAX_VALUE = MAX_VALUE;
-
- var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true);
-
- Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE;
-
- var MIN_VALUE = fromBits(0, 0x80000000|0, false);
-
- Long.MIN_VALUE = MIN_VALUE;
-
- var LongPrototype = Long.prototype;
-
- LongPrototype.toInt = function toInt() {
- return this.unsigned ? this.low >>> 0 : this.low;
- };
-
- 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);
- };
-
- LongPrototype.toString = function toString(radix) {
- radix = radix || 10;
- if (radix < 2 || 36 < radix)
- throw RangeError('radix');
- if (this.isZero())
- return '0';
- if (this.isNegative()) {
- if (this.eq(MIN_VALUE)) {
-
-
- 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);
- }
-
-
- 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;
- }
- }
- };
-
- LongPrototype.getHighBits = function getHighBits() {
- return this.high;
- };
-
- LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() {
- return this.high >>> 0;
- };
-
- LongPrototype.getLowBits = function getLowBits() {
- return this.low;
- };
-
- LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() {
- return this.low >>> 0;
- };
-
- LongPrototype.getNumBitsAbs = function getNumBitsAbs() {
- if (this.isNegative())
- 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;
- };
-
- LongPrototype.isZero = function isZero() {
- return this.high === 0 && this.low === 0;
- };
-
- LongPrototype.isNegative = function isNegative() {
- return !this.unsigned && this.high < 0;
- };
-
- LongPrototype.isPositive = function isPositive() {
- return this.unsigned || this.high >= 0;
- };
-
- LongPrototype.isOdd = function isOdd() {
- return (this.low & 1) === 1;
- };
-
- LongPrototype.isEven = function isEven() {
- return (this.low & 1) === 0;
- };
-
- 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;
- };
-
- LongPrototype.eq = LongPrototype.equals;
-
- LongPrototype.notEquals = function notEquals(other) {
- return !this.eq( other);
- };
-
- LongPrototype.neq = LongPrototype.notEquals;
-
- LongPrototype.lessThan = function lessThan(other) {
- return this.comp( other) < 0;
- };
-
- LongPrototype.lt = LongPrototype.lessThan;
-
- LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
- return this.comp( other) <= 0;
- };
-
- LongPrototype.lte = LongPrototype.lessThanOrEqual;
-
- LongPrototype.greaterThan = function greaterThan(other) {
- return this.comp( other) > 0;
- };
-
- LongPrototype.gt = LongPrototype.greaterThan;
-
- LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
- return this.comp( other) >= 0;
- };
-
- LongPrototype.gte = LongPrototype.greaterThanOrEqual;
-
- 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;
-
- if (!this.unsigned)
- return this.sub(other).isNegative() ? -1 : 1;
-
- return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
- };
-
- LongPrototype.comp = LongPrototype.compare;
-
- LongPrototype.negate = function negate() {
- if (!this.unsigned && this.eq(MIN_VALUE))
- return MIN_VALUE;
- return this.not().add(ONE);
- };
-
- LongPrototype.neg = LongPrototype.negate;
-
- LongPrototype.add = function add(addend) {
- if (!isLong(addend))
- addend = fromValue(addend);
-
- 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);
- };
-
- LongPrototype.subtract = function subtract(subtrahend) {
- if (!isLong(subtrahend))
- subtrahend = fromValue(subtrahend);
- return this.add(subtrahend.neg());
- };
-
- LongPrototype.sub = LongPrototype.subtract;
-
- 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 (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24))
- return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
-
-
- 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);
- };
-
- LongPrototype.mul = LongPrototype.multiply;
-
- 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;
- else if (divisor.eq(MIN_VALUE))
- return ONE;
- else {
-
- 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();
-
-
-
-
-
- res = ZERO;
- rem = this;
- while (rem.gte(divisor)) {
-
-
- approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
-
-
- var log2 = Math.ceil(Math.log(approx) / Math.LN2),
- delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48),
-
-
- approxRes = fromNumber(approx),
- approxRem = approxRes.mul(divisor);
- while (approxRem.isNegative() || approxRem.gt(rem)) {
- approx -= delta;
- approxRes = fromNumber(approx, this.unsigned);
- approxRem = approxRes.mul(divisor);
- }
-
-
- if (approxRes.isZero())
- approxRes = ONE;
- res = res.add(approxRes);
- rem = rem.sub(approxRem);
- }
- return res;
- };
-
- LongPrototype.div = LongPrototype.divide;
-
- LongPrototype.modulo = function modulo(divisor) {
- if (!isLong(divisor))
- divisor = fromValue(divisor);
- return this.sub(this.div(divisor).mul(divisor));
- };
-
- LongPrototype.mod = LongPrototype.modulo;
-
- LongPrototype.not = function not() {
- return fromBits(~this.low, ~this.high, this.unsigned);
- };
-
- LongPrototype.and = function and(other) {
- if (!isLong(other))
- other = fromValue(other);
- return fromBits(this.low & other.low, this.high & other.high, this.unsigned);
- };
-
- LongPrototype.or = function or(other) {
- if (!isLong(other))
- other = fromValue(other);
- return fromBits(this.low | other.low, this.high | other.high, this.unsigned);
- };
-
- LongPrototype.xor = function xor(other) {
- if (!isLong(other))
- other = fromValue(other);
- return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
- };
-
- 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);
- };
-
- LongPrototype.shl = LongPrototype.shiftLeft;
-
- 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);
- };
-
- LongPrototype.shr = LongPrototype.shiftRight;
-
- 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);
- }
- };
-
- LongPrototype.shru = LongPrototype.shiftRightUnsigned;
-
- LongPrototype.toSigned = function toSigned() {
- if (!this.unsigned)
- return this;
- return fromBits(this.low, this.high, false);
- };
-
- LongPrototype.toUnsigned = function toUnsigned() {
- if (this.unsigned)
- return this;
- return fromBits(this.low, this.high, true);
- };
- return Long;
- });
- ;
- (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 () {
- 'use strict';
-
- function EventEmitter() {}
-
- var proto = EventEmitter.prototype;
- var exports = this;
- 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;
- };
-
- 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;
- };
-
- 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);
- i = listeners.length;
- while (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;
- }
- }.call(this));
- ;
- var osc = osc || require("./osc.js"),
- slip = slip || require("slip"),
- EventEmitter = EventEmitter || require("events").EventEmitter;
- (function () {
- "use strict";
-
- 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.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;
- (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);
- };
- }());
|