Search code examples
javascriptweb-audio-apiweb-workeroffscreen-canvas

how can i use web audio api with offscreen canvas?


As is known, we can't access the dom element from the workers. When I create AudioContext:

var audioCtx = new (canvas.AudioContext || canvas.webkitAudioContext)();

I get:

Uncaught ReferenceError: window is not defined

Is there any way to walk around this problem?


Solution

  • Not yet no. Here is a specs issue discussing this very matter, and it is something a lot of actors would like to see, so there is hope it comes, one day.

    Note that there is an AudioWorklet API available, which will create its own Worklet (which also works in a parallel thread), but you still need to instantiate it from the UI thread, and you don't have access to everything that can be done in an AudioContext. Still, it may suit your needs.

    Also, note that it might be possible to do the computing you have to do in a Worker already, by transferring ArrayBuffers from your UI thread to the Worker's one, or by using SharedArrayBuffers.