comparison resources/osc/node_modules/osc-min/lib/index.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 (function() {
2
3 //~readme.out~
4 //[![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)
5 //# osc-min
6 //
7 // _simple utilities for open sound control in node.js_
8 //
9 // This package provides some node.js utilities for working with
10 // [OSC](http://opensoundcontrol.org/), a format for sound and systems control.
11 // Here we implement the [OSC 1.1][spec11] specification. OSC is a transport-independent
12 // protocol, so we don't provide any server objects, as you should be able to
13 // use OSC over any transport you like. The most common is probably udp, but tcp
14 // is not unheard of.
15 //
16 // [spec11]: http://opensoundcontrol.org/spec-1_1
17 //
18 //----
19 //## Installation
20 //~installation~
21 //----
22 //## Examples
23 //~examples~
24 //
25 // more examples are available in the `examples/` directory.
26 //
27 //----
28 //~api~
29 //----
30 //~representation~
31 //----
32 //## License
33 // Licensed under the terms found in COPYING (zlib license)
34
35 //~representation~
36 //## Javascript representations of the OSC types.
37 // See the [spec][spec] for more information on the OSC types.
38 //
39 // + An _OSC Packet_ is an _OSC Message_ or an _OSC Bundle_.
40 //
41 // + An _OSC Message_:
42 //
43 // {
44 // oscType : "message"
45 // address : "/address/pattern/might/have/wildcards"
46 // args : [arg1,arg2]
47 // }
48 //
49 // Where args is an array of _OSC Arguments_. `oscType` is optional.
50 // `args` can be a single element.
51 //
52 // + An _OSC Argument_ is represented as a javascript object with the following layout:
53 //
54 // {
55 // type : "string"
56 // value : "value"
57 // }
58 //
59 // Where the `type` is one of the following:
60 // + `string` - string value
61 // + `float` - numeric value
62 // + `integer` - numeric value
63 // + `blob` - node.js Buffer value
64 // + `true` - value is boolean true
65 // + `false` - value is boolean false
66 // + `null` - no value
67 // + `bang` - no value (this is the `I` type tag)
68 // + `timetag` - numeric value
69 // + `array` - array of _OSC Arguments_
70 //
71 // Note that `type` is always a string - i.e. `"true"` rather than `true`.
72 //
73 // The following non-standard types are also supported:
74 // + `double` - numeric value (encodes to a float64 value)
75 //
76 //
77 // For messages sent to the `toBuffer` function, `type` is optional.
78 // If the argument is not an object, it will be interpreted as either
79 // `string`, `float`, `array` or `blob`, depending on its javascript type
80 // (String, Number, Array, Buffer, respectively)
81 //
82 // + An _OSC Bundle_ is represented as a javascript object with the following fields:
83 //
84 // {
85 // oscType : "bundle"
86 // timetag : 7
87 // elements : [element1, element]
88 // }
89 //
90 // `oscType` "bundle"
91 //
92 // `timetag` is one of:
93 // - `null` - meaning now, the current time.
94 // By the time the bundle is received it will too late and depending
95 // on the receiver may be discarded or you may be scolded for being late.
96 // - `number` - relative seconds from now with millisecond accuracy.
97 // - `Date` - a JavaScript Date object in your local time zone.
98 // OSC timetags use UTC timezone, so do not try to adjust for timezones,
99 // this is not needed.
100 // - `Array` - `[numberOfSecondsSince1900, fractionalSeconds]`
101 // Both values are `number`s. This gives full timing accuracy of 1/(2^32) seconds.
102 //
103 // `elements` is an `Array` of either _OSC Message_ or _OSC Bundle_
104 //
105 //
106 // [spec]: http://opensoundcontrol.org/spec-1_0
107
108 var utils, coffee;
109 utils = require("./osc-utilities");
110 // ~api~
111 //## Exported functions
112 //
113 //------
114 //### .fromBuffer(buffer, [strict])
115 // takes a node.js Buffer of a complete _OSC Packet_ and
116 // outputs the javascript representation, or throws if the buffer is ill-formed.
117 //
118 // `strict` is an optional parameter that makes the function fail more often.
119 exports.fromBuffer = function(buffer, strict) {
120 if (buffer instanceof ArrayBuffer) {
121 buffer = new Buffer(new Uint8Array(buffer));
122 } else if (buffer instanceof Uint8Array) {
123 buffer = new Buffer(buffer);
124 }
125 return utils.fromOscPacket(buffer, strict);
126 };
127
128 //~api~
129 //----
130 //### .toBuffer(object, [strict])
131 // takes a _OSC packet_ javascript representation as defined below and returns
132 // a node.js Buffer, or throws if the representation is ill-formed.
133 //
134 // See "JavaScript representations of the OSC types" below.
135 //
136 //----
137 //### .toBuffer(address, args[], [strict])
138 // alternative syntax for above. Assumes this is an _OSC Message_ as defined below,
139 // and `args` is an array of _OSC Arguments_ or single _OSC Argument_
140 exports.toBuffer = function(object, strict, opt) {
141 if(typeof object === "string")
142 return utils.toOscPacket({'address' : object, 'args' : strict}, opt);
143 return utils.toOscPacket(object, strict);
144 };
145
146 //~api~
147 //----
148 //### .applyAddressTransform(buffer, transform)
149 // takes a callback that takes a string and outputs a string,
150 // and applies that to the address of the message encoded in the buffer,
151 // and outputs an encoded buffer.
152 //
153 // If the buffer encodes an _OSC Bundle_, this applies the function to each address
154 // in the bundle.
155 //
156 // There's two subtle reasons you'd want to use this function rather than
157 // composing `fromBuffer` and `toBuffer`:
158 // - Future-proofing - if the OSC message uses an argument typecode that
159 // we don't understand, calling `fromBuffer` will throw. The only time
160 // when `applyAddressTranform` might fail is if the address is malformed.
161 // - Accuracy - javascript represents numbers as 64-bit floats, so some
162 // OSC types will not be able to be represented accurately. If accuracy
163 // is important to you, then, you should never convert the OSC message to a
164 // javascript representation.
165 exports.applyAddressTransform = function(buffer, transform) {
166 return utils.applyTransform(buffer, utils.addressTransform(transform));
167 };
168
169 //~api~
170 //----
171 //### .applyMessageTransform(buffer, transform)
172 // takes a function that takes and returns a javascript _OSC Message_ representation,
173 // and applies that to each message encoded in the buffer,
174 // and outputs a new buffer with the new address.
175 //
176 // If the buffer encodes an osc-bundle, this applies the function to each message
177 // in the bundle.
178 //
179 // See notes above for applyAddressTransform for why you might want to use this.
180 // While this does parse and re-pack the messages, the bundle timetags are left
181 // in their accurate and prestine state.
182 exports.applyMessageTransform = function(buffer, transform) {
183 return utils.applyTransform(buffer, utils.messageTransform(transform));
184 };
185
186
187 //~api~
188 //----
189 //### .timetagToDate(ntpTimeTag)
190 // Convert a timetag array to a JavaScript Date object in your local timezone.
191 //
192 // Received OSC bundles converted with `fromBuffer` will have a timetag array:
193 // [secondsSince1970, fractionalSeconds]
194 // This utility is useful for logging. Accuracy is reduced to milliseconds.
195 exports.timetagToDate = utils.timetagToDate;
196
197 //~api~
198 //----
199 //### .dateToTimetag(date)
200 // Convert a JavaScript Date to a NTP timetag array [secondsSince1970, fractionalSeconds].
201 //
202 // `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.
203 exports.dateToTimetag = utils.dateToTimetag;
204
205 //~api~
206 //----
207 //### .timetagToTimestamp(timeTag)
208 // Convert a timetag array to the number of seconds since the UNIX epoch.
209 //
210 exports.timetagToTimestamp = utils.timetagToTimestamp;
211
212 //~api~
213 //----
214 //### .timestampToTimetag(timeStamp)
215 // Convert a number of seconds since the UNIX epoch to a timetag array.
216 //
217 exports.timestampToTimetag = utils.timestampToTimetag;
218
219 }).call(this);