DEV Community

Bhavesh Vaghela
Bhavesh Vaghela

Posted on AI-assisted

Beat detection in the browser: an onset curve, a tempo guess and a dynamic-programming tracker

Cutting on the beat is one of the quickest ways to make an edit feel right, and placing markers by hand takes ages. Here is how beat detection can run entirely in the browser, and where it goes wrong. I built a free tool around this on my site; the sketch below shows the core idea.

From audio to beats

  1. Mix to mono and reduce to about 22 kHz.
  2. Every 11.6 ms, measure how much the sound spectrum changed since the last step. Drum hits show up as peaks in this onset curve.
  3. Work out the tempo: it is the spacing at which that curve repeats most strongly, favouring tempos between 80 and 160 BPM.
  4. Run a dynamic-programming beat tracker (the method Dan Ellis published in 2007). It picks the chain of peaks that best fits the tempo, and each step pays a penalty for drifting from the expected spacing.

For steady music, fitting a straight line through all the beats sharpens the tempo to a fraction of a BPM.

A simplified tracker

Again, an illustration of the approach, not the tool's source. onset is the curve from step 2 and period is the expected beat spacing in frames.

// Sketch of the dynamic-programming beat tracker idea (Ellis, 2007). Not the tool's source.
// onset: onset-strength curve, one value per frame. period: expected beat spacing in frames.
function trackBeats(onset, period, tightness = 100) {
  const n = onset.length;
  const score = new Float64Array(n);
  const back = new Int32Array(n).fill(-1);
  for (let t = 0; t < n; t++) {
    let best = 0, from = -1;
    const lo = Math.max(0, Math.floor(t - 2 * period));
    const hi = Math.floor(t - period / 2);
    for (let p = lo; p <= hi; p++) {
      // penalty is zero when the gap equals one period, and grows as it drifts from it
      const penalty = -tightness * Math.pow(Math.log((t - p) / period), 2);
      const s = score[p] + penalty;
      if (from === -1 || s > best) { best = s; from = p; }
    }
    score[t] = onset[t] + (from === -1 ? 0 : best);
    back[t] = from;
  }
  // start from the best-scoring frame near the end, then walk back
  let t = 0;
  for (let i = Math.max(0, n - Math.round(period)); i < n; i++) if (score[i] > score[t]) t = i;
  const beats = [];
  for (; t >= 0 && t !== -1; t = back[t]) beats.push(t);
  return beats.reverse();
}
Enter fullscreen mode Exit fullscreen mode

On a generated 120 BPM onset curve with random clutter added, this returned 24 beats with a mean gap of 0.500 s.

Follow the audio, or use a grid

  • Follow the audio keeps the beats the tracker actually found. Use it for live music or songs where the tempo drifts.
  • Steady grid places perfectly even markers at the tempo. Use it for electronic music and anything made to a click.

What goes wrong

  • Double or half tempo. Many songs feel right at either speed, so offer x2 and /2 buttons, or let people type the tempo they expect, and track again.
  • Shifted markers. A nudge control plus a click preview lets you hear whether markers sit on the beat.
  • Hard material. Ambient pads, solo piano and heavy syncopation can fool any beat detector.

What I measured

On generated drum patterns from 75 to 140 BPM, the tool found the tempo within about 0.1 BPM and put every beat within 45 ms of the true beat. I tested generated patterns, not a library of real songs, so check the first result by ear.

Getting markers into an editor

  • After Effects: a .jsx script that adds markers to a layer or composition.
  • DaVinci Resolve: an EDL of markers (choose your timeline's frame rate first).
  • Premiere Pro or CapCut: an SRT where every caption starts on a beat.
  • CSV and TSV for spreadsheets and scripts.

The free tool is here: Beat Marker. A longer, editor-facing guide is here.

I checked the file layouts against their formats but have not opened them in every editor.

I'm Bhavesh, a freelance video editor and motion designer in Rajkot, India.

Top comments (1)

Collapse
 
fireflyteamapps profile image
Firefly Team •

Splitting "follow the audio" from a steady grid is a good call, for editors that choice matters more than the tracker itself. A small speed-up for step 1: render the decoded buffer through an OfflineAudioContext created with 1 channel at 22050 Hz, and the browser downmixes and resamples natively in one pass instead of in JS. Of the 45 ms worst case, how much comes from the 11.6 ms hop and how much from the onset peak landing a little after the actual transient?