CKTIS / JavaScript
AnalyserNode: FFT spectrum and waveform bytes
Set the FFT window size and spectrum smoothing, then size separate byte arrays for frequency bins and time-domain samples.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const analyser = context.createAnalyser();
analyser.fftSize = windowSize;
analyser.smoothingTimeConstant = smoothing;
const spectrum = new Uint8Array(analyser.frequencyBinCount);
const waveform = new Uint8Array(analyser.fftSize);
analyser.getByteFrequencyData(spectrum);
analyser.getByteTimeDomainData(waveform);AnalyserNode analyses a signal without changing the audio it passes through. It belongs to the Web Audio API, not ECMAScript 2024; check that your target runtime provides it.
fftSize is the number of samples in the fast Fourier transform (FFT) window. It defaults to 2048 and accepts powers of two from 32 to 32768, inclusive. The setter first converts the assigned value to Web IDL unsigned long. If the converted value is not one of those sizes, the setter throws an IndexSizeError DOMException. For example, assigning 256.5 succeeds because it converts to 256. At a fixed sample rate, a larger window covers more time and gives closer frequency bins. The read-only frequencyBinCount always equals fftSize / 2.
smoothingTimeConstant weights the previous smoothed magnitude of each frequency bin. Its inclusive range is 0 to 1, and its default is 0.8; a finite value outside that range throws IndexSizeError. At 0, the previous analysis has no influence; at 1, new data has no influence on the smoothed spectrum. This is a unitless weight, not a duration in seconds. It does not smooth the waveform.
getByteFrequencyData(array) writes bin levels into a Uint8Array, scaling decibels to 0–255: levels at or below minDecibels map to 0, and levels at or above maxDecibels map to 255. Use frequencyBinCount elements. getByteTimeDomainData(array) writes successive waveform samples in the range 0–255, with 128 representing zero amplitude. Use fftSize elements for the full waveform. Both methods return undefined and overwrite the supplied array: a short array drops data, and any extra elements in a long array are left unchanged. Reuse the arrays between reads and resize or replace them after changing fftSize.
Examples
<!doctype html>
<meta charset="utf-8">
<title>AnalyserNode: silence</title>
<pre id="output"></pre>
<script>
(async () => {
const context = new OfflineAudioContext(1, 512, 48000);
const analyser = context.createAnalyser();
analyser.fftSize = 256;
analyser.smoothingTimeConstant = 0;
analyser.connect(context.destination);
const spectrum = new Uint8Array(analyser.frequencyBinCount).fill(42);
const waveform = new Uint8Array(analyser.fftSize).fill(42);
await context.startRendering();
analyser.getByteFrequencyData(spectrum);
analyser.getByteTimeDomainData(waveform);
const lines = [
`spectrum: ${spectrum.length} bins; all 0: ${spectrum.every(value => value === 0)}`,
`waveform: ${waveform.length} samples; all 128: ${waveform.every(value => value === 128)}`
];
document.getElementById('output').textContent = lines.join('\n');
for (const line of lines) console.log(line);
})();
</script>Result
spectrum: 128 bins; all 0: true waveform: 256 samples; all 128: true
Save the example as an HTML file and open it in a browser with Web Audio and promise-based offline rendering support. The example analyses silence without microphone access or playback. Expect 128 bins of `0` and 256 samples of `128`, replacing the initial `42` values.
Related lessons
- JavaScript · Lesson 34 · Advanced · Version: 4
Sources
- AnalyserNode
- AnalyserNode: fftSize property
- AnalyserNode: smoothingTimeConstant property
- AnalyserNode: frequencyBinCount property
- AnalyserNode: getByteFrequencyData() method
- AnalyserNode: getByteTimeDomainData() method
- AnalyserNode: minDecibels property
- AnalyserNode: maxDecibels property
- OfflineAudioContext: OfflineAudioContext() constructor
- OfflineAudioContext: startRendering() method
- ECMAScript 2024 Language Specification — TypedArray Objects