How to connect a bufferSource to Tone's Destination

50 Views Asked by At

With regular WebAudio, I can connect a bufferSource to destination like so:

const audioCtx = new AudioContext()
const bufferSource = audioCtx.createBufferSource()
bufferSource.connect(audioCtx.destination)

I thought I could do the same with Tone.js, but this:

const audioCtxTone = Tone.context
const bufferSource = audioCtxTone.createBufferSource()
bufferSource.connect(audioCtxTone.destination)

give the error Argument of type 'Destination' is not assignable to parameter of type 'AudioNode'.

What am I doing/thinking wrong here?

1

There are 1 best solutions below

0
chrisguttandin On

I can't exactly explain why your code doesn't work. Tone.js uses standardized-audio-context which provides a wrapper around the Web Audio API to make it more compatible across different browsers. Tone.js has it's own very thin wrapper on top of standardized-audio-context. I assume something is going wrong along the way.

But it should work if you use the context property of the AudioBufferSourceNode instead. It should point to the right AudioContext.

const audioContext = Tone.context;
const audioBufferSourceNode = audioContext.createBufferSource();

audioBufferSourceNode.connect(audioBufferSourceNode.context.destination);