-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathomgosc.js
276 lines (242 loc) · 8.48 KB
/
omgosc.js
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
// OMG OSC, a Plask / NodeJS OSC implementation.
// (c) Dean McNamee <[email protected]>, 2011.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
var util = require('util');
var events = require('events');
var dgram = require('dgram');
function UdpSender(host, port, opts) {
opts = opts === undefined ? { } : opts;
var udp = dgram.createSocket('udp4');
if (opts.broadcast === true)
udp.setBroadcast(true);
// Some buffers to get the float bit pattern, for example.
var array_buffer = new ArrayBuffer(4);
var data_view = new DataView(array_buffer);
function appendString(octets, str) {
var len = str.length;
for (var i = 0; i < len; ++i) {
octets.push(str.charCodeAt(i) & 0x7f); // Should be 7-bit clean right?
}
// We want to add the null byte and pad to 4 byte boundary.
var num_nulls = 4 - (len & 3); // Will always be at least 1 for terminator.
for (var i = 0; i < num_nulls; ++i) {
octets.push(0);
}
}
function appendBlob(octets, val) {
var len = val.length;
appendInt(octets, len);
// grow byte array and carve out space for the Blob
var start = octets.length;
octets.length += len;
for (var i = 0; i < len; ++i) {
octets[start+i] = val[i];
}
// We want to pad to 4 byte boundary.
var num_nulls = (4 - (len & 3)) & 3;
for (var i = 0; i < num_nulls; ++i) {
octets.push(0);
}
}
function appendInt(octets, val) {
data_view.setInt32(0, val, false);
for (var i = 0; i < 4; ++i) {
octets.push(data_view.getUint8(i));
}
}
function appendFloat(octets, val) {
data_view.setFloat32(0, val, false);
for (var i = 0; i < 4; ++i) {
octets.push(data_view.getUint8(i));
}
}
function makeMessageOctets(path, typetag, params) {
var octets = [ ];
appendString(octets, path);
appendString(octets, ',' + typetag);
for (var i = 0, il = typetag.length; i < il; ++i) {
var tag = typetag[i];
switch (tag) {
case 'i':
appendInt(octets, params[i]);
break;
case 'f':
appendFloat(octets, params[i]);
break;
case 's':
appendString(octets, params[i]);
break;
case 'b':
appendBlob(octets, params[i]);
break;
// Types with implicit parameters, just ignore the passed parameter.
case 'T': case 'F': case 'N': case 'I':
break;
default:
throw 'Unknown osc type: ' + tag;
break;
}
}
return octets;
}
this.send = function(path, typetag, params) {
var octets = makeMessageOctets(path, typetag, params);
udp.send(new Buffer(octets), 0, octets.length, port, host);
};
this.sendBundled = function(path, typetag, params) {
var octets = [ ];
var message_octets = makeMessageOctets(path, typetag, params);
appendString(octets, "#bundle");
appendInt(octets, 0); appendInt(octets, 1); // timetag now.
appendInt(octets, message_octets.length);
octets = octets.concat(message_octets);
udp.send(new Buffer(octets), 0, octets.length, port, host);
};
// Close the underlying socket for the sender. Since this is UDP this
// doesn't really have an effect beyond just closing the file descriptor.
// Returns true if the socket was closed or false if it was already closed.
this.close = function() {
if (udp === null) return false; // Already closed.
udp.close();
udp = null; // Any further use (send, etc) should cause an exception.
return true;
};
}
var dgram = require('dgram');
// TODO(deanm): Support an opts with opts.host for binding.
function UdpReceiver(port) {
// Some buffers to get the float bit pattern, for example.
var array_buffer = new ArrayBuffer(4);
var data_view = new DataView(array_buffer);
function readString(buffer, start) {
var end = start;
var len = buffer.length;
// Seek to the end of the string (which will be terminated by 1-4 NULLs).
while (end < len && buffer[end] !== 0) end++;
// NOTE(deanm): At this point we could probably salvage the message and
// take the string (which was probably truncated due to UDP packet size),
// but it is probably the best decision to error out on malformed data.
if (end >= len)
throw "Encountered invalid OSC string, missing NULL termination.";
return buffer.toString('ascii', start, end);
}
function readBlob(buffer, start) {
var len = readInt(buffer, start);
start += 4;
return buffer.slice(start, start+len);
}
function readFloat(buffer, pos) {
data_view.setUint8(0, buffer[pos]);
data_view.setUint8(1, buffer[pos+1]);
data_view.setUint8(2, buffer[pos+2]);
data_view.setUint8(3, buffer[pos+3]);
return data_view.getFloat32(0, false);
}
function readInt(buffer, pos) {
data_view.setUint8(0, buffer[pos]);
data_view.setUint8(1, buffer[pos+1]);
data_view.setUint8(2, buffer[pos+2]);
data_view.setUint8(3, buffer[pos+3]);
return data_view.getInt32(0, false);
}
var udp = dgram.createSocket('udp4');
var this_ = this;
function processMessageOrBundle(msg, pos) {
var path = readString(msg, pos);
pos += path.length + 4 - (path.length & 3);
if (path === '#bundle') {
pos += 8; // Skip timetag, treat everything as 'immediately'.
while (pos < msg.length) {
var len = readInt(msg, pos);
pos += 4;
processMessageOrBundle(msg, pos);
pos += len;
}
return;
}
var typetag = readString(msg, pos);
pos += typetag.length + 4 - (typetag.length & 3);
var params = [ ];
for (var i = 1, il = typetag.length; i < il; ++i) {
var tag = typetag[i];
switch (tag) {
case 'T':
params.push(true);
break;
case 'F':
params.push(false);
break;
case 'N':
params.push(null);
break;
case 'I':
// NOTE(pizthewiz) - find better synthesized parameter for Impulse.
params.push(undefined);
break;
case 'f':
params.push(readFloat(msg, pos));
pos += 4;
break;
case 'i':
params.push(readInt(msg, pos));
pos += 4;
break;
case 's':
var str = readString(msg, pos);
pos += str.length + 4 - (str.length & 3);
params.push(str);
break;
case 'b':
var bytes = readBlob(msg, pos);
pos += 4 + bytes.length + ((4 - (bytes.length & 3)) & 3);
params.push(bytes);
break;
default:
console.log('WARNING: Unhandled OSC type tag: ' + tag);
break;
}
}
var e = {path: path, typetag: typetag.substr(1), params: params}
this_.emit(path + typetag, e);
this_.emit(path, e);
this_.emit('', e);
}
udp.on('message', function(msg, rinfo) {
try {
processMessageOrBundle(msg, 0);
} catch(e) {
console.log('WARNING: Skipping OSC message, error: ' + e);
}
});
udp.bind(port);
// Close the underlying socket for the receiver. No new messages should be
// received and the socket will be closed (although perhaps it is possible
// we will still get some messages that are already received and buffered).
// Returns true if the socket was closed or false if it was already closed.
this.close = function() {
if (udp === null) return false;
udp.close();
udp = null;
return true;
};
}
util.inherits(UdpReceiver, events.EventEmitter);
exports.UdpSender = UdpSender;
exports.UdpReceiver = UdpReceiver;