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