supersonic-scsynth 0.69.0 → 0.70.0

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 h(n){if(!Number.isFinite(n)||n<=0)return"0 B";let c=["B","KB","MB","GB"],s=0;for(;n>=1024&&s<c.length-1;)n/=1024,s++;return`${n.toFixed(s>0?1:0)} ${c[s]}`}function E(n){return n===4294967295?"-":String(n)}function k(n,c){return c[n]??String(n)}function v(n){return String(n|0)}function u(n){return(n/1e3).toFixed(1)}function C(n,c,s){if(n.format==="bytes")return e=>h(e);if(n.format==="headroom")return e=>E(e);if(n.format==="signed")return e=>v(e);if(n.format==="percent")return e=>String(e);if(n.format==="latencyUs")return e=>u(e);if(n.format==="enum"&&c?.values){let e=c.values;return i=>k(i,e)}let t=s.hasPlaybackStats?.offset;return n.format==="chromeOnly"?(e,i)=>t!==void 0&&i&&!i[t]?"-":String(e):n.format==="chromeLatencyUs"?(e,i)=>t!==void 0&&i&&!i[t]?"-":u(e):e=>String(e)}var m=class extends HTMLElement{#e=null;#t=null;#s=[];#n=[];#a=!1;buildFromSchema(c){let s=c.getMetricsSchema(),t=s.metrics,e=s.layout;this.innerHTML="",this.#s=[],this.#n=[];for(let i of e.panels){let o=document.createElement("div");o.className="ssm-panel"+(i.class?` ssm-panel--${i.class}`:"");let a=document.createElement("div");a.className="ssm-title",a.textContent=i.title,o.appendChild(a);for(let l of i.rows)l.type==="bar"?this.#i(o,l,t):this.#l(o,l,t);this.appendChild(o)}this.#a=!0}connect(c,s={}){this.disconnect(),this.#e=c,this.#a||this.buildFromSchema(c.constructor);let t=s.refreshRate??10;this.#t=setInterval(()=>this.#c(),1e3/t)}disconnect(){this.#t!==null&&(clearInterval(this.#t),this.#t=null),this.#e=null}disconnectedCallback(){this.disconnect()}#l(c,s,t){let e=document.createElement("div");if(e.className="ssm-row",s.tooltip)e.title=s.tooltip;else{let a=s.cells?.find(l=>l.key);if(a){let l=t[a.key];l?.description&&(e.title=l.description)}}let i=document.createElement("span");i.className="ssm-label",i.textContent=s.label,e.appendChild(i);let o=document.createElement("span");for(let a of s.cells||[])if(a.sep!==void 0){let l=document.createElement("span");l.className="ssm-sep",l.textContent=a.sep,o.appendChild(l)}else if(a.text!==void 0){let l=document.createElement("span");l.className="ssm-value",a.kind&&l.setAttribute("data-kind",a.kind),l.textContent=a.text,o.appendChild(l)}else if(a.key){let l=t[a.key];if(!l)continue;let r=document.createElement("span");r.className="ssm-value",a.kind&&r.setAttribute("data-kind",a.kind),r.textContent="-",o.appendChild(r),this.#s.push({offset:l.offset,format:C(a,l,t),el:r,prev:-1})}e.appendChild(o),c.appendChild(e)}#i(c,s,t){let e=document.createElement("div");e.className="ssm-bar";let i=t[s.usedKey],o=t[s.peakKey],a=t[s.capacityKey];i?.description&&(e.title=i.description);let l=document.createElement("span");l.className="ssm-bar-label",l.textContent=s.label,e.appendChild(l);let r=document.createElement("div");r.className="ssm-bar-track";let f=document.createElement("div");f.className=`ssm-bar-fill ssm-bar-fill--${s.color}`,r.appendChild(f);let p=document.createElement("div");p.className=`ssm-bar-peak ssm-bar-peak--${s.color}`,r.appendChild(p),e.appendChild(r);let d=document.createElement("span");d.className="ssm-bar-value ssm-value",s.color==="green"?d.setAttribute("data-kind","green"):s.color==="purple"&&d.setAttribute("data-kind","purple"),d.textContent="-",e.appendChild(d),c.appendChild(e),this.#n.push({usedOffset:i?.offset??0,peakOffset:o?.offset??0,capacityOffset:a?.offset??0,fillEl:f,peakEl:p,labelEl:d,prevUsed:-1,prevPeak:-1,prevCap:-1})}#c(){if(!this.#e)return;let c=this.#e.getMetricsArray();for(let s=0;s<this.#s.length;s++){let t=this.#s[s],e=c[t.offset];e!==t.prev&&(t.el.textContent=t.format(e,c),t.prev=e)}for(let s=0;s<this.#n.length;s++){let t=this.#n[s],e=c[t.usedOffset],i=c[t.peakOffset],o=c[t.capacityOffset];if(e!==t.prevUsed||o!==t.prevCap){if(o>0){let a=e/o*100;t.fillEl.style.width=a+"%",t.labelEl.textContent=a.toFixed(1)+"%"}else t.fillEl.style.width="0%",t.labelEl.textContent="N/A";t.prevUsed=e,t.prevCap=o}if(i!==t.prevPeak&&o>0){let a=i/o*100;t.peakEl.style.left=`calc(${a}% - 1px)`,t.prevPeak=i}}}};typeof customElements<"u"&&!customElements.get("supersonic-metrics")&&customElements.define("supersonic-metrics",m);export{m as SupersonicMetrics};
1
+ function h(s){if(!Number.isFinite(s)||s<=0)return"0 B";let o=["B","KB","MB","GB"],n=0;for(;s>=1024&&n<o.length-1;)s/=1024,n++;return`${s.toFixed(n>0?1:0)} ${o[n]}`}function E(s){return s===4294967295?"-":String(s)}function C(s,o){return o[s]??String(s)}function k(s){return String(s|0)}function u(s){return(s/1e3).toFixed(1)}function v(s){return(s/1e3).toFixed(1)}function b(s){return(s/100).toFixed(2)}function x(s,o,n){if(s.format==="bytes")return e=>h(e);if(s.format==="headroom")return e=>E(e);if(s.format==="signed")return e=>k(e);if(s.format==="percent")return e=>String(e);if(s.format==="latencyUs")return e=>u(e);if(s.format==="milliBpm")return e=>v(e);if(s.format==="centi")return e=>b(e);if(s.format==="enum"&&o?.values){let e=o.values;return l=>C(l,e)}let t=n.hasPlaybackStats?.offset;return s.format==="chromeOnly"?(e,l)=>t!==void 0&&l&&!l[t]?"-":String(e):s.format==="chromeLatencyUs"?(e,l)=>t!==void 0&&l&&!l[t]?"-":u(e):e=>String(e)}var p=class extends HTMLElement{#e=null;#t=null;#n=[];#s=[];#a=!1;buildFromSchema(o){let n=o.getMetricsSchema(),t=n.metrics,e=n.layout;this.innerHTML="",this.#n=[],this.#s=[];for(let l of e.panels){let r=document.createElement("div");r.className="ssm-panel"+(l.class?` ssm-panel--${l.class}`:"");let a=document.createElement("div");a.className="ssm-title",a.textContent=l.title,r.appendChild(a);for(let i of l.rows)i.type==="bar"?this.#l(r,i,t):this.#i(r,i,t);this.appendChild(r)}this.#a=!0}connect(o,n={}){this.disconnect(),this.#e=o,this.#a||this.buildFromSchema(o.constructor);let t=n.refreshRate??10;this.#t=setInterval(()=>this.#o(),1e3/t)}disconnect(){this.#t!==null&&(clearInterval(this.#t),this.#t=null),this.#e=null}disconnectedCallback(){this.disconnect()}#i(o,n,t){let e=document.createElement("div");if(e.className="ssm-row",n.tooltip)e.title=n.tooltip;else{let a=n.cells?.find(i=>i.key);if(a){let i=t[a.key];i?.description&&(e.title=i.description)}}let l=document.createElement("span");l.className="ssm-label",l.textContent=n.label,e.appendChild(l);let r=document.createElement("span");for(let a of n.cells||[])if(a.sep!==void 0){let i=document.createElement("span");i.className="ssm-sep",i.textContent=a.sep,r.appendChild(i)}else if(a.text!==void 0){let i=document.createElement("span");i.className="ssm-value",a.kind&&i.setAttribute("data-kind",a.kind),i.textContent=a.text,r.appendChild(i)}else if(a.key){let i=t[a.key];if(!i)continue;let c=document.createElement("span");c.className="ssm-value",a.kind&&c.setAttribute("data-kind",a.kind),c.textContent="-",r.appendChild(c),this.#n.push({offset:i.offset,format:x(a,i,t),el:c,prev:-1})}e.appendChild(r),o.appendChild(e)}#l(o,n,t){let e=document.createElement("div");e.className="ssm-bar";let l=t[n.usedKey],r=t[n.peakKey],a=t[n.capacityKey];l?.description&&(e.title=l.description);let i=document.createElement("span");i.className="ssm-bar-label",i.textContent=n.label,e.appendChild(i);let c=document.createElement("div");c.className="ssm-bar-track";let d=document.createElement("div");d.className=`ssm-bar-fill ssm-bar-fill--${n.color}`,c.appendChild(d);let m=document.createElement("div");m.className=`ssm-bar-peak ssm-bar-peak--${n.color}`,c.appendChild(m),e.appendChild(c);let f=document.createElement("span");f.className="ssm-bar-value ssm-value",n.color==="green"?f.setAttribute("data-kind","green"):n.color==="purple"&&f.setAttribute("data-kind","purple"),f.textContent="-",e.appendChild(f),o.appendChild(e),this.#s.push({usedOffset:l?.offset??0,peakOffset:r?.offset??0,capacityOffset:a?.offset??0,fillEl:d,peakEl:m,labelEl:f,prevUsed:-1,prevPeak:-1,prevCap:-1})}#o(){if(!this.#e)return;let o=this.#e.getMetricsArray();for(let n=0;n<this.#n.length;n++){let t=this.#n[n],e=o[t.offset];e!==t.prev&&(t.el.textContent=t.format(e,o),t.prev=e)}for(let n=0;n<this.#s.length;n++){let t=this.#s[n],e=o[t.usedOffset],l=o[t.peakOffset],r=o[t.capacityOffset];if(e!==t.prevUsed||r!==t.prevCap){if(r>0){let a=e/r*100;t.fillEl.style.width=a+"%",t.labelEl.textContent=a.toFixed(1)+"%"}else t.fillEl.style.width="0%",t.labelEl.textContent="N/A";t.prevUsed=e,t.prevCap=r}if(l!==t.prevPeak&&r>0){let a=l/r*100;t.peakEl.style.left=`calc(${a}% - 1px)`,t.prevPeak=l}}}};typeof customElements<"u"&&!customElements.get("supersonic-metrics")&&customElements.define("supersonic-metrics",p);export{p as SupersonicMetrics};
@@ -1 +1 @@
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:S=0,headerScratch:a=null,headerScratchView:u=null}){let i=n.length,p=c+i+3&-4,f=s-o;if(p>f){let l=a||new Uint8Array(c),A=u||new DataView(l.buffer);A.setUint32(0,E,!0),A.setUint32(4,p,!0),A.setUint32(8,_,!0),A.setUint32(12,S,!0);let d=e+o,I=e;if(f>=c){r.set(l,d);let R=f-c;for(let h=0;h<R;h++)r[d+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[d+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,p,!0),t.setUint32(l+8,_,!0),t.setUint32(l+12,S,!0),r.set(n,l+c)}return(o+p)%s}function U({uint8View:r,dataView:t,bufferStart:e,bufferSize:s,head:o,tail:n,messageMagic:_,paddingMagic:E,headerSize:c,maxMessages:S=1/0,onMessage:a,onCorruption:u}){let i=n,T=0,p=f=>{let l=f;if(l+4<=s)return t.getUint32(e+l,!0);let A=0;for(let d=0;d<4;d++)A|=r[e+(l+d)%s]<<d*8;return A};for(;i!==o&&T<S;){let f=s-i,l;if(f>=4?l=t.getUint32(e+i,!0):l=p(i),l===E){i=0;continue}if(l!==_){u&&u(i),i=(i+1)%s;continue}let A=p((i+4)%s),d=p((i+8)%s),I=p((i+12)%s);if(A<c||A>s){u&&u(i),i=(i+1)%s;continue}let R=A-c,h=e+(i+c)%s;a(h,R,d,I),i=(i+A)%s,T++}return{newTail:i,messagesRead:T}}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:S=!1,headerScratch:a=null,headerScratchView:u=null}){let i=_.length,T=s.MESSAGE_HEADER_SIZE+i;if(T>s.IN_BUFFER_SIZE-s.MESSAGE_HEADER_SIZE||!w(r,n.IN_WRITE_LOCK,c,S))return!1;try{let p=Atomics.load(r,n.IN_HEAD),f=Atomics.load(r,n.IN_TAIL),l=T+3&-4;if(Y(p,f,s.IN_BUFFER_SIZE)<l)return!1;let d=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:p,payload:_,sequence:d,messageMagic:s.MESSAGE_MAGIC,headerSize:s.MESSAGE_HEADER_SIZE,sourceId:E,headerScratch:a,headerScratchView:u});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 x(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 X(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 N(){return(performance.timeOrigin+performance.now())/1e3+2208988800}function B(r,t={}){let{getCurrentNTP:e=N,bypassLookaheadS:s=C}=t;if(!X(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 S=n+_/4294967296-E,a=128/48e3;return S<-a?"late":S<s?"nearFuture":"farFuture"}function D(r){return r!=="farFuture"}var v={nonBundle:38,immediate:39,nearFuture:40,late:41},L=class r{#e;#o;#s;#r;#n;#t;#N;#i;#L;#U;#P;#x;#A;#l;#d;#h;#S;#c;#R;#C;#_=-1;#T;#I;#u;#f;#a;#O=[];#B;#p;#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.#N=e.bypassLookaheadS??C,this.#i=e.sourceId??0,this.#L=e.blocking??this.#i!==0,this.#U=e.getCurrentNTP??N,this.#d=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.#x=e.nodeIdRange.from,this.#A=e.nodeIdRange.to,this.#l=e.nodeIdRange.from),e.replyWorkletPort&&(this.#B=e.replyWorkletPort),e.nodeIdPort&&(this.#S=e.nodeIdPort,this.#S.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 B(t,{getCurrentNTP:this.#U,bypassLookaheadS:this.#N})}#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=v[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.#L,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.#A&&this.#m();let t=this.#l++;return this.#S&&!this.#c&&this.#A-this.#l<=this.#d>>>1&&this.#M(),t}#m(){if(this.#h){let t=this.#h(this.#d);this.#x=t.from,this.#A=t.to,this.#l=t.from}else if(this.#c)this.#x=this.#c.from,this.#A=this.#c.to,this.#l=this.#c.from,this.#c=null,this.#M();else if(this.#S)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.#S&&this.#S.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.#p.dropsIndex)}get transferable(){let t={mode:this.#e,preschedulerPort:this.#n,bypassLookaheadS:this.#N,sourceId:this.#i,blocking:this.#L};if(this.#e==="postMessage"){let e=this.#d*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.#d;_.port1.onmessage=S=>{if(S.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.#K()}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.#X(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=x(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.#p=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.#u||this.#f||this.#T||typeof AudioWorkletGlobalScope<"u"||this.#e==="sab"&&(this.#I?(this.#u=()=>this.pollReplies(),this.#I.subscribe(this.#u)):typeof Atomics<"u"&&typeof Atomics.waitAsync=="function"?this.#G():this.#T=setInterval(()=>this.pollReplies(),5))}#Y(){this.#_<0||(Atomics.store(this.#r.atomicView,this.#p.activeIndex,0),this.#T&&(clearInterval(this.#T),this.#T=null),this.#u&&this.#I&&(this.#I.unsubscribe(this.#u),this.#u=null),this.#f=!1,this.#_=-1,this.#p=null)}#G(){let t=this.#r.atomicView,{headIndex:e,tailIndex:s}=this.#p;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.#p,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,S=E.REPLY_CHANNEL_BUFFER_SIZE,a=this.#r.uint8View,{newTail:u,messagesRead:i}=U({uint8View:a,dataView:this.#r.dataView,bufferStart:c,bufferSize:S,head:n,tail:_,messageMagic:E.MESSAGE_MAGIC,paddingMagic:E.PADDING_MAGIC,headerSize:E.MESSAGE_HEADER_SIZE||16,maxMessages:64,onMessage:(T,p,f)=>{t(a,T,p,f)}});return i>0&&Atomics.store(e,o,u),i}#k(){if(this.#a)return;let t=this.#B||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 S=0;S<n;S++){let a=E[S];if(!a)continue;let u=c.slice(a.offset,a.offset+a.length);this.#O.push({oscData:u,sequence:a.sequence})}else{let S=this.#C;if(!S)return;for(let a=0;a<n;a++){let u=E[a];if(!u)continue;let i=c.slice(u.offset,u.offset+u.length);S(i,u.sequence)}}}}#K(){if(this.#a){let t=this.#B||this.#o;t&&t.postMessage({type:"removeReplyPort",sourceId:this.#i}),this.#a.close(),this.#a=null}this.#O.length=0}#X(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{L as OscChannel};
1
+ function h(s,t,e){return(e-1-s+t)%e}function l(s,t,e,r){if(r>h(s,t,e))return!1;let o=e-s;return r<=o?!0:r<=(t>0?t-1:0)}function x({uint8View:s,dataView:t,bufferStart:e,bufferSize:r,head:o,payload:E,sequence:c,messageMagic:n,paddingMagic:A,headerSize:_,sourceId:I=0}){let C=E.length,S=_+C,T=S+3&-4,a=r-o;T>a&&(t.setUint32(e+o,A,!0),s.fill(0,e+o+4,e+r),o=0);let i=e+o;return t.setUint32(i,n,!0),t.setUint32(i+4,S,!0),t.setUint32(i+8,c,!0),t.setUint32(i+12,I,!0),s.set(E,i+_),T>S&&s.fill(0,i+S,i+T),(o+T)%r}function L(s,t,e=0,r=!1){for(let o=0;o<=e;o++)if(Atomics.compareExchange(s,t,0,1)===0)return!0;if(r){for(let E=0;E<100;E++)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 B(s,t){Atomics.store(s,t,0),Atomics.notify(s,t,1)}function N(s,t,e=0,r=!1){let{atomicView:o,dataView:E,uint8View:c,bufferConstants:n,ringBufferBase:A,controlIndices:_}=s,I=r?10:64,C=r,S=t.length,T=n.MESSAGE_HEADER_SIZE+S;if(T>n.IN_BUFFER_SIZE-n.MESSAGE_HEADER_SIZE||!L(o,_.IN_WRITE_LOCK,I,C))return!1;try{let a=Atomics.load(o,_.IN_HEAD),i=Atomics.load(o,_.IN_TAIL),R=T+3&-4;if(!l(a,i,n.IN_BUFFER_SIZE,R))return!1;let d=Atomics.add(o,_.IN_SEQUENCE,1),U=x({uint8View:c,dataView:E,bufferStart:A+n.IN_BUFFER_START,bufferSize:n.IN_BUFFER_SIZE,head:a,payload:t,sequence:d,messageMagic:n.MESSAGE_MAGIC,paddingMagic:n.PADDING_MAGIC,headerSize:n.MESSAGE_HEADER_SIZE,sourceId:e});return Atomics.store(o,_.IN_HEAD,U),Atomics.notify(o,_.IN_HEAD,1),!0}finally{B(o,_.IN_WRITE_LOCK)}}function p(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}}var f=class s{#t;#e;#s;#p;#o;#i;#A;#a;#C;#c;#n;#T;#_;#E;#r;#S;#I={messagesSent:0,bytesSent:0};constructor(t,e){if(this.#t=t,this.#i=e.sourceId??0,this.#A=e.blocking??this.#i!==0,this.#T=1e3,t==="postMessage")this.#e=e.port;else{if(this.#s={sharedBuffer:e.sharedBuffer,ringBufferBase:e.ringBufferBase,bufferConstants:e.bufferConstants,controlIndices:e.controlIndices},this.#O(),e.sharedBuffer&&e.bufferConstants){let r=e.ringBufferBase+e.bufferConstants.METRICS_START;this.#o=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.#a=new Int32Array(e.sharedBuffer,r,1),this.#f()}}e.nodeIdSource&&(this.#_=e.nodeIdSource,this.#f()),e.nodeIdRange&&(this.#C=e.nodeIdRange.from,this.#c=e.nodeIdRange.to,this.#n=e.nodeIdRange.from),e.nodeIdPort&&(this.#E=e.nodeIdPort,this.#E.onmessage=r=>{r.data.type==="nodeIdRange"&&(this.#r={from:r.data.from,to:r.data.to})},this.#N())}#O(){let t=this.#s.sharedBuffer;this.#p={atomicView:new Int32Array(t),dataView:new DataView(t),uint8View:new Uint8Array(t),bufferConstants:this.#s.bufferConstants,ringBufferBase:this.#s.ringBufferBase,controlIndices:this.#s.controlIndices}}#u(t){this.#t==="sab"&&this.#o?(Atomics.add(this.#o,9,1),Atomics.add(this.#o,10,t)):(this.#I.messagesSent++,this.#I.bytesSent+=t)}getAndResetMetrics(){let t={...this.#I};return this.#I={messagesSent:0,bytesSent:0},t}getMetrics(){return this.#t==="sab"&&this.#o?{messagesSent:Atomics.load(this.#o,9),bytesSent:Atomics.load(this.#o,10)}:{...this.#I}}send(t){if(this.#t==="postMessage")return this.#e?(this.#e.postMessage({type:"osc",oscData:t,sourceId:this.#i}),this.#u(t.length),!0):!1;let e=N(this.#p,t,this.#i,this.#A);return e?this.#u(t.length):this.#o&&Atomics.add(this.#o,26,1),e}sendDirect(t){return this.send(t)}nextNodeId(){if(this.#a)return Atomics.add(this.#a,0,1);this.#n>=this.#c&&this.#f();let t=this.#n++;return this.#E&&!this.#r&&this.#c-this.#n<=this.#T>>>1&&this.#N(),t}#f(){if(this.#_){let t=this.#_(this.#T);this.#C=t.from,this.#c=t.to,this.#n=t.from}else if(this.#r)this.#C=this.#r.from,this.#c=this.#r.to,this.#n=this.#r.from,this.#r=null,this.#N();else if(this.#E)throw new Error("[OscChannel] Node ID range exhausted before async refill arrived. Yield to the event loop between large batches of nextNodeId() calls.")}#N(){this.#E&&this.#E.postMessage({type:"requestNodeIdRange"})}get mode(){return this.#t}get transferable(){let t={mode:this.#t,sourceId:this.#i,blocking:this.#A};if(this.#t==="postMessage"){let e=this.#T*10,r,o;if(this.#_){let E=this.#_(e);r={from:E.from,to:E.to};let c=new MessageChannel,n=this.#_,A=this.#T;c.port1.onmessage=_=>{if(_.data.type==="requestNodeIdRange"){let I=n(A);c.port1.postMessage({type:"nodeIdRange",from:I.from,to:I.to})}},o=c.port2,this.#S=o}return{...t,port:this.#e,nodeIdRange:r,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.#t==="postMessage"&&this.#e&&t.push(this.#e),this.#S&&(t.push(this.#S),this.#S=null),t}close(){this.#t==="postMessage"&&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=p(t.ringBufferBase,t.bufferConstants.CONTROL_START)),new s("sab",{sharedBuffer:t.sharedBuffer,ringBufferBase:t.ringBufferBase,bufferConstants:t.bufferConstants,controlIndices:e,sourceId:t.sourceId,blocking:t.blocking})}static fromTransferable(t){return t.mode==="postMessage"?new s("postMessage",{port:t.port,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,sourceId:t.sourceId,blocking:t.blocking})}};export{f as OscChannel};