osc-module.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /*! osc.js 2.2.0, Copyright 2017 Colin Clark | github.com/colinbdclark/osc.js */
  2. define(["exports", "vwf/view/oscjs/slipjs/slip.min", "vwf/view/oscjs/EventEmitter/EventEmitter.min", "vwf/view/oscjs/longjs/Long.min"], function (exports, slip, EventEmitter, Long) {
  3. /*
  4. * osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js
  5. *
  6. * Copyright 2014-2016, Colin Clark
  7. * Licensed under the MIT and GPL 3 licenses.
  8. */
  9. /* global require, module, process, Buffer, dcodeIO */
  10. var osc = osc || {};
  11. (function () {
  12. "use strict";
  13. osc.SECS_70YRS = 2208988800;
  14. osc.TWO_32 = 4294967296;
  15. osc.defaults = {
  16. metadata: false,
  17. unpackSingleArgs: true
  18. };
  19. // A flag to tell us if we're in a Node.js-compatible environment with Buffers,
  20. // which we will assume are faster.
  21. // Unsupported, non-API property.
  22. osc.isCommonJS = typeof module !== "undefined" && module.exports ? true : false;
  23. // Unsupported, non-API property.
  24. osc.isNode = osc.isCommonJS && typeof window === "undefined";
  25. // Unsupported, non-API property.
  26. osc.isElectron = typeof process !== "undefined" &&
  27. process.versions && process.versions.electron ? true : false;
  28. // Unsupported, non-API property.
  29. osc.isBufferEnv = osc.isNode || osc.isElectron;
  30. // Unsupported, non-API function.
  31. osc.isArray = function (obj) {
  32. return obj && Object.prototype.toString.call(obj) === "[object Array]";
  33. };
  34. // Unsupported, non-API function
  35. osc.isTypedArrayView = function (obj) {
  36. return obj.buffer && obj.buffer instanceof ArrayBuffer;
  37. };
  38. // Unsupported, non-API function
  39. osc.isBuffer = function (obj) {
  40. return osc.isBufferEnv && obj instanceof Buffer;
  41. };
  42. // Private instance of the optional Long dependency.
  43. var Long = typeof dcodeIO !== "undefined" ? dcodeIO.Long :
  44. typeof Long !== "undefined" ? Long :
  45. osc.isNode ? require("long") : undefined;
  46. /**
  47. * Wraps the specified object in a DataView.
  48. *
  49. * @param {Array-like} obj the object to wrap in a DataView instance
  50. * @return {DataView} the DataView object
  51. */
  52. // Unsupported, non-API function.
  53. osc.dataView = function (obj, offset, length) {
  54. if (obj.buffer) {
  55. return new DataView(obj.buffer, offset, length);
  56. }
  57. if (obj instanceof ArrayBuffer) {
  58. return new DataView(obj, offset, length);
  59. }
  60. return new DataView(new Uint8Array(obj), offset, length);
  61. };
  62. /**
  63. * Takes an ArrayBuffer, TypedArray, DataView, Buffer, or array-like object
  64. * and returns a Uint8Array view of it.
  65. *
  66. * Throws an error if the object isn't suitably array-like.
  67. *
  68. * @param {Array-like or Array-wrapping} obj an array-like or array-wrapping object
  69. * @returns {Uint8Array} a typed array of octets
  70. */
  71. // Unsupported, non-API function.
  72. osc.byteArray = function (obj) {
  73. if (obj instanceof Uint8Array) {
  74. return obj;
  75. }
  76. var buf = obj.buffer ? obj.buffer : obj;
  77. if (!(buf instanceof ArrayBuffer) && (typeof buf.length === "undefined" || typeof buf === "string")) {
  78. throw new Error("Can't wrap a non-array-like object as Uint8Array. Object was: " +
  79. JSON.stringify(obj, null, 2));
  80. }
  81. // TODO gh-39: This is a potentially unsafe algorithm;
  82. // if we're getting anything other than a TypedArrayView (such as a DataView),
  83. // we really need to determine the range of the view it is viewing.
  84. return new Uint8Array(buf);
  85. };
  86. /**
  87. * Takes an ArrayBuffer, TypedArray, DataView, or array-like object
  88. * and returns a native buffer object
  89. * (i.e. in Node.js, a Buffer object and in the browser, a Uint8Array).
  90. *
  91. * Throws an error if the object isn't suitably array-like.
  92. *
  93. * @param {Array-like or Array-wrapping} obj an array-like or array-wrapping object
  94. * @returns {Buffer|Uint8Array} a buffer object
  95. */
  96. // Unsupported, non-API function.
  97. osc.nativeBuffer = function (obj) {
  98. if (osc.isBufferEnv) {
  99. return osc.isBuffer(obj) ? obj :
  100. new Buffer(obj.buffer ? obj : new Uint8Array(obj));
  101. }
  102. return osc.isTypedArrayView(obj) ? obj : new Uint8Array(obj);
  103. };
  104. // Unsupported, non-API function
  105. osc.copyByteArray = function (source, target, offset) {
  106. if (osc.isTypedArrayView(source) && osc.isTypedArrayView(target)) {
  107. target.set(source, offset);
  108. } else {
  109. var start = offset === undefined ? 0 : offset,
  110. len = Math.min(target.length - offset, source.length);
  111. for (var i = 0, j = start; i < len; i++ , j++) {
  112. target[j] = source[i];
  113. }
  114. }
  115. return target;
  116. };
  117. /**
  118. * Reads an OSC-formatted string.
  119. *
  120. * @param {DataView} dv a DataView containing the raw bytes of the OSC string
  121. * @param {Object} offsetState an offsetState object used to store the current offset index
  122. * @return {String} the JavaScript String that was read
  123. */
  124. osc.readString = function (dv, offsetState) {
  125. var charCodes = [],
  126. idx = offsetState.idx;
  127. for (; idx < dv.byteLength; idx++) {
  128. var charCode = dv.getUint8(idx);
  129. if (charCode !== 0) {
  130. charCodes.push(charCode);
  131. } else {
  132. idx++;
  133. break;
  134. }
  135. }
  136. // Round to the nearest 4-byte block.
  137. idx = (idx + 3) & ~0x03;
  138. offsetState.idx = idx;
  139. return String.fromCharCode.apply(null, charCodes);
  140. };
  141. /**
  142. * Writes a JavaScript string as an OSC-formatted string.
  143. *
  144. * @param {String} str the string to write
  145. * @return {Uint8Array} a buffer containing the OSC-formatted string
  146. */
  147. osc.writeString = function (str) {
  148. var terminated = str + "\u0000",
  149. len = terminated.length,
  150. paddedLen = (len + 3) & ~0x03,
  151. arr = new Uint8Array(paddedLen);
  152. for (var i = 0; i < terminated.length; i++) {
  153. var charCode = terminated.charCodeAt(i);
  154. arr[i] = charCode;
  155. }
  156. return arr;
  157. };
  158. // Unsupported, non-API function.
  159. osc.readPrimitive = function (dv, readerName, numBytes, offsetState) {
  160. var val = dv[readerName](offsetState.idx, false);
  161. offsetState.idx += numBytes;
  162. return val;
  163. };
  164. // Unsupported, non-API function.
  165. osc.writePrimitive = function (val, dv, writerName, numBytes, offset) {
  166. offset = offset === undefined ? 0 : offset;
  167. var arr;
  168. if (!dv) {
  169. arr = new Uint8Array(numBytes);
  170. dv = new DataView(arr.buffer);
  171. } else {
  172. arr = new Uint8Array(dv.buffer);
  173. }
  174. dv[writerName](offset, val, false);
  175. return arr;
  176. };
  177. /**
  178. * Reads an OSC int32 ("i") value.
  179. *
  180. * @param {DataView} dv a DataView containing the raw bytes
  181. * @param {Object} offsetState an offsetState object used to store the current offset index into dv
  182. * @return {Number} the number that was read
  183. */
  184. osc.readInt32 = function (dv, offsetState) {
  185. return osc.readPrimitive(dv, "getInt32", 4, offsetState);
  186. };
  187. /**
  188. * Writes an OSC int32 ("i") value.
  189. *
  190. * @param {Number} val the number to write
  191. * @param {DataView} [dv] a DataView instance to write the number into
  192. * @param {Number} [offset] an offset into dv
  193. */
  194. osc.writeInt32 = function (val, dv, offset) {
  195. return osc.writePrimitive(val, dv, "setInt32", 4, offset);
  196. };
  197. /**
  198. * Reads an OSC int64 ("h") value.
  199. *
  200. * @param {DataView} dv a DataView containing the raw bytes
  201. * @param {Object} offsetState an offsetState object used to store the current offset index into dv
  202. * @return {Number} the number that was read
  203. */
  204. osc.readInt64 = function (dv, offsetState) {
  205. var high = osc.readPrimitive(dv, "getInt32", 4, offsetState),
  206. low = osc.readPrimitive(dv, "getInt32", 4, offsetState);
  207. if (Long) {
  208. return new Long(low, high);
  209. } else {
  210. return {
  211. high: high,
  212. low: low,
  213. unsigned: false
  214. };
  215. }
  216. };
  217. /**
  218. * Writes an OSC int64 ("h") value.
  219. *
  220. * @param {Number} val the number to write
  221. * @param {DataView} [dv] a DataView instance to write the number into
  222. * @param {Number} [offset] an offset into dv
  223. */
  224. osc.writeInt64 = function (val, dv, offset) {
  225. var arr = new Uint8Array(8);
  226. arr.set(osc.writePrimitive(val.high, dv, "setInt32", 4, offset), 0);
  227. arr.set(osc.writePrimitive(val.low, dv, "setInt32", 4, offset + 4), 4);
  228. return arr;
  229. };
  230. /**
  231. * Reads an OSC float32 ("f") value.
  232. *
  233. * @param {DataView} dv a DataView containing the raw bytes
  234. * @param {Object} offsetState an offsetState object used to store the current offset index into dv
  235. * @return {Number} the number that was read
  236. */
  237. osc.readFloat32 = function (dv, offsetState) {
  238. return osc.readPrimitive(dv, "getFloat32", 4, offsetState);
  239. };
  240. /**
  241. * Writes an OSC float32 ("f") value.
  242. *
  243. * @param {Number} val the number to write
  244. * @param {DataView} [dv] a DataView instance to write the number into
  245. * @param {Number} [offset] an offset into dv
  246. */
  247. osc.writeFloat32 = function (val, dv, offset) {
  248. return osc.writePrimitive(val, dv, "setFloat32", 4, offset);
  249. };
  250. /**
  251. * Reads an OSC float64 ("d") value.
  252. *
  253. * @param {DataView} dv a DataView containing the raw bytes
  254. * @param {Object} offsetState an offsetState object used to store the current offset index into dv
  255. * @return {Number} the number that was read
  256. */
  257. osc.readFloat64 = function (dv, offsetState) {
  258. return osc.readPrimitive(dv, "getFloat64", 8, offsetState);
  259. };
  260. /**
  261. * Writes an OSC float64 ("d") value.
  262. *
  263. * @param {Number} val the number to write
  264. * @param {DataView} [dv] a DataView instance to write the number into
  265. * @param {Number} [offset] an offset into dv
  266. */
  267. osc.writeFloat64 = function (val, dv, offset) {
  268. return osc.writePrimitive(val, dv, "setFloat64", 8, offset);
  269. };
  270. /**
  271. * Reads an OSC 32-bit ASCII character ("c") value.
  272. *
  273. * @param {DataView} dv a DataView containing the raw bytes
  274. * @param {Object} offsetState an offsetState object used to store the current offset index into dv
  275. * @return {String} a string containing the read character
  276. */
  277. osc.readChar32 = function (dv, offsetState) {
  278. var charCode = osc.readPrimitive(dv, "getUint32", 4, offsetState);
  279. return String.fromCharCode(charCode);
  280. };
  281. /**
  282. * Writes an OSC 32-bit ASCII character ("c") value.
  283. *
  284. * @param {String} str the string from which the first character will be written
  285. * @param {DataView} [dv] a DataView instance to write the character into
  286. * @param {Number} [offset] an offset into dv
  287. * @return {String} a string containing the read character
  288. */
  289. osc.writeChar32 = function (str, dv, offset) {
  290. var charCode = str.charCodeAt(0);
  291. if (charCode === undefined || charCode < -1) {
  292. return undefined;
  293. }
  294. return osc.writePrimitive(charCode, dv, "setUint32", 4, offset);
  295. };
  296. /**
  297. * Reads an OSC blob ("b") (i.e. a Uint8Array).
  298. *
  299. * @param {DataView} dv a DataView instance to read from
  300. * @param {Object} offsetState an offsetState object used to store the current offset index into dv
  301. * @return {Uint8Array} the data that was read
  302. */
  303. osc.readBlob = function (dv, offsetState) {
  304. var len = osc.readInt32(dv, offsetState),
  305. paddedLen = (len + 3) & ~0x03,
  306. blob = new Uint8Array(dv.buffer, offsetState.idx, len);
  307. offsetState.idx += paddedLen;
  308. return blob;
  309. };
  310. /**
  311. * Writes a raw collection of bytes to a new ArrayBuffer.
  312. *
  313. * @param {Array-like} data a collection of octets
  314. * @return {ArrayBuffer} a buffer containing the OSC-formatted blob
  315. */
  316. osc.writeBlob = function (data) {
  317. data = osc.byteArray(data);
  318. var len = data.byteLength,
  319. paddedLen = (len + 3) & ~0x03,
  320. offset = 4, // Extra 4 bytes is for the size.
  321. blobLen = paddedLen + offset,
  322. arr = new Uint8Array(blobLen),
  323. dv = new DataView(arr.buffer);
  324. // Write the size.
  325. osc.writeInt32(len, dv);
  326. // Since we're writing to a real ArrayBuffer,
  327. // we don't need to pad the remaining bytes.
  328. arr.set(data, offset);
  329. return arr;
  330. };
  331. /**
  332. * Reads an OSC 4-byte MIDI message.
  333. *
  334. * @param {DataView} dv the DataView instance to read from
  335. * @param {Object} offsetState an offsetState object used to store the current offset index into dv
  336. * @return {Uint8Array} an array containing (in order) the port ID, status, data1 and data1 bytes
  337. */
  338. osc.readMIDIBytes = function (dv, offsetState) {
  339. var midi = new Uint8Array(dv.buffer, offsetState.idx, 4);
  340. offsetState.idx += 4;
  341. return midi;
  342. };
  343. /**
  344. * Writes an OSC 4-byte MIDI message.
  345. *
  346. * @param {Array-like} bytes a 4-element array consisting of the port ID, status, data1 and data1 bytes
  347. * @return {Uint8Array} the written message
  348. */
  349. osc.writeMIDIBytes = function (bytes) {
  350. bytes = osc.byteArray(bytes);
  351. var arr = new Uint8Array(4);
  352. arr.set(bytes);
  353. return arr;
  354. };
  355. /**
  356. * Reads an OSC RGBA colour value.
  357. *
  358. * @param {DataView} dv the DataView instance to read from
  359. * @param {Object} offsetState an offsetState object used to store the current offset index into dv
  360. * @return {Object} a colour object containing r, g, b, and a properties
  361. */
  362. osc.readColor = function (dv, offsetState) {
  363. var bytes = new Uint8Array(dv.buffer, offsetState.idx, 4),
  364. alpha = bytes[3] / 255;
  365. offsetState.idx += 4;
  366. return {
  367. r: bytes[0],
  368. g: bytes[1],
  369. b: bytes[2],
  370. a: alpha
  371. };
  372. };
  373. /**
  374. * Writes an OSC RGBA colour value.
  375. *
  376. * @param {Object} color a colour object containing r, g, b, and a properties
  377. * @return {Uint8Array} a byte array containing the written color
  378. */
  379. osc.writeColor = function (color) {
  380. var alpha = Math.round(color.a * 255),
  381. arr = new Uint8Array([color.r, color.g, color.b, alpha]);
  382. return arr;
  383. };
  384. /**
  385. * Reads an OSC true ("T") value by directly returning the JavaScript Boolean "true".
  386. */
  387. osc.readTrue = function () {
  388. return true;
  389. };
  390. /**
  391. * Reads an OSC false ("F") value by directly returning the JavaScript Boolean "false".
  392. */
  393. osc.readFalse = function () {
  394. return false;
  395. };
  396. /**
  397. * Reads an OSC nil ("N") value by directly returning the JavaScript "null" value.
  398. */
  399. osc.readNull = function () {
  400. return null;
  401. };
  402. /**
  403. * Reads an OSC impulse/bang/infinitum ("I") value by directly returning 1.0.
  404. */
  405. osc.readImpulse = function () {
  406. return 1.0;
  407. };
  408. /**
  409. * Reads an OSC time tag ("t").
  410. *
  411. * @param {DataView} dv the DataView instance to read from
  412. * @param {Object} offsetState an offset state object containing the current index into dv
  413. * @param {Object} a time tag object containing both the raw NTP as well as the converted native (i.e. JS/UNIX) time
  414. */
  415. osc.readTimeTag = function (dv, offsetState) {
  416. var secs1900 = osc.readPrimitive(dv, "getUint32", 4, offsetState),
  417. frac = osc.readPrimitive(dv, "getUint32", 4, offsetState),
  418. native = (secs1900 === 0 && frac === 1) ? Date.now() : osc.ntpToJSTime(secs1900, frac);
  419. return {
  420. raw: [secs1900, frac],
  421. native: native
  422. };
  423. };
  424. /**
  425. * Writes an OSC time tag ("t").
  426. *
  427. * Takes, as its argument, a time tag object containing either a "raw" or "native property."
  428. * The raw timestamp must conform to the NTP standard representation, consisting of two unsigned int32
  429. * values. The first represents the number of seconds since January 1, 1900; the second, fractions of a second.
  430. * "Native" JavaScript timestamps are specified as a Number representing milliseconds since January 1, 1970.
  431. *
  432. * @param {Object} timeTag time tag object containing either a native JS timestamp (in ms) or a NTP timestamp pair
  433. * @return {Uint8Array} raw bytes for the written time tag
  434. */
  435. osc.writeTimeTag = function (timeTag) {
  436. var raw = timeTag.raw ? timeTag.raw : osc.jsToNTPTime(timeTag.native),
  437. arr = new Uint8Array(8), // Two Unit32s.
  438. dv = new DataView(arr.buffer);
  439. osc.writeInt32(raw[0], dv, 0);
  440. osc.writeInt32(raw[1], dv, 4);
  441. return arr;
  442. };
  443. /**
  444. * Produces a time tag containing a raw NTP timestamp
  445. * relative to now by the specified number of seconds.
  446. *
  447. * @param {Number} secs the number of seconds relative to now (i.e. + for the future, - for the past)
  448. * @param {Number} now the number of milliseconds since epoch to use as the current time. Defaults to Date.now()
  449. * @return {Object} the time tag
  450. */
  451. osc.timeTag = function (secs, now) {
  452. secs = secs || 0;
  453. now = now || Date.now();
  454. var nowSecs = now / 1000,
  455. nowWhole = Math.floor(nowSecs),
  456. nowFracs = nowSecs - nowWhole,
  457. secsWhole = Math.floor(secs),
  458. secsFracs = secs - secsWhole,
  459. fracs = nowFracs + secsFracs;
  460. if (fracs > 1) {
  461. var fracsWhole = Math.floor(fracs),
  462. fracsFracs = fracs - fracsWhole;
  463. secsWhole += fracsWhole;
  464. fracs = fracsFracs;
  465. }
  466. var ntpSecs = nowWhole + secsWhole + osc.SECS_70YRS,
  467. ntpFracs = Math.round(osc.TWO_32 * fracs);
  468. return {
  469. raw: [ntpSecs, ntpFracs]
  470. };
  471. };
  472. /**
  473. * Converts OSC's standard time tag representation (which is the NTP format)
  474. * into the JavaScript/UNIX format in milliseconds.
  475. *
  476. * @param {Number} secs1900 the number of seconds since 1900
  477. * @param {Number} frac the number of fractions of a second (between 0 and 2^32)
  478. * @return {Number} a JavaScript-compatible timestamp in milliseconds
  479. */
  480. osc.ntpToJSTime = function (secs1900, frac) {
  481. var secs1970 = secs1900 - osc.SECS_70YRS,
  482. decimals = frac / osc.TWO_32,
  483. msTime = (secs1970 + decimals) * 1000;
  484. return msTime;
  485. };
  486. osc.jsToNTPTime = function (jsTime) {
  487. var secs = jsTime / 1000,
  488. secsWhole = Math.floor(secs),
  489. secsFrac = secs - secsWhole,
  490. ntpSecs = secsWhole + osc.SECS_70YRS,
  491. ntpFracs = Math.round(osc.TWO_32 * secsFrac);
  492. return [ntpSecs, ntpFracs];
  493. };
  494. /**
  495. * Reads the argument portion of an OSC message.
  496. *
  497. * @param {DataView} dv a DataView instance to read from
  498. * @param {Object} offsetState the offsetState object that stores the current offset into dv
  499. * @param {Oobject} [options] read options
  500. * @return {Array} an array of the OSC arguments that were read
  501. */
  502. osc.readArguments = function (dv, options, offsetState) {
  503. var typeTagString = osc.readString(dv, offsetState);
  504. if (typeTagString.indexOf(",") !== 0) {
  505. // Despite what the OSC 1.0 spec says,
  506. // it just doesn't make sense to handle messages without type tags.
  507. // scsynth appears to read such messages as if they have a single
  508. // Uint8 argument. sclang throws an error if the type tag is omitted.
  509. throw new Error("A malformed type tag string was found while reading " +
  510. "the arguments of an OSC message. String was: " +
  511. typeTagString, " at offset: " + offsetState.idx);
  512. }
  513. var argTypes = typeTagString.substring(1).split(""),
  514. args = [];
  515. osc.readArgumentsIntoArray(args, argTypes, typeTagString, dv, options, offsetState);
  516. return args;
  517. };
  518. // Unsupported, non-API function.
  519. osc.readArgument = function (argType, typeTagString, dv, options, offsetState) {
  520. var typeSpec = osc.argumentTypes[argType];
  521. if (!typeSpec) {
  522. throw new Error("'" + argType + "' is not a valid OSC type tag. Type tag string was: " + typeTagString);
  523. }
  524. var argReader = typeSpec.reader,
  525. arg = osc[argReader](dv, offsetState);
  526. if (options.metadata) {
  527. arg = {
  528. type: argType,
  529. value: arg
  530. };
  531. }
  532. return arg;
  533. };
  534. // Unsupported, non-API function.
  535. osc.readArgumentsIntoArray = function (arr, argTypes, typeTagString, dv, options, offsetState) {
  536. var i = 0;
  537. while (i < argTypes.length) {
  538. var argType = argTypes[i],
  539. arg;
  540. if (argType === "[") {
  541. var fromArrayOpen = argTypes.slice(i + 1),
  542. endArrayIdx = fromArrayOpen.indexOf("]");
  543. if (endArrayIdx < 0) {
  544. throw new Error("Invalid argument type tag: an open array type tag ('[') was found " +
  545. "without a matching close array tag ('[]'). Type tag was: " + typeTagString);
  546. }
  547. var typesInArray = fromArrayOpen.slice(0, endArrayIdx);
  548. arg = osc.readArgumentsIntoArray([], typesInArray, typeTagString, dv, options, offsetState);
  549. i += endArrayIdx + 2;
  550. } else {
  551. arg = osc.readArgument(argType, typeTagString, dv, options, offsetState);
  552. i++;
  553. }
  554. arr.push(arg);
  555. }
  556. return arr;
  557. };
  558. /**
  559. * Writes the specified arguments.
  560. *
  561. * @param {Array} args an array of arguments
  562. * @param {Object} options options for writing
  563. * @return {Uint8Array} a buffer containing the OSC-formatted argument type tag and values
  564. */
  565. osc.writeArguments = function (args, options) {
  566. var argCollection = osc.collectArguments(args, options);
  567. return osc.joinParts(argCollection);
  568. };
  569. // Unsupported, non-API function.
  570. osc.joinParts = function (dataCollection) {
  571. var buf = new Uint8Array(dataCollection.byteLength),
  572. parts = dataCollection.parts,
  573. offset = 0;
  574. for (var i = 0; i < parts.length; i++) {
  575. var part = parts[i];
  576. osc.copyByteArray(part, buf, offset);
  577. offset += part.length;
  578. }
  579. return buf;
  580. };
  581. // Unsupported, non-API function.
  582. osc.addDataPart = function (dataPart, dataCollection) {
  583. dataCollection.parts.push(dataPart);
  584. dataCollection.byteLength += dataPart.length;
  585. };
  586. osc.writeArrayArguments = function (args, dataCollection) {
  587. var typeTag = "[";
  588. for (var i = 0; i < args.length; i++) {
  589. var arg = args[i];
  590. typeTag += osc.writeArgument(arg, dataCollection);
  591. }
  592. typeTag += "]";
  593. return typeTag;
  594. };
  595. osc.writeArgument = function (arg, dataCollection) {
  596. if (osc.isArray(arg)) {
  597. return osc.writeArrayArguments(arg, dataCollection);
  598. }
  599. var type = arg.type,
  600. writer = osc.argumentTypes[type].writer;
  601. if (writer) {
  602. var data = osc[writer](arg.value);
  603. osc.addDataPart(data, dataCollection);
  604. }
  605. return arg.type;
  606. };
  607. // Unsupported, non-API function.
  608. osc.collectArguments = function (args, options, dataCollection) {
  609. if (!osc.isArray(args)) {
  610. args = typeof args === "undefined" ? [] : [args];
  611. }
  612. dataCollection = dataCollection || {
  613. byteLength: 0,
  614. parts: []
  615. };
  616. if (!options.metadata) {
  617. args = osc.annotateArguments(args);
  618. }
  619. var typeTagString = ",",
  620. currPartIdx = dataCollection.parts.length;
  621. for (var i = 0; i < args.length; i++) {
  622. var arg = args[i];
  623. typeTagString += osc.writeArgument(arg, dataCollection);
  624. }
  625. var typeData = osc.writeString(typeTagString);
  626. dataCollection.byteLength += typeData.byteLength;
  627. dataCollection.parts.splice(currPartIdx, 0, typeData);
  628. return dataCollection;
  629. };
  630. /**
  631. * Reads an OSC message.
  632. *
  633. * @param {Array-like} data an array of bytes to read from
  634. * @param {Object} [options] read options
  635. * @param {Object} [offsetState] an offsetState object that stores the current offset into dv
  636. * @return {Object} the OSC message, formatted as a JavaScript object containing "address" and "args" properties
  637. */
  638. osc.readMessage = function (data, options, offsetState) {
  639. options = options || osc.defaults;
  640. var dv = osc.dataView(data, data.byteOffset, data.byteLength);
  641. offsetState = offsetState || {
  642. idx: 0
  643. };
  644. var address = osc.readString(dv, offsetState);
  645. return osc.readMessageContents(address, dv, options, offsetState);
  646. };
  647. // Unsupported, non-API function.
  648. osc.readMessageContents = function (address, dv, options, offsetState) {
  649. if (address.indexOf("/") !== 0) {
  650. throw new Error("A malformed OSC address was found while reading " +
  651. "an OSC message. String was: " + address);
  652. }
  653. var args = osc.readArguments(dv, options, offsetState);
  654. return {
  655. address: address,
  656. args: args.length === 1 && options.unpackSingleArgs ? args[0] : args
  657. };
  658. };
  659. // Unsupported, non-API function.
  660. osc.collectMessageParts = function (msg, options, dataCollection) {
  661. dataCollection = dataCollection || {
  662. byteLength: 0,
  663. parts: []
  664. };
  665. osc.addDataPart(osc.writeString(msg.address), dataCollection);
  666. return osc.collectArguments(msg.args, options, dataCollection);
  667. };
  668. /**
  669. * Writes an OSC message.
  670. *
  671. * @param {Object} msg a message object containing "address" and "args" properties
  672. * @param {Object} [options] write options
  673. * @return {Uint8Array} an array of bytes containing the OSC message
  674. */
  675. osc.writeMessage = function (msg, options) {
  676. options = options || osc.defaults;
  677. if (!osc.isValidMessage(msg)) {
  678. throw new Error("An OSC message must contain a valid address. Message was: " +
  679. JSON.stringify(msg, null, 2));
  680. }
  681. var msgCollection = osc.collectMessageParts(msg, options);
  682. return osc.joinParts(msgCollection);
  683. };
  684. osc.isValidMessage = function (msg) {
  685. return msg.address && msg.address.indexOf("/") === 0;
  686. };
  687. /**
  688. * Reads an OSC bundle.
  689. *
  690. * @param {DataView} dv the DataView instance to read from
  691. * @param {Object} [options] read optoins
  692. * @param {Object} [offsetState] an offsetState object that stores the current offset into dv
  693. * @return {Object} the bundle or message object that was read
  694. */
  695. osc.readBundle = function (dv, options, offsetState) {
  696. return osc.readPacket(dv, options, offsetState);
  697. };
  698. // Unsupported, non-API function.
  699. osc.collectBundlePackets = function (bundle, options, dataCollection) {
  700. dataCollection = dataCollection || {
  701. byteLength: 0,
  702. parts: []
  703. };
  704. osc.addDataPart(osc.writeString("#bundle"), dataCollection);
  705. osc.addDataPart(osc.writeTimeTag(bundle.timeTag), dataCollection);
  706. for (var i = 0; i < bundle.packets.length; i++) {
  707. var packet = bundle.packets[i],
  708. collector = packet.address ? osc.collectMessageParts : osc.collectBundlePackets,
  709. packetCollection = collector(packet, options);
  710. dataCollection.byteLength += packetCollection.byteLength;
  711. osc.addDataPart(osc.writeInt32(packetCollection.byteLength), dataCollection);
  712. dataCollection.parts = dataCollection.parts.concat(packetCollection.parts);
  713. }
  714. return dataCollection;
  715. };
  716. /**
  717. * Writes an OSC bundle.
  718. *
  719. * @param {Object} a bundle object containing "timeTag" and "packets" properties
  720. * @param {object} [options] write options
  721. * @return {Uint8Array} an array of bytes containing the message
  722. */
  723. osc.writeBundle = function (bundle, options) {
  724. if (!osc.isValidBundle(bundle)) {
  725. throw new Error("An OSC bundle must contain 'timeTag' and 'packets' properties. " +
  726. "Bundle was: " + JSON.stringify(bundle, null, 2));
  727. }
  728. options = options || osc.defaults;
  729. var bundleCollection = osc.collectBundlePackets(bundle, options);
  730. return osc.joinParts(bundleCollection);
  731. };
  732. osc.isValidBundle = function (bundle) {
  733. return bundle.timeTag !== undefined && bundle.packets !== undefined;
  734. };
  735. // Unsupported, non-API function.
  736. osc.readBundleContents = function (dv, options, offsetState, len) {
  737. var timeTag = osc.readTimeTag(dv, offsetState),
  738. packets = [];
  739. while (offsetState.idx < len) {
  740. var packetSize = osc.readInt32(dv, offsetState),
  741. packetLen = offsetState.idx + packetSize,
  742. packet = osc.readPacket(dv, options, offsetState, packetLen);
  743. packets.push(packet);
  744. }
  745. return {
  746. timeTag: timeTag,
  747. packets: packets
  748. };
  749. };
  750. /**
  751. * Reads an OSC packet, which may consist of either a bundle or a message.
  752. *
  753. * @param {Array-like} data an array of bytes to read from
  754. * @param {Object} [options] read options
  755. * @return {Object} a bundle or message object
  756. */
  757. osc.readPacket = function (data, options, offsetState, len) {
  758. var dv = osc.dataView(data, data.byteOffset, data.byteLength);
  759. len = len === undefined ? dv.byteLength : len;
  760. offsetState = offsetState || {
  761. idx: 0
  762. };
  763. var header = osc.readString(dv, offsetState),
  764. firstChar = header[0];
  765. if (firstChar === "#") {
  766. return osc.readBundleContents(dv, options, offsetState, len);
  767. } else if (firstChar === "/") {
  768. return osc.readMessageContents(header, dv, options, offsetState);
  769. }
  770. throw new Error("The header of an OSC packet didn't contain an OSC address or a #bundle string." +
  771. " Header was: " + header);
  772. };
  773. /**
  774. * Writes an OSC packet, which may consist of either of a bundle or a message.
  775. *
  776. * @param {Object} a bundle or message object
  777. * @param {Object} [options] write options
  778. * @return {Uint8Array} an array of bytes containing the message
  779. */
  780. osc.writePacket = function (packet, options) {
  781. if (osc.isValidMessage(packet)) {
  782. return osc.writeMessage(packet, options);
  783. } else if (osc.isValidBundle(packet)) {
  784. return osc.writeBundle(packet, options);
  785. } else {
  786. throw new Error("The specified packet was not recognized as a valid OSC message or bundle." +
  787. " Packet was: " + JSON.stringify(packet, null, 2));
  788. }
  789. };
  790. // Unsupported, non-API.
  791. osc.argumentTypes = {
  792. i: {
  793. reader: "readInt32",
  794. writer: "writeInt32"
  795. },
  796. h: {
  797. reader: "readInt64",
  798. writer: "writeInt64"
  799. },
  800. f: {
  801. reader: "readFloat32",
  802. writer: "writeFloat32"
  803. },
  804. s: {
  805. reader: "readString",
  806. writer: "writeString"
  807. },
  808. S: {
  809. reader: "readString",
  810. writer: "writeString"
  811. },
  812. b: {
  813. reader: "readBlob",
  814. writer: "writeBlob"
  815. },
  816. t: {
  817. reader: "readTimeTag",
  818. writer: "writeTimeTag"
  819. },
  820. T: {
  821. reader: "readTrue"
  822. },
  823. F: {
  824. reader: "readFalse"
  825. },
  826. N: {
  827. reader: "readNull"
  828. },
  829. I: {
  830. reader: "readImpulse"
  831. },
  832. d: {
  833. reader: "readFloat64",
  834. writer: "writeFloat64"
  835. },
  836. c: {
  837. reader: "readChar32",
  838. writer: "writeChar32"
  839. },
  840. r: {
  841. reader: "readColor",
  842. writer: "writeColor"
  843. },
  844. m: {
  845. reader: "readMIDIBytes",
  846. writer: "writeMIDIBytes"
  847. },
  848. // [] are special cased within read/writeArguments()
  849. };
  850. // Unsupported, non-API function.
  851. osc.inferTypeForArgument = function (arg) {
  852. var type = typeof arg;
  853. // TODO: This is freaking hideous.
  854. switch (type) {
  855. case "boolean":
  856. return arg ? "T" : "F";
  857. case "string":
  858. return "s";
  859. case "number":
  860. return "f";
  861. case "undefined":
  862. return "N";
  863. case "object":
  864. if (arg === null) {
  865. return "N";
  866. } else if (arg instanceof Uint8Array ||
  867. arg instanceof ArrayBuffer) {
  868. return "b";
  869. } else if (typeof arg.high === "number" && typeof arg.low === "number") {
  870. return "h";
  871. }
  872. break;
  873. }
  874. throw new Error("Can't infer OSC argument type for value: " +
  875. JSON.stringify(arg, null, 2));
  876. };
  877. // Unsupported, non-API function.
  878. osc.annotateArguments = function (args) {
  879. var annotated = [];
  880. for (var i = 0; i < args.length; i++) {
  881. var arg = args[i],
  882. msgArg;
  883. if (typeof (arg) === "object" && arg.type && arg.value !== undefined) {
  884. // We've got an explicitly typed argument.
  885. msgArg = arg;
  886. } else if (osc.isArray(arg)) {
  887. // We've got an array of arguments,
  888. // so they each need to be inferred and expanded.
  889. msgArg = osc.annotateArguments(arg);
  890. } else {
  891. var oscType = osc.inferTypeForArgument(arg);
  892. msgArg = {
  893. type: oscType,
  894. value: arg
  895. };
  896. }
  897. annotated.push(msgArg);
  898. }
  899. return annotated;
  900. };
  901. if (osc.isCommonJS) {
  902. module.exports = osc;
  903. }
  904. }());
  905. ;/*
  906. * osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js
  907. *
  908. * Cross-platform base transport library for osc.js.
  909. *
  910. * Copyright 2014-2016, Colin Clark
  911. * Licensed under the MIT and GPL 3 licenses.
  912. */
  913. /* global require, module */
  914. var osc = osc,
  915. slip = slip,
  916. EventEmitter = EventEmitter;
  917. (function () {
  918. "use strict";
  919. // Unsupported, non-API function.
  920. osc.firePacketEvents = function (port, packet, timeTag, packetInfo) {
  921. if (packet.address) {
  922. port.emit("message", packet, timeTag, packetInfo);
  923. } else {
  924. osc.fireBundleEvents(port, packet, timeTag, packetInfo);
  925. }
  926. };
  927. // Unsupported, non-API function.
  928. osc.fireBundleEvents = function (port, bundle, timeTag, packetInfo) {
  929. port.emit("bundle", bundle, timeTag, packetInfo);
  930. for (var i = 0; i < bundle.packets.length; i++) {
  931. var packet = bundle.packets[i];
  932. osc.firePacketEvents(port, packet, bundle.timeTag, packetInfo);
  933. }
  934. };
  935. osc.fireClosedPortSendError = function (port, msg) {
  936. msg = msg || "Can't send packets on a closed osc.Port object. Please open (or reopen) this Port by calling open().";
  937. port.emit("error", msg);
  938. };
  939. osc.Port = function (options) {
  940. this.options = options || {};
  941. this.on("data", this.decodeOSC.bind(this));
  942. };
  943. var p = osc.Port.prototype = Object.create(EventEmitter.prototype);
  944. p.constructor = osc.Port;
  945. p.send = function (oscPacket) {
  946. var args = Array.prototype.slice.call(arguments),
  947. encoded = this.encodeOSC(oscPacket),
  948. buf = osc.nativeBuffer(encoded);
  949. args[0] = buf;
  950. this.sendRaw.apply(this, args);
  951. };
  952. p.encodeOSC = function (packet) {
  953. // TODO gh-39: This is unsafe; we should only access the underlying
  954. // buffer within the range of its view.
  955. packet = packet.buffer ? packet.buffer : packet;
  956. var encoded;
  957. try {
  958. encoded = osc.writePacket(packet, this.options);
  959. } catch (err) {
  960. this.emit("error", err);
  961. }
  962. return encoded;
  963. };
  964. p.decodeOSC = function (data, packetInfo) {
  965. data = osc.byteArray(data);
  966. this.emit("raw", data, packetInfo);
  967. try {
  968. var packet = osc.readPacket(data, this.options);
  969. this.emit("osc", packet, packetInfo);
  970. osc.firePacketEvents(this, packet, undefined, packetInfo);
  971. } catch (err) {
  972. this.emit("error", err);
  973. }
  974. };
  975. osc.SLIPPort = function (options) {
  976. var that = this;
  977. var o = this.options = options || {};
  978. o.useSLIP = o.useSLIP === undefined ? true : o.useSLIP;
  979. this.decoder = new slip.Decoder({
  980. onMessage: this.decodeOSC.bind(this),
  981. onError: function (err) {
  982. that.emit("error", err);
  983. }
  984. });
  985. var decodeHandler = o.useSLIP ? this.decodeSLIPData : this.decodeOSC;
  986. this.on("data", decodeHandler.bind(this));
  987. };
  988. p = osc.SLIPPort.prototype = Object.create(osc.Port.prototype);
  989. p.constructor = osc.SLIPPort;
  990. p.encodeOSC = function (packet) {
  991. // TODO gh-39: This is unsafe; we should only access the underlying
  992. // buffer within the range of its view.
  993. packet = packet.buffer ? packet.buffer : packet;
  994. var framed;
  995. try {
  996. var encoded = osc.writePacket(packet, this.options);
  997. framed = slip.encode(encoded);
  998. } catch (err) {
  999. this.emit("error", err);
  1000. }
  1001. return framed;
  1002. };
  1003. p.decodeSLIPData = function (data, packetInfo) {
  1004. // TODO: Get packetInfo through SLIP decoder.
  1005. this.decoder.decode(data, packetInfo);
  1006. };
  1007. // Unsupported, non-API function.
  1008. osc.relay = function (from, to, eventName, sendFnName, transformFn, sendArgs) {
  1009. eventName = eventName || "message";
  1010. sendFnName = sendFnName || "send";
  1011. transformFn = transformFn || function () { };
  1012. sendArgs = sendArgs ? [null].concat(sendArgs) : [];
  1013. var listener = function (data) {
  1014. sendArgs[0] = data;
  1015. data = transformFn(data);
  1016. to[sendFnName].apply(to, sendArgs);
  1017. };
  1018. from.on(eventName, listener);
  1019. return {
  1020. eventName: eventName,
  1021. listener: listener
  1022. };
  1023. };
  1024. // Unsupported, non-API function.
  1025. osc.relayPorts = function (from, to, o) {
  1026. var eventName = o.raw ? "raw" : "osc",
  1027. sendFnName = o.raw ? "sendRaw" : "send";
  1028. return osc.relay(from, to, eventName, sendFnName, o.transform);
  1029. };
  1030. // Unsupported, non-API function.
  1031. osc.stopRelaying = function (from, relaySpec) {
  1032. from.removeListener(relaySpec.eventName, relaySpec.listener);
  1033. };
  1034. /**
  1035. * A Relay connects two sources of OSC data together,
  1036. * relaying all OSC messages received by each port to the other.
  1037. * @constructor
  1038. *
  1039. * @param {osc.Port} port1 the first port to relay
  1040. * @param {osc.Port} port2 the second port to relay
  1041. * @param {Object} options the configuration options for this relay
  1042. */
  1043. osc.Relay = function (port1, port2, options) {
  1044. var o = this.options = options || {};
  1045. o.raw = false;
  1046. this.port1 = port1;
  1047. this.port2 = port2;
  1048. this.listen();
  1049. };
  1050. p = osc.Relay.prototype = Object.create(EventEmitter.prototype);
  1051. p.constructor = osc.Relay;
  1052. p.open = function () {
  1053. this.port1.open();
  1054. this.port2.open();
  1055. };
  1056. p.listen = function () {
  1057. if (this.port1Spec && this.port2Spec) {
  1058. this.close();
  1059. }
  1060. this.port1Spec = osc.relayPorts(this.port1, this.port2, this.options);
  1061. this.port2Spec = osc.relayPorts(this.port2, this.port1, this.options);
  1062. // Bind port close listeners to ensure that the relay
  1063. // will stop forwarding messages if one of its ports close.
  1064. // Users are still responsible for closing the underlying ports
  1065. // if necessary.
  1066. var closeListener = this.close.bind(this);
  1067. this.port1.on("close", closeListener);
  1068. this.port2.on("close", closeListener);
  1069. };
  1070. p.close = function () {
  1071. osc.stopRelaying(this.port1, this.port1Spec);
  1072. osc.stopRelaying(this.port2, this.port2Spec);
  1073. this.emit("close", this.port1, this.port2);
  1074. };
  1075. // If we're in a require-compatible environment, export ourselves.
  1076. if (typeof module !== "undefined" && module.exports) {
  1077. module.exports = osc;
  1078. }
  1079. }());
  1080. ;/*
  1081. * osc.js: An Open Sound Control library for JavaScript that works in both the browser and Node.js
  1082. *
  1083. * Cross-Platform Web Socket client transport for osc.js.
  1084. *
  1085. * Copyright 2014-2016, Colin Clark
  1086. * Licensed under the MIT and GPL 3 licenses.
  1087. */
  1088. /*global WebSocket, require*/
  1089. var osc = osc;
  1090. (function () {
  1091. "use strict";
  1092. if (typeof WebSocket !== "undefined"){
  1093. osc.WebSocket = WebSocket
  1094. }
  1095. osc.WebSocketPort = function (options) {
  1096. osc.Port.call(this, options);
  1097. this.on("open", this.listen.bind(this));
  1098. this.socket = options.socket;
  1099. if (this.socket) {
  1100. if (this.socket.readyState === 1) {
  1101. osc.WebSocketPort.setupSocketForBinary(this.socket);
  1102. this.emit("open", this.socket);
  1103. } else {
  1104. this.open();
  1105. }
  1106. }
  1107. };
  1108. var p = osc.WebSocketPort.prototype = Object.create(osc.Port.prototype);
  1109. p.constructor = osc.WebSocketPort;
  1110. p.open = function () {
  1111. if (!this.socket || this.socket.readyState > 1) {
  1112. this.socket = new osc.WebSocket(this.options.url);
  1113. }
  1114. osc.WebSocketPort.setupSocketForBinary(this.socket);
  1115. var that = this;
  1116. this.socket.onopen = function () {
  1117. that.emit("open", that.socket);
  1118. };
  1119. };
  1120. p.listen = function () {
  1121. var that = this;
  1122. this.socket.onmessage = function (e) {
  1123. that.emit("data", e.data, e);
  1124. };
  1125. this.socket.onerror = function (err) {
  1126. that.emit("error", err);
  1127. };
  1128. this.socket.onclose = function (e) {
  1129. that.emit("close", e);
  1130. };
  1131. that.emit("ready");
  1132. };
  1133. p.sendRaw = function (encoded) {
  1134. if (!this.socket || this.socket.readyState !== 1) {
  1135. osc.fireClosedPortSendError(this);
  1136. return;
  1137. }
  1138. this.socket.send(encoded);
  1139. };
  1140. p.close = function (code, reason) {
  1141. this.socket.close(code, reason);
  1142. };
  1143. osc.WebSocketPort.setupSocketForBinary = function (socket) {
  1144. socket.binaryType = osc.isNode ? "nodebuffer" : "arraybuffer";
  1145. };
  1146. }());
  1147. ;
  1148. return osc;
  1149. });