How to access the AudioContext from within an AudioWorkletProcessor?

277 Views Asked by At

When creating an AudioWorkletProcessor the context is provided:

new AudioWorklet( audioContext, 'worklet-name', options ).

Inside the worklet there doesn't seem to be any way to access that context. How do we access critical Audio functions like createBuffer?

Raw buffers (Float32Array) aren't sufficient because the audio buffer interface accepts the sample rate. Without the sample rate, the audio is played through at the wrong pitch.

1

There are 1 best solutions below

0
On

There is no way to access the AudioContext itself from within an AudioWorkletProcessor.

But there are some globals which might come in handy:

  • currentTime: This is the same value which can be accessed as a property with the same name of an AudioContext on the main thread.

  • currentFrame This is the same value as currentTime but in frames.

  • sampleRate: This is also the same value which can be accessed as a property with the same name of an AudioContext on the main thread.