Mercurial > hg > beaglert
comparison resources/osc/node_modules/osc-min/lib/osc-utilities.js @ 271:fb9c28a4676b prerelease
Added osc example project and node script for testing
author | Liam Donovan <l.b.donovan@qmul.ac.uk> |
---|---|
date | Tue, 17 May 2016 16:01:06 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
270:de37582ce6f3 | 271:fb9c28a4676b |
---|---|
1 // Generated by CoffeeScript 1.10.0 | |
2 (function() { | |
3 var IsArray, StrictError, TWO_POW_32, UNIX_EPOCH, binpack, getArrayArg, isOscBundleBuffer, makeTimetag, mapBundleList, oscTypeCodes, padding, toOscTypeAndArgs, | |
4 hasProp = {}.hasOwnProperty; | |
5 | |
6 binpack = require("binpack"); | |
7 | |
8 exports.concat = function(buffers) { | |
9 var buffer, copyTo, destBuffer, j, k, l, len, len1, len2, sumLength; | |
10 if (!IsArray(buffers)) { | |
11 throw new Error("concat must take an array of buffers"); | |
12 } | |
13 for (j = 0, len = buffers.length; j < len; j++) { | |
14 buffer = buffers[j]; | |
15 if (!Buffer.isBuffer(buffer)) { | |
16 throw new Error("concat must take an array of buffers"); | |
17 } | |
18 } | |
19 sumLength = 0; | |
20 for (k = 0, len1 = buffers.length; k < len1; k++) { | |
21 buffer = buffers[k]; | |
22 sumLength += buffer.length; | |
23 } | |
24 destBuffer = new Buffer(sumLength); | |
25 copyTo = 0; | |
26 for (l = 0, len2 = buffers.length; l < len2; l++) { | |
27 buffer = buffers[l]; | |
28 buffer.copy(destBuffer, copyTo); | |
29 copyTo += buffer.length; | |
30 } | |
31 return destBuffer; | |
32 }; | |
33 | |
34 exports.toOscString = function(str, strict) { | |
35 var i, j, nullIndex, ref; | |
36 if (!(typeof str === "string")) { | |
37 throw new Error("can't pack a non-string into an osc-string"); | |
38 } | |
39 nullIndex = str.indexOf("\u0000"); | |
40 if (nullIndex !== -1 && strict) { | |
41 throw StrictError("Can't pack an osc-string that contains NULL characters"); | |
42 } | |
43 if (nullIndex !== -1) { | |
44 str = str.slice(0, nullIndex); | |
45 } | |
46 for (i = j = 0, ref = padding(str); 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) { | |
47 str += "\u0000"; | |
48 } | |
49 return new Buffer(str); | |
50 }; | |
51 | |
52 exports.splitOscString = function(buffer, strict) { | |
53 var i, j, nullIndex, rawStr, ref, ref1, rest, splitPoint, str; | |
54 if (!Buffer.isBuffer(buffer)) { | |
55 throw StrictError("Can't split something that isn't a buffer"); | |
56 } | |
57 rawStr = buffer.toString("utf8"); | |
58 nullIndex = rawStr.indexOf("\u0000"); | |
59 if (nullIndex === -1) { | |
60 if (strict) { | |
61 throw new Error("All osc-strings must contain a null character"); | |
62 } | |
63 return { | |
64 string: rawStr, | |
65 rest: new Buffer(0) | |
66 }; | |
67 } | |
68 str = rawStr.slice(0, nullIndex); | |
69 splitPoint = Buffer.byteLength(str) + padding(str); | |
70 if (strict && splitPoint > buffer.length) { | |
71 throw StrictError("Not enough padding for osc-string"); | |
72 } | |
73 if (strict) { | |
74 for (i = j = ref = Buffer.byteLength(str), ref1 = splitPoint; ref <= ref1 ? j < ref1 : j > ref1; i = ref <= ref1 ? ++j : --j) { | |
75 if (buffer[i] !== 0) { | |
76 throw StrictError("Not enough or incorrect padding for osc-string"); | |
77 } | |
78 } | |
79 } | |
80 rest = buffer.slice(splitPoint, buffer.length); | |
81 return { | |
82 string: str, | |
83 rest: rest | |
84 }; | |
85 }; | |
86 | |
87 exports.splitInteger = function(buffer, type) { | |
88 var bytes, num, rest, value; | |
89 if (type == null) { | |
90 type = "Int32"; | |
91 } | |
92 bytes = (binpack["pack" + type](0)).length; | |
93 if (buffer.length < bytes) { | |
94 throw new Error("buffer is not big enough for integer type"); | |
95 } | |
96 num = 0; | |
97 value = binpack["unpack" + type](buffer.slice(0, bytes), "big"); | |
98 rest = buffer.slice(bytes, buffer.length); | |
99 return { | |
100 integer: value, | |
101 rest: rest | |
102 }; | |
103 }; | |
104 | |
105 exports.splitTimetag = function(buffer) { | |
106 var a, b, bytes, c, d, fractional, rest, seconds, type; | |
107 type = "Int32"; | |
108 bytes = (binpack["pack" + type](0)).length; | |
109 if (buffer.length < (bytes * 2)) { | |
110 throw new Error("buffer is not big enough to contain a timetag"); | |
111 } | |
112 a = 0; | |
113 b = bytes; | |
114 seconds = binpack["unpack" + type](buffer.slice(a, b), "big"); | |
115 c = bytes; | |
116 d = bytes + bytes; | |
117 fractional = binpack["unpack" + type](buffer.slice(c, d), "big"); | |
118 rest = buffer.slice(d, buffer.length); | |
119 return { | |
120 timetag: [seconds, fractional], | |
121 rest: rest | |
122 }; | |
123 }; | |
124 | |
125 UNIX_EPOCH = 2208988800; | |
126 | |
127 TWO_POW_32 = 4294967296; | |
128 | |
129 exports.dateToTimetag = function(date) { | |
130 return exports.timestampToTimetag(date.getTime() / 1000); | |
131 }; | |
132 | |
133 exports.timestampToTimetag = function(secs) { | |
134 var fracSeconds, wholeSecs; | |
135 wholeSecs = Math.floor(secs); | |
136 fracSeconds = secs - wholeSecs; | |
137 return makeTimetag(wholeSecs, fracSeconds); | |
138 }; | |
139 | |
140 exports.timetagToTimestamp = function(timetag) { | |
141 var seconds; | |
142 seconds = timetag[0] + exports.ntpToFractionalSeconds(timetag[1]); | |
143 return seconds - UNIX_EPOCH; | |
144 }; | |
145 | |
146 makeTimetag = function(unixseconds, fracSeconds) { | |
147 var ntpFracs, ntpSecs; | |
148 ntpSecs = unixseconds + UNIX_EPOCH; | |
149 ntpFracs = Math.round(TWO_POW_32 * fracSeconds); | |
150 return [ntpSecs, ntpFracs]; | |
151 }; | |
152 | |
153 exports.timetagToDate = function(timetag) { | |
154 var date, dd, fracs, fractional, seconds; | |
155 seconds = timetag[0], fractional = timetag[1]; | |
156 seconds = seconds - UNIX_EPOCH; | |
157 fracs = exports.ntpToFractionalSeconds(fractional); | |
158 date = new Date(); | |
159 date.setTime((seconds * 1000) + (fracs * 1000)); | |
160 dd = new Date(); | |
161 dd.setUTCFullYear(date.getUTCFullYear()); | |
162 dd.setUTCMonth(date.getUTCMonth()); | |
163 dd.setUTCDate(date.getUTCDate()); | |
164 dd.setUTCHours(date.getUTCHours()); | |
165 dd.setUTCMinutes(date.getUTCMinutes()); | |
166 dd.setUTCSeconds(date.getUTCSeconds()); | |
167 dd.setUTCMilliseconds(fracs * 1000); | |
168 return dd; | |
169 }; | |
170 | |
171 exports.deltaTimetag = function(seconds, now) { | |
172 var n; | |
173 n = (now != null ? now : new Date()) / 1000; | |
174 return exports.timestampToTimetag(n + seconds); | |
175 }; | |
176 | |
177 exports.ntpToFractionalSeconds = function(fracSeconds) { | |
178 return parseFloat(fracSeconds) / TWO_POW_32; | |
179 }; | |
180 | |
181 exports.toTimetagBuffer = function(timetag) { | |
182 var high, low, type; | |
183 if (typeof timetag === "number") { | |
184 timetag = exports.timestampToTimetag(timetag); | |
185 } else if (typeof timetag === "object" && ("getTime" in timetag)) { | |
186 timetag = exports.dateToTimetag(timetag); | |
187 } else if (timetag.length !== 2) { | |
188 throw new Error("Invalid timetag" + timetag); | |
189 } | |
190 type = "Int32"; | |
191 high = binpack["pack" + type](timetag[0], "big"); | |
192 low = binpack["pack" + type](timetag[1], "big"); | |
193 return exports.concat([high, low]); | |
194 }; | |
195 | |
196 exports.toIntegerBuffer = function(number, type) { | |
197 if (type == null) { | |
198 type = "Int32"; | |
199 } | |
200 if (typeof number !== "number") { | |
201 throw new Error("cannot pack a non-number into an integer buffer"); | |
202 } | |
203 return binpack["pack" + type](number, "big"); | |
204 }; | |
205 | |
206 oscTypeCodes = { | |
207 s: { | |
208 representation: "string", | |
209 split: function(buffer, strict) { | |
210 var split; | |
211 split = exports.splitOscString(buffer, strict); | |
212 return { | |
213 value: split.string, | |
214 rest: split.rest | |
215 }; | |
216 }, | |
217 toArg: function(value, strict) { | |
218 if (typeof value !== "string") { | |
219 throw new Error("expected string"); | |
220 } | |
221 return exports.toOscString(value, strict); | |
222 } | |
223 }, | |
224 i: { | |
225 representation: "integer", | |
226 split: function(buffer, strict) { | |
227 var split; | |
228 split = exports.splitInteger(buffer); | |
229 return { | |
230 value: split.integer, | |
231 rest: split.rest | |
232 }; | |
233 }, | |
234 toArg: function(value, strict) { | |
235 if (typeof value !== "number") { | |
236 throw new Error("expected number"); | |
237 } | |
238 return exports.toIntegerBuffer(value); | |
239 } | |
240 }, | |
241 t: { | |
242 representation: "timetag", | |
243 split: function(buffer, strict) { | |
244 var split; | |
245 split = exports.splitTimetag(buffer); | |
246 return { | |
247 value: split.timetag, | |
248 rest: split.rest | |
249 }; | |
250 }, | |
251 toArg: function(value, strict) { | |
252 return exports.toTimetagBuffer(value); | |
253 } | |
254 }, | |
255 f: { | |
256 representation: "float", | |
257 split: function(buffer, strict) { | |
258 return { | |
259 value: binpack.unpackFloat32(buffer.slice(0, 4), "big"), | |
260 rest: buffer.slice(4, buffer.length) | |
261 }; | |
262 }, | |
263 toArg: function(value, strict) { | |
264 if (typeof value !== "number") { | |
265 throw new Error("expected number"); | |
266 } | |
267 return binpack.packFloat32(value, "big"); | |
268 } | |
269 }, | |
270 d: { | |
271 representation: "double", | |
272 split: function(buffer, strict) { | |
273 return { | |
274 value: binpack.unpackFloat64(buffer.slice(0, 8), "big"), | |
275 rest: buffer.slice(8, buffer.length) | |
276 }; | |
277 }, | |
278 toArg: function(value, strict) { | |
279 if (typeof value !== "number") { | |
280 throw new Error("expected number"); | |
281 } | |
282 return binpack.packFloat64(value, "big"); | |
283 } | |
284 }, | |
285 b: { | |
286 representation: "blob", | |
287 split: function(buffer, strict) { | |
288 var length, ref; | |
289 ref = exports.splitInteger(buffer), length = ref.integer, buffer = ref.rest; | |
290 return { | |
291 value: buffer.slice(0, length), | |
292 rest: buffer.slice(length, buffer.length) | |
293 }; | |
294 }, | |
295 toArg: function(value, strict) { | |
296 var size; | |
297 if (!Buffer.isBuffer(value)) { | |
298 throw new Error("expected node.js Buffer"); | |
299 } | |
300 size = exports.toIntegerBuffer(value.length); | |
301 return exports.concat([size, value]); | |
302 } | |
303 }, | |
304 T: { | |
305 representation: "true", | |
306 split: function(buffer, strict) { | |
307 return { | |
308 rest: buffer, | |
309 value: true | |
310 }; | |
311 }, | |
312 toArg: function(value, strict) { | |
313 if (!value && strict) { | |
314 throw new Error("true must be true"); | |
315 } | |
316 return new Buffer(0); | |
317 } | |
318 }, | |
319 F: { | |
320 representation: "false", | |
321 split: function(buffer, strict) { | |
322 return { | |
323 rest: buffer, | |
324 value: false | |
325 }; | |
326 }, | |
327 toArg: function(value, strict) { | |
328 if (value && strict) { | |
329 throw new Error("false must be false"); | |
330 } | |
331 return new Buffer(0); | |
332 } | |
333 }, | |
334 N: { | |
335 representation: "null", | |
336 split: function(buffer, strict) { | |
337 return { | |
338 rest: buffer, | |
339 value: null | |
340 }; | |
341 }, | |
342 toArg: function(value, strict) { | |
343 if (value && strict) { | |
344 throw new Error("null must be false"); | |
345 } | |
346 return new Buffer(0); | |
347 } | |
348 }, | |
349 I: { | |
350 representation: "bang", | |
351 split: function(buffer, strict) { | |
352 return { | |
353 rest: buffer, | |
354 value: "bang" | |
355 }; | |
356 }, | |
357 toArg: function(value, strict) { | |
358 return new Buffer(0); | |
359 } | |
360 } | |
361 }; | |
362 | |
363 exports.oscTypeCodeToTypeString = function(code) { | |
364 var ref; | |
365 return (ref = oscTypeCodes[code]) != null ? ref.representation : void 0; | |
366 }; | |
367 | |
368 exports.typeStringToOscTypeCode = function(rep) { | |
369 var code, str; | |
370 for (code in oscTypeCodes) { | |
371 if (!hasProp.call(oscTypeCodes, code)) continue; | |
372 str = oscTypeCodes[code].representation; | |
373 if (str === rep) { | |
374 return code; | |
375 } | |
376 } | |
377 return null; | |
378 }; | |
379 | |
380 exports.argToTypeCode = function(arg, strict) { | |
381 var code, value; | |
382 if (((arg != null ? arg.type : void 0) != null) && (typeof arg.type === 'string') && ((code = exports.typeStringToOscTypeCode(arg.type)) != null)) { | |
383 return code; | |
384 } | |
385 value = (arg != null ? arg.value : void 0) != null ? arg.value : arg; | |
386 if (strict && (value == null)) { | |
387 throw new Error('Argument has no value'); | |
388 } | |
389 if (typeof value === 'string') { | |
390 return 's'; | |
391 } | |
392 if (typeof value === 'number') { | |
393 return 'f'; | |
394 } | |
395 if (Buffer.isBuffer(value)) { | |
396 return 'b'; | |
397 } | |
398 if (typeof value === 'boolean') { | |
399 if (value) { | |
400 return 'T'; | |
401 } else { | |
402 return 'F'; | |
403 } | |
404 } | |
405 if (value === null) { | |
406 return 'N'; | |
407 } | |
408 throw new Error("I don't know what type this is supposed to be."); | |
409 }; | |
410 | |
411 exports.splitOscArgument = function(buffer, type, strict) { | |
412 var osctype; | |
413 osctype = exports.typeStringToOscTypeCode(type); | |
414 if (osctype != null) { | |
415 return oscTypeCodes[osctype].split(buffer, strict); | |
416 } else { | |
417 throw new Error("I don't understand how I'm supposed to unpack " + type); | |
418 } | |
419 }; | |
420 | |
421 exports.toOscArgument = function(value, type, strict) { | |
422 var osctype; | |
423 osctype = exports.typeStringToOscTypeCode(type); | |
424 if (osctype != null) { | |
425 return oscTypeCodes[osctype].toArg(value, strict); | |
426 } else { | |
427 throw new Error("I don't know how to pack " + type); | |
428 } | |
429 }; | |
430 | |
431 exports.fromOscMessage = function(buffer, strict) { | |
432 var address, arg, args, arrayStack, built, j, len, ref, ref1, type, typeString, types; | |
433 ref = exports.splitOscString(buffer, strict), address = ref.string, buffer = ref.rest; | |
434 if (strict && address[0] !== '/') { | |
435 throw StrictError('addresses must start with /'); | |
436 } | |
437 if (!buffer.length) { | |
438 return { | |
439 address: address, | |
440 args: [] | |
441 }; | |
442 } | |
443 ref1 = exports.splitOscString(buffer, strict), types = ref1.string, buffer = ref1.rest; | |
444 if (types[0] !== ',') { | |
445 if (strict) { | |
446 throw StrictError('Argument lists must begin with ,'); | |
447 } | |
448 return { | |
449 address: address, | |
450 args: [] | |
451 }; | |
452 } | |
453 types = types.slice(1, +types.length + 1 || 9e9); | |
454 args = []; | |
455 arrayStack = [args]; | |
456 for (j = 0, len = types.length; j < len; j++) { | |
457 type = types[j]; | |
458 if (type === '[') { | |
459 arrayStack.push([]); | |
460 continue; | |
461 } | |
462 if (type === ']') { | |
463 if (arrayStack.length <= 1) { | |
464 if (strict) { | |
465 throw new StrictError("Mismatched ']' character."); | |
466 } | |
467 } else { | |
468 built = arrayStack.pop(); | |
469 arrayStack[arrayStack.length - 1].push({ | |
470 type: 'array', | |
471 value: built | |
472 }); | |
473 } | |
474 continue; | |
475 } | |
476 typeString = exports.oscTypeCodeToTypeString(type); | |
477 if (typeString == null) { | |
478 throw new Error("I don't understand the argument code " + type); | |
479 } | |
480 arg = exports.splitOscArgument(buffer, typeString, strict); | |
481 if (arg != null) { | |
482 buffer = arg.rest; | |
483 } | |
484 arrayStack[arrayStack.length - 1].push({ | |
485 type: typeString, | |
486 value: arg != null ? arg.value : void 0 | |
487 }); | |
488 } | |
489 if (arrayStack.length !== 1 && strict) { | |
490 throw new StrictError("Mismatched '[' character"); | |
491 } | |
492 return { | |
493 address: address, | |
494 args: args, | |
495 oscType: "message" | |
496 }; | |
497 }; | |
498 | |
499 exports.fromOscBundle = function(buffer, strict) { | |
500 var bundleTag, convertedElems, ref, ref1, timetag; | |
501 ref = exports.splitOscString(buffer, strict), bundleTag = ref.string, buffer = ref.rest; | |
502 if (bundleTag !== "\#bundle") { | |
503 throw new Error("osc-bundles must begin with \#bundle"); | |
504 } | |
505 ref1 = exports.splitTimetag(buffer), timetag = ref1.timetag, buffer = ref1.rest; | |
506 convertedElems = mapBundleList(buffer, function(buffer) { | |
507 return exports.fromOscPacket(buffer, strict); | |
508 }); | |
509 return { | |
510 timetag: timetag, | |
511 elements: convertedElems, | |
512 oscType: "bundle" | |
513 }; | |
514 }; | |
515 | |
516 exports.fromOscPacket = function(buffer, strict) { | |
517 if (isOscBundleBuffer(buffer, strict)) { | |
518 return exports.fromOscBundle(buffer, strict); | |
519 } else { | |
520 return exports.fromOscMessage(buffer, strict); | |
521 } | |
522 }; | |
523 | |
524 getArrayArg = function(arg) { | |
525 if (IsArray(arg)) { | |
526 return arg; | |
527 } else if (((arg != null ? arg.type : void 0) === "array") && (IsArray(arg != null ? arg.value : void 0))) { | |
528 return arg.value; | |
529 } else if ((arg != null) && (arg.type == null) && (IsArray(arg.value))) { | |
530 return arg.value; | |
531 } else { | |
532 return null; | |
533 } | |
534 }; | |
535 | |
536 toOscTypeAndArgs = function(argList, strict) { | |
537 var arg, buff, j, len, oscargs, osctype, ref, thisArgs, thisType, typeCode, value; | |
538 osctype = ""; | |
539 oscargs = []; | |
540 for (j = 0, len = argList.length; j < len; j++) { | |
541 arg = argList[j]; | |
542 if ((getArrayArg(arg)) != null) { | |
543 ref = toOscTypeAndArgs(getArrayArg(arg), strict), thisType = ref[0], thisArgs = ref[1]; | |
544 osctype += "[" + thisType + "]"; | |
545 oscargs = oscargs.concat(thisArgs); | |
546 continue; | |
547 } | |
548 typeCode = exports.argToTypeCode(arg, strict); | |
549 if (typeCode != null) { | |
550 value = arg != null ? arg.value : void 0; | |
551 if (value === void 0) { | |
552 value = arg; | |
553 } | |
554 buff = exports.toOscArgument(value, exports.oscTypeCodeToTypeString(typeCode), strict); | |
555 if (buff != null) { | |
556 oscargs.push(buff); | |
557 osctype += typeCode; | |
558 } | |
559 } | |
560 } | |
561 return [osctype, oscargs]; | |
562 }; | |
563 | |
564 exports.toOscMessage = function(message, strict) { | |
565 var address, allArgs, args, old_arg, oscaddr, oscargs, osctype, ref; | |
566 address = (message != null ? message.address : void 0) != null ? message.address : message; | |
567 if (typeof address !== "string") { | |
568 throw new Error("message must contain an address"); | |
569 } | |
570 args = message != null ? message.args : void 0; | |
571 if (args === void 0) { | |
572 args = []; | |
573 } | |
574 if (!IsArray(args)) { | |
575 old_arg = args; | |
576 args = []; | |
577 args[0] = old_arg; | |
578 } | |
579 oscaddr = exports.toOscString(address, strict); | |
580 ref = toOscTypeAndArgs(args, strict), osctype = ref[0], oscargs = ref[1]; | |
581 osctype = "," + osctype; | |
582 allArgs = exports.concat(oscargs); | |
583 osctype = exports.toOscString(osctype); | |
584 return exports.concat([oscaddr, osctype, allArgs]); | |
585 }; | |
586 | |
587 exports.toOscBundle = function(bundle, strict) { | |
588 var allElems, buff, e, elem, elements, elemstr, error, j, len, oscBundleTag, oscElems, oscTimeTag, ref, ref1, size, timetag; | |
589 if (strict && ((bundle != null ? bundle.timetag : void 0) == null)) { | |
590 throw StrictError("bundles must have timetags."); | |
591 } | |
592 timetag = (ref = bundle != null ? bundle.timetag : void 0) != null ? ref : new Date(); | |
593 elements = (ref1 = bundle != null ? bundle.elements : void 0) != null ? ref1 : []; | |
594 if (!IsArray(elements)) { | |
595 elemstr = elements; | |
596 elements = []; | |
597 elements.push(elemstr); | |
598 } | |
599 oscBundleTag = exports.toOscString("\#bundle"); | |
600 oscTimeTag = exports.toTimetagBuffer(timetag); | |
601 oscElems = []; | |
602 for (j = 0, len = elements.length; j < len; j++) { | |
603 elem = elements[j]; | |
604 try { | |
605 buff = exports.toOscPacket(elem, strict); | |
606 size = exports.toIntegerBuffer(buff.length); | |
607 oscElems.push(exports.concat([size, buff])); | |
608 } catch (error) { | |
609 e = error; | |
610 null; | |
611 } | |
612 } | |
613 allElems = exports.concat(oscElems); | |
614 return exports.concat([oscBundleTag, oscTimeTag, allElems]); | |
615 }; | |
616 | |
617 exports.toOscPacket = function(bundleOrMessage, strict) { | |
618 if ((bundleOrMessage != null ? bundleOrMessage.oscType : void 0) != null) { | |
619 if (bundleOrMessage.oscType === "bundle") { | |
620 return exports.toOscBundle(bundleOrMessage, strict); | |
621 } | |
622 return exports.toOscMessage(bundleOrMessage, strict); | |
623 } | |
624 if (((bundleOrMessage != null ? bundleOrMessage.timetag : void 0) != null) || ((bundleOrMessage != null ? bundleOrMessage.elements : void 0) != null)) { | |
625 return exports.toOscBundle(bundleOrMessage, strict); | |
626 } | |
627 return exports.toOscMessage(bundleOrMessage, strict); | |
628 }; | |
629 | |
630 exports.applyMessageTranformerToBundle = function(transform) { | |
631 return function(buffer) { | |
632 var bundleTagBuffer, copyIndex, elem, elems, j, k, len, len1, lengthBuff, outBuffer, ref, string, timetagBuffer, totalLength; | |
633 ref = exports.splitOscString(buffer), string = ref.string, buffer = ref.rest; | |
634 if (string !== "\#bundle") { | |
635 throw new Error("osc-bundles must begin with \#bundle"); | |
636 } | |
637 bundleTagBuffer = exports.toOscString(string); | |
638 timetagBuffer = buffer.slice(0, 8); | |
639 buffer = buffer.slice(8, buffer.length); | |
640 elems = mapBundleList(buffer, function(buffer) { | |
641 return exports.applyTransform(buffer, transform, exports.applyMessageTranformerToBundle(transform)); | |
642 }); | |
643 totalLength = bundleTagBuffer.length + timetagBuffer.length; | |
644 for (j = 0, len = elems.length; j < len; j++) { | |
645 elem = elems[j]; | |
646 totalLength += 4 + elem.length; | |
647 } | |
648 outBuffer = new Buffer(totalLength); | |
649 bundleTagBuffer.copy(outBuffer, 0); | |
650 timetagBuffer.copy(outBuffer, bundleTagBuffer.length); | |
651 copyIndex = bundleTagBuffer.length + timetagBuffer.length; | |
652 for (k = 0, len1 = elems.length; k < len1; k++) { | |
653 elem = elems[k]; | |
654 lengthBuff = exports.toIntegerBuffer(elem.length); | |
655 lengthBuff.copy(outBuffer, copyIndex); | |
656 copyIndex += 4; | |
657 elem.copy(outBuffer, copyIndex); | |
658 copyIndex += elem.length; | |
659 } | |
660 return outBuffer; | |
661 }; | |
662 }; | |
663 | |
664 exports.applyTransform = function(buffer, mTransform, bundleTransform) { | |
665 if (bundleTransform == null) { | |
666 bundleTransform = exports.applyMessageTranformerToBundle(mTransform); | |
667 } | |
668 if (isOscBundleBuffer(buffer)) { | |
669 return bundleTransform(buffer); | |
670 } else { | |
671 return mTransform(buffer); | |
672 } | |
673 }; | |
674 | |
675 exports.addressTransform = function(transform) { | |
676 return function(buffer) { | |
677 var ref, rest, string; | |
678 ref = exports.splitOscString(buffer), string = ref.string, rest = ref.rest; | |
679 string = transform(string); | |
680 return exports.concat([exports.toOscString(string), rest]); | |
681 }; | |
682 }; | |
683 | |
684 exports.messageTransform = function(transform) { | |
685 return function(buffer) { | |
686 var message; | |
687 message = exports.fromOscMessage(buffer); | |
688 return exports.toOscMessage(transform(message)); | |
689 }; | |
690 }; | |
691 | |
692 IsArray = Array.isArray; | |
693 | |
694 StrictError = function(str) { | |
695 return new Error("Strict Error: " + str); | |
696 }; | |
697 | |
698 padding = function(str) { | |
699 var bufflength; | |
700 bufflength = Buffer.byteLength(str); | |
701 return 4 - (bufflength % 4); | |
702 }; | |
703 | |
704 isOscBundleBuffer = function(buffer, strict) { | |
705 var string; | |
706 string = exports.splitOscString(buffer, strict).string; | |
707 return string === "\#bundle"; | |
708 }; | |
709 | |
710 mapBundleList = function(buffer, func) { | |
711 var e, elem, elems, j, len, nonNullElems, size, thisElemBuffer; | |
712 elems = (function() { | |
713 var error, ref, results; | |
714 results = []; | |
715 while (buffer.length) { | |
716 ref = exports.splitInteger(buffer), size = ref.integer, buffer = ref.rest; | |
717 if (size > buffer.length) { | |
718 throw new Error("Invalid bundle list: size of element is bigger than buffer"); | |
719 } | |
720 thisElemBuffer = buffer.slice(0, size); | |
721 buffer = buffer.slice(size, buffer.length); | |
722 try { | |
723 results.push(func(thisElemBuffer)); | |
724 } catch (error) { | |
725 e = error; | |
726 results.push(null); | |
727 } | |
728 } | |
729 return results; | |
730 })(); | |
731 nonNullElems = []; | |
732 for (j = 0, len = elems.length; j < len; j++) { | |
733 elem = elems[j]; | |
734 if (elem != null) { | |
735 nonNullElems.push(elem); | |
736 } | |
737 } | |
738 return nonNullElems; | |
739 }; | |
740 | |
741 }).call(this); |