Skip to content

Drum Machine: Vue Reactive Pattern ​

Turn on a snare step and press Play. The snare sounds each time the playhead reaches it. In Vue, the same Beat object holds the step’s on/off state and tells the template when to highlight it.

The same machine, built with the Vue composables.

120 BPM
Step: 1/16
KICK
SNARE
HIHAT
1
·
·
·
2
·
·
·
3
·
·
·
4
·
·
·
KICK
SNARE
HIHAT

How to Use ​

  • Click cells to toggle beats on/off
  • Press Play to start the pattern loop
  • Mute (M) silences a track by deactivating all its beats
  • Solo (S) mutes all other tracks
  • Adjust BPM to change tempo in real-time
  • Step counter shows current playhead position

How It Works ​

The demo creates one useBeatTrack() per lane. Its load() method wraps each Beat in a Vue reactive proxy. Changing beat.active edits the rhythm. As playback advances, beat.currentTimeIsPlaying changes even on rests, so the playhead can cross a silent step.

The grid is editable before you press Play. The component keeps those early edits in a local pattern, then copies them to the track when its samples finish loading. The tempo slider calls setTempo() during playback. Mute saves a lane’s active steps and clears them until you unmute it.

Complete Example ​

This Vue 3 component plays a single kick lane. It uses @ez-web-audio/vue from the 0.2.0 workspace. Put the kick sample at public/audio/drum-samples/kick1.wav in your app.

vue
<script setup lang="ts">
import { useBeatTrack, useCleanup, useEnsureLoaded } from '@ez-web-audio/vue'
import { computed, ref, watch } from 'vue'

const { instance: kick, load } = useBeatTrack()
const { register } = useCleanup()
const bpm = ref(120)
const playing = ref(false)
const draft = ref(Array.from({ length: 16 }, (_, i) => ({
  active: i % 4 === 0,
  currentTimeIsPlaying: false,
})))
const steps = computed(() => kick.value?.beats ?? draft.value)

const { ensureLoaded, loading, error } = useEnsureLoaded(async () => {
  const track = await load(['/audio/drum-samples/kick1.wav'], { numBeats: 16 })
  track.setPattern(draft.value.map(step => step.active))
  register(track)
})

async function toggle() {
  if (!(await ensureLoaded()) || !kick.value)
    return
  if (playing.value)
    kick.value.stop()
  else
    kick.value.playActiveBeats(bpm.value, 1 / 16)
  playing.value = !playing.value
}

watch(bpm, value => kick.value?.setTempo(value))
</script>

<template>
  <button type="button" :disabled="loading" @click="toggle">
    {{ playing ? 'Stop' : 'Play' }}
  </button>
  <label>Tempo {{ bpm }} BPM
    <input v-model.number="bpm" type="range" min="60" max="200">
  </label>
  <p v-if="error" role="alert">
    {{ error }}
  </p>
  <div>
    <button
      v-for="(step, i) in steps" :key="i" type="button"
      :aria-pressed="step.active"
      :class="{ current: playing && step.currentTimeIsPlaying }"
      @click="step.active = !step.active"
    >
      {{ i + 1 }}
    </button>
  </div>
</template>

<style scoped>
[aria-pressed="true"] { background: navy; color: white; }
.current { outline: 2px solid orange; }
</style>

playActiveBeats(bpm, 1 / 16) advances one sixteenth note per step. Only active steps trigger the sample. Stop returns the playhead to the start and keeps your edits. When the component unmounts, useCleanup() disposes the registered track.

See Also ​