0

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?

badcode
  • 581
  • 1
  • 9
  • 28

1 Answers1

2

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.

Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • Clarification: "Worklet" here is not https://developer.mozilla.org/en-US/docs/Web/API/Worklet. An OfflineAudioContext doesn't create this. It's basically just some object which will render an audio graph in its own thread separate from any other, and is intended to run faster than realtime. – Raymond Toy Jun 15 '21 at 14:27
  • @RaymondToy oh my, thanks for the heads up! I completely mixed OfflineAudioContext and AudioWorklets here, I meant to talk about the latter... – Kaiido Jun 15 '21 at 14:33