webassemblyweb-audio-apiemscriptensharedarraybufferaudio-worklet

WebAudioWorklet in webassembly Shared Array Buffer not defined


I ma trying to understand how to compile an Audio Worklet in webassembly using the Wasm Audio Worklets API of the emscripten.

Using the code listed in the tutorial on the API reference here and compiling the code using:

em++ audioworklet.cpp -sAUDIO_WORKLET=1 -sWASM_WORKERS=1 -o worklet.html

I get an error in the browser (both Firefox and Chrome):

Uncaught ReferenceError: SharedArrayBuffer is not defined

I also tried to compile some of the webaudio test code on the emscripten repository for example audioworklet.c using emcc instead of em++ but I always get the same error.

What am I doing wrong?

you can find a copy of the glue code (both js and html) automatically generated from the compilation instruction provided above: https://gist.github.com/ddgg-el/e6819f773abbfc75385c7fa15d79b692.

The error is thrown by line 459 of the worklet.js

thank you!


Solution

  • I think there is a good chance that you don't set the required headers which are mandatory these days to enable SharedArrayBuffer support.

    Cross-Origin-Opener-Policy: same-origin
    Cross-Origin-Embedder-Policy: require-corp
    

    As far as I can tell you even need to set those headers when running your website on localhost.

    You can read more about it on MDN or web.dev.