Drum Machine · How it works & code
16 Step Sequencer: Synthetic Bottom Drum, Military drums, Clap, Step on it, support the swing.
Runtime logic
Visiting `/audio/drum`, DemoHost lazy-loads `demos/kits/kit-audio.js` (registry `mod: 'kit-audio'`) and calls `mount(host, { variant: 'drum' })`. mount resolves the variant to family `drum`; `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.get — Reads current control values; 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.fill() — Fills the current path; ctx.stroke() — Strokes 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.closePath() — Closes the current sub-path; ctx.arcTo() — Adds an arc using tangent lines; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width; 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.drum (L810-L858)
Code walkthrough
L811-811: Canvas cursor style. L812-812: Control definitions (colour/speed/size/count/extras): The drum pad color, Tiles round corner, Steps, BPM (beats per minute), swing, Beats Mark. L813-813: Preset buttons: name → parameter overrides (6). L814-822: Info-panel content: live rows(v), formula, notes, params, steps. L823-857: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `fill`, `stroke`, `beginPath`, `moveTo`, `lineTo`, `closePath`; produces/reads sound via `A.voices`; math via `Math.min`, `Math.round`, `Math.floor`, `Math.exp`; returns readouts `fs`, `ml`, `mt`, `cw`, `ch`, `gw`, `gh` … to the info panel.
Principles
= 60 / BPM seconds; one step = 1 / 4 x 16 notes 第 k The moment of step = k × One step long swing × One step) Drum sound is all synthetic: Bottom drum = downward sine, Army drum = Noise + 190 Hz, Step = High frequency noise Drum Machine Split a small section 16 Small cells (one each)"Sixteen minutes"),每一行是一种鼓,亮着的格子就会在播放头经过时敲一下。电子音乐、嘻哈的节奏几乎都是这样排出来的。 Click the grid to open/Close. BPM Decision Slow: BPM 120 Time shot 0.5 Second, one 0.125 Sec. Swing (摇摆)会把每个偶数后面的格子稍微推迟,节奏马上变得"晃"Get up, jazz and Lo-fi Usual. The sound can be heard at the top right corner.