comparison resources/osc/node_modules/osc-min/readme.md @ 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 [![build status](https://secure.travis-ci.org/russellmcc/node-osc-min.png)](http://travis-ci.org/russellmcc/node-osc-min) [![Coverage Status](https://coveralls.io/repos/russellmcc/node-osc-min/badge.png?branch=master)](https://coveralls.io/r/russellmcc/node-osc-min?branch=master) [![dependencies](https://david-dm.org/russellmcc/node-osc-min.png)](https://david-dm.org/russellmcc/node-osc-min)
2 # osc-min
3
4 _simple utilities for open sound control in node.js_
5
6 This package provides some node.js utilities for working with
7 [OSC](http://opensoundcontrol.org/), a format for sound and systems control.
8 Here we implement the [OSC 1.1][spec11] specification. OSC is a transport-independent
9 protocol, so we don't provide any server objects, as you should be able to
10 use OSC over any transport you like. The most common is probably udp, but tcp
11 is not unheard of.
12
13 [spec11]: http://opensoundcontrol.org/spec-1_1
14
15 ----
16 ## Installation
17
18 The easiest way to get osc-min is through [NPM](http://npmjs.org).
19 After install npm, you can install osc-min in the current directory with
20
21 ```
22 npm install osc-min
23 ```
24
25 If you'd rather get osc-min through github (for example, if you're forking
26 it), you still need npm to install dependencies, which you can do with
27
28 ```
29 npm install --dev
30 ```
31
32 Once you've got all the dependencies you should be able to run the unit
33 tests with
34
35 ```
36 npm test
37 npm run-script coverage
38 ```
39
40 ### For the browser
41 If you want to use this library in a browser, you can build a browserified file (`build/osc-min.js`) with
42
43 ```
44 npm install --dev
45 npm run-script browserify
46 ```
47
48 ----
49 ## Examples
50 ### A simple OSC printer;
51 ```javascript
52
53 sock = udp.createSocket("udp4", function(msg, rinfo) {
54 var error, error1;
55 try {
56 return console.log(osc.fromBuffer(msg));
57 } catch (error1) {
58 error = error1;
59 return console.log("invalid OSC packet");
60 }
61 });
62
63 sock.bind(inport);
64
65 ```
66 ### Send a bunch of args every two seconds;
67 ```javascript
68
69 sendHeartbeat = function() {
70 var buf;
71 buf = osc.toBuffer({
72 address: "/heartbeat",
73 args: [
74 12, "sttttring", new Buffer("beat"), {
75 type: "integer",
76 value: 7
77 }
78 ]
79 });
80 return udp.send(buf, 0, buf.length, outport, "localhost");
81 };
82
83 setInterval(sendHeartbeat, 2000);
84
85 ```
86 ### A simple OSC redirecter;
87 ```javascript
88
89 sock = udp.createSocket("udp4", function(msg, rinfo) {
90 var error, error1, redirected;
91 try {
92 redirected = osc.applyAddressTransform(msg, function(address) {
93 return "/redirect" + address;
94 });
95 return sock.send(redirected, 0, redirected.length, outport, "localhost");
96 } catch (error1) {
97 error = error1;
98 return console.log("error redirecting: " + error);
99 }
100 });
101
102 sock.bind(inport);
103
104 ```
105
106
107 more examples are available in the `examples/` directory.
108
109 ----
110 ## Exported functions
111
112 ------
113 ### .fromBuffer(buffer, [strict])
114 takes a node.js Buffer of a complete _OSC Packet_ and
115 outputs the javascript representation, or throws if the buffer is ill-formed.
116
117 `strict` is an optional parameter that makes the function fail more often.
118
119 ----
120 ### .toBuffer(object, [strict])
121 takes a _OSC packet_ javascript representation as defined below and returns
122 a node.js Buffer, or throws if the representation is ill-formed.
123
124 See "JavaScript representations of the OSC types" below.
125
126 ----
127 ### .toBuffer(address, args[], [strict])
128 alternative syntax for above. Assumes this is an _OSC Message_ as defined below,
129 and `args` is an array of _OSC Arguments_ or single _OSC Argument_
130
131 ----
132 ### .applyAddressTransform(buffer, transform)
133 takes a callback that takes a string and outputs a string,
134 and applies that to the address of the message encoded in the buffer,
135 and outputs an encoded buffer.
136
137 If the buffer encodes an _OSC Bundle_, this applies the function to each address
138 in the bundle.
139
140 There's two subtle reasons you'd want to use this function rather than
141 composing `fromBuffer` and `toBuffer`:
142 - Future-proofing - if the OSC message uses an argument typecode that
143 we don't understand, calling `fromBuffer` will throw. The only time
144 when `applyAddressTranform` might fail is if the address is malformed.
145 - Accuracy - javascript represents numbers as 64-bit floats, so some
146 OSC types will not be able to be represented accurately. If accuracy
147 is important to you, then, you should never convert the OSC message to a
148 javascript representation.
149
150 ----
151 ### .applyMessageTransform(buffer, transform)
152 takes a function that takes and returns a javascript _OSC Message_ representation,
153 and applies that to each message encoded in the buffer,
154 and outputs a new buffer with the new address.
155
156 If the buffer encodes an osc-bundle, this applies the function to each message
157 in the bundle.
158
159 See notes above for applyAddressTransform for why you might want to use this.
160 While this does parse and re-pack the messages, the bundle timetags are left
161 in their accurate and prestine state.
162
163 ----
164 ### .timetagToDate(ntpTimeTag)
165 Convert a timetag array to a JavaScript Date object in your local timezone.
166
167 Received OSC bundles converted with `fromBuffer` will have a timetag array:
168 [secondsSince1970, fractionalSeconds]
169 This utility is useful for logging. Accuracy is reduced to milliseconds.
170
171 ----
172 ### .dateToTimetag(date)
173 Convert a JavaScript Date to a NTP timetag array [secondsSince1970, fractionalSeconds].
174
175 `toBuffer` already accepts Dates for timetags so you might not need this function. If you need to schedule bundles with finer than millisecond accuracy then you could use this to help assemble the NTP array.
176
177 ----
178 ### .timetagToTimestamp(timeTag)
179 Convert a timetag array to the number of seconds since the UNIX epoch.
180
181
182 ----
183 ### .timestampToTimetag(timeStamp)
184 Convert a number of seconds since the UNIX epoch to a timetag array.
185
186
187 ----
188 ## Javascript representations of the OSC types.
189 See the [spec][spec] for more information on the OSC types.
190
191 + An _OSC Packet_ is an _OSC Message_ or an _OSC Bundle_.
192
193 + An _OSC Message_:
194
195 {
196 oscType : "message"
197 address : "/address/pattern/might/have/wildcards"
198 args : [arg1,arg2]
199 }
200
201 Where args is an array of _OSC Arguments_. `oscType` is optional.
202 `args` can be a single element.
203
204 + An _OSC Argument_ is represented as a javascript object with the following layout:
205
206 {
207 type : "string"
208 value : "value"
209 }
210
211 Where the `type` is one of the following:
212 + `string` - string value
213 + `float` - numeric value
214 + `integer` - numeric value
215 + `blob` - node.js Buffer value
216 + `true` - value is boolean true
217 + `false` - value is boolean false
218 + `null` - no value
219 + `bang` - no value (this is the `I` type tag)
220 + `timetag` - numeric value
221 + `array` - array of _OSC Arguments_
222
223 Note that `type` is always a string - i.e. `"true"` rather than `true`.
224
225 The following non-standard types are also supported:
226 + `double` - numeric value (encodes to a float64 value)
227
228
229 For messages sent to the `toBuffer` function, `type` is optional.
230 If the argument is not an object, it will be interpreted as either
231 `string`, `float`, `array` or `blob`, depending on its javascript type
232 (String, Number, Array, Buffer, respectively)
233
234 + An _OSC Bundle_ is represented as a javascript object with the following fields:
235
236 {
237 oscType : "bundle"
238 timetag : 7
239 elements : [element1, element]
240 }
241
242 `oscType` "bundle"
243
244 `timetag` is one of:
245 - `null` - meaning now, the current time.
246 By the time the bundle is received it will too late and depending
247 on the receiver may be discarded or you may be scolded for being late.
248 - `number` - relative seconds from now with millisecond accuracy.
249 - `Date` - a JavaScript Date object in your local time zone.
250 OSC timetags use UTC timezone, so do not try to adjust for timezones,
251 this is not needed.
252 - `Array` - `[numberOfSecondsSince1900, fractionalSeconds]`
253 Both values are `number`s. This gives full timing accuracy of 1/(2^32) seconds.
254
255 `elements` is an `Array` of either _OSC Message_ or _OSC Bundle_
256
257
258 [spec]: http://opensoundcontrol.org/spec-1_0
259
260 ----
261 ## License
262 Licensed under the terms found in COPYING (zlib license)