supersonic-scsynth 0.65.0 → 0.67.2

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.
@@ -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};