Three.js 3D Spectrogram (Waterfall FFT Plot)

A scrolling 3D spectrogram in Three.js, with frequency across, time receding and level as height and colour, showing melodies, drums, chirp sweeps and harmonic series analysed by a 2048-point FFT on a log or linear axis.

Key topics: spectrogram, waterfall plot, fft visualization, time frequency analysis, three.js audio, web audio

What this code does

- FFT: each frame, a 2,048-point Hann-windowed FFT gives magnitude per 21.5 Hz bin, either from the AnalyserNode or an in-page FFT in silent mode.
- Waterfall: a 160 × 140 displaced plane where the newest spectrum enters at the front and older rows scroll back.
- Log Axis: pitch is logarithmic, so octaves are evenly spaced; the linear axis crowds musical content to the left.
- Signals: harmonics appear as parallel ridges at integer multiples, chirps as diagonal lines, and drums as broadband vertical stripes.
- Colour Maps: inferno and viridis are perceptually uniform; the noise floor sets where the colour ramp starts.
- Controls: signal, frequency axis, colour map, height and noise floor.

Continue Learning

JavaScript Implementation

ES6+
// 3D spectrogram (waterfall): scroll rows of FFT magnitudes through a displaced plane
analyser.getFloatFrequencyData(db)                              // dB per bin
history.copyWithin(0, BINS)                                     // drop the oldest row
for (let c = 0; c < BINS; c++) {
  const hz = 40 * Math.pow(16000 / 40, c / (BINS - 1))          // log-spaced columns
  history[(ROWS - 1) * BINS + c] = clamp((db[Math.round(hz / binHz)] + 95) / 80, 0, 1)
}
history.forEach((v, i) => { positions.setY(i, v * 3); colors.setXYZ(i, ...inferno(v)) })