Audio Metronome With Tap Tempo — Free HTML CSS JS Snippet

Audio Metronome With Tap Tempo · Misc · Plain HTML, CSS & JS · Live preview

What's included

Features

Fully working BPM metronome from 30 to 300 BPM with stepper controls
Synthesized square-wave click tone via OscillatorNode, no audio file
Tap Tempo button computing BPM from a rolling average of tap intervals
Automatic stale-tap reset after 2 seconds of inactivity
Live tempo changes while running via instant interval restart
Visual pulse dot synced to the exact same tick as the audio click
Start/Stop toggle with clear active-state button styling
No dependencies — pure Web Audio API and setInterval scheduling

About this UI Snippet

Audio Metronome With Tap Tempo — Synthesized Click With BPM and Tap Controls

Screenshot of the Audio Metronome With Tap Tempo snippet rendered live

This snippet is a fully functional metronome: a BPM readout, plus and minus stepper buttons, a Start/Stop toggle, and a tap-tempo button that lets you set the tempo by tapping along to a rhythm — all driven by a synthesized click tone rather than an audio file.

The click sound

playClick() builds a fresh OscillatorNode and GainNode on every beat: a short square wave burst at 1000Hz with the gain ramped down exponentially over 50 milliseconds. That fast decay is what turns a continuous tone into a short, percussive "tick" rather than a beep, and creating a new node pair per tick means overlapping or rapid tempo changes never leave a stuck-on oscillator behind.

Timing with setInterval

The beat scheduler uses setInterval at 60000 / bpm milliseconds — a straightforward interval-based approach rather than a full lookahead audio scheduler. Every change to bpm (via the stepper buttons or tap tempo) calls restartIfRunning(), which clears the existing interval and starts a new one at the updated tempo, so tempo changes take effect immediately without needing to stop and manually restart.

Tap tempo averaging

Each tap pushes a performance.now() timestamp into a rolling tapTimes array capped at the last five taps. From the second tap onward, the gaps between consecutive taps are averaged and converted to BPM (60000 divided by the average gap in milliseconds), so tempo settles quickly but still smooths out small timing variation between taps. A 2-second inactivity timeout resets the tap history so an old, stale tap never skews the start of a fresh tapping session.

Visual pulse synced to audio

A small dot pulses (scales up and changes color) via a CSS class toggled in the same tick() function that plays the click sound, so the visual beat indicator and the audible tick are always triggered from the exact same function call and never drift out of sync with each other.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Hand this snippet's HTML, CSS, and JavaScript to an AI coding assistant like Claude and ask it to adapt "Audio Metronome With Tap Tempo" to your project — restyle it to match your design system, wire it up to real data instead of the static example, or port it to the framework you're building in.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a misc component called "Audio Metronome With Tap Tempo" using plain HTML, CSS, and vanilla JavaScript — no framework, no build step.

Requirements:
- Match the structure and behavior of the "Audio Metronome With Tap Tempo" snippet from the UI Snippets Library.
- Keep the markup semantic and the styling self-contained (no external dependencies beyond what the original snippet uses).
- Keep the JavaScript vanilla, with no framework runtime required.
- Make it easy to restyle via CSS custom properties or class overrides so it can be dropped into a real project.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="mt-card">
  <div class="mt-head">
    <span class="mt-eyebrow">Metronome</span>
    <div class="mt-bpm-row">
      <button class="mt-step-btn" id="mtDownBtn" aria-label="Decrease BPM">-</button>
      <div class="mt-bpm-display">
        <span class="mt-bpm-num" id="mtBpmNum">120</span>
        <span class="mt-bpm-unit">BPM</span>
      </div>
      <button class="mt-step-btn" id="mtUpBtn" aria-label="Increase BPM">+</button>
    </div>
  </div>

  <div class="mt-pulse-wrap">
    <div class="mt-pulse" id="mtPulse"></div>
  </div>

  <div class="mt-controls">
    <button class="mt-btn mt-btn-primary" id="mtStartBtn">
      <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M8 5v14l11-7z"/></svg>
      Start
    </button>
    <button class="mt-btn mt-btn-tap" id="mtTapBtn">Tap Tempo</button>
  </div>
</div>

Step by step

How to Use

  1. 1
    Load the snippetClick "Audio Metronome With Tap Tempo" in the sidebar to load its HTML, CSS, and JS into the editor panels. The preview updates instantly.
  2. 2
    Edit the codeModify any panel — HTML, CSS, or JS. The preview refreshes as you type. Use Reset in each panel header to restore the original.
  3. 3
    Preview on devicesClick the Mobile (375px), Tablet (768px), or Desktop buttons in the preview header to check responsiveness.
  4. 4
    Export in your formatClick "HTML" to download a standalone file, "JSX" for a React component, "Tailwind" for a React + Tailwind CSS component, "Tailwind HTML" for a standalone HTML file with Tailwind CDN, "Vue" for a Vue 3 SFC with <template>/<script setup>/<style scoped>, or "Angular" for a standalone Angular .component.ts file. "Copy all" copies the full code to clipboard.
  5. 5
    Save your versionClick "Save as", type a name, and press Enter. Your snippet saves to IndexedDB and appears in the Saved tab.

Real-world uses

Common Use Cases

Music practice tools
A drop-in metronome for guitar, piano, or drum practice web apps.
Rhythm and timing tutorials
Demonstrates tap-tempo averaging and Web Audio click synthesis together.
DJ or DAW-adjacent tools
A reusable BPM input widget for any tempo-driven audio interface.
Web Audio scheduling reference
A simple starting point before upgrading to a full lookahead scheduler.

Got questions?

Frequently Asked Questions

Every tap records a timestamp. From the second tap onward, the time gaps between the last up to five taps are averaged, and BPM is computed as 60000 divided by that average gap in milliseconds — so it smooths out small inconsistencies in your tapping.

setInterval is simple and accurate enough for a UI metronome at typical BPM ranges. Professional audio applications often use a lookahead scheduler (scheduling notes slightly ahead of time via the AudioContext clock) for tighter timing under heavy main-thread load, which would be a natural upgrade to this snippet.

The existing setInterval is cleared and a new one is started immediately at the updated tempo, so tempo changes apply on the very next tick without needing to stop and restart.