Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions make.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ lm:conf {
defines = {
"_POSIX_C_SOURCE=200809L",
"_GNU_SOURCE",
"MA_ENABLE_AUDIO_WORKLETS",
},
},
defines = {
Expand All @@ -134,9 +135,10 @@ lm:exe "soluna" {
deps = deps,
emcc = {
ldflags = {
"-s MAIN_MODULE=1",
"-Wl,-u,emscripten_builtin_memalign",
"-Wl,--export=emscripten_builtin_memalign",
"-sJSPI",
"-sAUDIO_WORKLET=1",
"-sWASM_WORKERS=1",
"--js-library=src/platform/wasm/soluna_audio_wasm.js",
},
},
}
Expand Down
37 changes: 37 additions & 0 deletions src/platform/wasm/soluna_audio_wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Override emscripten_request_animation_frame_loop to support JSPI.
//
// When JSPI is enabled (-sJSPI), audio worklet initialization calls
// emscripten_sleep() which requires the WebAssembly call stack to be
// in a "promising" context (wrapped with WebAssembly.promising).
//
// This override wraps the C frame callback with WebAssembly.promising
// so that emscripten_sleep works correctly during audio init.
mergeInto(LibraryManager.library, {
emscripten_request_animation_frame_loop__deps: ['$getWasmTableEntry'],
emscripten_request_animation_frame_loop__sig: 'vpp',
emscripten_request_animation_frame_loop: function(cb, userData) {
var fn = getWasmTableEntry(cb);
var promisingFn = null;
if (typeof WebAssembly !== 'undefined' && typeof WebAssembly.promising === 'function') {
try {
promisingFn = WebAssembly.promising(fn);
} catch (e) {
promisingFn = null;
}
}
function tick(rAF_time) {
if (promisingFn) {
promisingFn(rAF_time, userData).then(function(keepGoing) {
if (keepGoing) requestAnimationFrame(tick);
}).catch(function(e) {
console.error('Soluna: JSPI frame callback error (WebAssembly.promising):', e instanceof Error ? e.message : String(e), e);
});
} else {
if (fn(rAF_time, userData)) {
requestAnimationFrame(tick);
}
}
}
requestAnimationFrame(tick);
},
});