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