Search code examples
javascriptsafariwebkitweb-audio-api

Web Audio Api: audioContext.createConstantSource() isn't implemented in Safari; workaround(s)?


I am using the web audio api to make a series of demonstrations of simple computer music / electronic music concepts. In one example, I show how to build a square wave by successively adding sine waves going up the harmonic series, and displaying an oscilloscope view of the process.

I am including an input range slider to adjust the fundamental frequency at will, which requires the use of a constantSourceNode: the frequencies of all of the oscillators need to be changed simultaneously, otherwise, they get out of phase, ruining the visuals of the demonstration. I've got it working great in chrome, but it seems that

audioContext.createConstantSource()

is not supported in Safari. Any suggestions for a workaround / alternate implementation?

(Here is a link to a codepen of the demonstration, working in chrome).


Solution

  • Safari does not yet have an implementation for the ConstantSourceNode. If you want to keep your code the way it is you could use standardized-audio-context which includes an implementation of the ConstantSourceNode for browsers which don't support it natively.

    But you could also build your own if you prefer that. A looped AudioBufferSourceNode with a constant signal and a GainNode can be used to achieve the same effect. The AudioBuffer used with the AudioBufferSourceNode needs to have at least two samples because if it only has one sample Safari will not loop it.