Skip to content

Transport + Sequencer ​

Control a BPM-synced transport driving 5 tracks -- 3 drum tracks and 2 melody tracks. Switch presets to hear five book-derived grooves (Rock, Funk, Disco, Bossa, Shuffle), then click any drum cell to edit the pattern live.

A BPM-synced, editable timeline with musical time notation.

1:1Bar:Beat
BPM
0%
Swing
Preset:

Click drum cells to edit the pattern (rest → normal → accent → rest). Bass/Lead follow the preset.

Bar 1
1
2
3
4
Bar 2
1
2
3
4
Kick
Snare
Hi-hat
Bass
E2
E2
G2
A2
E2
E2
G2
D2
Lead
Em
Em
Em
G

How It Works ​

One worker-backed Transport clock drives all five tracks. Each quarter note has 12 ticks, and the playhead moves one sixteenth note every three ticks. The two-bar loop keeps the drum patterns and melody sequences together.

Drum Tracks via BeatTrack ​

Each drum lane uses a BeatTrack with three sample variations. The track rotates through them on successive hits. Pass noteType: 1 / 16 to syncTo() to place one pattern step on each sixteenth note.

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

const transport = await createTransport({ bpm: 96, timeSignature: [4, 4], ticksPerBeat: 4 })
const kick = await createBeatTrack(['/kick1.wav', '/kick2.wav', '/kick3.wav'], { numBeats: 32 })

// Numbers set velocity (0-1) as well as active/inactive -- 1 = accent, 0.6 = ghost note
kick.setPattern([1, 0, 0, 0, 0, 0, 0.6, 0, 1, 0, 0, 0]) // first 12 of the 32-step pattern
kick.syncTo(transport, { noteType: 1 / 16 }) // lock to transport grid
transport.start()

Swing: One Knob vs. Hand-Placed Shuffle ​

The Shuffle preset is the pedagogical hook of this demo. Its drum and bass patterns are written in perfectly straight 8th/16th notes -- there is no shuffle baked into the pattern data at all. The groove comes entirely from a single Transport property:

typescript
transport.swing = 0.55 // MPC-style shuffle -- 0 = straight, 1 = full triplet feel
transport.swingSubdivision = 1 / 8 // shuffle the 8th-note grid (default is 1/16)

Turn the swing knob down to 0 while Shuffle is playing and the groove flattens back to a straight, mechanical feel instantly -- the pattern didn't change, only the delay applied to every other subdivision did. That's the difference between hand-placing offbeat notes at shuffled positions and letting swing do the work: one knob re-shapes the whole track's feel live, without touching a single step.

Melody Tracks via Sequence ​

A Sequence calls each event with its scheduled audio time. Beat 0 starts the first bar, beat 4 starts the second, and beat 1.5 falls halfway between the second and third quarter notes. Each bass note or chord tone gets a fresh oscillator. Its gain falls to zero at the end of the decay, then its source stops.

Call the following function from a Play button. It returns a cleanup function for Stop or unmount.

typescript
import { createSequence, createTransport, getAudioContext, Oscillator } from 'ez-web-audio'

async function startBass() {
  const ctx = await getAudioContext()
  const transport = await createTransport(ctx, { bpm: 96, timeSignature: [4, 4] })
  transport.loop = true
  transport.loopEnd = '2m'
  const sequence = createSequence(transport, { length: '2m', loop: true })
  const voices = new Set<Oscillator>()
  const envelope = { attack: 0.005, decay: 0.25, sustain: 0, release: 0.05 }

  for (const [beat, note] of [[0, 'E2'], [1.5, 'G2']] as const) {
    sequence.at(beat, (time) => {
      const voice = new Oscillator(ctx, { note, type: 'triangle', gain: 0.5, envelope })
      voices.add(voice)
      voice.once('end', () => {
        voices.delete(voice)
        voice.dispose()
      })
      voice.once('play', () => {
        voice.audioSourceNode.stop(Math.max(time, ctx.currentTime) + envelope.attack + envelope.decay)
      })
      void voice.playAt(time)
    })
  }
  transport.start()
  return () => {
    transport.stop()
    for (const voice of voices)
      voice.dispose()
    voices.clear()
    sequence.dispose()
    transport.dispose()
  }
}

Mute and Solo ​

The demo applies the mute and solo buttons before the first note. Solo takes priority across all five lanes. Drum tracks receive their effective muted value, and melody callbacks check the same rule before starting a note. Pause and Stop also silence any drum hits that the clock has already scheduled.

typescript
function shouldPlay(name: 'bass' | 'lead'): boolean {
  const anySoloed = Object.values(trackState).some(t => t.soloed)
  if (anySoloed)
    return trackState[name].soloed
  return !trackState[name].muted
}

Live Playhead ​

The transport.on('tick') event drives the step-grid highlight. Each tick carries { bar, beat, tick }, which maps to a 16th-note step index across the 2-bar loop:

typescript
transport.on('tick', (e) => {
  const { bar, beat, tick } = e.detail
  const step = ((bar - 1) * 16) + ((beat - 1) * 4) + Math.floor(tick / 3)
  currentStep.value = step % 32 // 32-step loop
  positionDisplay.value = `${bar}:${beat}` // bar : beat
})

Editable Drum Lanes ​

Every drum-lane cell is clickable. A click cycles that step's value rest → normal → accent → rest and immediately calls setPattern() on the underlying BeatTrack -- there's no separate "commit" step, so edits are audible on the next pass through the loop:

typescript
// Cycle a step's velocity: rest -> normal -> accent -> rest.
function cycleStep(v: number): number {
  if (v <= 0)
    return NORMAL // NORMAL = 0.7
  if (v < ACCENT)
    return ACCENT // ACCENT = 1
  return 0
}

drumSteps[key][index] = cycleStep(drumSteps[key][index])
drumTrackInstance(key)?.setPattern(drumSteps[key])

Preset Switching ​

Choose a preset during playback to update the tempo, swing grid and drum patterns. The demo clears the two melody sequences, fades the previous voices and schedules the new notes from the current transport position. Shuffle swings eighth notes. Funk swings sixteenths.

Demo Controls ​

ControlWhat It Does
Play / Pause / StopControls transport clock
BPM sliderChanges tempo immediately during playback
Swing knobSets transport.swing (0-1) live -- try it on the Shuffle preset
M buttonMutes / unmutes individual track
S buttonSolos track (multiple solos stack)
Preset buttonsSwitches all 5 track patterns, BPM, and swing simultaneously
Step gridClick a drum cell to cycle rest → normal → accent; shows the current playhead position

Further Reading ​

  • Transport Guide -- full Transport clock API, tick events, and time signature options
  • Sequence Guide -- musical time notation, bar:beat:tick addressing, and note scheduling
  • Drum Machine -- simpler BeatTrack-only pattern sequencer