osc.js 34 KB

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