Skip to content

Drum Machine ​

Create rhythmic patterns with a step sequencer using the BeatTrack API.

Build a beat in seconds — click cells, press play.

120 BPM
1
·
·
·
2
·
·
·
3
·
·
·
4
·
·
·
KICK
SNARE
HIHAT

How to Use ​

  • Click cells to toggle beats on/off in the 16-step grid
  • Press Play to hear your pattern loop
  • Adjust BPM with the slider (60-200 BPM)
  • Visual playhead highlights the current beat in sync with audio

Quick Start ​

Call startKick() from a Play button’s click handler. This pattern hits the kick on the first sixteenth note of each beat and leaves three rests before the next hit.

typescript
import { createBeatTrack } from 'ez-web-audio'

export async function startKick(sampleUrl: string) {
  const kick = await createBeatTrack([sampleUrl], { numBeats: 16 })
  kick.setPattern([1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0])
  kick.playActiveBeats(120, 1 / 16)
  return kick
}

Use the URL of a kick sample your app serves, such as /audio/drum-samples/kick1.wav for a copy of this site’s kick drum. Keep the returned track so the Stop button can call kick.stop(). Call kick.dispose() when removing the instrument.

Visual Sync ​

The playhead highlighting in the demo above works because each Beat object has properties that auto-toggle on the scheduler's timeline:

PropertyWhen trueUse for
beat.currentTimeIsPlayingThis beat's time slot is active (playing or resting)Playhead indicator on all steps
beat.isPlayingThis beat is producing sound right nowHighlight only when sound plays

Both reset to false automatically after duration ms (default 100ms). No manual bookkeeping needed.

Full Vue Example ​

useBeatTrack() supplies reactive beats to a Vue template. Bind a step button’s pressed state to beat.active and its playhead highlight to beat.currentTimeIsPlaying. The complete Vue component includes the editable grid, first-click loading and cleanup on unmount.

Advanced: Event-Based Sync ​

A standalone BeatTrack emits a beat event for every step. Use event.detail.beatIndex to move the highlight and event.detail.active to distinguish a hit from a rest. The vanilla TypeScript example updates the buttons in its own row.

Round-Robin Playback ​

For a little variation between hits, pass several recordings of the same drum to createBeatTrack(). The demo cycles through kick1.wav, kick2.wav and kick3.wav in the audio/drum-samples directory. Each active step advances to the next recording.

Integration Pattern Examples ​

These pages build on the drum machine concept with full implementations showing different UI sync approaches:

  • Vue Reactive Pattern — Beat properties drive UI directly via useBeatTrack(). No event listeners needed for visual sync. Includes mute/solo controls demonstrating direct Beat property manipulation.
  • Vanilla TS Events — track.on('beat', ...) drives DOM updates. Framework-agnostic pattern for React, vanilla JS, or any environment without reactive proxies.

API Reference ​

Method / PropertyDescription
createBeatTrack(urls, { numBeats })Create a step sequencer with sample variations
beats[i].activeToggle individual beats on/off
playActiveBeats(bpm, noteValue)Loop the pattern, sounding its active steps
playBeats(bpm, noteValue)Play every step
stop()Stop and reset to beginning
pause() / resume()Pause/resume from current position
setTempo(bpm)Change tempo while playing
gainTrack volume (0 to 1)
on('beat', fn)Beat event with { beatIndex, active, time }
on('stop', fn)Fired when stop() is called

Next Steps ​