supersonic-scsynth 0.66.0 → 0.68.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +11 -0
- package/README.md +395 -123
- package/README.npm.md +395 -123
- package/dist/osc_channel.js +1 -1
- package/dist/osc_fast.js +1 -1
- package/dist/supersonic.js +5 -5
- package/dist/workers/debug_worker.js +3 -3
- package/dist/workers/osc_in_worker.js +1 -1
- package/dist/workers/osc_out_log_sab_worker.js +1 -1
- package/dist/workers/osc_out_prescheduler_worker.js +1 -1
- package/package.json +1 -1
- package/supersonic.d.ts +186 -10
package/dist/osc_channel.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function g(s,t,e){return(e-1-s+t)%e}function Y({uint8View:s,dataView:t,bufferStart:e,bufferSize:r,head:n,payload:o,sequence:E,messageMagic:c,headerSize:i,sourceId:S=0,headerScratch:a=null,headerScratchView:I=null}){let f=o.length,u=i+f+3&-4,T=r-n;if(u>T){let _=a||new Uint8Array(i),A=I||new DataView(_.buffer);A.setUint32(0,c,!0),A.setUint32(4,u,!0),A.setUint32(8,E,!0),A.setUint32(12,S,!0);let d=e+n,l=e;if(T>=i){s.set(_,d);let h=T-i;h>0&&s.set(o.subarray(0,h),d+i),s.set(o.subarray(h),l)}else{s.set(_.subarray(0,T),d),s.set(_.subarray(T),l);let h=i-T;s.set(o,l+h)}}else{let _=e+n;t.setUint32(_,c,!0),t.setUint32(_+4,u,!0),t.setUint32(_+8,E,!0),t.setUint32(_+12,S,!0),s.set(o,_+i)}return(n+u)%r}function b(s,t,e=0,r=!1){for(let n=0;n<=e;n++)if(Atomics.compareExchange(s,t,0,1)===0)return!0;if(r){for(let o=0;o<100;o++)if(Atomics.wait(s,t,1,100),Atomics.compareExchange(s,t,0,1)===0)return!0;return console.error("[RingBuffer] Lock acquisition timeout after 10s - possible deadlock"),!1}return!1}function y(s,t){Atomics.store(s,t,0),Atomics.notify(s,t,1)}function N({atomicView:s,dataView:t,uint8View:e,bufferConstants:r,ringBufferBase:n,controlIndices:o,oscMessage:E,sourceId:c=0,maxSpins:i=0,useWait:S=!1}){let a=E.length,I=r.MESSAGE_HEADER_SIZE+a;if(I>r.IN_BUFFER_SIZE-r.MESSAGE_HEADER_SIZE||!b(s,o.IN_WRITE_LOCK,i,S))return!1;try{let f=Atomics.load(s,o.IN_HEAD),U=Atomics.load(s,o.IN_TAIL),u=I+3&-4;if(g(f,U,r.IN_BUFFER_SIZE)<u)return!1;let _=Atomics.add(s,o.IN_SEQUENCE,1),A=Y({uint8View:e,dataView:t,bufferStart:n+r.IN_BUFFER_START,bufferSize:r.IN_BUFFER_SIZE,head:f,payload:E,sequence:_,messageMagic:r.MESSAGE_MAGIC,headerSize:r.MESSAGE_HEADER_SIZE,sourceId:c});return Atomics.load(s,o.IN_HEAD),Atomics.store(s,o.IN_HEAD,A),Atomics.notify(s,o.IN_HEAD,1),!0}finally{y(s,o.IN_WRITE_LOCK)}}function B(s,t){let e=s+t;return{IN_HEAD:(e+0)/4,IN_TAIL:(e+4)/4,IN_SEQUENCE:(e+24)/4,IN_WRITE_LOCK:(e+40)/4,IN_LOG_TAIL:(e+44)/4}}function X(s){return s.length>=8&&s[0]===35&&s[1]===98&&s[2]===117&&s[3]===110&&s[4]===100&&s[5]===108&&s[6]===101&&s[7]===0}var p=.5;function w(s){if(s.length<16)return null;let t=new DataView(s.buffer,s.byteOffset,s.byteLength);return{ntpSeconds:t.getUint32(8,!1),ntpFraction:t.getUint32(12,!1)}}function C(){return(performance.timeOrigin+performance.now())/1e3+2208988800}function O(s,t={}){let{getCurrentNTP:e=C,bypassLookaheadS:r=p}=t;if(!X(s))return"nonBundle";let n=w(s);if(!n)return"nonBundle";let{ntpSeconds:o,ntpFraction:E}=n;if(o===0&&E<=1)return"immediate";let c=e();if(c===null||c===0)return"immediate";let S=o+E/4294967296-c;return S<0?"late":S<r?"nearFuture":"farFuture"}function L(s){return s!=="farFuture"}var K={nonBundle:38,immediate:39,nearFuture:40,late:41},R=class s{#s;#r;#o;#u;#e;#t;#I;#E;#f;#d;#l;#p;#T;#_;#a;#c;#S;#i;#A;#n={messagesSent:0,bytesSent:0,nonBundle:0,immediate:0,nearFuture:0,late:0,bypassed:0};constructor(t,e){if(this.#s=t,this.#e=e.preschedulerPort||null,this.#I=e.bypassLookaheadS??p,this.#E=e.sourceId??0,this.#f=e.blocking??this.#E!==0,this.#d=e.getCurrentNTP??C,this.#a=1e3,t==="postMessage")this.#r=e.port;else{if(this.#o={sharedBuffer:e.sharedBuffer,ringBufferBase:e.ringBufferBase,bufferConstants:e.bufferConstants,controlIndices:e.controlIndices},this.#B(),e.sharedBuffer&&e.bufferConstants){let r=e.ringBufferBase+e.bufferConstants.METRICS_START;this.#t=new Int32Array(e.sharedBuffer,r,e.bufferConstants.METRICS_SIZE/4)}if(e.sharedBuffer&&e.bufferConstants?.NODE_ID_COUNTER_START!==void 0){let r=e.ringBufferBase+e.bufferConstants.NODE_ID_COUNTER_START;this.#l=new Int32Array(e.sharedBuffer,r,1),this.#R()}}e.nodeIdSource&&(this.#c=e.nodeIdSource,this.#R()),e.nodeIdRange&&(this.#p=e.nodeIdRange.from,this.#T=e.nodeIdRange.to,this.#_=e.nodeIdRange.from),e.nodeIdPort&&(this.#S=e.nodeIdPort,this.#S.onmessage=r=>{r.data.type==="nodeIdRange"&&(this.#i={from:r.data.from,to:r.data.to})},this.#h())}#B(){let t=this.#o.sharedBuffer;this.#u={atomicView:new Int32Array(t),dataView:new DataView(t),uint8View:new Uint8Array(t)}}classify(t){return O(t,{getCurrentNTP:this.#d,bypassLookaheadS:this.#I})}#U(t,e=null){if(this.#s==="sab"&&this.#t){if(Atomics.add(this.#t,24,1),Atomics.add(this.#t,25,t),e){let r=K[e];r!==void 0&&(Atomics.add(this.#t,r,1),Atomics.add(this.#t,22,1))}}else this.#n.messagesSent++,this.#n.bytesSent+=t,e&&e in this.#n&&(this.#n[e]++,this.#n.bypassed++)}getAndResetMetrics(){let t={...this.#n};return this.#n={messagesSent:0,bytesSent:0,nonBundle:0,immediate:0,nearFuture:0,late:0,bypassed:0},t}getMetrics(){return this.#s==="sab"&&this.#t?{messagesSent:Atomics.load(this.#t,24),bytesSent:Atomics.load(this.#t,25),nonBundle:Atomics.load(this.#t,38),immediate:Atomics.load(this.#t,39),nearFuture:Atomics.load(this.#t,40),late:Atomics.load(this.#t,41),bypassed:Atomics.load(this.#t,22)}:{...this.#n}}#C(t,e=null,r=!0){if(this.#s==="postMessage")return this.#r?(this.#r.postMessage({type:"osc",oscData:t,bypassCategory:e,sourceId:this.#E}),!0):!1;{let n=this.#f,o=N({atomicView:this.#u.atomicView,dataView:this.#u.dataView,uint8View:this.#u.uint8View,bufferConstants:this.#o.bufferConstants,ringBufferBase:this.#o.ringBufferBase,controlIndices:this.#o.controlIndices,oscMessage:t,sourceId:this.#E,maxSpins:n?10:0,useWait:n});return!o&&!n&&r&&this.#e?(this.#t&&Atomics.add(this.#t,45,1),this.#e.postMessage({type:"directDispatch",oscData:t,sourceId:this.#E}),!0):o}}#N(t){return this.#e?(this.#e.postMessage({type:"osc",oscData:t,sourceId:this.#E}),!0):(console.error("[OscChannel] No prescheduler port, sending direct"),this.#C(t))}send(t){let e=this.classify(t);if(L(e)){let r=this.#C(t,e);return r&&this.#U(t.length,e),r}else{let r=this.#N(t);return r&&this.#U(t.length,null),r}}sendDirect(t){return this.#C(t)}sendToPrescheduler(t){return this.#N(t)}nextNodeId(){if(this.#l)return Atomics.add(this.#l,0,1);this.#_>=this.#T&&this.#R();let t=this.#_++;return this.#S&&!this.#i&&this.#T-this.#_<=this.#a>>>1&&this.#h(),t}#R(){if(this.#c){let t=this.#c(this.#a);this.#p=t.from,this.#T=t.to,this.#_=t.from}else this.#i?(this.#p=this.#i.from,this.#T=this.#i.to,this.#_=this.#i.from,this.#i=null,this.#h()):this.#S&&(console.warn("[OscChannel] nextNodeId() range exhausted before async refill arrived. IDs may not be unique. Yield to the event loop between large batches of nextNodeId() calls."),this.#h())}#h(){this.#S&&this.#S.postMessage({type:"requestNodeIdRange"})}set getCurrentNTP(t){this.#d=t}get mode(){return this.#s}get transferable(){let t={mode:this.#s,preschedulerPort:this.#e,bypassLookaheadS:this.#I,sourceId:this.#E,blocking:this.#f};if(this.#s==="postMessage"){let e=this.#a*10,r,n;if(this.#c){let o=this.#c(e);r={from:o.from,to:o.to};let E=new MessageChannel,c=this.#c,i=this.#a;E.port1.onmessage=S=>{if(S.data.type==="requestNodeIdRange"){let a=c(i);E.port1.postMessage({type:"nodeIdRange",from:a.from,to:a.to})}},n=E.port2,this.#A=n}return{...t,port:this.#r,nodeIdRange:r,nodeIdPort:n}}else return{...t,sharedBuffer:this.#o.sharedBuffer,ringBufferBase:this.#o.ringBufferBase,bufferConstants:this.#o.bufferConstants,controlIndices:this.#o.controlIndices}}get transferList(){let t=[];return this.#s==="postMessage"&&this.#r&&t.push(this.#r),this.#e&&t.push(this.#e),this.#A&&(t.push(this.#A),this.#A=null),t}close(){this.#s==="postMessage"&&this.#r&&(this.#r.close(),this.#r=null),this.#e&&(this.#e.close(),this.#e=null)}static createPostMessage(t){return t instanceof MessagePort?new s("postMessage",{port:t}):new s("postMessage",t)}static createSAB(t){let e=t.controlIndices;return e||(e=B(t.ringBufferBase,t.bufferConstants.CONTROL_START)),new s("sab",{sharedBuffer:t.sharedBuffer,ringBufferBase:t.ringBufferBase,bufferConstants:t.bufferConstants,controlIndices:e,preschedulerPort:t.preschedulerPort,bypassLookaheadS:t.bypassLookaheadS,sourceId:t.sourceId,blocking:t.blocking})}static fromTransferable(t){return t.mode==="postMessage"?new s("postMessage",{port:t.port,preschedulerPort:t.preschedulerPort,bypassLookaheadS:t.bypassLookaheadS,sourceId:t.sourceId,blocking:t.blocking,nodeIdRange:t.nodeIdRange,nodeIdPort:t.nodeIdPort}):new s("sab",{sharedBuffer:t.sharedBuffer,ringBufferBase:t.ringBufferBase,bufferConstants:t.bufferConstants,controlIndices:t.controlIndices,preschedulerPort:t.preschedulerPort,bypassLookaheadS:t.bypassLookaheadS,sourceId:t.sourceId,blocking:t.blocking})}};export{R as OscChannel};
|
|
1
|
+
function Y(r,t,e){return(e-1-r+t)%e}function G({uint8View:r,dataView:t,bufferStart:e,bufferSize:s,head:o,payload:n,sequence:_,messageMagic:E,headerSize:c,sourceId:u=0,headerScratch:a=null,headerScratchView:S=null}){let i=n.length,d=c+i+3&-4,f=s-o;if(d>f){let l=a||new Uint8Array(c),p=S||new DataView(l.buffer);p.setUint32(0,E,!0),p.setUint32(4,d,!0),p.setUint32(8,_,!0),p.setUint32(12,u,!0);let T=e+o,I=e;if(f>=c){r.set(l,T);let R=f-c;for(let h=0;h<R;h++)r[T+c+h]=n[h];for(let h=R;h<i;h++)r[I+h-R]=n[h]}else{for(let h=0;h<f;h++)r[T+h]=l[h];for(let h=f;h<c;h++)r[I+h-f]=l[h];let R=c-f;r.set(n,I+R)}}else{let l=e+o;t.setUint32(l,E,!0),t.setUint32(l+4,d,!0),t.setUint32(l+8,_,!0),t.setUint32(l+12,u,!0),r.set(n,l+c)}return(o+d)%s}function U({uint8View:r,dataView:t,bufferStart:e,bufferSize:s,head:o,tail:n,messageMagic:_,paddingMagic:E,headerSize:c,maxMessages:u=1/0,onMessage:a,onCorruption:S}){let i=n,A=0,d=f=>{let l=f;if(l+4<=s)return t.getUint32(e+l,!0);let p=0;for(let T=0;T<4;T++)p|=r[e+(l+T)%s]<<T*8;return p};for(;i!==o&&A<u;){let f=s-i,l;if(f>=4?l=t.getUint32(e+i,!0):l=d(i),l===E){i=0;continue}if(l!==_){S&&S(i),i=(i+1)%s;continue}let p=d((i+4)%s),T=d((i+8)%s),I=d((i+12)%s);if(p<c||p>s){S&&S(i),i=(i+1)%s;continue}let R=p-c,h=e+(i+c)%s;a(h,R,T,I),i=(i+p)%s,A++}return{newTail:i,messagesRead:A}}function w(r,t,e=0,s=!1){for(let o=0;o<=e;o++)if(Atomics.compareExchange(r,t,0,1)===0)return!0;if(s){for(let n=0;n<100;n++)if(Atomics.wait(r,t,1,100),Atomics.compareExchange(r,t,0,1)===0)return!0;return console.error("[RingBuffer] Lock acquisition timeout after 10s - possible deadlock"),!1}return!1}function k(r,t){Atomics.store(r,t,0),Atomics.notify(r,t,1)}function P({atomicView:r,dataView:t,uint8View:e,bufferConstants:s,ringBufferBase:o,controlIndices:n,oscMessage:_,sourceId:E=0,maxSpins:c=0,useWait:u=!1,headerScratch:a=null,headerScratchView:S=null}){let i=_.length,A=s.MESSAGE_HEADER_SIZE+i;if(A>s.IN_BUFFER_SIZE-s.MESSAGE_HEADER_SIZE||!w(r,n.IN_WRITE_LOCK,c,u))return!1;try{let d=Atomics.load(r,n.IN_HEAD),f=Atomics.load(r,n.IN_TAIL),l=A+3&-4;if(Y(d,f,s.IN_BUFFER_SIZE)<l)return!1;let T=Atomics.add(r,n.IN_SEQUENCE,1),I=G({uint8View:e,dataView:t,bufferStart:o+s.IN_BUFFER_START,bufferSize:s.IN_BUFFER_SIZE,head:d,payload:_,sequence:T,messageMagic:s.MESSAGE_MAGIC,headerSize:s.MESSAGE_HEADER_SIZE,sourceId:E,headerScratch:a,headerScratchView:S});return Atomics.load(r,n.IN_HEAD),Atomics.store(r,n.IN_HEAD,I),Atomics.notify(r,n.IN_HEAD,1),!0}finally{k(r,n.IN_WRITE_LOCK)}}function B(r,t,e,s){let o=r+t+s*e;return{controlBase:o,headIndex:(o+0)/4,tailIndex:(o+4)/4,activeIndex:(o+8)/4,dropsIndex:(o+12)/4}}function O(r,t){let e=r+t;return{IN_HEAD:(e+0)/4,IN_TAIL:(e+4)/4,IN_SEQUENCE:(e+24)/4,IN_WRITE_LOCK:(e+40)/4,IN_LOG_TAIL:(e+44)/4}}function W(r){return r.length>=8&&r[0]===35&&r[1]===98&&r[2]===117&&r[3]===110&&r[4]===100&&r[5]===108&&r[6]===101&&r[7]===0}var C=.5;function V(r){if(r.length<16)return null;let t=new DataView(r.buffer,r.byteOffset,r.byteLength);return{ntpSeconds:t.getUint32(8,!1),ntpFraction:t.getUint32(12,!1)}}function L(){return(performance.timeOrigin+performance.now())/1e3+2208988800}function x(r,t={}){let{getCurrentNTP:e=L,bypassLookaheadS:s=C}=t;if(!W(r))return"nonBundle";let o=V(r);if(!o)return"nonBundle";let{ntpSeconds:n,ntpFraction:_}=o;if(n===0&&_<=1)return"immediate";let E=e();if(E===null||E===0)return"immediate";let u=n+_/4294967296-E,a=128/48e3;return u<-a?"late":u<s?"nearFuture":"farFuture"}function D(r){return r!=="farFuture"}var K={nonBundle:38,immediate:39,nearFuture:40,late:41},N=class r{#e;#o;#s;#r;#n;#t;#L;#i;#N;#U;#P;#B;#p;#l;#T;#h;#u;#c;#R;#C;#_=-1;#A;#I;#S;#f;#a;#O=[];#x;#d;#E={messagesSent:0,bytesSent:0,nonBundle:0,immediate:0,nearFuture:0,late:0,bypassed:0};constructor(t,e){if(this.#e=t,this.#n=e.preschedulerPort||null,this.#L=e.bypassLookaheadS??C,this.#i=e.sourceId??0,this.#N=e.blocking??this.#i!==0,this.#U=e.getCurrentNTP??L,this.#T=1e3,t==="postMessage")this.#o=e.port;else{if(this.#s={sharedBuffer:e.sharedBuffer,ringBufferBase:e.ringBufferBase,bufferConstants:e.bufferConstants,controlIndices:e.controlIndices},this.#F(),this.#I=e.replyNotifier||null,e.sharedBuffer&&e.bufferConstants){let s=e.ringBufferBase+e.bufferConstants.METRICS_START;this.#t=new Int32Array(e.sharedBuffer,s,e.bufferConstants.METRICS_SIZE/4)}if(e.sharedBuffer&&e.bufferConstants?.NODE_ID_COUNTER_START!==void 0){let s=e.ringBufferBase+e.bufferConstants.NODE_ID_COUNTER_START;this.#P=new Int32Array(e.sharedBuffer,s,1),this.#m()}}e.nodeIdSource&&(this.#h=e.nodeIdSource,this.#m()),e.nodeIdRange&&(this.#B=e.nodeIdRange.from,this.#p=e.nodeIdRange.to,this.#l=e.nodeIdRange.from),e.replyWorkletPort&&(this.#x=e.replyWorkletPort),e.nodeIdPort&&(this.#u=e.nodeIdPort,this.#u.onmessage=s=>{s.data.type==="nodeIdRange"&&(this.#c={from:s.data.from,to:s.data.to})},this.#M())}#F(){let t=this.#s.sharedBuffer,e=this.#s.bufferConstants.MESSAGE_HEADER_SIZE||16,s=new Uint8Array(e);this.#r={atomicView:new Int32Array(t),dataView:new DataView(t),uint8View:new Uint8Array(t),headerScratch:s,headerScratchView:new DataView(s.buffer)}}classify(t){return x(t,{getCurrentNTP:this.#U,bypassLookaheadS:this.#L})}#H(t,e=null){if(this.#e==="sab"&&this.#t){if(Atomics.add(this.#t,24,1),Atomics.add(this.#t,25,t),e){let s=K[e];s!==void 0&&(Atomics.add(this.#t,s,1),Atomics.add(this.#t,22,1))}}else this.#E.messagesSent++,this.#E.bytesSent+=t,e&&e in this.#E&&(this.#E[e]++,this.#E.bypassed++)}getAndResetMetrics(){let t={...this.#E};return this.#E={messagesSent:0,bytesSent:0,nonBundle:0,immediate:0,nearFuture:0,late:0,bypassed:0},t}getMetrics(){return this.#e==="sab"&&this.#t?{messagesSent:Atomics.load(this.#t,24),bytesSent:Atomics.load(this.#t,25),nonBundle:Atomics.load(this.#t,38),immediate:Atomics.load(this.#t,39),nearFuture:Atomics.load(this.#t,40),late:Atomics.load(this.#t,41),bypassed:Atomics.load(this.#t,22)}:{...this.#E}}#D(t,e=null,s=!0){if(this.#e==="postMessage")return this.#o?(this.#o.postMessage({type:"osc",oscData:t,bypassCategory:e,sourceId:this.#i}),!0):!1;{let o=this.#N,n=P({atomicView:this.#r.atomicView,dataView:this.#r.dataView,uint8View:this.#r.uint8View,bufferConstants:this.#s.bufferConstants,ringBufferBase:this.#s.ringBufferBase,controlIndices:this.#s.controlIndices,oscMessage:t,sourceId:this.#i,maxSpins:o?10:0,useWait:o,headerScratch:this.#r.headerScratch,headerScratchView:this.#r.headerScratchView});return!n&&!o&&s&&this.#n?(this.#t&&Atomics.add(this.#t,45,1),this.#n.postMessage({type:"directDispatch",oscData:t,sourceId:this.#i}),!0):n}}#y(t){return this.#n?(this.#n.postMessage({type:"osc",oscData:t,sourceId:this.#i}),!0):(console.error("[OscChannel] No prescheduler port, sending direct"),this.#D(t))}send(t){let e=this.classify(t);if(D(e)){let s=this.#D(t,e);return s&&this.#H(t.length,e),s}else{let s=this.#y(t);return s&&this.#H(t.length,null),s}}sendDirect(t){return this.#D(t)}sendToPrescheduler(t){return this.#y(t)}nextNodeId(){if(this.#P)return Atomics.add(this.#P,0,1);this.#l>=this.#p&&this.#m();let t=this.#l++;return this.#u&&!this.#c&&this.#p-this.#l<=this.#T>>>1&&this.#M(),t}#m(){if(this.#h){let t=this.#h(this.#T);this.#B=t.from,this.#p=t.to,this.#l=t.from}else if(this.#c)this.#B=this.#c.from,this.#p=this.#c.to,this.#l=this.#c.from,this.#c=null,this.#M();else if(this.#u)throw new Error("[OscChannel] Node ID range exhausted before async refill arrived. Yield to the event loop between large batches of nextNodeId() calls.")}#M(){this.#u&&this.#u.postMessage({type:"requestNodeIdRange"})}set getCurrentNTP(t){this.#U=t}get mode(){return this.#e}get replyDrops(){if(!(this.#e!=="sab"||this.#_<0))return Atomics.load(this.#r.atomicView,this.#d.dropsIndex)}get transferable(){let t={mode:this.#e,preschedulerPort:this.#n,bypassLookaheadS:this.#L,sourceId:this.#i,blocking:this.#N};if(this.#e==="postMessage"){let e=this.#T*10,s,o;if(this.#h){let n=this.#h(e);s={from:n.from,to:n.to};let _=new MessageChannel,E=this.#h,c=this.#T;_.port1.onmessage=u=>{if(u.data.type==="requestNodeIdRange"){let a=E(c);_.port1.postMessage({type:"nodeIdRange",from:a.from,to:a.to})}},o=_.port2,this.#R=o}return{...t,port:this.#o,nodeIdRange:s,nodeIdPort:o}}else return{...t,sharedBuffer:this.#s.sharedBuffer,ringBufferBase:this.#s.ringBufferBase,bufferConstants:this.#s.bufferConstants,controlIndices:this.#s.controlIndices}}get transferList(){let t=[];return this.#e==="postMessage"&&this.#o&&t.push(this.#o),this.#n&&t.push(this.#n),this.#R&&(t.push(this.#R),this.#R=null),t}activateReplies(){this.#e==="sab"?this.#g():this.#k()}deactivateReplies(){this.#e==="sab"?this.#Y():this.#X()}setReplyHandler(t){this.#C=t,this.activateReplies(),this.#b()}clearReplyHandler(){this.#C=null,this.deactivateReplies()}pollReplies(t){let e=t||this.#C;return e?this.#e==="sab"?this.#_<0?0:this.#w(e):this.#a?this.#W(e):0:0}#g(){if(this.#_>=0)return;let t=this.#s.bufferConstants;if(!t.REPLY_CHANNEL_COUNT)throw new Error("WASM does not support reply channels");let e=this.#r.atomicView;for(let s=0;s<t.REPLY_CHANNEL_COUNT;s++){let o=B(this.#s.ringBufferBase,t.REPLY_CHANNELS_CONTROL_START,t.REPLY_CHANNEL_CONTROL_SIZE,s);if(Atomics.store(e,o.headIndex,0),Atomics.store(e,o.tailIndex,0),Atomics.store(e,o.dropsIndex,0),Atomics.compareExchange(e,o.activeIndex,0,1)===0){this.#d=o,this.#_=s;return}}throw new Error("All "+t.REPLY_CHANNEL_COUNT+" reply channel slots are in use \u2014 cannot register for replies")}#b(){this.#S||this.#f||this.#A||typeof AudioWorkletGlobalScope<"u"||this.#e==="sab"&&(this.#I?(this.#S=()=>this.pollReplies(),this.#I.subscribe(this.#S)):typeof Atomics<"u"&&typeof Atomics.waitAsync=="function"?this.#G():this.#A=setInterval(()=>this.pollReplies(),5))}#Y(){this.#_<0||(Atomics.store(this.#r.atomicView,this.#d.activeIndex,0),this.#A&&(clearInterval(this.#A),this.#A=null),this.#S&&this.#I&&(this.#I.unsubscribe(this.#S),this.#S=null),this.#f=!1,this.#_=-1,this.#d=null)}#G(){let t=this.#r.atomicView,{headIndex:e,tailIndex:s}=this.#d;this.#f=!0;let o=()=>{if(!this.#f)return;let n=Atomics.load(t,e),_=Atomics.load(t,s);if(n!==_&&this.pollReplies(),!this.#f)return;let E=Atomics.load(t,e),c=Atomics.waitAsync(t,e,E);c.async?c.value.then(()=>{this.#f&&(this.pollReplies(),o())}):(this.pollReplies(),setTimeout(o,0))};o()}#w(t){let e=this.#r.atomicView,{headIndex:s,tailIndex:o}=this.#d,n=Atomics.load(e,s),_=Atomics.load(e,o);if(n===_)return 0;let E=this.#s.bufferConstants,c=this.#s.ringBufferBase+E.REPLY_CHANNELS_BUFFER_START+this.#_*E.REPLY_CHANNEL_BUFFER_SIZE,u=E.REPLY_CHANNEL_BUFFER_SIZE,a=this.#r.uint8View,{newTail:S,messagesRead:i}=U({uint8View:a,dataView:this.#r.dataView,bufferStart:c,bufferSize:u,head:n,tail:_,messageMagic:E.MESSAGE_MAGIC,paddingMagic:E.PADDING_MAGIC,headerSize:E.MESSAGE_HEADER_SIZE||16,maxMessages:64,onMessage:(A,d,f)=>{t(a,A,d,f)}});return i>0&&Atomics.store(e,o,S),i}#k(){if(this.#a)return;let t=this.#x||this.#o;if(!t)throw new Error("No port available for reply registration");let e=new MessageChannel;t.postMessage({type:"addReplyPort",sourceId:this.#i},[e.port1]),this.#a=e.port2;let s=typeof AudioWorkletGlobalScope<"u";this.#a.onmessage=o=>{if(o.data.type!=="oscReplies"||!o.data.count)return;let{count:n,buffer:_,messages:E}=o.data;if(!E||!_)return;let c=new Uint8Array(_);if(s)for(let u=0;u<n;u++){let a=E[u];if(!a)continue;let S=c.slice(a.offset,a.offset+a.length);this.#O.push({oscData:S,sequence:a.sequence})}else{let u=this.#C;if(!u)return;for(let a=0;a<n;a++){let S=E[a];if(!S)continue;let i=c.slice(S.offset,S.offset+S.length);u(i,S.sequence)}}}}#X(){if(this.#a){let t=this.#x||this.#o;t&&t.postMessage({type:"removeReplyPort",sourceId:this.#i}),this.#a.close(),this.#a=null}this.#O.length=0}#W(t){let e=this.#O,s=e.length;if(s===0)return 0;for(let o=0;o<s;o++)t(e[o].oscData,e[o].sequence);return e.length=0,s}close(){this.clearReplyHandler(),this.#e==="postMessage"&&this.#o&&(this.#o.close(),this.#o=null),this.#n&&(this.#n.close(),this.#n=null)}static createPostMessage(t){return t instanceof MessagePort?new r("postMessage",{port:t}):new r("postMessage",t)}static createSAB(t){let e=t.controlIndices;return e||(e=O(t.ringBufferBase,t.bufferConstants.CONTROL_START)),new r("sab",{sharedBuffer:t.sharedBuffer,ringBufferBase:t.ringBufferBase,bufferConstants:t.bufferConstants,controlIndices:e,preschedulerPort:t.preschedulerPort,bypassLookaheadS:t.bypassLookaheadS,sourceId:t.sourceId,blocking:t.blocking,replyNotifier:t.replyNotifier})}static fromTransferable(t){return t.mode==="postMessage"?new r("postMessage",{port:t.port,preschedulerPort:t.preschedulerPort,bypassLookaheadS:t.bypassLookaheadS,sourceId:t.sourceId,blocking:t.blocking,nodeIdRange:t.nodeIdRange,nodeIdPort:t.nodeIdPort}):new r("sab",{sharedBuffer:t.sharedBuffer,ringBufferBase:t.ringBufferBase,bufferConstants:t.bufferConstants,controlIndices:t.controlIndices,preschedulerPort:t.preschedulerPort,bypassLookaheadS:t.bypassLookaheadS,sourceId:t.sourceId,blocking:t.blocking})}};export{N as OscChannel};
|
package/dist/osc_fast.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var I=new Uint8Array(2097152),P=new DataView(I.buffer),f=I,c=P,y=new Map,L=1e3,Q=new TextDecoder,p=new TextEncoder,x=[null,0],ee=2208988800,b=4294967296,v=new Uint8Array([35,98,117,110,100,108,101,0]),N=44,d=105,k=102,E=115,_=98,S=84,B=70,R=104,M=100,V=116,$=117,H=91,Z=93;function h(e,t,n=0){let i=e.length+4;i+=5;for(let r=n;r<t.length;r++)i+=W(t[r]);return i}function W(e){if(e instanceof Uint8Array)return 5+e.length+3;if(e instanceof ArrayBuffer)return 5+e.byteLength+3;if(typeof e=="string")return 1+e.length*3+4;if(Array.isArray(e)){let t=2;for(let n=0;n<e.length;n++)t+=W(e[n]);return t}if(e&&e.type==="string")return 1+e.value.length*3+4;if(e&&e.type==="blob"){let t=e.value;return 5+(t instanceof Uint8Array?t.length:t.byteLength)+3}return e&&e.type==="uuid"?17:9}function Y(e){let t=16;for(let n of e)t+=4,Array.isArray(n)?t+=h(n[0],n,1):n.packets!==void 0?t+=Y(n.packets):t+=h(n.address,n.args||[]);return t}function O(e){if(e<=2097152){f=I,c=P;return}f=new Uint8Array(e),c=new DataView(f.buffer)}function ce(e,t=[]){let n=h(e,t);O(n);let i=0;i=m(e,i),i=C(t,i);for(let r=0;r<t.length;r++)i=a(t[r],i);return f.subarray(0,i)}function le(e,t){let n=Y(t);O(n);let i=0;f.set(v,i),i+=8,i=w(e,i);for(let r=0;r<t.length;r++){let s=t[r],o=i;i+=4;let u=i;Array.isArray(s)?i=A(s[0],s,i,1):s.packets!==void 0?i=X(s.timeTag,s.packets,i):i=A(s.address,s.args||[],i);let l=i-u;c.setUint32(o,l,!1)}return f.subarray(0,i)}function se(e,t,n=[]){let i=20+h(t,n);O(i);let r=0;f.set(v,r),r+=8,r=w(e,r);let s=r;r+=4;let o=r;r=m(t,r),r=C(n,r);for(let u=0;u<n.length;u++)r=a(n[u],r);return c.setUint32(s,r-o,!1),f.subarray(0,r)}function A(e,t,n,i=0){n=m(e,n),n=C(t,n,i);for(let r=i;r<t.length;r++)n=a(t[r],n);return n}function X(e,t,n){f.set(v,n),n+=8,n=w(e,n);for(let i=0;i<t.length;i++){let r=t[i],s=n;n+=4;let o=n;Array.isArray(r)?n=A(r[0],r,n,1):r.packets!==void 0?n=X(r.timeTag,r.packets,n):n=A(r.address,r.args||[],n),c.setUint32(s,n-o,!1)}return n}function m(e,t){let n=y.get(e);if(n)return f.set(n,t),t+n.length;let i=t;if(t=F(e,t),y.size<L){let r=f.slice(i,t);y.set(e,r)}return t}function F(e,t){let n=!1;for(let i=0;i<e.length;i++)if(e.charCodeAt(i)>=128){n=!0;break}if(n){let i=p.encodeInto(e,f.subarray(t));t+=i.written}else for(let i=0;i<e.length;i++)f[t++]=e.charCodeAt(i);for(f[t++]=0;t&3;)f[t++]=0;return t}function C(e,t,n=0){f[t++]=N;for(let i=n;i<e.length;i++)t=g(e[i],t,i);for(f[t++]=0;t&3;)f[t++]=0;return t}function g(e,t,n){let i=typeof e;if(i==="number")f[t++]=Number.isInteger(e)?d:k;else if(i==="string")f[t++]=E;else if(i==="boolean")f[t++]=e?S:B;else if(e instanceof Uint8Array||e instanceof ArrayBuffer)f[t++]=_;else if(Array.isArray(e)){f[t++]=H;for(let r=0;r<e.length;r++)t=g(e[r],t,n);f[t++]=Z}else if(e&&e.type==="int")f[t++]=d;else if(e&&e.type==="float")f[t++]=k;else if(e&&e.type==="string")f[t++]=E;else if(e&&e.type==="blob")f[t++]=_;else if(e&&e.type==="bool")f[t++]=e.value?S:B;else if(e&&e.type==="int64")f[t++]=R;else if(e&&e.type==="double")f[t++]=M;else if(e&&e.type==="timetag")f[t++]=V;else if(e&&e.type==="uuid")f[t++]=$;else throw e==null?new Error(`OSC argument at index ${n} is ${e}`):new Error(`Unknown OSC argument type at index ${n}: ${i}`);return t}function a(e,t){let n=typeof e;if(n==="number")return Number.isInteger(e)?(c.setInt32(t,e,!1),t+4):(c.setFloat32(t,e,!1),t+4);if(n==="string")return F(e,t);if(n==="boolean")return t;if(e instanceof Uint8Array){let i=e.length;for(c.setUint32(t,i,!1),t+=4,f.set(e,t),t+=i;t&3;)f[t++]=0;return t}if(e instanceof ArrayBuffer)return a(new Uint8Array(e),t);if(Array.isArray(e)){for(let i=0;i<e.length;i++)t=a(e[i],t);return t}if(e&&e.type==="int")return c.setInt32(t,e.value,!1),t+4;if(e&&e.type==="float")return c.setFloat32(t,e.value,!1),t+4;if(e&&e.type==="string")return F(e.value,t);if(e&&e.type==="blob"){let i=e.value instanceof Uint8Array?e.value:new Uint8Array(e.value),r=i.length;for(c.setUint32(t,r,!1),t+=4,f.set(i,t),t+=r;t&3;)f[t++]=0;return t}return e&&e.type==="bool"?t:e&&e.type==="int64"?(c.setBigInt64(t,BigInt(e.value),!1),t+8):e&&e.type==="double"?(c.setFloat64(t,e.value,!1),t+8):e&&e.type==="timetag"?w(e.value,t):e&&e.type==="uuid"?(f.set(e.value,t),t+16):t}function w(e,t){if(e===1||e===null||e===void 0)return c.setUint32(t,0,!1),c.setUint32(t+4,1,!1),t+8;if(Array.isArray(e)){if(e.length!==2)throw new Error(`TimeTag array must have exactly 2 elements [seconds, fraction], got ${e.length}`);return c.setUint32(t,e[0]>>>0,!1),c.setUint32(t+4,e[1]>>>0,!1),t+8}if(typeof e!="number")throw new TypeError(`TimeTag must be a number, array, null, or undefined, got ${typeof e}`);e>1&&e<ee&&console.warn(`TimeTag ${e} looks like a Unix timestamp (< NTP_EPOCH_OFFSET). Did you mean to add NTP_EPOCH_OFFSET (2208988800)?`);let n=e>>>0,i=(e-Math.floor(e))*b>>>0;return c.setUint32(t,n,!1),c.setUint32(t+4,i,!1),t+8}function te(e){return e instanceof Uint8Array||(e=new Uint8Array(e)),e[0]===35&&e[1]===98?ie(e):ne(e)}function ne(e){e instanceof Uint8Array||(e=new Uint8Array(e));let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=0,[i,r]=U(e,n);if(n=r,n>=e.length||e[n]!==N)return[i];let[s,o]=U(e,n);n=o;let u=[i],l=u,z=[];for(let T=1;T<s.length;T++)switch(s.charCodeAt(T)){case d:l.push(t.getInt32(n,!1)),n+=4;break;case k:l.push(t.getFloat32(n,!1)),n+=4;break;case E:let[j,q]=U(e,n);l.push(j),n=q;break;case _:let G=t.getUint32(n,!1);n+=4,l.push(e.slice(n,n+G)),n+=G,n=n+3&-4;break;case R:l.push(t.getBigInt64(n,!1)),n+=8;break;case M:l.push(t.getFloat64(n,!1)),n+=8;break;case S:l.push(!0);break;case B:l.push(!1);break;case V:let J=t.getUint32(n,!1),K=t.getUint32(n+4,!1);l.push(J+K/b),n+=8;break;case $:l.push({type:"uuid",value:e.slice(n,n+16)}),n+=16;break;case H:{let D=[];l.push(D),z.push(l),l=D;break}case Z:l=z.pop()||u;break}return u}function ie(e){e instanceof Uint8Array||(e=new Uint8Array(e));let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=8,i=t.getUint32(n,!1),r=t.getUint32(n+4,!1),s=i+r/b;n+=8;let o=[];for(;n<e.length;){let u=t.getUint32(n,!1);if(n+=4,u>0&&n+u<=e.length){let l=e.subarray(n,n+u);o.push(te(l))}n+=u}return{timeTag:s,packets:o}}function U(e,t){let n=t;for(;n<e.length&&e[n]!==0;)n++;let i=Q.decode(e.subarray(t,n));return n++,n=n+3&-4,x[0]=i,x[1]=n,x}function ue(e){return e.slice()}function oe(){y.clear()}function ye(){return{stringCacheSize:y.size,maxSize:L}}function re(e){return!e||e.length<8?!1:e[0]===35&&e[1]===98}function ae(e){if(!re(e))return null;let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint32(8,!1),i=t.getUint32(12,!1);return n+i/b}export{ee as NTP_EPOCH_OFFSET,b as TWO_POW_32,oe as clearCache,ue as copyEncoded,ie as decodeBundle,ne as decodeMessage,te as decodePacket,le as encodeBundle,ce as encodeMessage,se as encodeSingleBundle,ae as getBundleTimeTag,ye as getCacheStats,re as isBundle};
|