Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -sAUDIO_WORKLET=2 build mode #20630

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

juj
Copy link
Collaborator

@juj juj commented Nov 6, 2023

This PR contains a new -sAUDIO_WORKLET=2 build mode, which enables building Emscripten output that is compatible with being manually loaded inside an AudioWorkletGlobalScope.

This seeks to resolve #20581. However this PR is not yet 100% complete for landing, but posting here to showcase what this code would look like to solicit reviews.

I copied and adapted the test code from https://github.com/GoogleChromeLabs/web-audio-samples/tree/main/src/audio-worklet/design-pattern/wasm. It is a good starting point, although I am not that happy about the contents of wasm-worklet-processor.js, that code does not show what I think would be good practices for people to follow when integrating JS and Wasm, but at least it gives a solid surface area to run in tests.

This is a bit of a draft, as the code hacks passing --export-es6 to acorn. Also noticed that Closure compiler and MINIMAL_RUNTIME don't quite work due to that --export-es6 syntax.

I am not sure if I want to champion developing this, I mainly wanted to post this as an example to help people see how adding a test case for this would work.

…ed WebAssembly Modules and Emscripten runtime .js that is compatible with being manually loaded inside an AudioWorkletGlobalScope.
@hoch
Copy link

hoch commented Nov 6, 2023

Thanks for working on this, @juj! 🙏

settings.EXPORTED_RUNTIME_METHODS += ['stackSave', 'stackAlloc', 'stackRestore']
elif settings.AUDIO_WORKLET == 2:
settings.BINARYEN_ASYNC_COMPILATION = 0 # Run synchronous Wasm initialization inside AudioWorklet.
settings.SINGLE_FILE = 1 # All code must be embedded in a single .js file when targeting -sAUDIO_WORKLET=2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs to be a requirement. I think a better pattern is to fetch the wasm binary on the main thread and then transfer it to the AudioWorklet processor.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be very neat

@danieljamesross
Copy link

Whatever happened to this PR? It would be great to have this merged, it's a wonderful feature.

@stefanholmer
Copy link

Whatever happened to this PR? It would be great to have this merged, it's a wonderful feature.

I still think this would be a great thing to land

@sbc100
Copy link
Collaborator

sbc100 commented Dec 3, 2024

I'm somewhat reluctant to add yet another build mode that we have to test and maintain over time. This one is especially tricky because we it cannot be tested except in interactive browser tests (since we don't have any way to play audio in our test framework without user interaction today).

@danieljamesross
Copy link

I'm somewhat reluctant to add yet another build mode that we have to test and maintain over time.

Fair enough, but the current methodology forces us to use SharedArrayBuffer which is not fully implemented in some environments (eg Chrome Extensions).

@sbc100
Copy link
Collaborator

sbc100 commented Dec 3, 2024

I'm somewhat reluctant to add yet another build mode that we have to test and maintain over time.

Fair enough, but the current methodology forces us to use SharedArrayBuffer which is not fully implemented in some environments (eg Chrome Extensions).

I won't push back too hard given there are genuine use cases. But I would like to make sure we have as good as possible test coverage.

Also, I think perhaps -sENVIRONMENT=audioworklet might be better than -sAUDIO_WORKLET=2 since the -sENVIRONMENT is normally how we decide where certain code can run.

@danieljamesross
Copy link

-sENVIRONMENT=audioworklet is a very sensible flag.

@stefanholmer
Copy link

I have no strong opinion on how to configure this, but I think it's actually an important use-case and I know it's already being used in a few cases using the -sENVIRONMENT=SHELL, https://developer.chrome.com/blog/audio-worklet-design-pattern/#setting-up being one of them, meaning it's most likely somewhat widespread. My concern is that without explicit support, the SHELL solution may break over time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Building wasm to run in AudioWorklet environment which is controlled by JS
5 participants