annotate resources/osc/node_modules/binpack/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 // t is a binpack typename
l@271 2 var sizeOfType = function(t) {
l@271 3 // unsigned are the same length as signed
l@271 4 if(t[0] === 'U') {
l@271 5 t = t.slice(1);
l@271 6 }
l@271 7
l@271 8 return {
l@271 9 'Float32' : 4,
l@271 10 'Float64' : 8,
l@271 11 'Int8' : 1,
l@271 12 'Int16' : 2,
l@271 13 'Int32' : 4,
l@271 14 'Int64' : 8
l@271 15 }[t];
l@271 16 };
l@271 17
l@271 18 var endianConv = function(e, t) {
l@271 19 // node doesn't define 8 bit endianness
l@271 20 if(t[t.length - 1] === '8')
l@271 21 return '';
l@271 22
l@271 23 if(e === 'big') {
l@271 24 return 'BE';
l@271 25 }
l@271 26 return 'LE';
l@271 27 };
l@271 28
l@271 29 var addBindings = function(binpackTypename, nodeTypename){
l@271 30 if(!(typeof nodeTypename !== "undefined" && nodeTypename !== null)) {
l@271 31 nodeTypename = binpackTypename;
l@271 32 }
l@271 33 module.exports['pack' + binpackTypename] = function(num, endian){
l@271 34 b = new Buffer(sizeOfType(binpackTypename));
l@271 35 b['write' + nodeTypename + endianConv(endian, binpackTypename)](num, 0, true);
l@271 36 return b;
l@271 37 }
l@271 38
l@271 39 module.exports['unpack' + binpackTypename] = function(buff, endian){
l@271 40 return buff['read' + nodeTypename + endianConv(endian, binpackTypename)](0);
l@271 41 }
l@271 42 }
l@271 43
l@271 44 var addIntBindings = function(n) {
l@271 45 addBindings("Int" + n);
l@271 46 addBindings("UInt" + n);
l@271 47 }
l@271 48
l@271 49 addIntBindings(8);
l@271 50 addIntBindings(16);
l@271 51 addIntBindings(32);
l@271 52
l@271 53 twoToThe32 = Math.pow(2, 32);
l@271 54
l@271 55 // 64 bit bindings require special care
l@271 56 var read64 = function(unsigned){return function(buff, endian){
l@271 57 var e = endianConv(endian, '');
l@271 58 var u = unsigned ? 'U' : '';
l@271 59 var low, high;
l@271 60 if(e === 'LE') {
l@271 61 low = buff.readUInt32LE(0);
l@271 62 high = buff['read' + u + 'Int32LE'](4);
l@271 63 } else {
l@271 64 low = buff.readUInt32BE(4);
l@271 65 high = buff['read' + u + 'Int32BE'](0);
l@271 66 }
l@271 67 return high * twoToThe32 + low;
l@271 68 };};
l@271 69
l@271 70 var write64 = function(unsigned){return function(num, endian){
l@271 71 var e = endianConv(endian, '');
l@271 72 var u = unsigned ? 'U' : '';
l@271 73 var b = new Buffer(8);
l@271 74 var high = Math.floor(num / twoToThe32);
l@271 75 var low = Math.floor(num - high * twoToThe32);
l@271 76 if(e == 'LE') {
l@271 77 b.writeUInt32LE(low, 0, true);
l@271 78 b['write' + u + 'Int32LE'](high, 4, true);
l@271 79 } else {
l@271 80 b.writeUInt32BE(low, 4, true);
l@271 81 b['write' + u + 'Int32BE'](high, 0, true);
l@271 82 }
l@271 83 return b;
l@271 84 };};
l@271 85
l@271 86 module.exports.unpackInt64 = read64(false);
l@271 87 module.exports.unpackUInt64 = read64(true);
l@271 88 module.exports.packInt64 = write64(false);
l@271 89 module.exports.packUInt64 = write64(true);
l@271 90
l@271 91 addBindings("Float32", "Float");
l@271 92 addBindings("Float64", "Double");