Spectrogram Waterfall · How it works & code
Short-Time Fourier Transform (STFT): X-axis = frequency, Y-axis = time, color = energy
Runtime logic
Visiting `/audio/waterfall`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'waterfall' })`. mount resolves the variant to family `waterfall`; `withControls` builds the control panel (6 presets + sliders). `createCanvasStage` creates the `<canvas>` (HiDPI ≤ 2×, ResizeObserver) and the info panel. The family’s `build(S)` initialises state and returns the per-frame callback. Every `requestAnimationFrame`: read controls, advance a speed-scaled clock (dt ≤ 50 ms), run the frame callback; returned readouts go to the info panel. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. With the top-right Sound switch on, sound plays through the shared AudioContext; off = muted. Changing parameters: colour/speed/size apply live; structural ones remount; leaving the page cancels rAF, removes listeners and frees resources.
Environment
Canvas 2D: 2D canvas drawing; requestAnimationFrame: Display-synced animation loop; ResizeObserver: Canvas follows container size; Pointer Events: Unified mouse/touch/pen events; devicePixelRatio: Crisp HiDPI rendering (capped at 2×) · react 18.3.1, react-router-dom 6.30.6, vite 5.4.21
Dependencies
react 18.3.1
Imported APIs
withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; createCanvasStage — Shared Canvas 2D stage: canvas + info panel, HiDPI, one rAF loop, pointer state, idle auto-pointer; hexRgb — Colour string → [r, g, b]; clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.clear — Clears with the background colour (translucent = trails); S.onDown — Pointer down; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.drawImage() — Draws an image or offscreen canvas; ctx.strokeRect() — Strokes a rectangle; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; AudioContext.createBiquadFilter() — Creates a biquad filter (low/band/high-pass); AudioContext.createBuffer() — Creates an audio sample buffer; AudioContext.createBufferSource() — Plays a sample buffer (drums/noise)
Source code
frontend/src/demos/kits/kit-audio.js: FAM.waterfall (L593-L639)
Code walkthrough
L595-595: Control definitions (colour/speed/size/count/extras): High energy colors, Contrast γ, Number of frequency columns, History rows, Background noise threshold dB, Log Frequency Axis. L596-596: Preset buttons: name → parameter overrides (6). L597-605: Info-panel content: live rows(v), formula, notes, params, steps. L606-638: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `drawImage`, `strokeRect`; produces/reads sound via `A.voices`; math via `Math.round`, `Math.pow`, `Math.cos`, `Math.floor`; returns readouts `t`, `r`, `chord`, `cols`, `rows`, `ago`, `f` … to the info panel.
Principles
Each line = once FFT Brightness = ((L - Door Limit) / (0 - door Measures Exploitation) New row entering from the top, old row moving down → The axis is time The column spectrum can only be seen"This moment", Waterfall Chart (也叫声谱图、语谱图)把每个时刻的频谱排成一行,一行行往下流,于是 Crossaxis = Frequency, vertical axis = Time, Brightness = Loudness , A picture shows the sound. The straight lines in the graph are constant sound (cosine, bass), the melody in a row, the drums in the horizontal strip (they cover a lot of frequencies in an instant), and the slashing is Sweep audio (频率随时间变化)。语音识别、鸟叫分析都靠它。