Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

AnalyserNode — audio spectrum and waveform

AnalyserNode exposes frequency-spectrum and waveform samples without modifying the audio passing through it.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const analyser = new AnalyserNode(context, { fftSize: 2048, smoothingTimeConstant: 0.8 });

AnalyserNode analyses audio without modifying it. This is Web Audio API, not ECMAScript; Node 24 does not establish browser support. Check availability of the constructors used.

AnalyserNode.fftSize sets the FFT size: a power of two from 32 through 32768, default 2048. Larger sizes improve frequency resolution at the cost of a longer window. Read-only AnalyserNode.frequencyBinCount equals fftSize / 2. If the size is invalid after Web IDL conversion to unsigned long, an IndexSizeError is thrown. The conversion can truncate a fractional input to a valid size: for example, 32.5 becomes 32.

AnalyserNode.smoothingTimeConstant smooths the spectrum: range 0–1, default 0.8; 0 disables smoothing. It does not smooth the waveform. A finite value outside 0–1 throws IndexSizeError, but a non-finite value such as NaN throws TypeError during Web IDL conversion, before the range check.

AnalyserNode.getByteFrequencyData(array) fills a Uint8Array with values 0–255 scaled using minDecibels and maxDecibels, not hertz. AnalyserNode.getByteTimeDomainData(array) writes waveform samples: silence corresponds to 128. Buffer sizes are respectively frequencyBinCount and fftSize; shorter buffers truncate data, while excess elements remain unchanged.

Examples

const context = new OfflineAudioContext(1, 128, 48000);
const analyser = new AnalyserNode(context, {
  fftSize: 32,
  smoothingTimeConstant: 0
});
const frequency = new Uint8Array(analyser.frequencyBinCount);
const waveform = new Uint8Array(analyser.fftSize);
analyser.getByteFrequencyData(frequency);
analyser.getByteTimeDomainData(waveform);
console.log(analyser.fftSize, frequency.length, waveform.length);

Result

32 16 32

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources