UNPKG

a2r-osc

Version:

An OSC implementation for node.js and the browser

1 lines 13.5 kB
(function(){var t,e={}.hasOwnProperty,r=function(t,r){function n(){this.constructor=t}for(var i in r)e.call(r,i)&&(t[i]=r[i]);return n.prototype=r.prototype,t.prototype=new n,t.__super__=r.prototype,t};"undefined"==typeof module?(window.a2r||(window.a2r={}),t=window.a2r.osc={}):t=module.exports,function(t){var e,n,i,s,o,a,u,f,h,d,c,p,l,g,w,y,m,b,A,I,v,T,E,P,B,O,S,_,z,D,U,V,k;b="function"==typeof Buffer,_=function(t){if(t=Number(t),0/0===t)throw Error("Value isn't a number");return t},O=function(t){return t=_(t),Math.round(t)},p=2208988800,y=function(t,e){var r,n;return 0===t&&1===e?new Date:(n=1e3*(t-p),n+=Math.round(1e3*e/4294967296),r=new Date(n),r.ntpSeconds=t,r.ntpFraction=e,r)},S=function(t){var e,r,n;return 1===t?[0,1]:Array.isArray(t)?t:(n=t.getTime(),r=Math.floor(n/1e3),e=Math.round(4294967296*(n%1e3)/1e3),[r+p,e])},f={i:{name:"integer",read:function(t){return t.readInt32()},write:function(t,e){return t.writeInt32(e)},cast:O,sizeOf:function(){return 4}},f:{name:"float",read:function(t){return t.readFloat()},write:function(t,e){return t.writeFloat(e)},cast:_,sizeOf:function(){return 4}},s:{name:"string",read:function(t){return t.readString()},write:function(t,e){return t.writeString(e)},cast:function(t){return""+t},sizeOf:function(t){return P(""+t)}},b:{name:"blob",read:function(t){return t.readBlob()},write:function(t,e){return t.writeBlob(e)},sizeOf:function(t){return v(t)}},d:{name:"double",read:function(t){return t.readDouble()},write:function(t,e){return t.writeDouble(e)},sizeOf:function(){return 8}},c:{name:"char",read:function(t){return String.fromCharCode(127&t.readInt32())},write:function(t,e){return t.writeInt32(e.charCodeAt(0))},cast:function(t){return(""+t).charAt(0)},sizeOf:function(){return 4}},r:{name:"color",read:function(t){return t.readInt32()},write:function(t,e){return t.writeInt32(e)},cast:O,sizeOf:function(){return 4}},t:{name:"time",read:function(t){return t.readTimetag()},write:function(t,e){return t.writeTimetag(e)},cast:function(t){return t instanceof Date?t:new Date(t)},sizeOf:function(){return 8}},T:{name:"true",read:function(){return!0}},F:{name:"false",read:function(){return!1}},N:{name:"null",read:function(){return null}},I:{name:"impulse",read:function(){return o}}},f.S=f.s,h={};for(l in f)z=f[l],"S"!==l&&(z.code=l),h[z.name]=z;u={Int32:{dataViewReader:"getInt32",dataViewWriter:"setInt32",bufferReader:"readInt32BE",bufferWriter:"writeInt32BE",size:4},UInt32:{dataViewReader:"getUint32",dataViewWriter:"setUint32",bufferReader:"readUInt32BE",bufferWriter:"writeUInt32BE",size:4},Float:{dataViewReader:"getFloat32",dataViewWriter:"setFloat32",bufferReader:"readFloatBE",bufferWriter:"writeFloatBE",size:4},Double:{dataViewReader:"getFloat64",dataViewWriter:"setFloat64",bufferReader:"readDoubleBE",bufferWriter:"writeDoubleBE",size:8}},A=function(t){return 4-t%4},o={},B=function(t){switch(typeof t){case"string":return"s";case"number":return"f";case"boolean":return t?"T":"F";case"undefined":throw Error("Value can't be undefined");case"object":if(null===t)return"N";if(t instanceof Date)return"t";if(b&&Buffer.isBuffer(t)||t instanceof ArrayBuffer)return"b";if(t===o)return"I";throw Error("Unsupported type `"+t+"`");default:throw Error("Unsupported type `"+t+"`")}},P=function(t){return t.length+A(t.length)},v=function(t){var e,r;return e=t instanceof ArrayBuffer?4+t.byteLength:4+t.length,r=A(e),4>r&&(e+=r),e},T=function(t,e){var r,n,i,s,o;for(n=16,o=t.elements,i=0,s=o.length;s>i;i++)r=o[i],n+=4+E(r,e);return n},E=function(t,e){var r,n,i,s,o,a,u;for(e&&(n=t.isPattern()?e.getPatternId(t.address):e.getAddressId(t.address)),s=n?8:P(t.address),o=n?t.typeTag.length+2:t.typeTag.length+1,s+=o+A(o),r=0,i=t.typeTag.length;i>r;)a=t.typeTag.charAt(r),u=t.arguments[r++],s+=I(u,a);return s},I=function(t,e){if(e){if(z=f[e]||h[e],!z)throw Error("Type `"+e+"` isn't supported");return z.sizeOf?z.sizeOf(t):0}return e=B(t),I(t,e)},s=function(){function t(t,e){var r,n,i;if(this.idToAddress={},this.addressToId={},this.idToPattern={},this.patternToId={},t)for(n in t)r=t[n],this.addAddress(n,r);if(e)for(n in e)i=e[n],this.addPattern(n,i)}return t.prototype.addAddress=function(t,e){return this.idToAddress[t]=e,this.addressToId[e]=Number(t)},t.prototype.getAddress=function(t){return this.idToAddress[t]},t.prototype.getAddressId=function(t){return this.addressToId[t]},t.prototype.removeAddress=function(t){var e,r;return"number"==typeof t?(r=t,e=this.idToAddress[r]):(e=t,r=this.addressToId[e]),delete this.idToAddress[r],delete this.addressToId[e]},t.prototype.addPattern=function(t,e){return this.idToPattern[t]=e,this.patternToId[e]=Number(t)},t.prototype.getPattern=function(t){return this.idToPattern[t]},t.prototype.getPatternId=function(t){return this.patternToId[t]},t.prototype.removePattern=function(t){var e,r;return"number"==typeof t?(e=t,r=this.idToPattern[e]):(r=t,e=this.patternToId[r]),delete this.idToPattern[e],delete this.patternToId[r]},t}(),a=function(){function t(e,r,n){var i,s,o,a,u,d,c,p;if(e instanceof t)return o=e,this.address=o.address,this.typeTag=o.typeTag,this.arguments=o.arguments.slice(0),void 0;if(this.address=e,this.arguments=[],void 0!==r&&void 0===n&&(n=r,r=null),void 0!==n)if(Array.isArray(n)||(n=[n]),r){if(n.length!==r.length)throw Error("Arguments length doesn't match typetag length");for(s=u=0,c=n.length;c>u;s=++u)i=n[s],this.add(r.charAt(s),i)}else for(d=0,p=n.length;p>d;d++){if(a=n[d],l=null,"object"==typeof a&&null!=(null!=a?a.type:void 0)){if(z=a.type,z=f[z]||h[z],l=z.code,!z)throw Error("Unsupported type `"+l+"`");a=z.sizeOf?a.value:z.read()}l?this.add(l,a):this.add(a)}}return t.prototype.clone=function(){return new t(this)},t.prototype.isPattern=function(){return null!=this._isPattern?this._isPattern:this._isPattern=/(?:\*|\?|\[|\{|\/\/)/.test(this.address)},t.prototype.add=function(t,e){if(this.buffer&&delete this.buffer,void 0===e&&(e=t,t=null),t){if(z=f[t]||h[t],!z)throw Error("Unsupported type `"+t+"`");z.cast&&(e=z.cast(e))}else t=B(e),z=f[t];return this.arguments.push(e),this.typeTag?this.typeTag+=t:this.typeTag=t,this},t.prototype.toBuffer=function(t){return this.buffer||(this.buffer=b?new OscBufferPacketGenerator(this,t).generate():new d(this,t).generate())},t.prototype.equal=function(e){var r,n,i,s,o;if(!(e instanceof t))return!1;if(e.address!==this.address)return!1;if(e.typeTag!==this.typeTag)return!1;if(e.arguments.length!==this.arguments.length)return!1;for(o=this.arguments,n=i=0,s=o.length;s>i;n=++i)if(r=o[n],e.arguments[n]!==r)return!1;return!0},t}(),i=function(){function t(e,r){var n,i,s,o,a,u,f;if(e instanceof t)for(n=e,this.timetag=new Date(n.timetag.valueOf()),this.elements=[],f=n.elements,s=0,a=f.length;a>s;s++)i=f[s],this.addElement(i.clone());else if(e instanceof Date?this.timetag=e:1===e?this.timetag=new Date:(this.timetag=new Date,r=e),this.elements=[],r)if(Array.isArray(r))for(o=0,u=r.length;u>o;o++)i=r[o],this.addElement(i);else this.addElement(r)}return t.prototype.clone=function(){return new t(this)},t.prototype.addElement=function(t,e,r){var n;if(this.buffer&&delete this.buffer,t instanceof a)return this.elements.push(t),t;if("string"==typeof t)return n=new a(t,e,r),this.elements.push(n),n;throw Error("A bundle element must be an instance of Message")},t.prototype.add=function(t,e,r){return this.addElement(t,e,r),this},t.prototype.toBuffer=function(t){return this.buffer||(this.buffer=b?new OscBufferPacketGenerator(this,t).generate():new d(this,t).generate())},t.prototype.equal=function(e){var r,n,i,s,o;if(!(e instanceof t))return!1;if(e.timetag!==this.timetag)return!1;if(e.elements.length!==this.elements.length)return!1;for(o=this.elements,n=i=0,s=o.length;s>i;n=++i)if(r=o[n],!r.equal(e.elements[n]))return!1;return!0},t}(),e=function(){function t(t,e){this.dict=e,t instanceof i?(this.bundle=t,this.size=T(this.bundle,this.dict)):(this.message=t,this.size=E(this.message,this.dict))}return t.prototype._generateMessage=function(t){var e,r,n,i,s;for(this.dict&&(r=t.isPattern()?this.dict.getPatternId(t.address):this.dict.getAddressId(t.address)),r?(t.isPattern()?this.writeString("/?"):this.writeString("/"),this.writeString(",i"+t.typeTag),this.writeInt32(O(r))):(this.writeString(t.address),this.writeString(","+t.typeTag)),e=0,n=t.typeTag.length,s=[];n>e;){if(l=t.typeTag.charAt(e),i=t.arguments[e++],z=f[l],!z)throw Error("Type `"+l+"` isn't supported");z.write?s.push(z.write(this,i)):s.push(void 0)}return s},t.prototype._generateBundle=function(t){var e,r,n,i,s;for(this.writeString("#bundle"),r=t.timetag<=new Date?[0,1]:S(t.timetag),this.writeTimetag(r),s=t.elements,n=0,i=s.length;i>n;n++)e=s[n],this.writeInt32(E(e,this.dict)),this._generateMessage(e);return null},t.prototype.writeTimetag=function(t){var e;return e=S(t),this.writeUInt32(e[0]),this.writeUInt32(e[1])},t.prototype.generate=function(){return this.bundle?this._generateBundle(this.bundle):this._generateMessage(this.message),this.buffer},t.prototype.writeString=function(t,e){throw null==e&&(e="ascii"),Error("Abstract method `AbstractOscPacketGenerator::writeString` called")},t}(),D=function(t){return t="write"+t,e.prototype[t]=function(){throw Error("Abstract method `AbstractOscPacketGenerator::"+t+"` called")}};for(m in u)g=u[m],D(m);d=function(t){function e(t,r){e.__super__.constructor.call(this,t,r),this.buffer=new ArrayBuffer(this.size),this.view=new DataView(this.buffer),this.pos=0}return r(e,t),e.prototype.writeString=function(t,e){var r,n,i,s,o;if(null==e&&(e="ascii"),"ascii"!==e)throw Error("OscBufferWriter::writeString only supports ASCII encoding for ArrayBuffer");for(i=t.length,n=0;i>n;)r=t.charCodeAt(n++),this.view.setInt8(this.pos++,127&r);for(s=A(i),n=0,o=[];s>n;)this.view.setInt8(this.pos++,0),o.push(n++);return o},e.prototype.writeBlob=function(t){var e,r,n,i;if(b&&Buffer.isBuffer(t)){for(n=t.length,this.writeInt32(n),r=0;n>r;)this.view.setInt8(this.pos+r,t[r]),r++;this.pos+=n}else{for(n=t.byteLength,e=new Int8Array(t),this.writeInt32(n),r=0;n>r;)this.view.setInt8(this.pos+r,e[r]),r++;this.pos+=n}if(i=A(4+n),i&&4>i){for(r=0;i>r;)this.view.setInt8(this.pos+r,0),r++;return this.pos+=i}},e}(e),U=function(t,e){return d.prototype["write"+t]=function(t){return t=this.view[e.dataViewWriter](this.pos,t,!1),this.pos+=e.size,t}};for(z in u)g=u[z],U(z,g);n=function(){function t(t,e,r){null==e&&(e=0),this.buffer=t,"object"==typeof e?(this.dict=e,this.pos=0):(this.dict=r,this.pos=e)}return t.prototype.parse=function(){var t;return t=this.readString(),"#bundle"===t?this._parseBundle():this._parseMessage(t)},t.prototype._parseMessage=function(t){var e,r,n,i;if("/"!==t.charAt(0))throw Error("An address must start with a '/'");return this.dict&&((n="/"===t)||"/?"===t)?(i=this.readTypeTag(),e=this.parseArguments(i),"i"===i.charAt(0)&&(r=e[0],t=n?this.dict.getAddress(r):this.dict.getPattern(r),t&&(i=i.slice(1,1),e.shift()))):(i=this.readTypeTag(),e=this.parseArguments(i)),new a(t,i,e)},t.prototype._parseBundle=function(){var t,e,r,n;for(n=this.readTimetag(),e=[];!this.isEnd();)r=this.readInt32(),t=this.pos+r,e.push(this.parse());return new i(n,e)},t.prototype.parseArguments=function(t,e){var r,n;for(r=0,n=[];t.length>r;){if(e&&this.pos>=e)throw Error("Message boundary reached");if(l=t.charAt(r++),z=f[l],!z)throw Error("Type `"+l+"` isn't supported");n.push(z.read(this))}return n},t.prototype.readTypeTag=function(){var t;if(t=this.readString(),","!==t.charAt(0))throw Error("A type tag must start with a ','");return t=t.slice(1)},t.prototype.readTimetag=function(){return y(this.readUInt32(),this.readUInt32())},t.prototype.readString=function(){throw Error("Abstract method `AbstractOscPacketParser::writeString` called")},t.prototype.isEnd=function(){throw Error("Abstract method `AbstractOscPacketParser::isEnd` called")},t}(),V=function(t){return t="read"+t,n.prototype[t]=function(){throw Error("Abstract method `AbstractOscPacketParser::"+t+"` called")}};for(m in u)g=u[m],V(m);c=function(t){function e(){e.__super__.constructor.apply(this,arguments),this.view=new DataView(this.buffer)}return r(e,t),e.prototype.isEnd=function(){return 0===this.buffer.byteLength||this.pos===this.buffer.byteLength},e.prototype.toString=function(t,e,r){var n,i;for(e=null!=e?e:0,r=null!=r?r:this.buffer.byteLength,i="";r>e;)n=this.view.getInt8(e++),i+=String.fromCharCode(127&n);return i},e.prototype.readBlob=function(t){var e,r,n,i;for(null==t&&(t=!0),i=this.readInt32(),r=0,e=new Int8Array(new ArrayBuffer(i));i>r;)e[r]=this.view.getInt8(this.pos+r),r++;return t&&(n=A(4+i),4>n&&(i+=n),this.pos+=i),e.buffer},e.prototype.readString=function(t,e){var r,n,i,s,o;if(null==t&&(t="ascii"),null==e&&(e=!0),this.isEnd())throw Error("No data left");for(r=4,n=!1;(i=this.pos+r-1)<this.buffer.byteLength;){if(0===this.view.getInt8(i)){n=!0;break}r+=4}if(0===r||n===!1)throw Error("No string data found");for(o=r-4;r>o&&0!==this.view.getInt8(this.pos+o);)o++;return s=this.toString(t,this.pos,this.pos+o),e&&(this.pos+=r),s},e}(n),k=function(t,e){return c.prototype["read"+t]=function(t){var r;return null==t&&(t=!0),r=this.view[e.dataViewReader](this.pos,!1),t&&(this.pos+=e.size),r}};for(z in u)g=u[z],k(z,g);return w=function(t,e,r){return b&&Buffer.isBuffer(t)?new OscBufferPacketParser(t,e,r).parse():new c(t,e,r).parse()},t.NUMBERS=u,t.toNTP=S,t.Message=a,t.Bundle=i,t.Impulse=o,t.Dictionary=s,t.AbstractOscPacketGenerator=e,t.AbstractOscPacketParser=n,t.OscArrayBufferPacketGenerator=d,t.OscArrayBufferPacketParser=c,t.fromBuffer=w}(t)}).call(this);