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

New Emscripten audioWorklet draft #7727

Open
wants to merge 41 commits into
base: master
Choose a base branch
from

Conversation

Jonathhhan
Copy link
Contributor

@Jonathhhan Jonathhhan commented Oct 20, 2023

This draft should work as it is (without any additional changes). It works well, but maybe its not implemented nicely.
Besides that: The main downside is, that sharedArrayBuffer needs to be enabled with -S USE_PTHREADS and because of that CORS header need to be set, if uploaded to a server: emscripten-core/emscripten#20454
The sharedArrayBuffer and the other audioWorklet related Emscripten flags are also only needed, if ofSoundStream is used.
But what speaks for audioWorklets is, that it performs much better and the old ScriptProcessorNode method https://developer.mozilla.org/en-US/docs/Web/API/ScriptProcessorNode is deprecated and will be removed at some point.

Here is the main part of the implementation: https://github.com/Jonathhhan/openFrameworks/blob/audioWorklet-3/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp

Maybe this PR is good for testing and thinking about a proper implementation?

@ofTheo
Copy link
Member

ofTheo commented Oct 21, 2023 via email

@ofTheo
Copy link
Member

ofTheo commented Oct 31, 2023

Hey @Jonathhhan

could you give this a try and see if it solves the CORS issue?
https://stackoverflow.com/a/72628095

@Jonathhhan
Copy link
Contributor Author

Jonathhhan commented Oct 31, 2023

@ofTheo Yes, I will give it a try on Thursday. Maybe earlier.
Actually there is a threading problem with ofxPd and USE_PTHREADS=1 or Audioworklets, that I tried to solve (not really successful), which has to to with Pure Data's sys_lock:
pure-data/pure-data#2104

Generally, sys_lock and sys_unlock is not safe to use from within an external as it can always deadlock.

But without Pure Data (libpd) Audioworklets work really well (at least I did not had any issues).

@Jonathhhan
Copy link
Contributor Author

Jonathhhan commented Nov 7, 2023

@ofTheo

could you give this a try and see if it solves the CORS issue?

Yes, it does work. Only flag I still need to set in the .htaccess file is: AddType application/wasm wasm (should not be a problem to add that line to the script).
Her an update with my audioWorklet issue: emscripten-core/emscripten#20617

@ofTheo
Copy link
Member

ofTheo commented Nov 29, 2023

@Jonathhhan - that's awesome!
if you want to update this PR with those changes - I'll merge it in.

@Jonathhhan
Copy link
Contributor Author

Jonathhhan commented Nov 29, 2023

@ofTheo maybe I first make a pr that solves the CORS issue?
The audioWorklet issue is described here: emscripten-core/emscripten#20617
And it makes it impossible to use ofxPd without hacks (which is the fault of Pure Data's sys_lock system).
Perhaps it makes sense to wait with this PR. As soon as the audioWorklet mutex is implemented it will work.
Another option would be to have the possibility to switch between audioWorklet and ScriptProcessorNode.

@Jonathhhan
Copy link
Contributor Author

Jonathhhan commented Apr 10, 2024

@ofTheo it seems that the recent Pure Data / ofxPd commits solved my issue and ofxPd is now working well with audioWorklets. My only thought: Since it is necessary to enable pthreads, it is more complicated (mainly the CORS thing), so maybe it would be nice to have the scriptProcessorNode as the standard and audioworklets as an option (in the best case automatically enabled, if -s AUDIO_WORKLET=1 is set)? scriptProcessorNode on the other hand will be deprecated at some point, so maybe we need to switch sooner or later...

@Jonathhhan
Copy link
Contributor Author

Jonathhhan commented Apr 12, 2024

Here is an updated branch: https://github.com/Jonathhhan/openFrameworks/tree/emscripten_3.1.57_audioWorklet
Maybe someone can have a look, if there is a more elegant way to implement audioWorklets. But I tested it and it works well.

@danoli3 danoli3 self-assigned this Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants