emscripten-core / emscripten-core/emscripten
AudioWorklet and SharedArrayBuffer
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
I've been tinkering with AudioWorklet and I'm becoming increasingly confused about the proper way to use `SharedArrayBuffer` to share content between my UI and audio threads.
Generally I'd set up a C++ lockless reader-writer queue, to send data from one thread to another. I'd allocate stuff in my main thread (say like a node as part of an audio graph) and send the pointer over to the audio thread via a queue.
I notice when using pthreads and atomics with emscripten in non audio projects, it "just works" - and I'm presuming thats because everything within the application (including workers) all allocate from the same `SharedArrayBuffer`. I'm aware this is more difficult in a worklet because it runs in a completely different global scope, but is there any overlap other than just writing audio data to these queues? I notice in the examples google give (https://developers.google.com/web/updates/2018/06/audio-worklet-design-pattern#webaudio_powerhouse_audio_worklet_and_sharedarraybuffer) that they use an additional worker to do audio processing, and pipe input/output via SAB's, but this just seems so needlessly complex.
Maybe I'm just misunderstanding how SAB is meant to be used, and I'm finding it difficult to word the questions I need to ask, but here goes:
* When I create a standard emscripten application that uses pthreads, does `malloc` get "overridden" to allocate directly from the underlying `SharedArrayBuffer`?
* If I can share a `SharedArrayBuffer` via a message pipe to an `AudioWorkletProcessor`, can I not just share the buffer that is used to conain the application in the main thread?
* Do I need to use multiple `SharedArrayBuffer`'s for this? For example: all my UI related stuff gets `malloc`'d via a UI specific buffer, and anything that needs to be shared with the audio thread get allocated via a `SharedArrayBuffer` that is specifically only shared with the audio thread?
* Am I going mad?
Any help would be greatly appreciated!
Contributor guide
Assessment
This issue has not been assessed yet.