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

Microphone Visualizer · How it works & code

Spectrogram + Vowel Formants: Real-time breaks microphone sound into frequencies.

Runtime logic

Visiting `/audio/mic`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'mic' })`. mount resolves the variant to family `mic`; `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. Tapping the on-stage “Mic” pill gets the live waveform via getUserMedia (separate analysis-only AudioContext, no output). 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; MediaDevices.getUserMedia: Microphone input (HTTPS + permission required); 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.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.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.arc() — Adds a circular arc (center, radius, start/end angle); 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.createRadialGradient() — Creates a radial gradient; ctx.fillRect() — Fills a rectangle; AudioContext.createBiquadFilter() — Creates a biquad filter (low/band/high-pass)

Source code

frontend/src/demos/kits/kit-audio.js: FAM.mic (L267-L313)

Code walkthrough

L268-268: Label of the on-stage input pill. L269-269: Control definitions (colour/speed/size/count/extras): Spectrum color, Ring radius, Band count, Sensitivity, Smooth τ, Mirror left-right. L270-270: Preset buttons: name → parameter overrides (6). L271-279: Info-panel content: live rows(v), formula, notes, params, steps. L280-312: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `stroke`, `arc`; produces/reads sound via `A.startMic`, `A.micBuf`; math via `Math.round`, `Math.floor`, `Math.sin`, `Math.pow`, `Math.min`, `Math.max` …; returns readouts `src`, `f0`, `vw`, `band`, `rng`, `lv`, `rms` … to the info panel.

Principles

Range of frequency band Among i: f = 60 x (5,000) / 60)^(i / N) … 60 × (5000 / 60)^((i+1) / N) Level L = 10·log₁₀(Average power in the band) Smooth: Shows the value, but · Old value + (1-rather) Management · New Value When a man speaks, a vocal vibration comes out Fundamental frequency f₀ (决定音高)和一串谐波;口腔、舌头的形状像一个"Filter",It's gonna make a few special frequencies"Bulge"叫 Resonance peaks F1、F2。Different vowel(s) a、i、u…)的 F1/F2 Different, so we can tell References. Here 64 One band around: Angle = Frequency (From low to high, distributed in logarithms, and in human ears, Length outward = Loudness 。Default playing a simulator, in a/o/i/u/e 之间循环;点右上角「麦克风」授权后,就换成你自己的声音。

Open the interactive demo · GitHub