/
osc-utilities.coffee
632 lines (527 loc) · 19 KB
/
osc-utilities.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# # osc-utilities.coffee
# ## Intro
# This file contains some lower-level utilities for OSC handling.
# My guess is client code won't need this. If you do need this, you must
# require coffee first, then write:
#
# require("coffee-script");
# osc-utils = require("osc/lib/osc-utilities");
#
# See the comments in osc.coffee for more information about the structure of
# the objects we're dealing with here.
#
# ## Dependencies
# require the minimal binary packing utilities
binpack = require "binpack"
# ## Exported Functions
# Utility for working with buffers. takes an array of buffers,
# output one buffer with all of the array concatenated
#
# This is really only exported for TDD, but maybe it'll be useful
# to someone else too.
exports.concat = (buffers) ->
if not IsArray buffers
throw new Error "concat must take an array of buffers"
for buffer in buffers
if not Buffer.isBuffer(buffer)
throw new Error "concat must take an array of buffers"
sumLength = 0
sumLength += buffer.length for buffer in buffers
destBuffer = new Buffer(sumLength)
copyTo = 0
for buffer in buffers
buffer.copy destBuffer, copyTo
copyTo += buffer.length
destBuffer
#
# Convert a javascript string into a node.js Buffer containing an OSC-String.
#
# str must not contain any \u0000 characters.
#
# `strict` is an optional boolean paramter that fails if the string is invalid
# (i.e. contains a \u0000 character)
exports.toOscString = (str, strict) ->
if not (typeof str == "string")
throw new Error "can't pack a non-string into an osc-string"
# strip off any \u0000 characters.
nullIndex = str.indexOf("\u0000")
# if we're being strict, we can't allow strings with null characters
if (nullIndex != -1 and strict)
throw StrictError "Can't pack an osc-string that contains NULL characters"
str = str[0...nullIndex] if nullIndex != -1
# osc-strings must have length divisible by 4 and end with at least one zero.
for i in [0...(padding str)]
str += "\u0000"
# create a new buffer from the string.
new Buffer(str)
#
# Try to split a buffer into a leading osc-string and the rest of the buffer,
# with the following layout:
# { string : "blah" rest : <Buffer>}.
#
# `strict`, as above, is an optional boolean parameter that defaults to false -
# if it is true, then an invalid buffer will always return null.
#
exports.splitOscString = (buffer, strict) ->
if not Buffer.isBuffer buffer
throw StrictError "Can't split something that isn't a buffer"
# extract the string
rawStr = buffer.toString "utf8"
nullIndex = rawStr.indexOf "\u0000"
# the rest of the code doesn't apply if there's no null character.
if nullIndex == -1
throw new Error "All osc-strings must contain a null character" if strict
return {string:rawStr, rest:(new Buffer 0)}
# extract the string.
str = rawStr[0...nullIndex]
# find the length of the string's buffer
splitPoint = Buffer.byteLength(str) + padding(str)
# in strict mode, don't succeed if there's not enough padding.
if strict and splitPoint > buffer.length
throw StrictError "Not enough padding for osc-string"
# if we're in strict mode, check that all the padding is null
if strict
for i in [Buffer.byteLength(str)...splitPoint]
if buffer[i] != 0
throw StrictError "Not enough or incorrect padding for osc-string"
# return a split
rest = buffer[splitPoint...(buffer.length)]
{string: str, rest: rest}
# This has similar semantics to splitOscString but works with integers instead.
# bytes is the number of bytes in the integer, defaults to 4.
exports.splitInteger = (buffer, type) ->
type = "Int32" if not type?
bytes = (binpack["pack" + type] 0).length
if buffer.length < bytes
throw new Error "buffer is not big enough for integer type"
num = 0
# integers are stored in big endian format.
value = binpack["unpack" + type] buffer[0...bytes], "big"
rest = buffer[bytes...(buffer.length)]
return {integer : value, rest : rest}
exports.toIntegerBuffer = (number, type) ->
type = "Int32" if not type?
if typeof number isnt "number"
throw new Error "cannot pack a non-number into an integer buffer"
binpack["pack" + type] number, "big"
# This mapping contains three fields for each type:
# - representation : the javascript string representation of this type.
# - split : a function to split a buffer into a decoded value and
# the rest of the buffer.
# - toArg : a function that takes the representation of the type and
# outputs a buffer.
oscTypeCodes =
s : {
representation : "string"
split : (buffer, strict) ->
# just pass it through to splitOscString
split = exports.splitOscString buffer, strict
{value : split.string, rest : split.rest}
toArg : (value, strict) ->
throw new Error "expected string" if typeof value isnt "string"
exports.toOscString value, strict
}
i : {
representation : "integer"
split : (buffer, strict) ->
split = exports.splitInteger buffer
{value : split.integer, rest : split.rest}
toArg : (value, strict) ->
throw new Error "expected number" if typeof value isnt "number"
exports.toIntegerBuffer value
}
t : {
representation : "timetag"
split : (buffer, strict) ->
split = exports.splitInteger buffer, "UInt64"
{value : split.integer, rest : split.rest}
toArg : (value, strict) ->
throw new Error "expected number" if typeof value isnt "number"
exports.toIntegerBuffer value, "UInt64"
}
f : {
representation : "float"
split : (buffer, strict) ->
value : (binpack.unpackFloat32 buffer[0...4], "big")
rest : buffer[4...(buffer.length)]
toArg : (value, strict) ->
throw new Error "expected number" if typeof value isnt "number"
binpack.packFloat32 value, "big"
}
d : {
representation : "double"
split : (buffer, strict) ->
value : (binpack.unpackFloat64 buffer[0...8], "big")
rest : buffer[8...(buffer.length)]
toArg : (value, strict) ->
throw new Error "expected number" if typeof value isnt "number"
binpack.packFloat64 value, "big"
}
b : {
representation : "blob"
split : (buffer, strict) ->
# not much to do here, first grab an 4 byte int from the buffer
{integer : length, rest : buffer} = exports.splitInteger buffer
{value : buffer[0...length], rest : buffer[length...(buffer.length)]}
toArg : (value, strict) ->
throw new Error "expected node.js Buffer" if not Buffer.isBuffer value
size = exports.toIntegerBuffer value.length
exports.concat [size, value]
}
T : {
representation : "true"
split : (buffer, strict) ->
rest : buffer
value : true
toArg : (value, strict) ->
throw new Error "true must be true" if not value and strict
new Buffer 0
}
F : {
representation : "false"
split : (buffer, strict) ->
rest : buffer
value : false
toArg : (value, strict) ->
throw new Error "false must be false" if value and strict
new Buffer 0
}
N : {
representation : "null"
split : (buffer, strict) ->
rest : buffer
value : null
toArg : (value, strict) ->
throw new Error "null must be false" if value and strict
new Buffer 0
}
I : {
representation : "bang"
split : (buffer, strict) ->
rest : buffer
value : "bang"
toArg : (value, strict) ->
new Buffer 0
}
# simple function that converts a type code into it's javascript
# string representation.
exports.oscTypeCodeToTypeString = (code) ->
oscTypeCodes[code]?.representation
# simple function that converts a javascript string representation
# into its OSC type code.
exports.typeStringToOscTypeCode = (rep) ->
for own code, {representation : str} of oscTypeCodes
return code if str is rep
return null
exports.argToTypeCode = (arg, strict) ->
# if there's an explicit type annotation, back-translate that.
if arg?.type? and
(typeof arg.type is 'string') and
(code = exports.typeStringToOscTypeCode arg.type)?
return code
value = if arg?.value? then arg.value else arg
# now, we try to guess the type.
throw new Error 'Argument has no value' if strict and not value?
# if it's a string, use 's'
if typeof value is 'string'
return 's'
# if it's a number, use 'f' by default.
if typeof value is 'number'
return 'f'
# if it's a buffer, use 'b'
if Buffer.isBuffer(value)
return 'b'
#### These are 1.1 specific types.
# if it's a boolean, use 'T' or 'F'
if typeof value is 'boolean'
if value then return 'T' else return 'F'
# if it's null, use 'N'
if value is null
return 'N'
throw new Error "I don't know what type this is supposed to be."
# Splits out an argument from buffer. Same thing as splitOscString but
# works for all argument types.
exports.splitOscArgument = (buffer, type, strict) ->
osctype = exports.typeStringToOscTypeCode type
if osctype?
oscTypeCodes[osctype].split buffer, strict
else
throw new Error "I don't understand how I'm supposed to unpack #{type}"
# Create a buffer with the given javascript type
exports.toOscArgument = (value, type, strict) ->
osctype = exports.typeStringToOscTypeCode type
if osctype?
oscTypeCodes[osctype].toArg value, strict
else
throw new Error "I don't know how to pack #{type}"
#
# translates an OSC message into a javascript representation.
#
exports.fromOscMessage = (buffer, strict) ->
# break off the address
{ string : address, rest : buffer} = exports.splitOscString buffer, strict
# technically, addresses have to start with '/'.
if strict and address[0] isnt '/'
throw StrictError 'addresses must start with /'
# if there's no type string, this is technically illegal, but
# the specification says we should accept this until all
# implementations that send message without a type string are fixed.
# this will never happen, so we should accept this, even in
# strict mode.
return {address : address, args : []} if not buffer.length
# if there's more data but no type string, we can't parse the arguments.
{string : types, rest : buffer} = exports.splitOscString buffer, strict
# if the first letter isn't a ',' this isn't a valid type so we can't
# parse the arguments.
if types[0] isnt ','
throw StrictError 'Argument lists must begin with ,' if strict
return {address : address, args : []}
# we don't need the comma anymore
types = types[1..(types.length)]
args = []
# grab each argument.
for type in types
# by the standard, we have to ignore the whole message
# if we don't understand an argument
typeString = exports.oscTypeCodeToTypeString type
if not typeString?
throw new Error "I don't understand the argument code #{type}"
arg = exports.splitOscArgument buffer, typeString, strict
# consume the argument from the buffer
buffer = arg.rest if arg?
# add it to the list.
args.push(
type : typeString
value : arg?.value
)
{address : address, args : args, oscType : "message"}
#
# Try to parse an OSC bundle into a javascript object.
#
exports.fromOscBundle = (buffer, strict) ->
# break off the bundletag
{ string : bundleTag, rest : buffer} = exports.splitOscString buffer, strict
# bundles have to start with "#bundle".
if bundleTag isnt "\#bundle"
throw new Error "osc-bundles must begin with \#bundle"
# grab the 8 - bit timetag
{ integer : timetag, rest : buffer} = exports.splitInteger buffer, "UInt64"
# convert each element.
convertedElems = mapBundleList buffer, (buffer) ->
exports.fromOscPacket buffer, strict
return {timetag : timetag, elements : convertedElems, oscType : "bundle"}
#
# convert the buffer into a bundle or a message, depending on the first string
#
exports.fromOscPacket = (buffer, strict) ->
if isOscBundleBuffer buffer, strict
exports.fromOscBundle buffer, strict
else
exports.fromOscMessage buffer, strict
#
# convert a javascript format message into an osc buffer
#
exports.toOscMessage = (message, strict) ->
# the message must have addresses and arguments.
address = if message?.address? then message.address else message
if typeof address isnt "string"
throw new Error "message must contain an address"
args = message?.args
if args is undefined
args = []
# pack single args
if not IsArray args
old_arg = args
args = []
args[0] = old_arg
oscaddr = exports.toOscString address, strict
osctype = ","
oscargs = []
# fill in args
for arg in args
typeCode = exports.argToTypeCode arg, strict
if typeCode?
value = arg?.value
if value is undefined
value = arg
buff = exports.toOscArgument value,
exports.oscTypeCodeToTypeString(typeCode), strict
if buff?
oscargs.push buff
osctype += typeCode
# convert the type tag into an oscString.
osctype = exports.toOscString osctype
# bundle everything together.
allArgs = exports.concat oscargs
exports.concat [oscaddr, osctype, allArgs]
#
# convert a javascript format bundle into an osc buffer
#
exports.toOscBundle = (bundle, strict) ->
# the bundle must have timetag and elements.
if strict and not bundle?.timetag?
throw StrictError "bundles must have timetags."
timetag = bundle?.timetag ? 0
elements = bundle?.elements ? []
if not IsArray elements
elemstr = elements
elements = []
elements.push elemstr
oscBundleTag = exports.toOscString "\#bundle"
oscTimeTag = exports.toIntegerBuffer timetag, "UInt64"
oscElems = []
for elem in elements
try
# try to convert this sub-element into a buffer
buff = exports.toOscPacket elem, strict
# okay, pack in the size.
size = exports.toIntegerBuffer buff.length
oscElems.push exports.concat [size, buff]
catch e
null
allElems = exports.concat oscElems
exports.concat [oscBundleTag, oscTimeTag, allElems]
# convert a javascript format bundle or message into a buffer
exports.toOscPacket = (bundleOrMessage, strict) ->
# first, determine whether or not this is a bundle.
if bundleOrMessage?.oscType?
if bundleOrMessage.oscType is "bundle"
return exports.toOscBundle bundleOrMessage, strict
return exports.toOscMessage bundleOrMessage, strict
# bundles have "timetags" and "elements"
if bundleOrMessage?.timetag? or bundleOrMessage?.elements?
return exports.toOscBundle bundleOrMessage, strict
exports.toOscMessage bundleOrMessage, strict
#
# Helper function for transforming all messages in a bundle with a given message
# transform.
#
exports.applyMessageTranformerToBundle = (transform) -> (buffer) ->
# parse out the bundle-id and the tag, we don't want to change these
{ string, rest : buffer} = exports.splitOscString buffer
# bundles have to start with "#bundle".
if string isnt "\#bundle"
throw new Error "osc-bundles must begin with \#bundle"
bundleTagBuffer = exports.toOscString string
# we know that the timetag is 8 bytes, we don't want to mess with it,
# so grab it as a buffer. There is some subtle loss of precision with
# the round trip from int64 to float64.
timetagBuffer = buffer[0...8]
buffer = buffer[8...buffer.length]
# convert each element.
elems = mapBundleList buffer, (buffer) ->
exports.applyTransform(
buffer,
transform,
exports.applyMessageTranformerToBundle transform
)
totalLength = bundleTagBuffer.length + timetagBuffer.length
totalLength += 4 + elem.length for elem in elems
# okay, now we have to reconcatenate everything.
outBuffer = new Buffer totalLength
bundleTagBuffer.copy outBuffer, 0
timetagBuffer.copy outBuffer, bundleTagBuffer.length
copyIndex = bundleTagBuffer.length + timetagBuffer.length
for elem in elems
lengthBuff = exports.toIntegerBuffer elem.length
lengthBuff.copy outBuffer, copyIndex
copyIndex += 4
elem.copy outBuffer, copyIndex
copyIndex += elem.length
outBuffer
#
# Applies a transformation function (that is, a function from buffers
# to buffers) to each element of given osc-bundle or message.
#
# `buffer` is the buffer to transform, which must be a buffer of a full packet.
# `messageTransform` is function from message buffers to message buffers
# `bundleTransform` is an optional parameter for functions from bundle buffers
# to bundle buffers.
# if `bundleTransform` is not set, it defaults to just applying the
# `messageTransform` to each message in the bundle.
#
exports.applyTransform = (buffer, mTransform, bundleTransform) ->
if not bundleTransform?
bundleTransform = exports.applyMessageTranformerToBundle mTransform
if isOscBundleBuffer buffer
bundleTransform buffer
else
mTransform buffer
# Converts a javascript function from string to string to a function
# from message buffer to message buffer, applying the function to the
# parsed strings.
#
# We pre-curry this because we expect to use this with `applyMessageTransform`
# above
#
exports.addressTransform = (transform) -> (buffer) ->
# parse out the address
{string, rest} = exports.splitOscString buffer
# apply the function
string = transform string
# re-concatenate
exports.concat [
exports.toOscString string
rest
]
#
# Take a function that transform a javascript _OSC Message_ and
# convert it to a function that transforms osc-buffers.
#
exports.messageTransform = (transform) -> (buffer) ->
message = exports.fromOscMessage buffer
exports.toOscMessage transform message
## Private utilities
#
# is it an array?
#
IsArray = Array.isArray
#
# An error that only throws when we're in strict mode.
#
StrictError = (str) ->
new Error "Strict Error: " + str
# this private utility finds the amount of padding for a given string.
padding = (str) ->
bufflength = Buffer.byteLength(str)
4 - (bufflength % 4)
#
# Internal function to check if this is a message or bundle.
#
isOscBundleBuffer = (buffer, strict) ->
# both formats begin with strings, so we should just grab the front but not
# consume it.
{string} = exports.splitOscString buffer, strict
return string is "\#bundle"
#
# Does something for each element in an array of osc-message-or-bundles,
# each prefixed by a length (such as appears in osc-messages), then
# return the result as an array.
#
# This is not exported because it doesn't validate the format and it's
# not really a generally useful function.
#
# If a function throws on an element, we discard that element in the map
# but we don't give up completely.
#
mapBundleList = (buffer, func) ->
elems = while buffer.length
# the length of the element is stored in an integer
{integer : size, rest : buffer} = exports.splitInteger buffer
# if the size is bigger than the packet, something's messed up, so give up.
if size > buffer.length
throw new Error(
"Invalid bundle list: size of element is bigger than buffer")
thisElemBuffer = buffer[0...size]
# move the buffer to after the element we're just parsing.
buffer = buffer[size...buffer.length]
# record this element
try
func thisElemBuffer
catch e
null
# remove all null from elements
nonNullElems = []
for elem in elems
(nonNullElems.push elem) if elem?
nonNullElems