Synthesizer · How it works & code
Oscillator waveforms, LFO vibrato, and harmonic spectrum: see why square and sawtooth waves appear 'brighter'.
Runtime logic
Visiting `/audio/synth`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'synth' })`. mount resolves the variant to family `synth`; `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.onDown — Pointer down; S.get — Reads current control values; S.onUp — Pointer up; S.clear — Clears with the background colour (translucent = trails); S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.strokeRect() — Strokes a rectangle; 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.createLinearGradient() — Creates a linear gradient; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.arcTo() — Adds an arc using tangent lines; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width
Source code
frontend/src/demos/kits/kit-audio.js: FAM.synth (L198-L263)
Code walkthrough
L199-199: Control definitions (colour/speed/size/count/extras): Sound Color, Volume, Harmonic Limit, Wave shape (0 sine 1 square 2 sawn 3 triangle), Tremolo Rate Hz, Tremolo Depth (cents). L200-200: Preset buttons: name → parameter overrides (6). L201-209: Info-panel content: live rows(v), formula, notes, params, steps. L210-262: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `strokeRect`, `beginPath`, `moveTo`, `lineTo`, `stroke`, `createLinearGradient` …; produces/reads sound via `A.voices`; math via `Math.floor`, `Math.round`, `Math.exp`, `Math.pow`, `Math.sin`, `Math.max` …; returns readouts `fs`, `ml`, `mr`, `mt`, `wy`, `wh`, `sy` … to the info panel.
Principles
Sound high: f₀ = 440 × 2^((n − 69) / 12) (n = MIDI Acoustics, A4 = 69) Sawtooth Wave = Σ (1/k)·sin(2π·k·f₀·t), Fong Bo only takes odd numbers k, Delta Wave 1/k² Damping Tremendous: f(t) = f₀ × 2^(Depth · sin(2π·Speed · t) / 1200) Synthesizer Use a mathematical function directly"造"Make a sound. The simplest thing is Sinewave :只有一个频率,听起来像口哨。方波、锯齿波、三角波都可以拆成很多个正弦波相加——These chords Harmonic ,The frequency is exactly the base frequency f₀ 的 1、2、3… Double. Top Waveform (The sound over time Spectrum (How many frequencies per frequency) FFT(快速傅里叶变换)从波形算出来。你会看到频谱上等间距的"Sharp peak",It's a single wave; the more the waves, the stronger the sound"Bright"、越"刺"。 The lower key can click and play; it automatically plays a melody without touching it. The sound in the top right corner can really hear it.