How the drum machine works

This page is a 16-step sequencer with six tracks. Each pad is a gate: on or off for that 16th note. The sound is built with oscillators and filtered noise in the Web Audio API. There is no MP3, WAV, or OGG file to fetch, and there is no account.

Play and Stop are also bound to the space bar, unless you are typing in a field. The first click or key press resumes the audio context, which browsers keep silent until a gesture.

Mute and volume sit on each track. Master volume sits under the grid. The sub-bass root changes the pitch of the lowest track without touching the drum gates.

How each track is built

Kick

A sine oscillator with a fast downward pitch slide and a short punch envelope.

Snare

A band-pass and high-pass noise burst plus a short triangle body tone.

Clap

Three or four very short band-pass noise taps with a slightly longer tail on the last tap.

Closed Hat

Bright high-pass noise with a very short decay. It also chokes a ringing open hat.

Open Hat

The same metallic noise with a longer decay, until a closed hat cuts it off.

Sub Bass

A low-pass sine and a little triangle, tuned to the root you pick.

Timing and swing

A 16th note lasts 60 / BPM / 4 seconds. Notes are scheduled against AudioContext.currentTime with a short lookahead, so the clock is not a setInterval beat.

Swing delays only the odd 16th steps (steps 2, 4, 6, and so on in 1-based counting) by up to 60% of one 16th. Even steps stay on the grid, so a 4-bar loop is still exactly 64 sixteenths long.

The 4-bar WAV export

Export renders four bars into an OfflineAudioContext at 44.1 kHz, then writes a standard RIFF/WAVE 16-bit PCM file (mono) and downloads it from this tab. Sample values are clamped to the 16-bit range. The file is made on your computer.

Back to the machine

Questions

Why is there no setInterval click?

Notes are scheduled on AudioContext.currentTime with a short lookahead. A 25 ms timer only decides what to queue next; it does not fire the sounds.

Why is the export mono 16-bit?

A mono 44.1 kHz 16-bit PCM WAV is a small, widely opened loop format. The mix is already a single bus in this machine.