加载应用…
云澜 Clyune Clyune Component Gallery · 中文

Noise Filter Pad · How it works & code

White/pink/brown noise passed through low-pass/band-pass/high-pass filters, observe how the spectrum is sculpted.

Runtime logic

Visiting `/audio/noise-pad`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'noise-pad' })`. mount resolves the variant to family `noise-pad`; `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; hueOf — Hue of a colour (0–360°); clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.onMove — Pointer move; S.onDown — Pointer down; S.clear — Clears with the background colour (translucent = trails); S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.createLinearGradient() — Creates a linear gradient; ctx.fill() — Fills the current path; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.createRadialGradient() — Creates a radial gradient; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.strokeRect() — Strokes a rectangle; ctx.fillRect() — Fills a rectangle; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path

Source code

frontend/src/demos/kits/kit-audio.js: FAM.noise-pad (L756-L805)

Code walkthrough

L757-757: Canvas cursor style. L758-758: Control definitions (colour/speed/size/count/extras): Filter color, Dot size, Spectrum bars, Filter type (0 low 1 with 2 high), Noise colour (0 white, 1 powder, 2 brown), Q upper limit. L759-759: Preset buttons: name → parameter overrides (6). L760-768: Info-panel content: live rows(v), formula, notes, params, steps. L769-804: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onMove`; draws with Canvas 2D `createLinearGradient`, `fill`, `beginPath`, `moveTo`, `lineTo`, `stroke` …; produces/reads sound via `A.noise`; math via `Math.round`, `Math.sin`, `Math.pow`, `Math.log10`, `Math.floor`, `Math.max` …; returns readouts `fs`, `x0`, `y0`, `w`, `ph`, `sy`, `sh` … to the info panel.

Principles

White noise: same energy per frequency; pink: ∝1 / f (equal energy of 8 degrees each); brown: ∝1 / f² Second stage filter (RBJ biquad): ω0 = 2 π · fc / fs,α = sin ω₀ / (2Q) Output spectrum = noise spectrum + filter response (dB plus) Noise It's all the random noises, like TV snowflakes"Rustle"Sound. Just one in the back Filter ,就能把它变成风声、海浪、呼吸声:低通只留低频(闷、像远处的风),高通只留高频(嘶嘶声),带通只留中间一段(像吹口哨的风)。 This one XY Mat: Horizontal = Cut-off frequency , Longitudinal = Resonance Q (The more up, the more the cut-off frequency"Point",会听到一个像音高的鸣响)。下方是滤波后的频谱和滤波器曲线。点右上角「声音」能听到真实效果。

Open the interactive demo · GitHub