Three.js 3D Drum Machine (16-Step Sequencer)

A playable 3D drum machine in Three.js with six synthesised instruments on a 16-step grid of pads, click-to-toggle, swing, presets, randomise, and look-ahead scheduling on the Web Audio clock for tight timing.

Key topics: drum machine, step sequencer, web audio drums, beat maker, three.js music, 808

What this code does

- Synthesis: the kick is a sine with a fast pitch drop, snare and claps are noise bursts with a tonal body, hats are high-passed noise and the tom is a gliding sine.
- Look-Ahead Scheduling: each frame, notes falling in the next 120 ms are scheduled at exact AudioContext times, avoiding setTimeout jitter.
- Swing: off-beat 16ths are delayed by a fraction of a step for a shuffled groove.
- Pads: 96 instances of one mesh; raycasting returns the instanceId, which maps to (track, step).
- Feedback: pads pop when they actually sound, timed to the audio clock rather than the scheduler.
- Controls: pattern, tempo, swing, play, clear and randomise; click pads to edit.

Continue Learning

JavaScript Implementation

ES6+
// Look-ahead step sequencer: schedule notes slightly ahead on the audio clock, not setTimeout
let next = ctx.currentTime, step = 0

function tick () {                                        // call every animation frame
  while (next < ctx.currentTime + 0.12) {
    const swing = step % 2 ? swingAmount * stepDur * 0.5 : 0
    grid.forEach((row, track) => { if (row[step]) instruments[track](next + swing) })
    next += stepDur                                       // 60 / bpm / 4 for 16th notes
    step = (step + 1) % 16
  }
}
// Each pad is one instance of an InstancedMesh; raycast instanceId → (track, step) toggles it