Web Audio API example
Web Audio API visualizer. AnalyserNode FFT from the microphone, drawn on canvas. Frequency data the audio element will not give you.
The problem
An <audio> element plays sound. It will not tell you what the signal is doing. Frequency and
amplitude stay inside the player.
The method
AudioContext is a graph. A source feeds a GainNode, then an AnalyserNode.
The analyser runs an FFT. getByteFrequencyData() is the spectrum.
getByteTimeDomainData() is the waveform. Same node, two reads.
What to do
- Run
AudioContext. That is the graph. - Connect the mic, or pick a file and connect that.
- Run the analyser. Then switch to the time domain.
AudioContext
Creates the graph. Browsers keep it suspended until a user gesture. resume() is that yes.
const ctx = new AudioContext()
await ctx.resume()
createMediaStreamSource()
Puts the microphone on the graph. Do not connect a mic to destination or you get feedback.
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
const source = ctx.createMediaStreamSource(stream)
source.connect(gain)
createMediaElementSource()
Puts an <audio> element on the graph. One element can only make one source node. Connect
through to destination or you will not hear it.
const source = ctx.createMediaElementSource(audio)
source.connect(gain)
gain.connect(ctx.destination)
AnalyserNode
An FFT on the graph. fftSize is the window. frequencyBinCount is half of that.
getByteFrequencyData() fills one byte per bin. Gain and fftSize are the node, not a CSS scale.
const analyser = ctx.createAnalyser()
analyser.fftSize = 256
gain.connect(analyser)
analyser.getByteFrequencyData(bins)
0.8
getByteTimeDomainData()
Same analyser, other buffer. 128 is silence. The line is the waveform, not the spectrum.
analyser.getByteTimeDomainData(bins)