Skip to content

New Emscripten audioWorklet draft#7727

Open
Jonathhhan wants to merge 41 commits into
openframeworks:masterfrom
Jonathhhan:audioWorklet-3
Open

New Emscripten audioWorklet draft#7727
Jonathhhan wants to merge 41 commits into
openframeworks:masterfrom
Jonathhhan:audioWorklet-3

Conversation

@Jonathhhan

@Jonathhhan Jonathhhan commented Oct 20, 2023

Copy link
Copy Markdown
Contributor

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

ofTheo commented Oct 21, 2023 via email

Copy link
Copy Markdown
Member

@ofTheo

ofTheo commented Oct 31, 2023

Copy link
Copy Markdown
Member

Hey @Jonathhhan

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

@Jonathhhan

Jonathhhan commented Oct 31, 2023

Copy link
Copy Markdown
Contributor Author

@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

Jonathhhan commented Nov 7, 2023

Copy link
Copy Markdown
Contributor Author

@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

ofTheo commented Nov 29, 2023

Copy link
Copy Markdown
Member

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

@Jonathhhan

Jonathhhan commented Nov 29, 2023

Copy link
Copy Markdown
Contributor Author

@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

Jonathhhan commented Apr 10, 2024

Copy link
Copy Markdown
Contributor Author

@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

Jonathhhan commented Apr 12, 2024

Copy link
Copy Markdown
Contributor Author

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 commented Jan 14, 2025

Copy link
Copy Markdown
Member

On this should we just enable PTHEADS by default for libraries and proceed with this

@danoli3

danoli3 commented Jan 14, 2025

Copy link
Copy Markdown
Member

Pthreads support

==============================

Emscripten has support for multithreading using SharedArrayBuffer in browsers. That API allows sharing memory between the main thread and web workers as well as atomic operations for synchronization, which enables Emscripten to implement support for the Pthreads (POSIX threads) API. This support is considered stable in Emscripten.

.. note:: As of Sep 2019, some browsers have disabled SharedArrayBuffer due to
the Spectre set of vulnerabilities. Until it is restored you can still
experiment with it if you flip a pref in those browsers. In other
browsers (like Chrome on desktop), SharedArrayBuffer is fully enabled
by default and you don't need to flip any flags.

  Yeah I think we have no issues with PTHEAD as default! Solves a lot of issues. 
  Will make amends

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