supersonic-scsynth 0.24.0 → 0.24.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
Binary file
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(()=>{var
|
|
2
|
-
`,h=new TextEncoder().encode(o);if(h.length>s)return;let a=this.CONTROL_INDICES.DEBUG_HEAD,i=this.atomicLoad(a),f=s-i,c=i;h.length>f&&(this.uint8View[this.ringBufferBase+t+i]=r,c=0);let u=this.ringBufferBase+t;for(let E=0;E<h.length;E++)this.uint8View[u+c+E]=h[E];let _=c+h.length;this.atomicStore(a,_)}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}drainOscQueue(){if(this.oscQueue.length===0)return;let e=this.bufferConstants.IN_BUFFER_START,t=this.bufferConstants.IN_BUFFER_SIZE,s=this.bufferConstants.MESSAGE_MAGIC,r=this.bufferConstants.MESSAGE_HEADER_SIZE,o=this.ringBufferBase+e;for(;this.oscQueue.length>0;){let n=this.oscQueue[0],h=n.byteLength,i=r+h+3&-4,f=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),c=this.atomicLoad(this.CONTROL_INDICES.IN_TAIL),u;if(f>=c?u=t-f+c-1:u=c-f-1,i>u)break;this.oscQueue.shift();let _=f;this.dataView.setUint32(o+_,s,!0),this.dataView.setUint32(o+_+4,i,!0);let E=this.atomicLoad(this.CONTROL_INDICES.IN_SEQUENCE);this.dataView.setUint32(o+_+8,E,!0),this.dataView.setUint32(o+_+12,0,!0),this.atomicStore(this.CONTROL_INDICES.IN_SEQUENCE,E+1);let m=new Uint8Array(n);for(let l=0;l<h;l++)this.uint8View[o+_+r+l]=m[l];let S=(_+i)%t;this.atomicStore(this.CONTROL_INDICES.IN_HEAD,S)}}readOscReplies(){let e=this.bufferConstants.OUT_BUFFER_START,t=this.bufferConstants.OUT_BUFFER_SIZE,s=this.bufferConstants.MESSAGE_MAGIC,r=this.bufferConstants.PADDING_MAGIC,o=this.bufferConstants.MESSAGE_HEADER_SIZE,n=this.ringBufferBase+e,h=[],a=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),i=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);for(;a!==i;){let f=this.dataView.getUint32(n+i,!0);if(f===r){i=0;continue}if(f!==s){i=(i+1)%t;continue}let c=this.dataView.getUint32(n+i+4,!0),u=this.dataView.getUint32(n+i+8,!0);if(c<o||c>t){i=(i+1)%t;continue}let _=c-o,E=new Uint8Array(_);for(let S=0;S<_;S++)E[S]=this.uint8View[n+i+o+S];h.push({oscData:E.buffer,sequence:u});let m=c+3&-4;i=(i+m)%t}this.atomicStore(this.CONTROL_INDICES.OUT_TAIL,i),h.length>0&&this.port.postMessage({type:"oscReplies",messages:h})}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),r=s[0],o=s[1],n=t+e.NODE_TREE_HEADER_SIZE,h=e.NODE_TREE_MAX_NODES,a=e.NODE_TREE_ENTRY_SIZE,i=e.NODE_TREE_DEF_NAME_SIZE,f=new DataView(this.wasmMemory.buffer,n,h*a),c=[],u=0;for(let _=0;_<h&&u<r;_++){let E=_*a,m=f.getInt32(E,!0);if(m===-1)continue;u++;let S=n+E+24,l=new Uint8Array(this.wasmMemory.buffer,S,i),I="";for(let p=0;p<i&&l[p]!==0;p++)I+=String.fromCharCode(l[p]);c.push({id:m,parentId:f.getInt32(E+4,!0),isGroup:f.getInt32(E+8,!0)===1,prevId:f.getInt32(E+12,!0),nextId:f.getInt32(E+16,!0),headId:f.getInt32(E+20,!0),defName:I})}return{nodeCount:r,version:o,nodes:c}}readMetrics(){return this.metricsView?new Uint32Array(this.metricsView):null}checkAndSendSnapshot(e){let t=this.bufferConstants;if(!t||!this.wasmMemory||this.ringBufferBase===null)return;let s=this.ringBufferBase+t.NODE_TREE_START,o=new Uint32Array(this.wasmMemory.buffer,s,2)[1];if(o!==this.lastTreeVersion)this.lastTreeVersion=o,this.lastTreeSendTime=e;else{if(this.lastTreeSendTime>=0&&e-this.lastTreeSendTime<this.treeSnapshotMinInterval)return;this.lastTreeSendTime=e}let h=this.readMetricsAndTreeBuffer();h&&(this.treeSnapshotsSent++,this.port.postMessage({type:"snapshot",buffer:h,snapshotsSent:this.treeSnapshotsSent},[h]))}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,r=new Uint8Array(this.wasmMemory.buffer,t,s),o=new ArrayBuffer(s);return new Uint8Array(o).set(r),o}readDebugMessages(){let e=this.bufferConstants.DEBUG_BUFFER_START,t=this.bufferConstants.DEBUG_BUFFER_SIZE,s=this.bufferConstants.MESSAGE_MAGIC,r=this.bufferConstants.PADDING_MAGIC,o=this.bufferConstants.MESSAGE_HEADER_SIZE,n=this.ringBufferBase+e,h=[],a=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),i=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);for(;a!==i;){let f=this.dataView.getUint32(n+i,!0);if(f===r){i=0;continue}if(f!==s){i=(i+1)%t;continue}let c=this.dataView.getUint32(n+i+4,!0),u=this.dataView.getUint32(n+i+8,!0);if(c<o||c>t){i=(i+1)%t;continue}let _=c-o,E=n+i+o,m=new Uint8Array(_);for(let S=0;S<_;S++)m[S]=this.uint8View[E+S];h.push({bytes:m.buffer,sequence:u}),i=(i+c)%t}this.atomicStore(this.CONTROL_INDICES.DEBUG_TAIL,i),h.length>0&&this.port.postMessage({type:"debugRawBatch",messages:h},h.map(f=>f.bytes))}async handleMessage(e){let{data:t}=e;try{if(t.type==="osc"){this.mode==="postMessage"&&t.oscData&&this.oscQueue.push(t.oscData);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 r={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,h,a)=>{let i=new DataView(s.buffer),f=BigInt(Math.floor(Date.now()*1e6));return i.setBigUint64(a,f,!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)}}},o=await WebAssembly.compile(t.wasmBytes);if(this.wasmInstance=await WebAssembly.instantiate(o,r),this.wasmInstance.exports.get_ring_buffer_base&&(this.ringBufferBase=this.wasmInstance.exports.get_ring_buffer_base(),this.loadBufferConstants(),this.calculateBufferIndices(this.ringBufferBase),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,h={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:n};this.port.postMessage(h,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.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,r={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:s};this.port.postMessage(r,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(),r=new Uint8Array(this.wasmMemory.buffer),o="";for(let n=s;r[n]!==0;n++)o+=String.fromCharCode(r[n]);this.port.postMessage({type:"version",version:o})}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,r=new Float64Array(this.wasmMemory.buffer,s,1);r[0]=t.ntpStartTime}if(t.type==="setDriftOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.DRIFT_OFFSET_START,r=new Int32Array(this.wasmMemory.buffer,s,1);r[0]=t.driftOffsetMs}if(t.type==="setGlobalOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.GLOBAL_OFFSET_START,r=new Int32Array(this.wasmMemory.buffer,s,1);r[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:r,data:o}=t;if(!this.wasmMemory||!this.wasmMemory.buffer)throw new Error("WASM memory not initialized");let n=new Float32Array(o);new Float32Array(this.wasmMemory.buffer,r,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){this.mode==="postMessage"&&this.drainOscQueue();let r=currentTime,o=e[0]?.length||0,n=t[0]?.length||0;if(o>0&&this.wasmInstance?.exports?.get_audio_input_bus)try{let a=this.wasmInstance.exports.get_audio_input_bus(),i=this.wasmInstance.exports.get_audio_buffer_samples();if(a&&a>0){let f=this.sharedBuffer||this.wasmMemory?.buffer;if(f){let c=this.worldOptions?.numInputBusChannels||2,u=Math.min(o,c);(!this.inputView||this.lastInputBusPtr!==a||this.lastInputChannels!==c)&&(this.inputView=new Float32Array(f,a,i*c),this.lastInputBusPtr=a,this.lastInputChannels=c);for(let _=0;_<u;_++)e[0]?.[_]&&this.inputView.set(e[0][_],_*i)}}}catch{}let h=this.wasmInstance.exports.process_audio(r,n,o);if(this.wasmInstance.exports.get_audio_output_bus&&t[0]&&t[0].length>=2)try{let a=this.wasmInstance.exports.get_audio_output_bus(),i=this.wasmInstance.exports.get_audio_buffer_samples();if(a&&a>0){let f=this.wasmInstance.exports.memory||this.wasmMemory;if(!f||!f.buffer)return!0;let c=f.buffer,u=c.byteLength,_=a+i*2*4;if(a<0||a>u||_>u)return!0;(!this.audioView||this.lastAudioBufferPtr!==a||this.lastWasmBufferSize!==u||c!==this.audioView.buffer)&&(this.audioView=new Float32Array(c,a,i*2),this.lastAudioBufferPtr=a,this.lastWasmBufferSize=u),t[0][0].set(this.audioView.subarray(0,i)),t[0][1].set(this.audioView.subarray(i,i*2))}}catch{}if(this.mode==="postMessage")this.readOscReplies(),this.readDebugMessages(),this.checkAndSendSnapshot(r);else if(this.atomicView){let a=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),i=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);a!==i&&Atomics.notify(this.atomicView,this.CONTROL_INDICES.OUT_HEAD,1)}return this.processCallCount%3750===0&&this.checkStatus(),h!==0}}catch(r){console.error("[AudioWorklet] process() error:",r),console.error("[AudioWorklet] Stack:",r.stack),this.atomicView&&this.mode==="sab"&&Atomics.or(this.atomicView,this.CONTROL_INDICES.STATUS_FLAGS,this.STATUS_FLAGS.WASM_ERROR)}return!0}checkStatus(){if(!this.atomicView)return;let e=this.atomicLoad(this.CONTROL_INDICES.STATUS_FLAGS);if(e!==this.STATUS_FLAGS.OK){let t={bufferFull:!!(e&this.STATUS_FLAGS.BUFFER_FULL),overrun:!!(e&this.STATUS_FLAGS.OVERRUN),wasmError:!!(e&this.STATUS_FLAGS.WASM_ERROR),fragmented:!!(e&this.STATUS_FLAGS.FRAGMENTED_MSG)},s={processCount:this.metricsView[0],messagesProcessed:this.metricsView[1],messagesDropped:this.metricsView[2],schedulerQueueDepth:this.metricsView[3],schedulerQueueMax:this.metricsView[4],schedulerQueueDropped:this.metricsView[5]};this.port.postMessage({type:"status",flags:e,status:t,metrics:s});let r=e&this.STATUS_FLAGS.BUFFER_FULL;this.atomicStore(this.CONTROL_INDICES.STATUS_FLAGS,r)}}};registerProcessor("scsynth-processor",d);})();
|
|
1
|
+
(()=>{var T=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.audioView=null,this.lastAudioBufferPtr=0,this.lastWasmBufferSize=0,this.lastTreeVersion=-1,this.treeSnapshotsSent=0,this.lastTreeSendTime=-1,this.treeSnapshotMinInterval=.025,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.oscQueue=[],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),r=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_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:r[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={IN_HEAD:(e+t+0)/4,IN_TAIL:(e+t+4)/4,OUT_HEAD:(e+t+8)/4,OUT_TAIL:(e+t+12)/4,DEBUG_HEAD:(e+t+16)/4,DEBUG_TAIL:(e+t+20)/4,IN_SEQUENCE:(e+t+24)/4,OUT_SEQUENCE:(e+t+28)/4,DEBUG_SEQUENCE:(e+t+32)/4,STATUS_FLAGS:(e+t+36)/4,IN_WRITE_LOCK:(e+t+40)/4},this.mode==="sab"){let r=e+s;this.metricsView=new Uint32Array(this.sharedBuffer,r,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 r=e+s;this.metricsView=new Uint32Array(this.wasmMemory.buffer,r,this.bufferConstants.METRICS_SIZE/4)}}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}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,r=this.bufferConstants.DEBUG_PADDING_MARKER,o="[JS] "+e+`
|
|
2
|
+
`,f=new TextEncoder().encode(o);if(f.length>s)return;let a=this.CONTROL_INDICES.DEBUG_HEAD,i=this.atomicLoad(a),h=s-i,c=i;f.length>h&&(this.uint8View[this.ringBufferBase+t+i]=r,c=0);let E=this.ringBufferBase+t;for(let _=0;_<f.length;_++)this.uint8View[E+c+_]=f[_];let u=c+f.length;this.atomicStore(a,u)}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}drainOscQueue(){if(this.oscQueue.length===0)return;let e=this.bufferConstants.IN_BUFFER_START,t=this.bufferConstants.IN_BUFFER_SIZE,s=this.bufferConstants.MESSAGE_MAGIC,r=this.bufferConstants.MESSAGE_HEADER_SIZE,o=this.ringBufferBase+e;for(;this.oscQueue.length>0;){let n=this.oscQueue[0],f=n.byteLength,i=r+f+3&-4,h=this.atomicLoad(this.CONTROL_INDICES.IN_HEAD),c=this.atomicLoad(this.CONTROL_INDICES.IN_TAIL),E=(t-1-h+c)%t;if(i>E)break;this.oscQueue.shift();let u=this.atomicLoad(this.CONTROL_INDICES.IN_SEQUENCE);this.atomicStore(this.CONTROL_INDICES.IN_SEQUENCE,u+1);let _=t-h,l=new Uint8Array(n);if(i>_){let m=new Uint8Array(r),p=new DataView(m.buffer);p.setUint32(0,s,!0),p.setUint32(4,i,!0),p.setUint32(8,u,!0),p.setUint32(12,0,!0);let d=o+h,I=o;if(_>=r){this.uint8View.set(m,d);let w=_-r;w>0&&this.uint8View.set(l.subarray(0,w),d+r),this.uint8View.set(l.subarray(w),I)}else{this.uint8View.set(m.subarray(0,_),d),this.uint8View.set(m.subarray(_),I);let w=r-_;this.uint8View.set(l,I+w)}}else{let m=o+h;this.dataView.setUint32(m,s,!0),this.dataView.setUint32(m+4,i,!0),this.dataView.setUint32(m+8,u,!0),this.dataView.setUint32(m+12,0,!0),this.uint8View.set(l,m+r)}let S=(h+i)%t;this.atomicStore(this.CONTROL_INDICES.IN_HEAD,S)}}readOscReplies(){let e=this.bufferConstants.OUT_BUFFER_START,t=this.bufferConstants.OUT_BUFFER_SIZE,s=this.bufferConstants.MESSAGE_MAGIC,r=this.bufferConstants.PADDING_MAGIC,o=this.bufferConstants.MESSAGE_HEADER_SIZE,n=this.ringBufferBase+e,f=[],a=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),i=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);for(;a!==i;){let h=this.dataView.getUint32(n+i,!0);if(h===r){i=0;continue}if(h!==s){i=(i+1)%t;continue}let c=this.dataView.getUint32(n+i+4,!0),E=this.dataView.getUint32(n+i+8,!0);if(c<o||c>t){i=(i+1)%t;continue}let u=c-o,_=new Uint8Array(u);for(let S=0;S<u;S++)_[S]=this.uint8View[n+i+o+S];f.push({oscData:_.buffer,sequence:E});let l=c+3&-4;i=(i+l)%t}this.atomicStore(this.CONTROL_INDICES.OUT_TAIL,i),f.length>0&&this.port.postMessage({type:"oscReplies",messages:f})}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),r=s[0],o=s[1],n=t+e.NODE_TREE_HEADER_SIZE,f=e.NODE_TREE_MAX_NODES,a=e.NODE_TREE_ENTRY_SIZE,i=e.NODE_TREE_DEF_NAME_SIZE,h=new DataView(this.wasmMemory.buffer,n,f*a),c=[],E=0;for(let u=0;u<f&&E<r;u++){let _=u*a,l=h.getInt32(_,!0);if(l===-1)continue;E++;let S=n+_+24,m=new Uint8Array(this.wasmMemory.buffer,S,i),p="";for(let d=0;d<i&&m[d]!==0;d++)p+=String.fromCharCode(m[d]);c.push({id:l,parentId:h.getInt32(_+4,!0),isGroup:h.getInt32(_+8,!0)===1,prevId:h.getInt32(_+12,!0),nextId:h.getInt32(_+16,!0),headId:h.getInt32(_+20,!0),defName:p})}return{nodeCount:r,version:o,nodes:c}}readMetrics(){return this.metricsView?new Uint32Array(this.metricsView):null}checkAndSendSnapshot(e){let t=this.bufferConstants;if(!t||!this.wasmMemory||this.ringBufferBase===null)return;let s=this.ringBufferBase+t.NODE_TREE_START,o=new Uint32Array(this.wasmMemory.buffer,s,2)[1];if(o!==this.lastTreeVersion)this.lastTreeVersion=o,this.lastTreeSendTime=e;else{if(this.lastTreeSendTime>=0&&e-this.lastTreeSendTime<this.treeSnapshotMinInterval)return;this.lastTreeSendTime=e}let f=this.readMetricsAndTreeBuffer();f&&(this.treeSnapshotsSent++,this.port.postMessage({type:"snapshot",buffer:f,snapshotsSent:this.treeSnapshotsSent},[f]))}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,r=new Uint8Array(this.wasmMemory.buffer,t,s),o=new ArrayBuffer(s);return new Uint8Array(o).set(r),o}readDebugMessages(){let e=this.bufferConstants.DEBUG_BUFFER_START,t=this.bufferConstants.DEBUG_BUFFER_SIZE,s=this.bufferConstants.MESSAGE_MAGIC,r=this.bufferConstants.PADDING_MAGIC,o=this.bufferConstants.MESSAGE_HEADER_SIZE,n=this.ringBufferBase+e,f=[],a=this.atomicLoad(this.CONTROL_INDICES.DEBUG_HEAD),i=this.atomicLoad(this.CONTROL_INDICES.DEBUG_TAIL);for(;a!==i;){let h=this.dataView.getUint32(n+i,!0);if(h===r){i=0;continue}if(h!==s){i=(i+1)%t;continue}let c=this.dataView.getUint32(n+i+4,!0),E=this.dataView.getUint32(n+i+8,!0);if(c<o||c>t){i=(i+1)%t;continue}let u=c-o,_=n+i+o,l=new Uint8Array(u);for(let S=0;S<u;S++)l[S]=this.uint8View[_+S];f.push({bytes:l.buffer,sequence:E}),i=(i+c)%t}this.atomicStore(this.CONTROL_INDICES.DEBUG_TAIL,i),f.length>0&&this.port.postMessage({type:"debugRawBatch",messages:f},f.map(h=>h.bytes))}async handleMessage(e){let{data:t}=e;try{if(t.type==="osc"){this.mode==="postMessage"&&t.oscData&&this.oscQueue.push(t.oscData);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 r={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,f,a)=>{let i=new DataView(s.buffer),h=BigInt(Math.floor(Date.now()*1e6));return i.setBigUint64(a,h,!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)}}},o=await WebAssembly.compile(t.wasmBytes);if(this.wasmInstance=await WebAssembly.instantiate(o,r),this.wasmInstance.exports.get_ring_buffer_base&&(this.ringBufferBase=this.wasmInstance.exports.get_ring_buffer_base(),this.loadBufferConstants(),this.calculateBufferIndices(this.ringBufferBase),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,f={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:n};this.port.postMessage(f,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.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,r={type:"initialized",success:!0,ringBufferBase:this.ringBufferBase,bufferConstants:this.bufferConstants,exports:Object.keys(this.wasmInstance.exports),initialSnapshot:s};this.port.postMessage(r,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(),r=new Uint8Array(this.wasmMemory.buffer),o="";for(let n=s;r[n]!==0;n++)o+=String.fromCharCode(r[n]);this.port.postMessage({type:"version",version:o})}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,r=new Float64Array(this.wasmMemory.buffer,s,1);r[0]=t.ntpStartTime}if(t.type==="setDriftOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.DRIFT_OFFSET_START,r=new Int32Array(this.wasmMemory.buffer,s,1);r[0]=t.driftOffsetMs}if(t.type==="setGlobalOffset"&&this.wasmMemory&&this.ringBufferBase!==null&&this.bufferConstants){let s=this.ringBufferBase+this.bufferConstants.GLOBAL_OFFSET_START,r=new Int32Array(this.wasmMemory.buffer,s,1);r[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:r,data:o}=t;if(!this.wasmMemory||!this.wasmMemory.buffer)throw new Error("WASM memory not initialized");let n=new Float32Array(o);new Float32Array(this.wasmMemory.buffer,r,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){this.mode==="postMessage"&&this.drainOscQueue();let r=currentTime,o=e[0]?.length||0,n=t[0]?.length||0;if(o>0&&this.wasmInstance?.exports?.get_audio_input_bus)try{let a=this.wasmInstance.exports.get_audio_input_bus(),i=this.wasmInstance.exports.get_audio_buffer_samples();if(a&&a>0){let h=this.sharedBuffer||this.wasmMemory?.buffer;if(h){let c=this.worldOptions?.numInputBusChannels||2,E=Math.min(o,c);(!this.inputView||this.lastInputBusPtr!==a||this.lastInputChannels!==c)&&(this.inputView=new Float32Array(h,a,i*c),this.lastInputBusPtr=a,this.lastInputChannels=c);for(let u=0;u<E;u++)e[0]?.[u]&&this.inputView.set(e[0][u],u*i)}}}catch{}let f=this.wasmInstance.exports.process_audio(r,n,o);if(this.wasmInstance.exports.get_audio_output_bus&&t[0]&&t[0].length>=2)try{let a=this.wasmInstance.exports.get_audio_output_bus(),i=this.wasmInstance.exports.get_audio_buffer_samples();if(a&&a>0){let h=this.wasmInstance.exports.memory||this.wasmMemory;if(!h||!h.buffer)return!0;let c=h.buffer,E=c.byteLength,u=a+i*2*4;if(a<0||a>E||u>E)return!0;(!this.audioView||this.lastAudioBufferPtr!==a||this.lastWasmBufferSize!==E||c!==this.audioView.buffer)&&(this.audioView=new Float32Array(c,a,i*2),this.lastAudioBufferPtr=a,this.lastWasmBufferSize=E),t[0][0].set(this.audioView.subarray(0,i)),t[0][1].set(this.audioView.subarray(i,i*2))}}catch{}if(this.mode==="postMessage")this.readOscReplies(),this.readDebugMessages(),this.checkAndSendSnapshot(r);else if(this.atomicView){let a=this.atomicLoad(this.CONTROL_INDICES.OUT_HEAD),i=this.atomicLoad(this.CONTROL_INDICES.OUT_TAIL);a!==i&&Atomics.notify(this.atomicView,this.CONTROL_INDICES.OUT_HEAD,1)}return this.processCallCount%3750===0&&this.checkStatus(),f!==0}}catch(r){console.error("[AudioWorklet] process() error:",r),console.error("[AudioWorklet] Stack:",r.stack),this.atomicView&&this.mode==="sab"&&Atomics.or(this.atomicView,this.CONTROL_INDICES.STATUS_FLAGS,this.STATUS_FLAGS.WASM_ERROR)}return!0}checkStatus(){if(!this.atomicView)return;let e=this.atomicLoad(this.CONTROL_INDICES.STATUS_FLAGS);if(e!==this.STATUS_FLAGS.OK){let t={bufferFull:!!(e&this.STATUS_FLAGS.BUFFER_FULL),overrun:!!(e&this.STATUS_FLAGS.OVERRUN),wasmError:!!(e&this.STATUS_FLAGS.WASM_ERROR),fragmented:!!(e&this.STATUS_FLAGS.FRAGMENTED_MSG)},s={processCount:this.metricsView[0],messagesProcessed:this.metricsView[1],messagesDropped:this.metricsView[2],schedulerQueueDepth:this.metricsView[3],schedulerQueueMax:this.metricsView[4],schedulerQueueDropped:this.metricsView[5]};this.port.postMessage({type:"status",flags:e,status:t,metrics:s});let r=e&this.STATUS_FLAGS.BUFFER_FULL;this.atomicStore(this.CONTROL_INDICES.STATUS_FLAGS,r)}}};registerProcessor("scsynth-processor",T);})();
|
package/package.json
CHANGED