supersonic-scsynth-core 0.38.0 → 0.40.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.
package/package.json
CHANGED
package/wasm/scsynth-nrt.wasm
CHANGED
|
Binary file
|
package/workers/debug_worker.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(()=>{function B({uint8View:o,dataView:t,bufferStart:e,bufferSize:
|
|
2
|
-
`)&&(D=D.slice(0,-1)),e.push({text:D,timestamp:performance.now(),sequence:L}),C&&(Atomics.add(C,30,1),Atomics.add(C,31,T))},onCorruption:U=>{console.error("[DebugWorker] Corrupted message at position",U)}});return S>0&&Atomics.store(r,c.DEBUG_TAIL,
|
|
3
|
-
`)&&(S=S.slice(0,-1)),t.push({text:S,timestamp:performance.now(),sequence:e.sequence})}catch(
|
|
1
|
+
(()=>{function B({uint8View:o,dataView:t,bufferStart:e,bufferSize:E,head:S,tail:U,messageMagic:T,paddingMagic:L,headerSize:A,maxMessages:l=1/0,onMessage:D,onCorruption:n}){let s=U,N=0;for(;s!==S&&N<l;){if(E-s<A){s=0;continue}let i=e+s,f=t.getUint32(i,!0);if(f===L){s=0;continue}if(f!==T){n&&n(s),s=(s+1)%E;continue}let I=t.getUint32(i+4,!0),G=t.getUint32(i+8,!0),P=t.getUint32(i+12,!0);if(I<A||I>E){n&&n(s),s=(s+1)%E;continue}let d=I-A,m=i+A;D(m,d,G,P),s=(s+I)%E,N++}return{newTail:s,messagesRead:N}}function H(o,t){let e=o+t;return{DEBUG_HEAD:(e+16)/4,DEBUG_TAIL:(e+20)/4}}var R="sab",a=null,p=null,r=null,g=null,x=null,_=null,c={},C=null,u=!1,O=new TextDecoder("utf-8"),Y=(...o)=>{},F=(o,t,e)=>{a=o,p=t,_=e,r=new Int32Array(a),g=new DataView(a),x=new Uint8Array(a),c=H(p,_.CONTROL_START);let E=p+_.METRICS_START;C=new Uint32Array(a,E,_.METRICS_SIZE/4)},w=()=>{let o=Atomics.load(r,c.DEBUG_HEAD),t=Atomics.load(r,c.DEBUG_TAIL);if(o===t)return null;let e=[],{newTail:E,messagesRead:S}=B({uint8View:x,dataView:g,bufferStart:p+_.DEBUG_BUFFER_START,bufferSize:_.DEBUG_BUFFER_SIZE,head:o,tail:t,messageMagic:_.MESSAGE_MAGIC,paddingMagic:_.PADDING_MAGIC,headerSize:_.MESSAGE_HEADER_SIZE,maxMessages:1e3,onMessage:(U,T,L,A)=>{let l=new Uint8Array(T);for(let n=0;n<T;n++)l[n]=x[U+n];let D=O.decode(l);D.endsWith(`
|
|
2
|
+
`)&&(D=D.slice(0,-1)),e.push({text:D,timestamp:performance.now(),sequence:L}),C&&(Atomics.add(C,30,1),Atomics.add(C,31,T))},onCorruption:U=>{console.error("[DebugWorker] Corrupted message at position",U)}});return S>0&&Atomics.store(r,c.DEBUG_TAIL,E),e.length>0?e:null},W=()=>{for(;u;)try{let o=Atomics.load(r,c.DEBUG_HEAD),t=Atomics.load(r,c.DEBUG_TAIL);o===t&&Atomics.wait(r,c.DEBUG_HEAD,o);let e=w();e&&e.length>0&&self.postMessage({type:"debug",messages:e})}catch(o){console.error("[DebugWorker] Error in wait loop:",o),self.postMessage({type:"error",error:o.message}),Atomics.wait(r,0,r[0],10)}},k=()=>{if(!a){console.error("[DebugWorker] Cannot start - not initialized");return}u||(u=!0,W())},Q=()=>{u=!1},K=()=>{a&&(Atomics.store(r,c.DEBUG_HEAD,0),Atomics.store(r,c.DEBUG_TAIL,0))},h=o=>{let t=[];for(let e of o)try{let E=new Uint8Array(e.bytes),S=O.decode(E);S.endsWith(`
|
|
3
|
+
`)&&(S=S.slice(0,-1)),t.push({text:S,timestamp:performance.now(),sequence:e.sequence})}catch(E){console.error("[DebugWorker] Failed to decode message:",E)}t.length>0&&self.postMessage({type:"debug",messages:t})};self.addEventListener("message",o=>{let{data:t}=o;try{switch(t.type){case"init":R=t.mode||"sab",R==="sab"&&F(t.sharedBuffer,t.ringBufferBase,t.bufferConstants),self.postMessage({type:"initialized"});break;case"start":R==="sab"&&k();break;case"stop":Q();break;case"clear":R==="sab"&&K();break;case"debugRaw":t.messages&&h(t.messages);break;default:}}catch(e){console.error("[DebugWorker] Error:",e),self.postMessage({type:"error",error:e.message})}});Y("[DebugWorker] Script loaded");})();
|
package/workers/osc_in_worker.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{function u({uint8View:e,dataView:t,bufferStart:
|
|
1
|
+
(()=>{function u({uint8View:e,dataView:t,bufferStart:E,bufferSize:r,head:O,tail:I,messageMagic:D,paddingMagic:T,headerSize:a,maxMessages:C=1/0,onMessage:s,onCorruption:S}){let o=I,L=0;for(;o!==O&&L<C;){if(r-o<a){o=0;continue}let i=E+o,f=t.getUint32(i,!0);if(f===T){o=0;continue}if(f!==D){S&&S(o),o=(o+1)%r;continue}let R=t.getUint32(i+4,!0),G=t.getUint32(i+8,!0),M=t.getUint32(i+12,!0);if(R<a||R>r){S&&S(o),o=(o+1)%r;continue}let F=R-a,g=i+a;s(g,F,G,M),o=(o+R)%r,L++}return{newTail:o,messagesRead:L}}function H(e,t){let E=e+t;return{OUT_HEAD:(E+8)/4,OUT_TAIL:(E+12)/4}}var A=null,p=null,_=null,P=null,x=null,c=null,U={},n=null,l=!1,w=(...e)=>{},N=-1,W=(e,t,E)=>{A=e,p=t,c=E,_=new Int32Array(A),P=new DataView(A),x=new Uint8Array(A),U=H(p,c.CONTROL_START);let r=p+c.METRICS_START;n=new Uint32Array(A,r,c.METRICS_SIZE/4)},Q=()=>{let e=Atomics.load(_,U.OUT_HEAD),t=Atomics.load(_,U.OUT_TAIL);if(e===t)return[];let E=[],{newTail:r,messagesRead:O}=u({uint8View:x,dataView:P,bufferStart:p+c.OUT_BUFFER_START,bufferSize:c.OUT_BUFFER_SIZE,head:e,tail:t,messageMagic:c.MESSAGE_MAGIC,paddingMagic:c.PADDING_MAGIC,headerSize:c.MESSAGE_HEADER_SIZE,maxMessages:100,onMessage:(I,D,T,a)=>{if(N>=0){let s=N+1&4294967295;if(T!==s){let S=T-s+4294967296&4294967295;S<1e3&&(console.error("[OSCInWorker] Detected",S,"dropped messages (expected seq",s,"got",T,")"),n&&Atomics.add(n,28,S))}}N=T;let C=new Uint8Array(D);for(let s=0;s<D;s++)C[s]=x[I+s];E.push({oscData:C,sequence:T}),n&&(Atomics.add(n,26,1),Atomics.add(n,27,D))},onCorruption:I=>{console.error("[OSCInWorker] Corrupted message at position",I),n&&(Atomics.add(n,28,1),Atomics.add(n,29,1))}});return O>0&&Atomics.store(_,U.OUT_TAIL,r),E},b=()=>{for(;l;)try{let e=Atomics.load(_,U.OUT_HEAD),t=Atomics.load(_,U.OUT_TAIL);e===t&&Atomics.wait(_,U.OUT_HEAD,e);let E=Q();E.length>0&&self.postMessage({type:"messages",messages:E})}catch(e){console.error("[OSCInWorker] Error in wait loop:",e),self.postMessage({type:"error",error:e.message}),Atomics.wait(_,0,_[0],10)}},k=()=>{if(!A){console.error("[OSCInWorker] Cannot start - not initialized");return}l||(l=!0,b())},K=()=>{l=!1};self.addEventListener("message",e=>{let{data:t}=e;try{switch(t.type){case"init":W(t.sharedBuffer,t.ringBufferBase,t.bufferConstants),self.postMessage({type:"initialized"});break;case"start":k();break;case"stop":K();break;default:}}catch(E){console.error("[OSCInWorker] Error:",E),self.postMessage({type:"error",error:E.message})}});w("[OSCInWorker] Script loaded");})();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{function C({uint8View:
|
|
1
|
+
(()=>{function C({uint8View:s,dataView:t,bufferStart:e,bufferSize:r,head:f,tail:l,messageMagic:A,paddingMagic:N,headerSize:_,maxMessages:u=1/0,onMessage:i,onCorruption:T}){let o=l,O=0;for(;o!==f&&O<u;){if(r-o<_){o=0;continue}let E=e+o,p=t.getUint32(E,!0);if(p===N){o=0;continue}if(p!==A){T&&T(o),o=(o+1)%r;continue}let L=t.getUint32(E+4,!0),d=t.getUint32(E+8,!0),m=t.getUint32(E+12,!0);if(L<_||L>r){T&&T(o),o=(o+1)%r;continue}let y=L-_,H=E+_;i(H,y,d,m),o=(o+L)%r,O++}return{newTail:o,messagesRead:O}}function G(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 I=null,g=null,n=null,S=null,D=null,a=null,c={},U=!1,B=(...s)=>{},x=(s,t,e)=>{I=s,g=t,a=e,n=new Int32Array(I),S=new DataView(I),D=new Uint8Array(I),c=G(g,a.CONTROL_START);let r=Atomics.load(n,c.IN_HEAD);Atomics.store(n,c.IN_LOG_TAIL,r),B("Initialized, IN_LOG_TAIL set to",r)},w=()=>{let s=Atomics.load(n,c.IN_HEAD),t=Atomics.load(n,c.IN_LOG_TAIL);if(s===t)return[];let e=[],{newTail:r,messagesRead:f}=C({uint8View:D,dataView:S,bufferStart:g+a.IN_BUFFER_START,bufferSize:a.IN_BUFFER_SIZE,head:s,tail:t,messageMagic:a.MESSAGE_MAGIC,paddingMagic:a.PADDING_MAGIC,headerSize:a.MESSAGE_HEADER_SIZE,maxMessages:100,onMessage:(l,A,N,_)=>{let u=new Uint8Array(A);for(let i=0;i<A;i++)u[i]=D[l+i];e.push({sourceId:_,oscData:u,sequence:N})},onCorruption:l=>{console.error("[OSCOutLogWorker] Corrupted message at position",l)}});return f>0&&Atomics.store(n,c.IN_LOG_TAIL,r),e},M=()=>{for(;U;)try{let s=Atomics.load(n,c.IN_HEAD),t=Atomics.load(n,c.IN_LOG_TAIL);s===t&&Atomics.wait(n,c.IN_HEAD,s);let e=w();e.length>0&&self.postMessage({type:"oscLog",entries:e})}catch(s){console.error("[OSCOutLogWorker] Error in wait loop:",s),self.postMessage({type:"error",error:s.message}),Atomics.wait(n,0,n[0],10)}},R=()=>{if(!I){console.error("[OSCOutLogWorker] Cannot start - not initialized");return}U||(U=!0,M())},b=()=>{U=!1};self.addEventListener("message",s=>{let{data:t}=s;try{switch(t.type){case"init":x(t.sharedBuffer,t.ringBufferBase,t.bufferConstants),self.postMessage({type:"initialized"});break;case"start":R();break;case"stop":b();break;default:}}catch(e){console.error("[OSCOutLogWorker] Error:",e),self.postMessage({type:"error",error:e.message})}});B("Script loaded");})();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{function
|
|
1
|
+
(()=>{function J(e,t,r){return(r-1-e+t)%r}function j({uint8View:e,dataView:t,bufferStart:r,bufferSize:s,head:n,payload:c,sequence:l,messageMagic:_,headerSize:T,sourceId:a=0,headerScratch:G=null,headerScratchView:M=null}){let O=c.length,g=T+O+3&-4,U=s-n;if(g>U){let u=G||new Uint8Array(T),d=M||new DataView(u.buffer);d.setUint32(0,_,!0),d.setUint32(4,g,!0),d.setUint32(8,l,!0),d.setUint32(12,a,!0);let w=r+n,k=r;if(U>=T){e.set(u,w);let h=U-T;h>0&&e.set(c.subarray(0,h),w+T),e.set(c.subarray(h),k)}else{e.set(u.subarray(0,U),w),e.set(u.subarray(U),k);let h=T-U;e.set(c,k+h)}}else{let u=r+n;t.setUint32(u,_,!0),t.setUint32(u+4,g,!0),t.setUint32(u+8,l,!0),t.setUint32(u+12,a,!0),e.set(c,u+T)}return(n+g)%s}function ge(e,t,r=0,s=!1){for(let n=0;n<=r;n++)if(Atomics.compareExchange(e,t,0,1)===0)return!0;if(s){for(let c=0;c<100;c++)if(Atomics.wait(e,t,1,100),Atomics.compareExchange(e,t,0,1)===0)return!0;return console.error("[RingBuffer] Lock acquisition timeout after 10s - possible deadlock"),!1}return!1}function de(e,t){Atomics.store(e,t,0),Atomics.notify(e,t,1)}function ee({atomicView:e,dataView:t,uint8View:r,bufferConstants:s,ringBufferBase:n,controlIndices:c,oscMessage:l,sourceId:_=0,maxSpins:T=0,useWait:a=!1}){let G=l.length,M=s.MESSAGE_HEADER_SIZE+G;if(M>s.IN_BUFFER_SIZE-s.MESSAGE_HEADER_SIZE||!ge(e,c.IN_WRITE_LOCK,T,a))return!1;try{let O=Atomics.load(e,c.IN_HEAD),V=Atomics.load(e,c.IN_TAIL),g=M+3&-4;if(J(O,V,s.IN_BUFFER_SIZE)<g)return!1;let u=Atomics.add(e,c.IN_SEQUENCE,1),d=j({uint8View:r,dataView:t,bufferStart:n+s.IN_BUFFER_START,bufferSize:s.IN_BUFFER_SIZE,head:O,payload:l,sequence:u,messageMagic:s.MESSAGE_MAGIC,headerSize:s.MESSAGE_HEADER_SIZE,sourceId:_});return Atomics.load(e,c.IN_HEAD),Atomics.store(e,c.IN_HEAD,d),Atomics.notify(e,c.IN_HEAD,1),!0}finally{de(e,c.IN_WRITE_LOCK)}}function te(e,t){let r=e+t;return{IN_HEAD:(r+0)/4,IN_TAIL:(r+4)/4,IN_SEQUENCE:(r+24)/4,IN_WRITE_LOCK:(r+40)/4,IN_LOG_TAIL:(r+44)/4}}var p="sab",K=null,W=1024,me=65536,oe=3600,Se=4294967295,D=null,x=null,f=null,P=null,ue=null,fe=null,B={},E=null,y=null,ce=null,Z=150,C=(e,t)=>{E&&(p==="sab"?Atomics.store(E,e,t):E[e]=t)},Te=e=>E?p==="sab"?Atomics.load(E,e):E[e]:0,A=(e,t)=>{E&&(p==="sab"?Atomics.add(E,e,t):E[e]+=t)},F=(e,t)=>{E&&(p==="sab"?Atomics.store(E,e,t):E[e]=t)},Ee=e=>E?p==="sab"?Atomics.load(E,e):E[e]:0,Me=()=>{if(p!=="postMessage"||ce!==null)return;let e=()=>{y&&E&&self.postMessage({type:"preschedulerMetrics",metrics:new Uint32Array(y.slice(0))}),ce=setTimeout(e,Z)};e(),S("[PreScheduler] Started metrics sending (every "+Z+"ms)")};var o=[],R=null,H=1/0,Oe=0,le=!1,i=[],Y=!1,L=65536,He=2208988800,X=.5,S=(...e)=>{},$=()=>(performance.timeOrigin+performance.now())/1e3+He,xe=e=>{if(e.length>=16&&e[0]===35){let t=new DataView(e.buffer,e.byteOffset),r=t.getUint32(8,!1),s=t.getUint32(12,!1);return r+s/4294967296}return null},Be=()=>{if(!D||!f){console.error("[PreScheduler] Cannot init - missing buffer or constants");return}P=new Int32Array(D),ue=new DataView(D),fe=new Uint8Array(D),B=te(x,f.CONTROL_START);let e=x+f.METRICS_START;E=new Uint32Array(D,e,f.METRICS_SIZE/4),S("[PreScheduler] SharedArrayBuffer initialized with direct ring buffer writing and metrics")},b=()=>{if(!E)return;C(9,o.length);let e=o.length,t=Te(10);e>t&&C(10,e)},I=(e,t,r=0,s=!1)=>{if(p==="postMessage")return K?(K.postMessage({type:"osc",oscData:e,sourceId:r}),A(12,1),!0):(console.error("[PreScheduler] No worklet port available"),!1);if(!D||!P)return console.error("[PreScheduler] Not initialized for ring buffer writing"),!1;let n=e.length,c=f.MESSAGE_HEADER_SIZE+n;return c>f.IN_BUFFER_SIZE-f.MESSAGE_HEADER_SIZE?(console.error("[PreScheduler] Message too large:",c),!1):ee({atomicView:P,dataView:ue,uint8View:fe,bufferConstants:f,ringBufferBase:x,controlIndices:B,oscMessage:e,sourceId:r,maxSpins:10,useWait:s})?(A(12,1),!0):!1},N=(e,t,r=0)=>{let s=o.length+i.length;if(s>=L){console.error("[PreScheduler] Backpressure: dropping retry ("+s+" pending)"),A(17,1);return}i.push({oscData:e,context:t||"unknown",queuedAt:performance.now(),sourceId:r}),C(18,i.length);let n=Te(19);i.length>n&&C(19,i.length),S("[PreScheduler] Queued message for retry:",t,"queue size:",i.length),pe()},pe=()=>{if(Y||i.length===0||p!=="sab")return;Y=!0;let e=Atomics.load(P,B.IN_TAIL),t=Atomics.waitAsync(P,B.IN_TAIL,e),r=()=>{Y=!1,ye(),i.length>0&&pe()};t.async?t.value.then(r):queueMicrotask(r)},ye=()=>{if(i.length===0)return;let e=0;for(;e<i.length;){let t=i[e];if(I(t.oscData,!0,t.sourceId,!0))i.splice(e,1),A(16,1),C(18,i.length);else break}},ie=(e,t,r,s=0)=>{let n=o.length+i.length;if(n>=L){let a=`Prescheduler queue full (${n} >= ${L} max)`;return console.error("[PreScheduler]",a),self.postMessage({type:"error",error:a,code:"PRESCHEDULER_QUEUE_FULL"}),!1}let c=xe(e);if(c===null)return S("[PreScheduler] Non-bundle message, dispatching immediately"),I(e,!1,s,!0)||N(e,"immediate message",s),!0;let l=$(),_=c-l;if(e.length>W){let a=`Bundle too large for scheduler (${e.length} > ${W} bytes)`;return console.error("[PreScheduler]",a),self.postMessage({type:"error",error:a,code:"BUNDLE_TOO_LARGE"}),!1}if(_>oe){let a=`Bundle scheduled too far in future (${_.toFixed(0)}s > ${oe}s max)`;return console.error("[PreScheduler]",a),self.postMessage({type:"error",error:a,code:"BUNDLE_TOO_FAR_FUTURE"}),!1}let T={ntpTime:c,seq:Oe++,sessionId:t||0,runTag:r||"",oscData:e,sourceId:s};return Fe(T),A(11,1),b(),S("[PreScheduler] Scheduled bundle:","NTP="+c.toFixed(3),"current="+l.toFixed(3),"wait="+(_*1e3).toFixed(1)+"ms","pending="+o.length),m(),!0},Fe=e=>{o.push(e),Ge(o.length-1)},Ae=()=>o.length>0?o[0]:null,be=()=>{if(o.length===0)return null;let e=o[0],t=o.pop();return o.length>0&&(o[0]=t,Re(0)),e},Ge=e=>{for(;e>0;){let t=Math.floor((e-1)/2);if(q(o[e],o[t])>=0)break;Ue(e,t),e=t}},Re=e=>{let t=o.length;for(;;){let r=2*e+1,s=2*e+2,n=e;if(r<t&&q(o[r],o[n])<0&&(n=r),s<t&&q(o[s],o[n])<0&&(n=s),n===e)break;Ue(e,n),e=n}},q=(e,t)=>e.ntpTime===t.ntpTime?e.seq-t.seq:e.ntpTime-t.ntpTime,Ue=(e,t)=>{let r=o[e];o[e]=o[t],o[t]=r},m=()=>{if(o.length===0){R!==null&&(clearTimeout(R),R=null,H=1/0);return}let e=Ae().ntpTime-X,t=$();if(e<H){R!==null&&clearTimeout(R);let r=Math.max(0,(e-t)*1e3);H=e,R=setTimeout(ke,r)}},we=()=>{R===null&&(S("[PreScheduler] Starting demand-driven dispatching"),m())};var ke=()=>{le=!0;let e=$(),t=e+X,r=0;for(;o.length>0;){let s=Ae();if(s.ntpTime<=t){be(),b();let n=s.ntpTime-e;if(A(21,1),n<0){let l=Math.round(-n*1e3);A(15,1);let _=Ee(23);l>_&&F(23,l)}else{let l=Math.round(n*1e3),_=Ee(14);(_===Se||l<_)&&F(14,l)}S("[PreScheduler] Dispatching bundle:","NTP="+s.ntpTime.toFixed(3),"current="+e.toFixed(3),"early="+(n*1e3).toFixed(1)+"ms","remaining="+o.length),I(s.oscData,!1,s.sourceId,!0)||N(s.oscData,"scheduled bundle NTP="+s.ntpTime.toFixed(3),s.sourceId),r++}else break}(r>0||o.length>0||i.length>0)&&S("[PreScheduler] Dispatch cycle complete:","dispatched="+r,"pending="+o.length,"retrying="+i.length),le=!1,R=null,H=1/0,m()},z=e=>{if(o.length===0)return;let t=o.length,r=[];for(let n=0;n<o.length;n++){let c=o[n];e(c)||r.push(c)}let s=t-r.length;s>0&&(o=r,Ye(),A(13,s),b(),S("[PreScheduler] Cancelled "+s+" events, "+o.length+" remaining"),m())},Ye=()=>{for(let e=Math.floor(o.length/2)-1;e>=0;e--)Re(e)},Qe=(e,t)=>{z(r=>r.sessionId===e&&r.runTag===t)},ve=e=>{z(t=>t.sessionId===e)},Ke=e=>{z(t=>t.runTag===e)},We=()=>{if(o.length===0)return;let e=o.length;A(13,e),o=[],b(),S("[PreScheduler] Cancelled all "+e+" events"),m()},Ze=e=>!e||e.length<8?!1:e[0]===35&&e[1]===98&&e[2]===117&&e[3]===110&&e[4]===100&&e[5]===108&&e[6]===101&&e[7]===0,qe=e=>{let t=[],r=new DataView(e.buffer,e.byteOffset,e.byteLength),s=16;for(;s+4<=e.length;){let n=r.getInt32(s,!1);if(s+=4,n<=0||n>me||s+n>e.length)break;let c=e.slice(s,s+n);for(t.push(c),s+=n;s%4!==0&&s<e.length;)s++}return t},Xe=(e,t=0)=>{if(Ze(e)){let r=qe(e);for(let s=0;s<r.length;s++)I(r[s],!1,t,!0)||N(r[s],"immediate bundle message "+s,t)}else I(e,!1,t,!0)||N(e,"immediate message",t)};self.addEventListener("message",e=>{let{data:t}=e;try{switch(t.type){case"init":if(p=t.mode||"sab",t.maxPendingMessages&&(L=t.maxPendingMessages),t.snapshotIntervalMs&&(Z=t.snapshotIntervalMs),t.bypassLookaheadS!==void 0&&(X=t.bypassLookaheadS),p==="sab")D=t.sharedBuffer,x=t.ringBufferBase,f=t.bufferConstants,Be(),f&&f.scheduler_slot_size&&(W=f.scheduler_slot_size);else{K=t.workletPort;let s=184;y=new ArrayBuffer(s),E=new Uint32Array(y),Me()}F(14,Se),F(23,0),we(),S("[OSCPreSchedulerWorker] Initialized with NTP-based scheduling, mode="+p+", capacity="+L),self.postMessage({type:"initialized"});break;case"addOscSource":let r=e.ports[0];r&&(r.onmessage=s=>{s.data.type==="osc"&&s.data.oscData&&ie(s.data.oscData,0,"",s.data.sourceId||0)},S("[OSCPreSchedulerWorker] Added external OSC source"));break;case"send":ie(t.oscData,t.sessionId||0,t.runTag||"",t.sourceId||0);break;case"sendImmediate":Xe(t.oscData,t.sourceId||0);break;case"directDispatch":I(t.oscData,!1,t.sourceId||0,!0)||N(t.oscData,"directDispatch fallback",t.sourceId||0);break;case"cancelSessionTag":t.runTag!==void 0&&t.runTag!==null&&t.runTag!==""&&Qe(t.sessionId||0,t.runTag);break;case"cancelSession":ve(t.sessionId||0);break;case"cancelTag":t.runTag!==void 0&&t.runTag!==null&&t.runTag!==""&&Ke(t.runTag);break;case"cancelAll":We(),t.ack&&self.postMessage({type:"cancelAllAck"});break;default:}}catch(r){console.error("[OSCPreSchedulerWorker] Error:",r),self.postMessage({type:"error",error:r.message})}});S("[OSCPreSchedulerWorker] Script loaded");})();
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(()=>{function
|
|
2
|
-
`,c=new TextEncoder().encode(r);if(c.length>s)return;let o=this.CONTROL_INDICES.DEBUG_HEAD,_=this.atomicLoad(o),a=s-_,u=_;c.length>a&&(this.uint8View[this.ringBufferBase+t+_]=i,u=0);let E=this.ringBufferBase+t;for(let f=0;f<c.length;f++)this.uint8View[E+u+f]=c[f];let h=u+c.length;this.atomicStore(o,h)}catch{}}atomicLoad(e){return this.mode==="sab"?Atomics.load(this.atomicView,e):this.atomicView[e]}atomicStore(e,t){this.mode==="sab"?Atomics.store(this.atomicView,e,t):this.atomicView[e]=t}initPMPools(){if(this.mode!=="postMessage")return;let e=w;this.pmPools={replies:{message:{type:"oscReplies",messages:null,count:0},buffer:new ArrayBuffer(e.REPLY_BUFFER_SIZE),bufferView:null,entries:new Array(e.MAX_REPLY_MESSAGES).fill(null).map(()=>({offset:0,length:0,sequence:0}))},debug:{message:{type:"debugRawBatch",messages:null,count:0},buffer:new ArrayBuffer(e.DEBUG_BUFFER_SIZE),bufferView:null,entries:new Array(e.MAX_DEBUG_MESSAGES).fill(null).map(()=>({offset:0,length:0,sequence:0}))},snapshot:{message:{type:"snapshot",buffer:null,snapshotsSent:0},buffer:null,bufferView:null,size:0},log:{message:{type:"oscLog",entries:null,count:0,buffer:null},buffer:new ArrayBuffer(e.LOG_BUFFER_SIZE),bufferView:null,entries:new Array(e.MAX_LOG_ENTRIES).fill(null).map(()=>({offset:0,length:0,originalLength:0,sourceId:0,sequence:0}))},incoming:{headerBytes:new Uint8Array(16),headerView:null}};let t=this.pmPools;if(t.replies.bufferView=new Uint8Array(t.replies.buffer),t.debug.bufferView=new Uint8Array(t.debug.buffer),t.log.bufferView=new Uint8Array(t.log.buffer),t.replies.message.messages=t.replies.entries,t.debug.message.messages=t.debug.entries,t.log.message.entries=t.log.entries,t.incoming.headerView=new DataView(t.incoming.headerBytes.buffer),this.bufferConstants&&this.wasmMemory){let s=this.bufferConstants,i=s.METRICS_SIZE+s.NODE_TREE_SIZE;t.snapshot.buffer=new ArrayBuffer(i),t.snapshot.bufferView=new Uint8Array(t.snapshot.buffer),t.snapshot.size=i,t.snapshot.message.buffer=t.snapshot.buffer;let r=this.ringBufferBase+s.METRICS_START;t.snapshot.sourceView=new Uint8Array(this.wasmMemory.buffer,r,i)}}writeOscToRingBuffer(e,t=0){if(!this.bufferConstants||!this.uint8View||!this.pmPools)return!1;let s=this.bufferConstants.IN_BUFFER_SIZE,i=this.bufferConstants.MESSAGE_HEADER_SIZE,r=this.ringBufferBase+this.bufferConstants.IN_BUFFER_START,n=e.byteLength,o=i+n+3&-4,_=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),a=this.atomicLoad(this.CONTROL_INDICES.IN_TAIL),u=C(_,a,s);if(o>u)return console.warn("[AudioWorklet] Ring buffer full, dropping OSC message"),!1;let E=this.atomicLoad(this.CONTROL_INDICES.IN_SEQUENCE);this.atomicStore(this.CONTROL_INDICES.IN_SEQUENCE,E+1);let h=new Uint8Array(e),f=O({uint8View:this.uint8View,dataView:this.dataView,bufferStart:r,bufferSize:s,head:_,payload:h,sequence:E,messageMagic:this.bufferConstants.MESSAGE_MAGIC,headerSize:i,sourceId:t,headerScratch:this.pmPools.incoming.headerBytes,headerScratchView:this.pmPools.incoming.headerView});return this.atomicStore(this.CONTROL_INDICES.IN_HEAD,f),!0}readOscReplies(){if(!this.pmPools)return;let e=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),t=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);if(e===t)return;let s=this.pmPools.replies,i=w,r=0,n=0,{newTail:c,messagesRead:o}=d({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.OUT_BUFFER_START,bufferSize:this.bufferConstants.OUT_BUFFER_SIZE,head:e,tail:t,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_REPLY_MESSAGES,onMessage:(_,a,u,E)=>{if(r>=i.MAX_REPLY_MESSAGES||n+a>i.REPLY_BUFFER_SIZE)return;for(let f=0;f<a;f++)s.bufferView[n+f]=this.uint8View[_+f];let h=s.entries[r];h.offset=n,h.length=a,h.sequence=u,n+=a,r++}});o>0&&this.atomicStore(this.CONTROL_INDICES.OUT_TAIL,c),r>0&&(s.message.count=r,s.message.buffer=s.buffer,this.port.postMessage(s.message))}readNodeTree(){if(!this.bufferConstants||!this.wasmMemory||this.ringBufferBase===null)return null;let e=this.bufferConstants,t=this.ringBufferBase+e.NODE_TREE_START,s=new Uint32Array(this.wasmMemory.buffer,t,2),i=s[0],r=s[1],n=t+e.NODE_TREE_HEADER_SIZE,c=e.NODE_TREE_MIRROR_MAX_NODES,o=e.NODE_TREE_ENTRY_SIZE,_=e.NODE_TREE_DEF_NAME_SIZE,a=new DataView(this.wasmMemory.buffer,n,c*o),u=[],E=0;for(let h=0;h<c&&E<i;h++){let f=h*o,S=a.getInt32(f,!0);if(S===-1)continue;E++;let l=n+f+24,T=new Uint8Array(this.wasmMemory.buffer,l,_),p="";for(let I=0;I<_&&T[I]!==0;I++)p+=String.fromCharCode(T[I]);u.push({id:S,parentId:a.getInt32(f+4,!0),isGroup:a.getInt32(f+8,!0)===1,prevId:a.getInt32(f+12,!0),nextId:a.getInt32(f+16,!0),headId:a.getInt32(f+20,!0),defName:p})}return{nodeCount:i,version:r,nodes:u}}readMetrics(){return this.metricsView?new Uint32Array(this.metricsView):null}recordOscReceived(e,t=null){if(this.metricsView){if(this.mode==="sab")Atomics.add(this.metricsView,24,1),Atomics.add(this.metricsView,25,e);else if(this.metricsView[24]++,this.metricsView[25]+=e,t){this.metricsView[22]++;let i={nonBundle:38,immediate:39,nearFuture:40,late:41}[t];i!==void 0&&this.metricsView[i]++}}}checkAndSendSnapshot(e){let t=this.bufferConstants;if(!t||!this.wasmMemory||this.ringBufferBase===null||!this.pmPools)return!1;let i=(this.ringBufferBase+t.NODE_TREE_START+4)/4,r=this.atomicView[i];if(r!==this.lastTreeVersion)this.lastTreeVersion=r,this.lastTreeSendTime=e;else{if(this.lastTreeSendTime>=0&&e-this.lastTreeSendTime<this.treeSnapshotMinInterval)return!1;this.lastTreeSendTime=e}let c=this.pmPools.snapshot;return!c.buffer||!c.sourceView?!1:(c.bufferView.set(c.sourceView),this.treeSnapshotsSent++,c.message.snapshotsSent=this.treeSnapshotsSent,this.port.postMessage(c.message),!0)}readMetricsAndTreeBuffer(){if(!this.bufferConstants||!this.wasmMemory||this.ringBufferBase===null)return null;let e=this.bufferConstants,t=this.ringBufferBase+e.METRICS_START,s=e.METRICS_SIZE+e.NODE_TREE_SIZE,i=new Uint8Array(this.wasmMemory.buffer,t,s),r=new ArrayBuffer(s);return new Uint8Array(r).set(i),r}sendLogEntries(){if(!this.CONTROL_INDICES||!this.bufferConstants||!this.pmPools)return;let e=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),t=this.atomicLoad(this.CONTROL_INDICES.IN_LOG_TAIL);if(e===t)return;let s=this.pmPools.log,i=w,r=0,n=0,{newTail:c,messagesRead:o}=d({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.IN_BUFFER_START,bufferSize:this.bufferConstants.IN_BUFFER_SIZE,head:e,tail:t,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_LOG_ENTRIES,onMessage:(_,a,u,E)=>{if(r>=i.MAX_LOG_ENTRIES)return;let h=Math.min(a,i.LOG_MAX_MESSAGE_SIZE);if(n+h>i.LOG_BUFFER_SIZE)return;for(let S=0;S<h;S++)s.bufferView[n+S]=this.uint8View[_+S];let f=s.entries[r];f.offset=n,f.length=h,f.originalLength=a,f.sourceId=E??0,f.sequence=u,n+=h,r++}});o>0&&this.atomicStore(this.CONTROL_INDICES.IN_LOG_TAIL,c),r>0&&(s.message.count=r,s.message.buffer=s.buffer,this.port.postMessage(s.message))}readDebugMessages(){if(!this.pmPools)return;let e=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),t=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);if(e===t)return;let s=this.pmPools.debug,i=w,r=0,n=0,{newTail:c,messagesRead:o}=d({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.DEBUG_BUFFER_START,bufferSize:this.bufferConstants.DEBUG_BUFFER_SIZE,head:e,tail:t,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_DEBUG_MESSAGES,onMessage:(_,a,u,E)=>{if(r>=i.MAX_DEBUG_MESSAGES||n+a>i.DEBUG_BUFFER_SIZE)return;for(let f=0;f<a;f++)s.bufferView[n+f]=this.uint8View[_+f];let h=s.entries[r];h.offset=n,h.length=a,h.sequence=u,n+=a,r++}});o>0&&this.atomicStore(this.CONTROL_INDICES.DEBUG_TAIL,c),r>0&&(s.message.count=r,s.message.buffer=s.buffer,this.port.postMessage(s.message))}async handleMessage(e){let{data:t}=e;try{if(t.type==="osc"){this.mode==="postMessage"&&t.oscData&&(this.writeOscToRingBuffer(t.oscData,t.sourceId??0),this.recordOscReceived(t.oscData.byteLength,t.bypassCategory));return}if(t.type==="addOscPort"){let s=e.ports[0];if(s){let i=t.sourceId??0;this.portSourceIds.set(s,i),s.onmessage=r=>{if(r.data.type==="osc"&&r.data.oscData){let n=r.data.sourceId??this.portSourceIds.get(s)??0;this.writeOscToRingBuffer(r.data.oscData,n),this.recordOscReceived(r.data.oscData.byteLength,r.data.bypassCategory)}},this.oscPorts.push(s)}return}if(t.type==="init"&&(this.mode=t.mode||"sab",t.snapshotIntervalMs&&(this.treeSnapshotMinInterval=t.snapshotIntervalMs/1e3),this.mode==="sab"&&t.sharedBuffer&&(this.sharedBuffer=t.sharedBuffer,this.atomicView=new Int32Array(this.sharedBuffer),this.uint8View=new Uint8Array(this.sharedBuffer),this.dataView=new DataView(this.sharedBuffer))),t.type==="loadWasm"){if(t.wasmBytes){let s;if(this.mode==="sab"){if(s=t.wasmMemory,!s){this.port.postMessage({type:"error",error:"No WASM memory provided!"});return}}else{let n=t.memoryPages||1280;s=new WebAssembly.Memory({initial:n,maximum:n,shared:!0})}this.wasmMemory=s,this.worldOptions=t.worldOptions||{},this.sampleRate=t.sampleRate||48e3;let i={env:{memory:s,emscripten_asm_const_double:()=>Date.now()*1e3,__syscall_getdents64:()=>0,__syscall_unlinkat:()=>0,_emscripten_init_main_thread_js:()=>{},_emscripten_thread_mailbox_await:()=>{},_emscripten_thread_set_strongref:()=>{},emscripten_exit_with_live_runtime:()=>{},_emscripten_receive_on_main_thread_js:()=>{},emscripten_check_blocking_allowed:()=>{},_emscripten_thread_cleanup:()=>{},emscripten_num_logical_cores:()=>1,_emscripten_notify_mailbox_postmessage:()=>{}},wasi_snapshot_preview1:{clock_time_get:(n,c,o)=>{let _=new DataView(s.buffer),a=BigInt(Math.floor(Date.now()*1e6));return _.setBigUint64(o,a,!0),0},environ_sizes_get:()=>0,environ_get:()=>0,fd_close:()=>0,fd_write:()=>0,fd_seek:()=>0,fd_read:()=>0,proc_exit:n=>{console.error("[AudioWorklet] WASM tried to exit with code:",n)}}},r=await WebAssembly.compile(t.wasmBytes);if(this.wasmInstance=await WebAssembly.instantiate(r,i),this.wasmInstance.exports.get_ring_buffer_base&&(this.ringBufferBase=this.wasmInstance.exports.get_ring_buffer_base(),this.loadBufferConstants(),this.calculateBufferIndices(this.ringBufferBase),this.initPMPools(),this.writeWorldOptionsToMemory(),this.wasmInstance.exports.init_memory)){this.wasmInstance.exports.init_memory(this.sampleRate),this.isInitialized=!0;let n=this.mode==="postMessage"?this.readMetricsAndTreeBuffer():void 0,c={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:n};this.port.postMessage(c,n?[n]:[])}}else if(t.wasmInstance&&(this.wasmInstance=t.wasmInstance,this.wasmInstance.exports.get_ring_buffer_base&&(this.ringBufferBase=this.wasmInstance.exports.get_ring_buffer_base(),this.loadBufferConstants(),this.calculateBufferIndices(this.ringBufferBase),this.initPMPools(),this.writeWorldOptionsToMemory(),this.wasmInstance.exports.init_memory))){this.wasmInstance.exports.init_memory(this.sampleRate),this.isInitialized=!0;let s=this.mode==="postMessage"?this.readMetricsAndTreeBuffer():void 0,i={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:s};this.port.postMessage(i,s?[s]:[])}}if(t.type==="getVersion")if(this.wasmInstance&&this.wasmInstance.exports.get_supersonic_version_string){let s=this.wasmInstance.exports.get_supersonic_version_string(),i=new Uint8Array(this.wasmMemory.buffer),r="";for(let n=s;i[n]!==0;n++)r+=String.fromCharCode(i[n]);this.port.postMessage({type:"version",version:r})}else this.port.postMessage({type:"version",version:"unknown"});if(t.type==="getTimeOffset")if(this.wasmInstance&&this.wasmInstance.exports.get_time_offset){let s=this.wasmInstance.exports.get_time_offset();this.port.postMessage({type:"timeOffset",offset:s})}else console.error("[AudioWorklet] get_time_offset not available! wasmInstance:",!!this.wasmInstance),this.port.postMessage({type:"error",error:"get_time_offset function not available in WASM exports"});if(t.type==="setNTPStartTime"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.NTP_START_TIME_START,i=new Float64Array(this.wasmMemory.buffer,s,1);i[0]=t.ntpStartTime}if(t.type==="setDriftOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.DRIFT_OFFSET_START,i=new Int32Array(this.wasmMemory.buffer,s,1);i[0]=t.driftOffsetMs}if(t.type==="setGlobalOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.GLOBAL_OFFSET_START,i=new Int32Array(this.wasmMemory.buffer,s,1);i[0]=t.globalOffsetMs}if(t.type==="getMetrics"){let s=this.metricsView?new Uint32Array(this.metricsView):null;this.port.postMessage({type:"metricsSnapshot",requestId:t.requestId,metrics:s})}if(t.type==="copyBufferData")try{let{copyId:s,ptr:i,data:r}=t;if(!this.wasmMemory||!this.wasmMemory.buffer)throw new Error("WASM memory not initialized");let n=new Float32Array(r);new Float32Array(this.wasmMemory.buffer,i,n.length).set(n),this.port.postMessage({type:"bufferCopied",copyId:s,success:!0})}catch(s){console.error("[AudioWorklet] Buffer copy failed:",s),this.port.postMessage({type:"bufferCopied",copyId:t.copyId,success:!1,error:s.message})}}catch(s){console.error("[AudioWorklet] Error handling message:",s),this.port.postMessage({type:"error",error:s.message,stack:s.stack})}}process(e,t,s){if(this.processCallCount++,!this.isInitialized)return!0;try{if(this.wasmInstance&&this.wasmInstance.exports.process_audio){let i=currentTime,r=e[0]?.length||0,n=t[0]?.length||0;if(r>0&&this.wasmInstance?.exports?.get_audio_input_bus)try{let o=this.wasmInstance.exports.get_audio_input_bus(),_=this.wasmInstance.exports.get_audio_buffer_samples();if(o&&o>0){let a=this.sharedBuffer||this.wasmMemory?.buffer;if(a){let u=this.worldOptions?.numInputBusChannels||2,E=Math.min(r,u);(!this.inputView||this.lastInputBusPtr!==o||this.lastInputChannels!==u)&&(this.inputView=new Float32Array(a,o,_*u),this.lastInputBusPtr=o,this.lastInputChannels=u);for(let h=0;h<E;h++)e[0]?.[h]&&this.inputView.set(e[0][h],h*_)}}}catch{}let c=this.wasmInstance.exports.process_audio(i,n,r);if(this.wasmInstance.exports.get_audio_output_bus&&t[0]&&t[0].length>=2)try{let o=this.wasmInstance.exports.get_audio_output_bus(),_=this.wasmInstance.exports.get_audio_buffer_samples();if(o&&o>0){let a=this.wasmInstance.exports.memory||this.wasmMemory;if(!a||!a.buffer)return!0;let u=a.buffer,E=u.byteLength,h=o+_*2*4;if(o<0||o>E||h>E)return!0;(!this.audioView||this.lastAudioBufferPtr!==o||this.lastWasmBufferSize!==E||u!==this.audioView.buffer)&&(this.audioView=new Float32Array(u,o,_*2),this.lastAudioBufferPtr=o,this.lastWasmBufferSize=E),(!this.leftChannelView||this.lastNumSamples!==_||this.leftChannelView.buffer!==this.audioView.buffer)&&(this.leftChannelView=this.audioView.subarray(0,_),this.rightChannelView=this.audioView.subarray(_,_*2),this.lastNumSamples=_),t[0][0].set(this.leftChannelView),t[0][1].set(this.rightChannelView)}}catch{}if(this.mode==="postMessage")this.readOscReplies(),this.readDebugMessages(),this.checkAndSendSnapshot(i)&&this.sendLogEntries();else if(this.atomicView){let o=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),_=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);o!==_&&Atomics.notify(this.atomicView,this.CONTROL_INDICES.OUT_HEAD,1)}return this.processCallCount%3750===0&&this.checkStatus(),c!==0}}catch(i){console.error("[AudioWorklet] process() error:",i),console.error("[AudioWorklet] Stack:",i.stack),this.atomicView&&this.mode==="sab"&&Atomics.or(this.atomicView,this.CONTROL_INDICES.STATUS_FLAGS,this.STATUS_FLAGS.WASM_ERROR),this.metricsView&&(this.mode==="sab"?Atomics.add(this.metricsView,7,1):this.metricsView[7]++)}return!0}checkStatus(){if(!this.atomicView)return;let e=this.atomicLoad(this.CONTROL_INDICES.STATUS_FLAGS);if(e!==this.STATUS_FLAGS.OK){this._statusObj.bufferFull=!!(e&this.STATUS_FLAGS.BUFFER_FULL),this._statusObj.overrun=!!(e&this.STATUS_FLAGS.OVERRUN),this._statusObj.wasmError=!!(e&this.STATUS_FLAGS.WASM_ERROR),this._statusObj.fragmented=!!(e&this.STATUS_FLAGS.FRAGMENTED_MSG),this._metricsObj.processCount=this.metricsView[0],this._metricsObj.messagesProcessed=this.metricsView[1],this._metricsObj.messagesDropped=this.metricsView[2],this._metricsObj.schedulerQueueDepth=this.metricsView[3],this._metricsObj.schedulerQueueMax=this.metricsView[4],this._metricsObj.schedulerQueueDropped=this.metricsView[5],this._statusMessage.flags=e,this.port.postMessage(this._statusMessage);let t=e&this.STATUS_FLAGS.BUFFER_FULL;this.atomicStore(this.CONTROL_INDICES.STATUS_FLAGS,t)}}};registerProcessor("scsynth-processor",R);})();
|
|
1
|
+
(()=>{function R(m,e,t){return(t-1-m+e)%t}function O({uint8View:m,dataView:e,bufferStart:t,bufferSize:s,head:i,payload:r,sequence:o,messageMagic:E,headerSize:n,sourceId:_=0,headerScratch:a=null,headerScratchView:u=null}){let c=r.length,f=n+c+3&-4,S=s-i;if(f>S){let l=a||new Uint8Array(n),I=u||new DataView(l.buffer);I.setUint32(0,E,!0),I.setUint32(4,f,!0),I.setUint32(8,o,!0),I.setUint32(12,_,!0);let p=t+i,T=t;if(S>=n){m.set(l,p);let A=S-n;A>0&&m.set(r.subarray(0,A),p+n),m.set(r.subarray(A),T)}else{m.set(l.subarray(0,S),p),m.set(l.subarray(S),T);let A=n-S;m.set(r,T+A)}}else{let l=t+i;e.setUint32(l,E,!0),e.setUint32(l+4,f,!0),e.setUint32(l+8,o,!0),e.setUint32(l+12,_,!0),m.set(r,l+n)}return(i+f)%s}function d({uint8View:m,dataView:e,bufferStart:t,bufferSize:s,head:i,tail:r,messageMagic:o,paddingMagic:E,headerSize:n,maxMessages:_=1/0,onMessage:a,onCorruption:u}){let c=r,h=0;for(;c!==i&&h<_;){if(s-c<n){c=0;continue}let S=t+c,l=e.getUint32(S,!0);if(l===E){c=0;continue}if(l!==o){u&&u(c),c=(c+1)%s;continue}let I=e.getUint32(S+4,!0),p=e.getUint32(S+8,!0),T=e.getUint32(S+12,!0);if(I<n||I>s){u&&u(c),c=(c+1)%s;continue}let A=I-n,U=S+n;a(U,A,p,T),c=(c+I)%s,h++}return{newTail:c,messagesRead:h}}function g(m,e){let t=m+e;return{IN_HEAD:(t+0)/4,IN_TAIL:(t+4)/4,OUT_HEAD:(t+8)/4,OUT_TAIL:(t+12)/4,DEBUG_HEAD:(t+16)/4,DEBUG_TAIL:(t+20)/4,IN_SEQUENCE:(t+24)/4,OUT_SEQUENCE:(t+28)/4,DEBUG_SEQUENCE:(t+32)/4,STATUS_FLAGS:(t+36)/4,IN_WRITE_LOCK:(t+40)/4,IN_LOG_TAIL:(t+44)/4}}var C={MAX_REPLY_MESSAGES:64,MAX_DEBUG_MESSAGES:32,MAX_LOG_ENTRIES:100,REPLY_BUFFER_SIZE:128*1024,DEBUG_BUFFER_SIZE:64*1024,LOG_BUFFER_SIZE:256*1024,LOG_MAX_MESSAGE_SIZE:16*1024,MAX_OSC_MESSAGE_SIZE:8192},w=class extends AudioWorkletProcessor{constructor(){super(),this.mode="sab",this.sharedBuffer=null,this.wasmModule=null,this.wasmInstance=null,this.isInitialized=!1,this.processCallCount=0,this.lastStatusCheck=0,this.ringBufferBase=null,this.pendingClearSched=!1,this.audioView=null,this.lastAudioBufferPtr=0,this.lastWasmBufferSize=0,this.lastTreeVersion=-1,this.treeSnapshotsSent=0,this.lastTreeSendTime=-1,this.treeSnapshotMinInterval=.15,this.atomicView=null,this.uint8View=null,this.dataView=null,this.localClockOffsetView=null,this.bufferConstants=null,this.CONTROL_INDICES=null,this.metricsView=null,this.STATUS_FLAGS={OK:0,BUFFER_FULL:1,OVERRUN:2,WASM_ERROR:4,FRAGMENTED_MSG:8},this.oscPorts=[],this.portSourceIds=new Map,this.leftChannelView=null,this.rightChannelView=null,this.lastNumSamples=0,this.pmPools=null,this._statusObj={bufferFull:!1,overrun:!1,wasmError:!1,fragmented:!1},this._metricsObj={processCount:0,messagesProcessed:0,messagesDropped:0,schedulerQueueDepth:0,schedulerQueueMax:0,schedulerQueueDropped:0},this._statusMessage={type:"status",flags:0,status:this._statusObj,metrics:this._metricsObj},this.port.onmessage=this.handleMessage.bind(this)}loadBufferConstants(){if(!this.wasmInstance||!this.wasmInstance.exports.get_buffer_layout)throw new Error("WASM instance does not export get_buffer_layout");let e=this.wasmInstance.exports.get_buffer_layout(),t=this.wasmMemory;if(!t)throw new Error("WASM memory not available");let s=new Uint32Array(t.buffer,e,34),i=new Uint8Array(t.buffer,e,140);if(this.bufferConstants={IN_BUFFER_START:s[0],IN_BUFFER_SIZE:s[1],OUT_BUFFER_START:s[2],OUT_BUFFER_SIZE:s[3],DEBUG_BUFFER_START:s[4],DEBUG_BUFFER_SIZE:s[5],CONTROL_START:s[6],CONTROL_SIZE:s[7],METRICS_START:s[8],METRICS_SIZE:s[9],NODE_TREE_START:s[10],NODE_TREE_SIZE:s[11],NODE_TREE_HEADER_SIZE:s[12],NODE_TREE_ENTRY_SIZE:s[13],NODE_TREE_DEF_NAME_SIZE:s[14],NODE_TREE_MIRROR_MAX_NODES:s[15],NTP_START_TIME_START:s[16],NTP_START_TIME_SIZE:s[17],DRIFT_OFFSET_START:s[18],DRIFT_OFFSET_SIZE:s[19],GLOBAL_OFFSET_START:s[20],GLOBAL_OFFSET_SIZE:s[21],AUDIO_CAPTURE_START:s[22],AUDIO_CAPTURE_SIZE:s[23],AUDIO_CAPTURE_HEADER_SIZE:s[24],AUDIO_CAPTURE_FRAMES:s[25],AUDIO_CAPTURE_CHANNELS:s[26],AUDIO_CAPTURE_SAMPLE_RATE:s[27],TOTAL_BUFFER_SIZE:s[28],MAX_MESSAGE_SIZE:s[29],MESSAGE_MAGIC:s[30],PADDING_MAGIC:s[31],scheduler_slot_size:s[32],scheduler_slot_count:s[33],DEBUG_PADDING_MARKER:i[136],MESSAGE_HEADER_SIZE:16},this.bufferConstants.MESSAGE_MAGIC!==3735928559)throw new Error("Invalid buffer constants from WASM")}calculateBufferIndices(e){if(!this.bufferConstants)throw new Error("Buffer constants not loaded. Call loadBufferConstants() first.");let t=this.bufferConstants.CONTROL_START,s=this.bufferConstants.METRICS_START;if(this.CONTROL_INDICES=g(e,t),this.mode==="sab"){let i=e+s;this.metricsView=new Uint32Array(this.sharedBuffer,i,this.bufferConstants.METRICS_SIZE/4)}else{this.atomicView=new Int32Array(this.wasmMemory.buffer),this.uint8View=new Uint8Array(this.wasmMemory.buffer),this.dataView=new DataView(this.wasmMemory.buffer);let i=e+s;this.metricsView=new Uint32Array(this.wasmMemory.buffer,i,this.bufferConstants.METRICS_SIZE/4);let r=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD);this.atomicStore(this.CONTROL_INDICES.IN_LOG_TAIL,r)}}writeWorldOptionsToMemory(){if(!this.worldOptions||!this.wasmMemory)return;let e=this.ringBufferBase+65536,t=new Uint32Array(this.wasmMemory.buffer,e,32),s=new Float32Array(this.wasmMemory.buffer,e,32);t[0]=this.worldOptions.numBuffers||1024,t[1]=this.worldOptions.maxNodes||1024,t[2]=this.worldOptions.maxGraphDefs||1024,t[3]=this.worldOptions.maxWireBufs||64,t[4]=this.worldOptions.numAudioBusChannels||128,t[5]=this.worldOptions.numInputBusChannels||0,t[6]=this.worldOptions.numOutputBusChannels||0,t[7]=this.worldOptions.numControlBusChannels||4096,t[8]=this.worldOptions.bufLength||128,t[9]=this.worldOptions.realTimeMemorySize||16384,t[10]=this.worldOptions.numRGens||64,t[11]=this.worldOptions.realTime?1:0,t[12]=this.worldOptions.memoryLocking?1:0,t[13]=this.worldOptions.loadGraphDefs||0,t[14]=this.worldOptions.preferredSampleRate||0,t[15]=this.worldOptions.verbosity||0,t[16]=this.mode==="postMessage"?1:0}js_debug(e){if(!(!this.uint8View||!this.atomicView||!this.CONTROL_INDICES||!this.ringBufferBase))try{let t=this.bufferConstants.DEBUG_BUFFER_START,s=this.bufferConstants.DEBUG_BUFFER_SIZE,i=this.bufferConstants.DEBUG_PADDING_MARKER,r="[JS] "+e+`
|
|
2
|
+
`,E=new TextEncoder().encode(r);if(E.length>s)return;let n=this.CONTROL_INDICES.DEBUG_HEAD,_=this.atomicLoad(n),a=s-_,u=_;E.length>a&&(this.uint8View[this.ringBufferBase+t+_]=i,u=0);let c=this.ringBufferBase+t;for(let f=0;f<E.length;f++)this.uint8View[c+u+f]=E[f];let h=u+E.length;this.atomicStore(n,h),this.mode==="sab"&&Atomics.notify(this.atomicView,n,1)}catch{}}atomicLoad(e){return this.mode==="sab"?Atomics.load(this.atomicView,e):this.atomicView[e]}atomicStore(e,t){this.mode==="sab"?Atomics.store(this.atomicView,e,t):this.atomicView[e]=t}initPMPools(){if(this.mode!=="postMessage")return;let e=C;this.pmPools={replies:{message:{type:"oscReplies",messages:null,count:0},buffer:new ArrayBuffer(e.REPLY_BUFFER_SIZE),bufferView:null,entries:new Array(e.MAX_REPLY_MESSAGES).fill(null).map(()=>({offset:0,length:0,sequence:0}))},debug:{message:{type:"debugRawBatch",messages:null,count:0},buffer:new ArrayBuffer(e.DEBUG_BUFFER_SIZE),bufferView:null,entries:new Array(e.MAX_DEBUG_MESSAGES).fill(null).map(()=>({offset:0,length:0,sequence:0}))},snapshot:{message:{type:"snapshot",buffer:null,snapshotsSent:0},buffer:null,bufferView:null,size:0},log:{message:{type:"oscLog",entries:null,count:0,buffer:null},buffer:new ArrayBuffer(e.LOG_BUFFER_SIZE),bufferView:null,entries:new Array(e.MAX_LOG_ENTRIES).fill(null).map(()=>({offset:0,length:0,originalLength:0,sourceId:0,sequence:0}))},incoming:{headerBytes:new Uint8Array(16),headerView:null}};let t=this.pmPools;if(t.replies.bufferView=new Uint8Array(t.replies.buffer),t.debug.bufferView=new Uint8Array(t.debug.buffer),t.log.bufferView=new Uint8Array(t.log.buffer),t.replies.message.messages=t.replies.entries,t.debug.message.messages=t.debug.entries,t.log.message.entries=t.log.entries,t.incoming.headerView=new DataView(t.incoming.headerBytes.buffer),this.bufferConstants&&this.wasmMemory){let s=this.bufferConstants,i=s.METRICS_SIZE+s.NODE_TREE_SIZE;t.snapshot.buffer=new ArrayBuffer(i),t.snapshot.bufferView=new Uint8Array(t.snapshot.buffer),t.snapshot.size=i,t.snapshot.message.buffer=t.snapshot.buffer;let r=this.ringBufferBase+s.METRICS_START;t.snapshot.sourceView=new Uint8Array(this.wasmMemory.buffer,r,i)}}writeOscToRingBuffer(e,t=0){if(!this.bufferConstants||!this.uint8View||!this.pmPools)return!1;let s=this.bufferConstants.IN_BUFFER_SIZE,i=this.bufferConstants.MESSAGE_HEADER_SIZE,r=this.ringBufferBase+this.bufferConstants.IN_BUFFER_START,o=e.byteLength,n=i+o+3&-4,_=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),a=this.atomicLoad(this.CONTROL_INDICES.IN_TAIL),u=R(_,a,s);if(n>u)return console.error("[AudioWorklet] Ring buffer full, dropping OSC message"),!1;let c=this.atomicLoad(this.CONTROL_INDICES.IN_SEQUENCE);this.atomicStore(this.CONTROL_INDICES.IN_SEQUENCE,c+1);let h=new Uint8Array(e),f=O({uint8View:this.uint8View,dataView:this.dataView,bufferStart:r,bufferSize:s,head:_,payload:h,sequence:c,messageMagic:this.bufferConstants.MESSAGE_MAGIC,headerSize:i,sourceId:t,headerScratch:this.pmPools.incoming.headerBytes,headerScratchView:this.pmPools.incoming.headerView});return this.atomicStore(this.CONTROL_INDICES.IN_HEAD,f),!0}readOscReplies(){if(!this.pmPools)return;let e=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),t=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);if(e===t)return;let s=this.pmPools.replies,i=C,r=0,o=0,{newTail:E,messagesRead:n}=d({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.OUT_BUFFER_START,bufferSize:this.bufferConstants.OUT_BUFFER_SIZE,head:e,tail:t,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_REPLY_MESSAGES,onMessage:(_,a,u,c)=>{if(r>=i.MAX_REPLY_MESSAGES||o+a>i.REPLY_BUFFER_SIZE)return;for(let f=0;f<a;f++)s.bufferView[o+f]=this.uint8View[_+f];let h=s.entries[r];h.offset=o,h.length=a,h.sequence=u,o+=a,r++}});n>0&&this.atomicStore(this.CONTROL_INDICES.OUT_TAIL,E),r>0&&(s.message.count=r,s.message.buffer=s.buffer,this.port.postMessage(s.message))}readNodeTree(){if(!this.bufferConstants||!this.wasmMemory||this.ringBufferBase===null)return null;let e=this.bufferConstants,t=this.ringBufferBase+e.NODE_TREE_START,s=new Uint32Array(this.wasmMemory.buffer,t,2),i=s[0],r=s[1],o=t+e.NODE_TREE_HEADER_SIZE,E=e.NODE_TREE_MIRROR_MAX_NODES,n=e.NODE_TREE_ENTRY_SIZE,_=e.NODE_TREE_DEF_NAME_SIZE,a=new DataView(this.wasmMemory.buffer,o,E*n),u=[],c=0;for(let h=0;h<E&&c<i;h++){let f=h*n,S=a.getInt32(f,!0);if(S===-1)continue;c++;let l=o+f+24,I=new Uint8Array(this.wasmMemory.buffer,l,_),p="";for(let T=0;T<_&&I[T]!==0;T++)p+=String.fromCharCode(I[T]);u.push({id:S,parentId:a.getInt32(f+4,!0),isGroup:a.getInt32(f+8,!0)===1,prevId:a.getInt32(f+12,!0),nextId:a.getInt32(f+16,!0),headId:a.getInt32(f+20,!0),defName:p})}return{nodeCount:i,version:r,nodes:u}}readMetrics(){return this.metricsView?new Uint32Array(this.metricsView):null}recordOscReceived(e,t=null){if(this.metricsView){if(this.mode==="sab")Atomics.add(this.metricsView,24,1),Atomics.add(this.metricsView,25,e);else if(this.metricsView[24]++,this.metricsView[25]+=e,t){this.metricsView[22]++;let i={nonBundle:38,immediate:39,nearFuture:40,late:41}[t];i!==void 0&&this.metricsView[i]++}}}checkAndSendSnapshot(e){let t=this.bufferConstants;if(!t||!this.wasmMemory||this.ringBufferBase===null||!this.pmPools)return!1;let i=(this.ringBufferBase+t.NODE_TREE_START+4)/4,r=this.atomicView[i];if(r!==this.lastTreeVersion)this.lastTreeVersion=r,this.lastTreeSendTime=e;else{if(this.lastTreeSendTime>=0&&e-this.lastTreeSendTime<this.treeSnapshotMinInterval)return!1;this.lastTreeSendTime=e}let E=this.pmPools.snapshot;return!E.buffer||!E.sourceView?!1:(E.bufferView.set(E.sourceView),this.treeSnapshotsSent++,E.message.snapshotsSent=this.treeSnapshotsSent,this.port.postMessage(E.message),!0)}readMetricsAndTreeBuffer(){if(!this.bufferConstants||!this.wasmMemory||this.ringBufferBase===null)return null;let e=this.bufferConstants,t=this.ringBufferBase+e.METRICS_START,s=e.METRICS_SIZE+e.NODE_TREE_SIZE,i=new Uint8Array(this.wasmMemory.buffer,t,s),r=new ArrayBuffer(s);return new Uint8Array(r).set(i),r}sendLogEntries(){if(!this.CONTROL_INDICES||!this.bufferConstants||!this.pmPools)return;let e=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),t=this.atomicLoad(this.CONTROL_INDICES.IN_LOG_TAIL);if(e===t)return;let s=this.pmPools.log,i=C,r=0,o=0,{newTail:E,messagesRead:n}=d({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.IN_BUFFER_START,bufferSize:this.bufferConstants.IN_BUFFER_SIZE,head:e,tail:t,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_LOG_ENTRIES,onMessage:(_,a,u,c)=>{if(r>=i.MAX_LOG_ENTRIES)return;let h=Math.min(a,i.LOG_MAX_MESSAGE_SIZE);if(o+h>i.LOG_BUFFER_SIZE)return;for(let S=0;S<h;S++)s.bufferView[o+S]=this.uint8View[_+S];let f=s.entries[r];f.offset=o,f.length=h,f.originalLength=a,f.sourceId=c??0,f.sequence=u,o+=h,r++}});n>0&&this.atomicStore(this.CONTROL_INDICES.IN_LOG_TAIL,E),r>0&&(s.message.count=r,s.message.buffer=s.buffer,this.port.postMessage(s.message))}readDebugMessages(){if(!this.pmPools)return;let e=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),t=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);if(e===t)return;let s=this.pmPools.debug,i=C,r=0,o=0,{newTail:E,messagesRead:n}=d({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.DEBUG_BUFFER_START,bufferSize:this.bufferConstants.DEBUG_BUFFER_SIZE,head:e,tail:t,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_DEBUG_MESSAGES,onMessage:(_,a,u,c)=>{if(r>=i.MAX_DEBUG_MESSAGES||o+a>i.DEBUG_BUFFER_SIZE)return;for(let f=0;f<a;f++)s.bufferView[o+f]=this.uint8View[_+f];let h=s.entries[r];h.offset=o,h.length=a,h.sequence=u,o+=a,r++}});n>0&&this.atomicStore(this.CONTROL_INDICES.DEBUG_TAIL,E),r>0&&(s.message.count=r,s.message.buffer=s.buffer,this.port.postMessage(s.message))}async handleMessage(e){let{data:t}=e;try{if(t.type==="osc"){this.mode==="postMessage"&&t.oscData&&(this.writeOscToRingBuffer(t.oscData,t.sourceId??0),this.recordOscReceived(t.oscData.byteLength,t.bypassCategory));return}if(t.type==="addOscPort"){let s=e.ports[0];if(s){let i=t.sourceId??0;this.portSourceIds.set(s,i),s.onmessage=r=>{if(r.data.type==="osc"&&r.data.oscData){let o=r.data.sourceId??this.portSourceIds.get(s)??0;this.writeOscToRingBuffer(r.data.oscData,o),this.recordOscReceived(r.data.oscData.byteLength,r.data.bypassCategory)}},this.oscPorts.push(s)}return}if(t.type==="clearSched"){this.pendingClearSched=!0,t.ack&&this.port.postMessage({type:"clearSchedAck"});return}if(t.type==="init"&&(this.mode=t.mode||"sab",t.snapshotIntervalMs&&(this.treeSnapshotMinInterval=t.snapshotIntervalMs/1e3),this.mode==="sab"&&t.sharedBuffer&&(this.sharedBuffer=t.sharedBuffer,this.atomicView=new Int32Array(this.sharedBuffer),this.uint8View=new Uint8Array(this.sharedBuffer),this.dataView=new DataView(this.sharedBuffer))),t.type==="loadWasm"){if(t.wasmBytes){let s;if(this.mode==="sab"){if(s=t.wasmMemory,!s){this.port.postMessage({type:"error",error:"No WASM memory provided!"});return}}else{let o=t.memoryPages||1280;s=new WebAssembly.Memory({initial:o,maximum:o,shared:!0})}this.wasmMemory=s,this.worldOptions=t.worldOptions||{},this.sampleRate=t.sampleRate||48e3;let i={env:{memory:s,emscripten_asm_const_double:()=>Date.now()*1e3,__syscall_getdents64:()=>0,__syscall_unlinkat:()=>0,_emscripten_init_main_thread_js:()=>{},_emscripten_thread_mailbox_await:()=>{},_emscripten_thread_set_strongref:()=>{},emscripten_exit_with_live_runtime:()=>{},_emscripten_receive_on_main_thread_js:()=>{},emscripten_check_blocking_allowed:()=>{},_emscripten_thread_cleanup:()=>{},emscripten_num_logical_cores:()=>1,_emscripten_notify_mailbox_postmessage:()=>{}},wasi_snapshot_preview1:{clock_time_get:(o,E,n)=>{let _=new DataView(s.buffer),a=BigInt(Math.floor(Date.now()*1e6));return _.setBigUint64(n,a,!0),0},environ_sizes_get:()=>0,environ_get:()=>0,fd_close:()=>0,fd_write:()=>0,fd_seek:()=>0,fd_read:()=>0,proc_exit:o=>{console.error("[AudioWorklet] WASM tried to exit with code:",o)}}},r=await WebAssembly.compile(t.wasmBytes);if(this.wasmInstance=await WebAssembly.instantiate(r,i),this.wasmInstance.exports.get_ring_buffer_base&&(this.ringBufferBase=this.wasmInstance.exports.get_ring_buffer_base(),this.loadBufferConstants(),this.calculateBufferIndices(this.ringBufferBase),this.initPMPools(),this.writeWorldOptionsToMemory(),this.wasmInstance.exports.init_memory)){this.wasmInstance.exports.init_memory(this.sampleRate),this.isInitialized=!0;let o=this.mode==="postMessage"?this.readMetricsAndTreeBuffer():void 0,E={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:o};this.port.postMessage(E,o?[o]:[])}}else if(t.wasmInstance&&(this.wasmInstance=t.wasmInstance,this.wasmInstance.exports.get_ring_buffer_base&&(this.ringBufferBase=this.wasmInstance.exports.get_ring_buffer_base(),this.loadBufferConstants(),this.calculateBufferIndices(this.ringBufferBase),this.initPMPools(),this.writeWorldOptionsToMemory(),this.wasmInstance.exports.init_memory))){this.wasmInstance.exports.init_memory(this.sampleRate),this.isInitialized=!0;let s=this.mode==="postMessage"?this.readMetricsAndTreeBuffer():void 0,i={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:s};this.port.postMessage(i,s?[s]:[])}}if(t.type==="getVersion")if(this.wasmInstance&&this.wasmInstance.exports.get_supersonic_version_string){let s=this.wasmInstance.exports.get_supersonic_version_string(),i=new Uint8Array(this.wasmMemory.buffer),r="";for(let o=s;i[o]!==0;o++)r+=String.fromCharCode(i[o]);this.port.postMessage({type:"version",version:r})}else this.port.postMessage({type:"version",version:"unknown"});if(t.type==="getTimeOffset")if(this.wasmInstance&&this.wasmInstance.exports.get_time_offset){let s=this.wasmInstance.exports.get_time_offset();this.port.postMessage({type:"timeOffset",offset:s})}else console.error("[AudioWorklet] get_time_offset not available! wasmInstance:",!!this.wasmInstance),this.port.postMessage({type:"error",error:"get_time_offset function not available in WASM exports"});if(t.type==="setNTPStartTime"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.NTP_START_TIME_START,i=new Float64Array(this.wasmMemory.buffer,s,1);i[0]=t.ntpStartTime}if(t.type==="setDriftOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.DRIFT_OFFSET_START,i=new Int32Array(this.wasmMemory.buffer,s,1);i[0]=t.driftOffsetMs}if(t.type==="setGlobalOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.GLOBAL_OFFSET_START,i=new Int32Array(this.wasmMemory.buffer,s,1);i[0]=t.globalOffsetMs}if(t.type==="getMetrics"){let s=this.metricsView?new Uint32Array(this.metricsView):null;this.port.postMessage({type:"metricsSnapshot",requestId:t.requestId,metrics:s})}if(t.type==="copyBufferData")try{let{copyId:s,ptr:i,data:r}=t;if(!this.wasmMemory||!this.wasmMemory.buffer)throw new Error("WASM memory not initialized");let o=new Float32Array(r);new Float32Array(this.wasmMemory.buffer,i,o.length).set(o),this.port.postMessage({type:"bufferCopied",copyId:s,success:!0})}catch(s){console.error("[AudioWorklet] Buffer copy failed:",s),this.port.postMessage({type:"bufferCopied",copyId:t.copyId,success:!1,error:s.message})}}catch(s){console.error("[AudioWorklet] Error handling message:",s),this.port.postMessage({type:"error",error:s.message,stack:s.stack})}}process(e,t,s){if(this.processCallCount++,!this.isInitialized)return!0;try{if(this.wasmInstance&&this.wasmInstance.exports.process_audio){this.pendingClearSched&&(this.pendingClearSched=!1,this.wasmInstance.exports.clear_scheduler&&this.wasmInstance.exports.clear_scheduler());let i=currentTime,r=e[0]?.length||0,o=t[0]?.length||0;if(r>0&&this.wasmInstance?.exports?.get_audio_input_bus)try{let n=this.wasmInstance.exports.get_audio_input_bus(),_=this.wasmInstance.exports.get_audio_buffer_samples();if(n&&n>0){let a=this.sharedBuffer||this.wasmMemory?.buffer;if(a){let u=this.worldOptions?.numInputBusChannels||2,c=Math.min(r,u);(!this.inputView||this.lastInputBusPtr!==n||this.lastInputChannels!==u)&&(this.inputView=new Float32Array(a,n,_*u),this.lastInputBusPtr=n,this.lastInputChannels=u);for(let h=0;h<c;h++)e[0]?.[h]&&this.inputView.set(e[0][h],h*_)}}}catch{}let E=this.wasmInstance.exports.process_audio(i,o,r);if(this.wasmInstance.exports.get_audio_output_bus&&t[0]&&t[0].length>=2)try{let n=this.wasmInstance.exports.get_audio_output_bus(),_=this.wasmInstance.exports.get_audio_buffer_samples();if(n&&n>0){let a=this.wasmInstance.exports.memory||this.wasmMemory;if(!a||!a.buffer)return!0;let u=a.buffer,c=u.byteLength,h=n+_*2*4;if(n<0||n>c||h>c)return!0;(!this.audioView||this.lastAudioBufferPtr!==n||this.lastWasmBufferSize!==c||u!==this.audioView.buffer)&&(this.audioView=new Float32Array(u,n,_*2),this.lastAudioBufferPtr=n,this.lastWasmBufferSize=c),(!this.leftChannelView||this.lastNumSamples!==_||this.leftChannelView.buffer!==this.audioView.buffer)&&(this.leftChannelView=this.audioView.subarray(0,_),this.rightChannelView=this.audioView.subarray(_,_*2),this.lastNumSamples=_),t[0][0].set(this.leftChannelView),t[0][1].set(this.rightChannelView)}}catch{}if(this.mode==="postMessage")this.readOscReplies(),this.readDebugMessages(),this.checkAndSendSnapshot(i)&&this.sendLogEntries();else if(this.atomicView){let n=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),_=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);n!==_&&Atomics.notify(this.atomicView,this.CONTROL_INDICES.OUT_HEAD,1);let a=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),u=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);a!==u&&Atomics.notify(this.atomicView,this.CONTROL_INDICES.DEBUG_HEAD,1),Atomics.notify(this.atomicView,this.CONTROL_INDICES.IN_TAIL,1)}return this.processCallCount%3750===0&&this.checkStatus(),E!==0}}catch(i){console.error("[AudioWorklet] process() error:",i),console.error("[AudioWorklet] Stack:",i.stack),this.atomicView&&this.mode==="sab"&&Atomics.or(this.atomicView,this.CONTROL_INDICES.STATUS_FLAGS,this.STATUS_FLAGS.WASM_ERROR),this.metricsView&&(this.mode==="sab"?Atomics.add(this.metricsView,7,1):this.metricsView[7]++)}return!0}checkStatus(){if(!this.atomicView)return;let e=this.atomicLoad(this.CONTROL_INDICES.STATUS_FLAGS);if(e!==this.STATUS_FLAGS.OK){this._statusObj.bufferFull=!!(e&this.STATUS_FLAGS.BUFFER_FULL),this._statusObj.overrun=!!(e&this.STATUS_FLAGS.OVERRUN),this._statusObj.wasmError=!!(e&this.STATUS_FLAGS.WASM_ERROR),this._statusObj.fragmented=!!(e&this.STATUS_FLAGS.FRAGMENTED_MSG),this._metricsObj.processCount=this.metricsView[0],this._metricsObj.messagesProcessed=this.metricsView[1],this._metricsObj.messagesDropped=this.metricsView[2],this._metricsObj.schedulerQueueDepth=this.metricsView[3],this._metricsObj.schedulerQueueMax=this.metricsView[4],this._metricsObj.schedulerQueueDropped=this.metricsView[5],this._statusMessage.flags=e,this.port.postMessage(this._statusMessage);let t=e&this.STATUS_FLAGS.BUFFER_FULL;this.atomicStore(this.CONTROL_INDICES.STATUS_FLAGS,t)}}};registerProcessor("scsynth-processor",w);})();
|