supersonic-scsynth-core 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.
package/package.json
CHANGED
package/wasm/scsynth-nrt.wasm
CHANGED
|
Binary file
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(()=>{function
|
|
2
|
-
`;C||(C=new TextEncoder);let r=C.encode(o);if(r.length>t)return;let h=this.CONTROL_INDICES.DEBUG_HEAD,n=this.atomicLoad(h),E=t-n,_=n;r.length>E&&(this.uint8View[this.ringBufferBase+e+n]=i,_=0);let S=this.ringBufferBase+e;for(let c=0;c<r.length;c++)this.uint8View[S+_+c]=r[c];let f=_+r.length;this.atomicStore(h,f),this.mode==="sab"&&Atomics.notify(this.atomicView,h,1)}catch{}}atomicLoad(s){return this.mode==="sab"?Atomics.load(this.atomicView,s):this.atomicView[s]}atomicStore(s,e){this.mode==="sab"?Atomics.store(this.atomicView,s,e):this.atomicView[s]=e}initPMPools(){if(this.mode!=="postMessage")return;let s=R;this.pmPools={replies:{message:{type:"oscReplies",messages:null,count:0},buffer:new ArrayBuffer(s.REPLY_BUFFER_SIZE),bufferView:null,entries:new Array(s.MAX_REPLY_MESSAGES).fill(null).map(()=>({offset:0,length:0,sequence:0}))},debug:{message:{type:"debugRawBatch",messages:null,count:0},buffer:new ArrayBuffer(s.DEBUG_BUFFER_SIZE),bufferView:null,entries:new Array(s.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(s.LOG_BUFFER_SIZE),bufferView:null,entries:new Array(s.MAX_LOG_ENTRIES).fill(null).map(()=>({offset:0,length:0,originalLength:0,sourceId:0,sequence:0}))},incoming:{headerBytes:new Uint8Array(16),headerView:null}};let e=this.pmPools;if(e.replies.bufferView=new Uint8Array(e.replies.buffer),e.debug.bufferView=new Uint8Array(e.debug.buffer),e.log.bufferView=new Uint8Array(e.log.buffer),e.replies.message.messages=e.replies.entries,e.debug.message.messages=e.debug.entries,e.log.message.entries=e.log.entries,e.incoming.headerView=new DataView(e.incoming.headerBytes.buffer),this.bufferConstants&&this.wasmMemory){let t=this.bufferConstants,i=t.METRICS_SIZE+t.NODE_TREE_SIZE;e.snapshot.buffer=new ArrayBuffer(i),e.snapshot.bufferView=new Uint8Array(e.snapshot.buffer),e.snapshot.size=i,e.snapshot.message.buffer=e.snapshot.buffer;let o=this.ringBufferBase+t.METRICS_START;e.snapshot.sourceView=new Uint8Array(this.wasmMemory.buffer,o,i)}}writeOscToRingBuffer(s,e=0){if(!this.bufferConstants||!this.uint8View||!this.pmPools)return!1;let t=this.bufferConstants.IN_BUFFER_SIZE,i=this.bufferConstants.MESSAGE_HEADER_SIZE,o=this.ringBufferBase+this.bufferConstants.IN_BUFFER_START,r=s.byteLength,n=i+r+3&-4,E=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),_=this.atomicLoad(this.CONTROL_INDICES.IN_TAIL),S=w(E,_,t);if(n>S)return console.error("[AudioWorklet] Ring buffer full, dropping OSC message"),!1;let f=this.atomicLoad(this.CONTROL_INDICES.IN_SEQUENCE);this.atomicStore(this.CONTROL_INDICES.IN_SEQUENCE,f+1);let c=new Uint8Array(s),a=g({uint8View:this.uint8View,dataView:this.dataView,bufferStart:o,bufferSize:t,head:E,payload:c,sequence:f,messageMagic:this.bufferConstants.MESSAGE_MAGIC,headerSize:i,sourceId:e,headerScratch:this.pmPools.incoming.headerBytes,headerScratchView:this.pmPools.incoming.headerView});return this.atomicStore(this.CONTROL_INDICES.IN_HEAD,a),!0}readOscReplies(){if(!this.pmPools)return;let s=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),e=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);if(s===e)return;let t=this.pmPools.replies,i=R,o=0,r=0,{newTail:h,messagesRead:n}=p({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.OUT_BUFFER_START,bufferSize:this.bufferConstants.OUT_BUFFER_SIZE,head:s,tail:e,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_REPLY_MESSAGES,onMessage:(E,_,S,f)=>{if(o>=i.MAX_REPLY_MESSAGES||r+_>i.REPLY_BUFFER_SIZE)return;for(let a=0;a<_;a++)t.bufferView[r+a]=this.uint8View[E+a];let c=t.entries[o];c.offset=r,c.length=_,c.sequence=S,r+=_,o++}});n>0&&this.atomicStore(this.CONTROL_INDICES.OUT_TAIL,h),o>0&&(t.message.count=o,t.message.buffer=t.buffer,this.port.postMessage(t.message))}readNodeTree(){if(!this.bufferConstants||!this.wasmMemory||this.ringBufferBase===null)return null;let s=this.bufferConstants,e=this.ringBufferBase+s.NODE_TREE_START,t=new Uint32Array(this.wasmMemory.buffer,e,2),i=t[0],o=t[1],r=e+s.NODE_TREE_HEADER_SIZE,h=s.NODE_TREE_MIRROR_MAX_NODES,n=s.NODE_TREE_ENTRY_SIZE,E=s.NODE_TREE_DEF_NAME_SIZE,_=new DataView(this.wasmMemory.buffer,r,h*n),S=[],f=0;for(let c=0;c<h&&f<i;c++){let a=c*n,l=_.getInt32(a,!0);if(l===-1)continue;f++;let u=r+a+24,m=new Uint8Array(this.wasmMemory.buffer,u,E),T="";for(let d=0;d<E&&m[d]!==0;d++)T+=String.fromCharCode(m[d]);S.push({id:l,parentId:_.getInt32(a+4,!0),isGroup:_.getInt32(a+8,!0)===1,prevId:_.getInt32(a+12,!0),nextId:_.getInt32(a+16,!0),headId:_.getInt32(a+20,!0),defName:T})}return{nodeCount:i,version:o,nodes:S}}readMetrics(){return this.metricsView?new Uint32Array(this.metricsView):null}recordOscReceived(s,e=null){if(this.metricsView){if(this.mode==="sab")Atomics.add(this.metricsView,24,1),Atomics.add(this.metricsView,25,s);else if(this.metricsView[24]++,this.metricsView[25]+=s,e){this.metricsView[22]++;let t=X[e];t!==void 0&&this.metricsView[t]++}}}checkAndSendSnapshot(s){let e=this.bufferConstants;if(!e||!this.wasmMemory||this.ringBufferBase===null||!this.pmPools)return!1;let i=(this.ringBufferBase+e.NODE_TREE_START+4)/4,o=this.atomicView[i];if(o!==this.lastTreeVersion)this.lastTreeVersion=o,this.lastTreeSendTime=s;else{if(this.lastTreeSendTime>=0&&s-this.lastTreeSendTime<this.treeSnapshotMinInterval)return!1;this.lastTreeSendTime=s}let h=this.pmPools.snapshot;return!h.buffer||!h.sourceView?!1:(h.bufferView.set(h.sourceView),this.treeSnapshotsSent++,h.message.snapshotsSent=this.treeSnapshotsSent,this.port.postMessage(h.message),!0)}readMetricsAndTreeBuffer(){if(!this.bufferConstants||!this.wasmMemory||this.ringBufferBase===null)return null;let s=this.bufferConstants,e=this.ringBufferBase+s.METRICS_START,t=s.METRICS_SIZE+s.NODE_TREE_SIZE,i=new Uint8Array(this.wasmMemory.buffer,e,t),o=new ArrayBuffer(t);return new Uint8Array(o).set(i),o}sendLogEntries(){if(!this.CONTROL_INDICES||!this.bufferConstants||!this.pmPools)return;let s=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),e=this.atomicLoad(this.CONTROL_INDICES.IN_LOG_TAIL);if(s===e)return;let t=this.pmPools.log,i=R,o=0,r=0,{newTail:h,messagesRead:n}=p({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.IN_BUFFER_START,bufferSize:this.bufferConstants.IN_BUFFER_SIZE,head:s,tail:e,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_LOG_ENTRIES,onMessage:(E,_,S,f)=>{if(o>=i.MAX_LOG_ENTRIES)return;let c=Math.min(_,i.LOG_MAX_MESSAGE_SIZE);if(r+c>i.LOG_BUFFER_SIZE)return;for(let l=0;l<c;l++)t.bufferView[r+l]=this.uint8View[E+l];let a=t.entries[o];a.offset=r,a.length=c,a.originalLength=_,a.sourceId=f??0,a.sequence=S,r+=c,o++}});n>0&&this.atomicStore(this.CONTROL_INDICES.IN_LOG_TAIL,h),o>0&&(t.message.count=o,t.message.buffer=t.buffer,this.port.postMessage(t.message))}readDebugMessages(){if(!this.pmPools)return;let s=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),e=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);if(s===e)return;let t=this.pmPools.debug,i=R,o=0,r=0,{newTail:h,messagesRead:n}=p({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.DEBUG_BUFFER_START,bufferSize:this.bufferConstants.DEBUG_BUFFER_SIZE,head:s,tail:e,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_DEBUG_MESSAGES,onMessage:(E,_,S,f)=>{if(o>=i.MAX_DEBUG_MESSAGES||r+_>i.DEBUG_BUFFER_SIZE)return;for(let a=0;a<_;a++)t.bufferView[r+a]=this.uint8View[E+a];let c=t.entries[o];c.offset=r,c.length=_,c.sequence=S,r+=_,o++}});n>0&&this.atomicStore(this.CONTROL_INDICES.DEBUG_TAIL,h),o>0&&(t.message.count=o,t.message.buffer=t.buffer,this.port.postMessage(t.message))}async handleMessage(s){let{data:e}=s;try{if(e.type==="osc"){this.mode==="postMessage"&&e.oscData&&(this.writeOscToRingBuffer(e.oscData,e.sourceId??0),this.recordOscReceived(e.oscData.byteLength,e.bypassCategory));return}if(e.type==="addOscPort"){let t=s.ports[0];if(t){let i=e.sourceId??0;this.portSourceIds.set(t,i),t.onmessage=o=>{if(o.data.type==="osc"&&o.data.oscData){let r=o.data.sourceId??this.portSourceIds.get(t)??0;this.writeOscToRingBuffer(o.data.oscData,r),this.recordOscReceived(o.data.oscData.byteLength,o.data.bypassCategory)}},this.oscPorts.push(t)}return}if(e.type==="clearSched"){if(this.CONTROL_INDICES){let t=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD);this.atomicStore(this.CONTROL_INDICES.IN_TAIL,t)}this.pendingClearSched=!0,e.ack&&this.port.postMessage({type:"clearSchedAck"});return}if(e.type==="nodeIdRange"){e.from!==void 0&&e.to!==void 0&&this.pushNodeIdRange(e.from,e.to);let t=s.ports[0];t&&(this.nodeIdPort=t,this.nodeIdPort.onmessage=i=>{i.data.type==="nodeIdRange"&&this.pushNodeIdRange(i.data.from,i.data.to)});return}if(e.type==="init"&&(this.mode=e.mode||"sab",e.snapshotIntervalMs&&(this.treeSnapshotMinInterval=e.snapshotIntervalMs/1e3),this.mode==="sab"&&e.sharedBuffer&&(this.sharedBuffer=e.sharedBuffer,this.atomicView=new Int32Array(this.sharedBuffer),this.uint8View=new Uint8Array(this.sharedBuffer),this.dataView=new DataView(this.sharedBuffer))),e.type==="loadWasm"){if(e.wasmBytes){let t;if(this.mode==="sab"){if(t=e.wasmMemory,!t){this.port.postMessage({type:"error",error:"No WASM memory provided!"});return}}else{let r=e.memoryPages||1280,h=e.maxMemoryPages||r;t=new WebAssembly.Memory({initial:r,maximum:h,shared:!0})}this.wasmMemory=t,this.worldOptions=e.worldOptions||{},this.sampleRate=e.sampleRate||48e3;let i={env:{memory:t,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:()=>{},emscripten_notify_memory_growth:()=>{}},wasi_snapshot_preview1:{clock_time_get:(r,h,n)=>{let E=new DataView(t.buffer),_=BigInt(Math.floor(Date.now()*1e6));return E.setBigUint64(n,_,!0),0},environ_sizes_get:()=>0,environ_get:()=>0,fd_close:()=>0,fd_write:()=>0,fd_seek:()=>0,fd_read:()=>0,proc_exit:r=>{console.error("[AudioWorklet] WASM tried to exit with code:",r)}}},o=await WebAssembly.compile(e.wasmBytes);if(this.wasmInstance=await WebAssembly.instantiate(o,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.initNodeIdCounter(),this.isInitialized=!0;let r=this.mode==="postMessage"?this.readMetricsAndTreeBuffer():void 0,h={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:r};this.port.postMessage(h,r?[r]:[])}}else if(e.wasmInstance&&(this.wasmInstance=e.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.initNodeIdCounter(),this.isInitialized=!0;let t=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:t};this.port.postMessage(i,t?[t]:[])}}if(e.type==="getVersion")if(this.wasmInstance&&this.wasmInstance.exports.get_supersonic_version_string){let t=this.wasmInstance.exports.get_supersonic_version_string(),i=new Uint8Array(this.wasmMemory.buffer),o="";for(let r=t;i[r]!==0;r++)o+=String.fromCharCode(i[r]);this.port.postMessage({type:"version",version:o})}else this.port.postMessage({type:"version",version:"unknown"});if(e.type==="getTimeOffset")if(this.wasmInstance&&this.wasmInstance.exports.get_time_offset){let t=this.wasmInstance.exports.get_time_offset();this.port.postMessage({type:"timeOffset",offset:t})}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(e.type==="setNTPStartTime"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let t=this.ringBufferBase+this.bufferConstants.NTP_START_TIME_START,i=new Float64Array(this.wasmMemory.buffer,t,1);i[0]=e.ntpStartTime}if(e.type==="setDriftOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let t=this.ringBufferBase+this.bufferConstants.DRIFT_OFFSET_START,i=new Int32Array(this.wasmMemory.buffer,t,1);i[0]=e.driftOffsetMs}if(e.type==="setClockOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let t=this.ringBufferBase+this.bufferConstants.GLOBAL_OFFSET_START,i=new Int32Array(this.wasmMemory.buffer,t,1);i[0]=e.clockOffsetMs}if(e.type==="getMetrics"){let t=this.metricsView?new Uint32Array(this.metricsView):null;this.port.postMessage({type:"metricsSnapshot",requestId:e.requestId,metrics:t})}if(e.type==="copyBufferData")try{let{copyId:t,ptr:i,data:o}=e;if(!this.wasmMemory||!this.wasmMemory.buffer)throw new Error("WASM memory not initialized");let r=new Float32Array(o);new Float32Array(this.wasmMemory.buffer,i,r.length).set(r),this.port.postMessage({type:"bufferCopied",copyId:t,success:!0})}catch(t){console.error("[AudioWorklet] Buffer copy failed:",t),this.port.postMessage({type:"bufferCopied",copyId:e.copyId,success:!1,error:t.message})}if(e.type==="growMemory")try{let{growId:t,pages:i}=e;if(!this.wasmMemory)throw new Error("WASM memory not initialized");let r=this.wasmMemory.grow(i)!==-1;this.port.postMessage({type:"memoryGrown",growId:t,success:r,newBufferSize:this.wasmMemory.buffer.byteLength})}catch(t){console.error("[AudioWorklet] Memory grow failed:",t),this.port.postMessage({type:"memoryGrown",growId:e.growId,success:!1,error:t.message})}}catch(t){console.error("[AudioWorklet] Error handling message:",t),this.port.postMessage({type:"error",error:t.message,stack:t.stack})}}process(s,e,t){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,o=s[0]?.length||0,r=e[0]?.length||0;if(o>0&&this.wasmInstance?.exports?.get_audio_input_bus)try{let n=this.wasmInstance.exports.get_audio_input_bus(),E=this.wasmInstance.exports.get_audio_buffer_samples();if(n&&n>0){let _=this.sharedBuffer||this.wasmMemory?.buffer;if(_){let S=this.worldOptions?.numInputBusChannels||2,f=Math.min(o,S);(!this.inputView||this.lastInputBusPtr!==n||this.lastInputChannels!==S)&&(this.inputView=new Float32Array(_,n,E*S),this.lastInputBusPtr=n,this.lastInputChannels=S);for(let c=0;c<f;c++)s[0]?.[c]&&this.inputView.set(s[0][c],c*E)}}}catch{}let h=this.wasmInstance.exports.process_audio(i,r,o);if(this.nodeIdCounterView&&this.nodeIdRangeCount>0){let n=Atomics.load(this.nodeIdCounterView,0);if(n>=this.nodeIdRanges[0].to){if(this.nodeIdRangeCount>1){let E=this.nodeIdRanges[1];this.nodeIdRanges[0].from=E.from,this.nodeIdRanges[0].to=E.to,E.from=0,E.to=0,this.nodeIdRangeCount=1,Atomics.store(this.nodeIdCounterView,0,this.nodeIdRanges[0].from)}!this.nodeIdRefillRequested&&this.nodeIdPort&&(this.nodeIdRefillRequested=!0,this.nodeIdPort.postMessage({type:"requestNodeIdRange"}))}else if(!this.nodeIdRefillRequested&&this.nodeIdRangeCount<2&&this.nodeIdPort){let E=this.nodeIdRanges[0].to-n,_=this.nodeIdRanges[0].to-this.nodeIdRanges[0].from;E<=_>>>1&&(this.nodeIdRefillRequested=!0,this.nodeIdPort.postMessage({type:"requestNodeIdRange"}))}}if(this.wasmInstance.exports.get_audio_output_bus&&e[0]&&e[0].length>=1)try{let n=this.wasmInstance.exports.get_audio_output_bus(),E=this.wasmInstance.exports.get_audio_buffer_samples();if(n&&n>0){let _=this.wasmInstance.exports.memory||this.wasmMemory;if(!_||!_.buffer)return!0;let S=_.buffer,f=S.byteLength,c=this.worldOptions?.numOutputBusChannels||2,a=Math.min(e[0].length,c),l=n+E*a*4;if(n<0||n>f||l>f)return!0;if((!this.audioView||this.lastAudioBufferPtr!==n||this.lastWasmBufferSize!==f||this.lastNumChannels!==a||S!==this.audioView.buffer)&&(this.audioView=new Float32Array(S,n,E*a),this.lastAudioBufferPtr=n,this.lastWasmBufferSize=f),!this.channelViews||this.lastNumSamples!==E||this.lastNumChannels!==a||this.channelViews[0].buffer!==this.audioView.buffer){this.channelViews=new Array(a);for(let u=0;u<a;u++)this.channelViews[u]=this.audioView.subarray(u*E,(u+1)*E);this.lastNumSamples=E,this.lastNumChannels=a}for(let u=0;u<a;u++)e[0][u].set(this.channelViews[u])}}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),E=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);n!==E&&Atomics.notify(this.atomicView,this.CONTROL_INDICES.OUT_HEAD,1);let _=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),S=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);_!==S&&Atomics.notify(this.atomicView,this.CONTROL_INDICES.DEBUG_HEAD,1);let f=this.atomicLoad(this.CONTROL_INDICES.IN_TAIL);f!==this.lastInTail&&(Atomics.notify(this.atomicView,this.CONTROL_INDICES.IN_TAIL,1),this.lastInTail=f)}return this.processCallCount%3750===0&&this.checkStatus(),h!==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 s=this.atomicLoad(this.CONTROL_INDICES.STATUS_FLAGS);if(s!==this.STATUS_FLAGS.OK){this._statusObj.bufferFull=!!(s&this.STATUS_FLAGS.BUFFER_FULL),this._statusObj.overrun=!!(s&this.STATUS_FLAGS.OVERRUN),this._statusObj.wasmError=!!(s&this.STATUS_FLAGS.WASM_ERROR),this._statusObj.fragmented=!!(s&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=s,this.port.postMessage(this._statusMessage);let e=s&this.STATUS_FLAGS.BUFFER_FULL;this.atomicStore(this.CONTROL_INDICES.STATUS_FLAGS,e)}}};registerProcessor("scsynth-processor",O);})();
|
|
1
|
+
(()=>{function D(m,s,e){return(e-1-m+s)%e}function M({uint8View:m,dataView:s,bufferStart:e,bufferSize:t,head:i,payload:o,sequence:r,messageMagic:c,headerSize:n,sourceId:_=0,headerScratch:E=null,headerScratchView:S=null}){let f=o.length,a=n+f+3&-4,I=t-i;if(a>I){let u=E||new Uint8Array(n),T=S||new DataView(u.buffer);T.setUint32(0,c,!0),T.setUint32(4,a,!0),T.setUint32(8,r,!0),T.setUint32(12,_,!0);let A=e+i,R=e;if(I>=n){m.set(u,A);let p=I-n;for(let l=0;l<p;l++)m[A+n+l]=o[l];for(let l=p;l<f;l++)m[R+l-p]=o[l]}else{for(let l=0;l<I;l++)m[A+l]=u[l];for(let l=I;l<n;l++)m[R+l-I]=u[l];let p=n-I;m.set(o,R+p)}}else{let u=e+i;s.setUint32(u,c,!0),s.setUint32(u+4,a,!0),s.setUint32(u+8,r,!0),s.setUint32(u+12,_,!0),m.set(o,u+n)}return(i+a)%t}function O({uint8View:m,dataView:s,bufferStart:e,bufferSize:t,head:i,tail:o,messageMagic:r,paddingMagic:c,headerSize:n,maxMessages:_=1/0,onMessage:E,onCorruption:S}){let f=o,h=0,a=I=>{let u=I;if(u+4<=t)return s.getUint32(e+u,!0);let T=0;for(let A=0;A<4;A++)T|=m[e+(u+A)%t]<<A*8;return T};for(;f!==i&&h<_;){let I=t-f,u;if(I>=4?u=s.getUint32(e+f,!0):u=a(f),u===c){f=0;continue}if(u!==r){S&&S(f),f=(f+1)%t;continue}let T=a((f+4)%t),A=a((f+8)%t),R=a((f+12)%t);if(T<n||T>t){S&&S(f),f=(f+1)%t;continue}let p=T-n,l=e+(f+n)%t;E(l,p,A,R),f=(f+T)%t,h++}return{newTail:f,messagesRead:h}}function L(m,s){let e=m+s;return{IN_HEAD:(e+0)/4,IN_TAIL:(e+4)/4,OUT_HEAD:(e+8)/4,OUT_TAIL:(e+12)/4,DEBUG_HEAD:(e+16)/4,DEBUG_TAIL:(e+20)/4,IN_SEQUENCE:(e+24)/4,OUT_SEQUENCE:(e+28)/4,DEBUG_SEQUENCE:(e+32)/4,STATUS_FLAGS:(e+36)/4,IN_WRITE_LOCK:(e+40)/4,IN_LOG_TAIL:(e+44)/4}}var g=null,k={nonBundle:38,immediate:39,nearFuture:40,late:41},w={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},N=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.lastInTail=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.replyPorts=[],this.channelViews=null,this.lastNumSamples=0,this.lastNumChannels=0,this.pmPools=null,this.nodeIdRanges=[{from:0,to:0},{from:0,to:0}],this.nodeIdRangeCount=0,this.nodeIdRefillRequested=!1,this.nodeIdPort=null,this.nodeIdCounterView=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 s=this.wasmInstance.exports.get_buffer_layout(),e=this.wasmMemory;if(!e)throw new Error("WASM memory not available");let t=new Uint32Array(e.buffer,s,54),i=new Uint8Array(e.buffer,s,220);if(this.bufferConstants={IN_BUFFER_START:t[0],IN_BUFFER_SIZE:t[1],OUT_BUFFER_START:t[2],OUT_BUFFER_SIZE:t[3],DEBUG_BUFFER_START:t[4],DEBUG_BUFFER_SIZE:t[5],CONTROL_START:t[6],CONTROL_SIZE:t[7],METRICS_START:t[8],METRICS_SIZE:t[9],NODE_TREE_START:t[10],NODE_TREE_SIZE:t[11],NODE_TREE_HEADER_SIZE:t[12],NODE_TREE_ENTRY_SIZE:t[13],NODE_TREE_DEF_NAME_SIZE:t[14],NODE_TREE_MIRROR_MAX_NODES:t[15],NTP_START_TIME_START:t[16],NTP_START_TIME_SIZE:t[17],DRIFT_OFFSET_START:t[18],DRIFT_OFFSET_SIZE:t[19],GLOBAL_OFFSET_START:t[20],GLOBAL_OFFSET_SIZE:t[21],SHM_AUDIO_START:t[22],SHM_AUDIO_TOTAL_SIZE:t[23],SHM_AUDIO_HEADER_SIZE:t[24],SHM_AUDIO_FRAMES:t[25],SHM_AUDIO_CHANNELS:t[26],SHM_AUDIO_SAMPLE_RATE:t[27],NODE_ID_COUNTER_START:t[28],NODE_ID_COUNTER_SIZE:t[29],WORLD_OPTIONS_START:t[30],WORLD_OPTIONS_SIZE:t[31],REPLY_CHANNELS_CONTROL_START:t[32],REPLY_CHANNELS_CONTROL_SIZE:t[33],REPLY_CHANNELS_BUFFER_START:t[34],REPLY_CHANNEL_BUFFER_SIZE:t[35],REPLY_CHANNEL_CONTROL_SIZE:t[36],REPLY_CHANNEL_COUNT:t[37],SHM_SCOPE_START:t[38],SHM_SCOPE_TOTAL_SIZE:t[39],SHM_SCOPE_HEADER_SIZE:t[40],SHM_SCOPE_SLOT_SIZE:t[41],SHM_SCOPE_SLOT_HEADER_SIZE:t[42],SHM_SCOPE_REGION_SIZE:t[43],SHM_SCOPE_MAX_SCOPES:t[44],SHM_SCOPE_FRAMES_PER_SCOPE:t[45],SHM_SCOPE_CHANNELS:t[46],TOTAL_BUFFER_SIZE:t[47],MAX_MESSAGE_SIZE:t[48],MESSAGE_MAGIC:t[49],PADDING_MAGIC:t[50],scheduler_data_pool_size:t[51],scheduler_slot_count:t[52],DEBUG_PADDING_MARKER:i[212],MESSAGE_HEADER_SIZE:16},this.bufferConstants.MESSAGE_MAGIC!==3735928559)throw new Error("Invalid buffer constants from WASM")}calculateBufferIndices(s){if(!this.bufferConstants)throw new Error("Buffer constants not loaded. Call loadBufferConstants() first.");let e=this.bufferConstants.CONTROL_START,t=this.bufferConstants.METRICS_START;if(this.CONTROL_INDICES=L(s,e),this.mode==="sab"){let i=s+t;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=s+t;this.metricsView=new Uint32Array(this.wasmMemory.buffer,i,this.bufferConstants.METRICS_SIZE/4);let o=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD);this.atomicStore(this.CONTROL_INDICES.IN_LOG_TAIL,o)}}initNodeIdCounter(){if(!this.wasmMemory||!this.bufferConstants||!this.ringBufferBase||this.mode!=="postMessage")return;let s=this.ringBufferBase+this.bufferConstants.NODE_ID_COUNTER_START;this.nodeIdCounterView=new Int32Array(this.wasmMemory.buffer,s,1),this.nodeIdRangeCount>0&&Atomics.store(this.nodeIdCounterView,0,this.nodeIdRanges[0].from)}pushNodeIdRange(s,e){if(this.nodeIdRangeCount<2){let t=this.nodeIdRanges[this.nodeIdRangeCount];t.from=s,t.to=e,this.nodeIdRangeCount++,this.nodeIdRefillRequested=!1,this.nodeIdRangeCount===1&&this.nodeIdCounterView&&Atomics.store(this.nodeIdCounterView,0,s)}}writeWorldOptionsToMemory(){if(!this.worldOptions||!this.wasmMemory)return;let s=this.bufferConstants?.WORLD_OPTIONS_START;if(s===void 0){console.error("WORLD_OPTIONS_START not available in bufferConstants");return}let e=this.ringBufferBase+s,t=new Uint32Array(this.wasmMemory.buffer,e,32),i=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.worldOptions.rtPoolOffset||0,t[17]=this.mode==="postMessage"?1:0}js_debug(s){if(!(!this.uint8View||!this.atomicView||!this.CONTROL_INDICES||!this.ringBufferBase))try{let e=this.bufferConstants.DEBUG_BUFFER_START,t=this.bufferConstants.DEBUG_BUFFER_SIZE,i=this.bufferConstants.DEBUG_PADDING_MARKER,o="[JS] "+s+`
|
|
2
|
+
`;g||(g=new TextEncoder);let r=g.encode(o);if(r.length>t)return;let c=this.CONTROL_INDICES.DEBUG_HEAD,n=this.atomicLoad(c),_=t-n,E=n;r.length>_&&(this.uint8View[this.ringBufferBase+e+n]=i,E=0);let S=this.ringBufferBase+e;for(let h=0;h<r.length;h++)this.uint8View[S+E+h]=r[h];let f=E+r.length;this.atomicStore(c,f),this.mode==="sab"&&Atomics.notify(this.atomicView,c,1)}catch{}}atomicLoad(s){return this.mode==="sab"?Atomics.load(this.atomicView,s):this.atomicView[s]}atomicStore(s,e){this.mode==="sab"?Atomics.store(this.atomicView,s,e):this.atomicView[s]=e}initPMPools(){if(this.mode!=="postMessage")return;let s=w;this.pmPools={replies:{message:{type:"oscReplies",messages:null,count:0},buffer:new ArrayBuffer(s.REPLY_BUFFER_SIZE),bufferView:null,entries:new Array(s.MAX_REPLY_MESSAGES).fill(null).map(()=>({offset:0,length:0,sequence:0}))},debug:{message:{type:"debugRawBatch",messages:null,count:0},buffer:new ArrayBuffer(s.DEBUG_BUFFER_SIZE),bufferView:null,entries:new Array(s.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(s.LOG_BUFFER_SIZE),bufferView:null,entries:new Array(s.MAX_LOG_ENTRIES).fill(null).map(()=>({offset:0,length:0,originalLength:0,sourceId:0,sequence:0}))},incoming:{headerBytes:new Uint8Array(16),headerView:null}};let e=this.pmPools;if(e.replies.bufferView=new Uint8Array(e.replies.buffer),e.debug.bufferView=new Uint8Array(e.debug.buffer),e.log.bufferView=new Uint8Array(e.log.buffer),e.replies.message.messages=e.replies.entries,e.debug.message.messages=e.debug.entries,e.log.message.entries=e.log.entries,e.incoming.headerView=new DataView(e.incoming.headerBytes.buffer),this.bufferConstants&&this.wasmMemory){let t=this.bufferConstants,i=t.METRICS_SIZE+t.NODE_TREE_SIZE;e.snapshot.buffer=new ArrayBuffer(i),e.snapshot.bufferView=new Uint8Array(e.snapshot.buffer),e.snapshot.size=i,e.snapshot.message.buffer=e.snapshot.buffer;let o=this.ringBufferBase+t.METRICS_START;e.snapshot.sourceView=new Uint8Array(this.wasmMemory.buffer,o,i)}}writeOscToRingBuffer(s,e=0){if(!this.bufferConstants||!this.uint8View||!this.pmPools)return!1;let t=this.bufferConstants.IN_BUFFER_SIZE,i=this.bufferConstants.MESSAGE_HEADER_SIZE,o=this.ringBufferBase+this.bufferConstants.IN_BUFFER_START,r=s.byteLength,n=i+r+3&-4,_=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),E=this.atomicLoad(this.CONTROL_INDICES.IN_TAIL),S=D(_,E,t);if(n>S)return console.error("[AudioWorklet] Ring buffer full, dropping OSC message"),!1;let f=this.atomicLoad(this.CONTROL_INDICES.IN_SEQUENCE);this.atomicStore(this.CONTROL_INDICES.IN_SEQUENCE,f+1);let h=new Uint8Array(s),a=M({uint8View:this.uint8View,dataView:this.dataView,bufferStart:o,bufferSize:t,head:_,payload:h,sequence:f,messageMagic:this.bufferConstants.MESSAGE_MAGIC,headerSize:i,sourceId:e,headerScratch:this.pmPools.incoming.headerBytes,headerScratchView:this.pmPools.incoming.headerView});return this.atomicStore(this.CONTROL_INDICES.IN_HEAD,a),!0}readOscReplies(){if(!this.pmPools)return;let s=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),e=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);if(s===e)return;let t=this.pmPools.replies,i=w,o=0,r=0,{newTail:c,messagesRead:n}=O({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.OUT_BUFFER_START,bufferSize:this.bufferConstants.OUT_BUFFER_SIZE,head:s,tail:e,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_REPLY_MESSAGES,onMessage:(_,E,S,f)=>{if(o>=i.MAX_REPLY_MESSAGES||r+E>i.REPLY_BUFFER_SIZE)return;for(let a=0;a<E;a++)t.bufferView[r+a]=this.uint8View[_+a];let h=t.entries[o];h.offset=r,h.length=E,h.sequence=S,r+=E,o++}});if(n>0&&this.atomicStore(this.CONTROL_INDICES.OUT_TAIL,c),o>0){t.message.count=o,t.message.buffer=t.buffer,this.port.postMessage(t.message);for(let _=0;_<this.replyPorts.length;_++)this.replyPorts[_].port.postMessage(t.message)}}readNodeTree(){if(!this.bufferConstants||!this.wasmMemory||this.ringBufferBase===null)return null;let s=this.bufferConstants,e=this.ringBufferBase+s.NODE_TREE_START,t=new Uint32Array(this.wasmMemory.buffer,e,2),i=t[0],o=t[1],r=e+s.NODE_TREE_HEADER_SIZE,c=s.NODE_TREE_MIRROR_MAX_NODES,n=s.NODE_TREE_ENTRY_SIZE,_=s.NODE_TREE_DEF_NAME_SIZE,E=new DataView(this.wasmMemory.buffer,r,c*n),S=[],f=0;for(let h=0;h<c&&f<i;h++){let a=h*n,I=E.getInt32(a,!0);if(I===-1)continue;f++;let u=r+a+24,T=new Uint8Array(this.wasmMemory.buffer,u,_),A="";for(let d=0;d<_&&T[d]!==0;d++)A+=String.fromCharCode(T[d]);let R=r+a+56,p=new Uint8Array(this.wasmMemory.buffer,R,16),l=!1;for(let d=0;d<16;d++)if(p[d]!==0){l=!0;break}let C=null;if(l){C=new Uint8Array(16);for(let d=0;d<8;d++)C[d]=p[7-d];for(let d=0;d<8;d++)C[8+d]=p[15-d]}S.push({id:I,parentId:E.getInt32(a+4,!0),isGroup:E.getInt32(a+8,!0)===1,prevId:E.getInt32(a+12,!0),nextId:E.getInt32(a+16,!0),headId:E.getInt32(a+20,!0),defName:A,uuid:C})}return{nodeCount:i,version:o,nodes:S}}readMetrics(){return this.metricsView?new Uint32Array(this.metricsView):null}recordOscReceived(s,e=null){if(this.metricsView){if(this.mode==="sab")Atomics.add(this.metricsView,24,1),Atomics.add(this.metricsView,25,s);else if(this.metricsView[24]++,this.metricsView[25]+=s,e){this.metricsView[22]++;let t=k[e];t!==void 0&&this.metricsView[t]++}}}checkAndSendSnapshot(s){let e=this.bufferConstants;if(!e||!this.wasmMemory||this.ringBufferBase===null||!this.pmPools)return!1;let i=(this.ringBufferBase+e.NODE_TREE_START+4)/4,o=this.atomicView[i];if(o!==this.lastTreeVersion)this.lastTreeVersion=o,this.lastTreeSendTime=s;else{if(this.lastTreeSendTime>=0&&s-this.lastTreeSendTime<this.treeSnapshotMinInterval)return!1;this.lastTreeSendTime=s}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 s=this.bufferConstants,e=this.ringBufferBase+s.METRICS_START,t=s.METRICS_SIZE+s.NODE_TREE_SIZE,i=new Uint8Array(this.wasmMemory.buffer,e,t),o=new ArrayBuffer(t);return new Uint8Array(o).set(i),o}sendLogEntries(){if(!this.CONTROL_INDICES||!this.bufferConstants||!this.pmPools)return;let s=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),e=this.atomicLoad(this.CONTROL_INDICES.IN_LOG_TAIL);if(s===e)return;let t=this.pmPools.log,i=w,o=0,r=0,{newTail:c,messagesRead:n}=O({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.IN_BUFFER_START,bufferSize:this.bufferConstants.IN_BUFFER_SIZE,head:s,tail:e,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_LOG_ENTRIES,onMessage:(_,E,S,f)=>{if(o>=i.MAX_LOG_ENTRIES)return;let h=Math.min(E,i.LOG_MAX_MESSAGE_SIZE);if(r+h>i.LOG_BUFFER_SIZE)return;for(let I=0;I<h;I++)t.bufferView[r+I]=this.uint8View[_+I];let a=t.entries[o];a.offset=r,a.length=h,a.originalLength=E,a.sourceId=f??0,a.sequence=S,r+=h,o++}});n>0&&this.atomicStore(this.CONTROL_INDICES.IN_LOG_TAIL,c),o>0&&(t.message.count=o,t.message.buffer=t.buffer,this.port.postMessage(t.message))}readDebugMessages(){if(!this.pmPools)return;let s=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),e=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);if(s===e)return;let t=this.pmPools.debug,i=w,o=0,r=0,{newTail:c,messagesRead:n}=O({uint8View:this.uint8View,dataView:this.dataView,bufferStart:this.ringBufferBase+this.bufferConstants.DEBUG_BUFFER_START,bufferSize:this.bufferConstants.DEBUG_BUFFER_SIZE,head:s,tail:e,messageMagic:this.bufferConstants.MESSAGE_MAGIC,paddingMagic:this.bufferConstants.PADDING_MAGIC,headerSize:this.bufferConstants.MESSAGE_HEADER_SIZE,maxMessages:i.MAX_DEBUG_MESSAGES,onMessage:(_,E,S,f)=>{if(o>=i.MAX_DEBUG_MESSAGES||r+E>i.DEBUG_BUFFER_SIZE)return;for(let a=0;a<E;a++)t.bufferView[r+a]=this.uint8View[_+a];let h=t.entries[o];h.offset=r,h.length=E,h.sequence=S,r+=E,o++}});n>0&&this.atomicStore(this.CONTROL_INDICES.DEBUG_TAIL,c),o>0&&(t.message.count=o,t.message.buffer=t.buffer,this.port.postMessage(t.message))}async handleMessage(s){let{data:e}=s;try{if(e.type==="osc"){this.mode==="postMessage"&&e.oscData&&(this.writeOscToRingBuffer(e.oscData,e.sourceId??0),this.recordOscReceived(e.oscData.byteLength,e.bypassCategory));return}if(e.type==="addOscPort"){let t=s.ports[0];if(t){let i=e.sourceId??0;this.portSourceIds.set(t,i),t.onmessage=o=>{if(o.data.type==="osc"&&o.data.oscData){let r=o.data.sourceId??this.portSourceIds.get(t)??0;this.writeOscToRingBuffer(o.data.oscData,r),this.recordOscReceived(o.data.oscData.byteLength,o.data.bypassCategory)}else if(o.data.type==="addReplyPort"){let r=o.ports[0];r&&this.replyPorts.push({port:r,sourceId:o.data.sourceId??i})}else if(o.data.type==="removeReplyPort"){let r=o.data.sourceId??i,c=this.replyPorts.findIndex(n=>n.sourceId===r);c>=0&&(this.replyPorts[c].port.close(),this.replyPorts.splice(c,1))}},this.oscPorts.push(t)}return}if(e.type==="addReplyPort"){let t=s.ports[0];t&&this.replyPorts.push({port:t,sourceId:e.sourceId??0});return}if(e.type==="removeReplyPort"){let t=e.sourceId,i=this.replyPorts.findIndex(o=>o.sourceId===t);i>=0&&(this.replyPorts[i].port.close(),this.replyPorts.splice(i,1));return}if(e.type==="clearSched"){if(this.CONTROL_INDICES){let t=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD);this.atomicStore(this.CONTROL_INDICES.IN_TAIL,t)}this.pendingClearSched=!0,e.ack&&this.port.postMessage({type:"clearSchedAck"});return}if(e.type==="nodeIdRange"){e.from!==void 0&&e.to!==void 0&&this.pushNodeIdRange(e.from,e.to);let t=s.ports[0];t&&(this.nodeIdPort=t,this.nodeIdPort.onmessage=i=>{i.data.type==="nodeIdRange"&&this.pushNodeIdRange(i.data.from,i.data.to)});return}if(e.type==="init"&&(this.mode=e.mode||"sab",e.snapshotIntervalMs&&(this.treeSnapshotMinInterval=e.snapshotIntervalMs/1e3),this.mode==="sab"&&e.sharedBuffer&&(this.sharedBuffer=e.sharedBuffer,this.atomicView=new Int32Array(this.sharedBuffer),this.uint8View=new Uint8Array(this.sharedBuffer),this.dataView=new DataView(this.sharedBuffer))),e.type==="loadWasm"){if(e.wasmBytes){let t;if(this.mode==="sab"){if(t=e.wasmMemory,!t){this.port.postMessage({type:"error",error:"No WASM memory provided!"});return}}else{let r=e.memoryPages||1280,c=e.maxMemoryPages||r;t=new WebAssembly.Memory({initial:r,maximum:c,shared:!0})}this.wasmMemory=t,this.worldOptions=e.worldOptions||{},this.sampleRate=e.sampleRate||48e3;let i={env:{memory:t,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:()=>{},emscripten_notify_memory_growth:()=>{}},wasi_snapshot_preview1:{clock_time_get:(r,c,n)=>{let _=new DataView(t.buffer),E=BigInt(Math.floor(Date.now()*1e6));return _.setBigUint64(n,E,!0),0},environ_sizes_get:()=>0,environ_get:()=>0,fd_close:()=>0,fd_write:()=>0,fd_seek:()=>0,fd_read:()=>0,proc_exit:r=>{console.error("[AudioWorklet] WASM tried to exit with code:",r)}}},o=await WebAssembly.compile(e.wasmBytes);if(this.wasmInstance=await WebAssembly.instantiate(o,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.initNodeIdCounter(),this.isInitialized=!0;let r=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:r};this.port.postMessage(c,r?[r]:[])}}else if(e.wasmInstance&&(this.wasmInstance=e.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.initNodeIdCounter(),this.isInitialized=!0;let t=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:t};this.port.postMessage(i,t?[t]:[])}}if(e.type==="getVersion")if(this.wasmInstance&&this.wasmInstance.exports.get_supersonic_version_string){let t=this.wasmInstance.exports.get_supersonic_version_string(),i=new Uint8Array(this.wasmMemory.buffer),o="";for(let r=t;i[r]!==0;r++)o+=String.fromCharCode(i[r]);this.port.postMessage({type:"version",version:o})}else this.port.postMessage({type:"version",version:"unknown"});if(e.type==="getTimeOffset")if(this.wasmInstance&&this.wasmInstance.exports.get_time_offset){let t=this.wasmInstance.exports.get_time_offset();this.port.postMessage({type:"timeOffset",offset:t})}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(e.type==="setNTPStartTime"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let t=this.ringBufferBase+this.bufferConstants.NTP_START_TIME_START,i=new Float64Array(this.wasmMemory.buffer,t,1);i[0]=e.ntpStartTime}if(e.type==="setDriftOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let t=this.ringBufferBase+this.bufferConstants.DRIFT_OFFSET_START,i=new Int32Array(this.wasmMemory.buffer,t,1);i[0]=e.driftOffsetUs}if(e.type==="setClockOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let t=this.ringBufferBase+this.bufferConstants.GLOBAL_OFFSET_START,i=new Int32Array(this.wasmMemory.buffer,t,1);i[0]=e.clockOffsetMs}if(e.type==="getMetrics"){let t=this.metricsView?new Uint32Array(this.metricsView):null;this.port.postMessage({type:"metricsSnapshot",requestId:e.requestId,metrics:t})}if(e.type==="copyBufferData")try{let{copyId:t,ptr:i,data:o}=e;if(!this.wasmMemory||!this.wasmMemory.buffer)throw new Error("WASM memory not initialized");let r=new Float32Array(o);new Float32Array(this.wasmMemory.buffer,i,r.length).set(r),this.port.postMessage({type:"bufferCopied",copyId:t,success:!0})}catch(t){console.error("[AudioWorklet] Buffer copy failed:",t),this.port.postMessage({type:"bufferCopied",copyId:e.copyId,success:!1,error:t.message})}if(e.type==="growMemory")try{let{growId:t,pages:i}=e;if(!this.wasmMemory)throw new Error("WASM memory not initialized");let r=this.wasmMemory.grow(i)!==-1;this.port.postMessage({type:"memoryGrown",growId:t,success:r,newBufferSize:this.wasmMemory.buffer.byteLength})}catch(t){console.error("[AudioWorklet] Memory grow failed:",t),this.port.postMessage({type:"memoryGrown",growId:e.growId,success:!1,error:t.message})}}catch(t){console.error("[AudioWorklet] Error handling message:",t),this.port.postMessage({type:"error",error:t.message,stack:t.stack})}}process(s,e,t){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,o=s[0]?.length||0,r=e[0]?.length||0;if(o>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 E=this.sharedBuffer||this.wasmMemory?.buffer;if(E){let S=this.worldOptions?.numInputBusChannels||2,f=Math.min(o,S);(!this.inputView||this.lastInputBusPtr!==n||this.lastInputChannels!==S)&&(this.inputView=new Float32Array(E,n,_*S),this.lastInputBusPtr=n,this.lastInputChannels=S);for(let h=0;h<f;h++)s[0]?.[h]&&this.inputView.set(s[0][h],h*_)}}}catch{}let c=this.wasmInstance.exports.process_audio(i,r,o);if(this.nodeIdCounterView&&this.nodeIdRangeCount>0){let n=Atomics.load(this.nodeIdCounterView,0);if(n>=this.nodeIdRanges[0].to){if(this.nodeIdRangeCount>1){let _=this.nodeIdRanges[1];this.nodeIdRanges[0].from=_.from,this.nodeIdRanges[0].to=_.to,_.from=0,_.to=0,this.nodeIdRangeCount=1,Atomics.store(this.nodeIdCounterView,0,this.nodeIdRanges[0].from)}!this.nodeIdRefillRequested&&this.nodeIdPort&&(this.nodeIdRefillRequested=!0,this.nodeIdPort.postMessage({type:"requestNodeIdRange"}))}else if(!this.nodeIdRefillRequested&&this.nodeIdRangeCount<2&&this.nodeIdPort){let _=this.nodeIdRanges[0].to-n,E=this.nodeIdRanges[0].to-this.nodeIdRanges[0].from;_<=E>>>1&&(this.nodeIdRefillRequested=!0,this.nodeIdPort.postMessage({type:"requestNodeIdRange"}))}}if(this.wasmInstance.exports.get_audio_output_bus&&e[0]&&e[0].length>=1)try{let n=this.wasmInstance.exports.get_audio_output_bus(),_=this.wasmInstance.exports.get_audio_buffer_samples();if(n&&n>0){let E=this.wasmInstance.exports.memory||this.wasmMemory;if(!E||!E.buffer)return!0;let S=E.buffer,f=S.byteLength,h=this.worldOptions?.numOutputBusChannels||2,a=Math.min(e[0].length,h),I=n+_*a*4;if(n<0||n>f||I>f)return!0;if((!this.audioView||this.lastAudioBufferPtr!==n||this.lastWasmBufferSize!==f||this.lastNumChannels!==a||S!==this.audioView.buffer)&&(this.audioView=new Float32Array(S,n,_*a),this.lastAudioBufferPtr=n,this.lastWasmBufferSize=f),!this.channelViews||this.lastNumSamples!==_||this.lastNumChannels!==a||this.channelViews[0].buffer!==this.audioView.buffer){this.channelViews=new Array(a);for(let u=0;u<a;u++)this.channelViews[u]=this.audioView.subarray(u*_,(u+1)*_);this.lastNumSamples=_,this.lastNumChannels=a}for(let u=0;u<a;u++)e[0][u].set(this.channelViews[u])}}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 E=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),S=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);E!==S&&Atomics.notify(this.atomicView,this.CONTROL_INDICES.DEBUG_HEAD,1);let f=this.atomicLoad(this.CONTROL_INDICES.IN_TAIL);f!==this.lastInTail&&(Atomics.notify(this.atomicView,this.CONTROL_INDICES.IN_TAIL,1),this.lastInTail=f)}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 s=this.atomicLoad(this.CONTROL_INDICES.STATUS_FLAGS);if(s!==this.STATUS_FLAGS.OK){this._statusObj.bufferFull=!!(s&this.STATUS_FLAGS.BUFFER_FULL),this._statusObj.overrun=!!(s&this.STATUS_FLAGS.OVERRUN),this._statusObj.wasmError=!!(s&this.STATUS_FLAGS.WASM_ERROR),this._statusObj.fragmented=!!(s&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=s,this.port.postMessage(this._statusMessage);let e=s&this.STATUS_FLAGS.BUFFER_FULL;this.atomicStore(this.CONTROL_INDICES.STATUS_FLAGS,e)}}};registerProcessor("scsynth-processor",N);})();
|