<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mike Michalczewsky</title>
    <description>The latest articles on DEV Community by Mike Michalczewsky (@m16averick).</description>
    <link>https://dev.to/m16averick</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4229251%2Fed0ab9b8-86a9-47d3-af55-afadfa46d97b.png</url>
      <title>DEV Community: Mike Michalczewsky</title>
      <link>https://dev.to/m16averick</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/m16averick"/>
    <language>en</language>
    <item>
      <title>Building a browser metronome that stays in time at 1000 BPM</title>
      <dc:creator>Mike Michalczewsky</dc:creator>
      <pubDate>Thu, 05 Nov 2026 20:46:35 +0000</pubDate>
      <link>https://dev.to/m16averick/building-a-browser-metronome-that-stays-in-time-at-1000-bpm-28mf</link>
      <guid>https://dev.to/m16averick/building-a-browser-metronome-that-stays-in-time-at-1000-bpm-28mf</guid>
      <description>&lt;p&gt;I play fast music, and most metronomes I tried stop at 240 or 300 BPM. So I built one that goes to 1000: &lt;a href="https://unlimitedmetronome.com/" rel="noopener noreferrer"&gt;Unlimited Metronome&lt;/a&gt;. It is free, needs no account and works offline. This post is about the parts that turned out to be harder than "play a click every N milliseconds".&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;setInterval&lt;/code&gt; is not a metronome
&lt;/h2&gt;

&lt;p&gt;The obvious version is a timer that plays a sound on every tick. It sounds fine at 80 BPM and falls apart as soon as anything else happens on the page: timers drift, the main thread is busy rendering, and browsers throttle timers in background tabs to once a second or less. At 260 BPM a beat is 230 ms long, so even a 20 ms wobble is audible.&lt;/p&gt;

&lt;p&gt;The fix is the pattern from Chris Wilson's article "A Tale of Two Clocks": keep two clocks with different jobs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A coarse JavaScript timer wakes up every 25 ms. It never plays anything itself.&lt;/li&gt;
&lt;li&gt;On every wake-up it looks a short window ahead (150 ms) and schedules every click that falls inside that window &lt;strong&gt;on the audio clock&lt;/strong&gt;, with &lt;code&gt;AudioBufferSourceNode.start(time)&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTime&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;horizon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;now&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lookahead&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sequencer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;peekTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;horizon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sequencer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bpm&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pulses&lt;/span&gt;
    &lt;span class="c1"&gt;// A tick more than 50 ms in the past means the timer stalled:&lt;/span&gt;
    &lt;span class="c1"&gt;// skip its sound instead of playing a burst of late clicks.&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scheduleClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The audio clock is sample-accurate, so the timer's jitter no longer matters: it only has to wake up before the window runs out. The UI (the beat dots, the bar counter) reads from what has been scheduled and never touches this loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping time in a background tab
&lt;/h2&gt;

&lt;p&gt;Musicians switch to a tab with sheet music or a video and expect the click to keep going. Two things make that work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The timer runs in a dedicated Web Worker.&lt;/strong&gt; Browsers throttle main-thread timers of hidden tabs hard; a worker is throttled far less. If workers are unavailable it falls back to &lt;code&gt;setInterval&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The look-ahead grows while the page is hidden&lt;/strong&gt;, from 150 ms to 1.2 s, so even a late wake-up still finds clicks already queued on the audio clock.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Clicks that do not pile up at 1000 BPM
&lt;/h2&gt;

&lt;p&gt;At 1000 BPM with sixteenth-note subdivision there are 66 clicks per second, about 15 ms apart. A click sample longer than that overlaps the next one and turns into mush.&lt;/p&gt;

&lt;p&gt;The clicks are not samples at all. Each one is synthesised on the device into an &lt;code&gt;AudioBuffer&lt;/code&gt; (a short tone with a few partials, an exponential decay and a little filtered noise in the attack), and every click is rendered no longer than 90% of the time until the next one. The rendered buffers are cached by length in 5 ms buckets, so the cache stays small even when the tempo changes all the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  A speed trainer that changes tempo on the bar line
&lt;/h2&gt;

&lt;p&gt;The feature I wanted most was a trainer: start at 220, go up by 1 BPM every 4 bars until 240. The rule that made it feel right is that &lt;strong&gt;tempo changes are only evaluated on bar boundaries&lt;/strong&gt;. The sequencer knows which bar and stage it is in, and a new stage always starts exactly on beat one: no extra click, no missing one, no tempo change in the middle of a bar.&lt;/p&gt;

&lt;p&gt;Because the sequencer is plain TypeScript with no audio in it, it is easy to test: give it a plan and a start time, pull ticks, and check that the first tick of every stage lands on a downbeat with the new tempo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Odd meters
&lt;/h2&gt;

&lt;p&gt;Time signatures go from 1 to 32 beats, and beats can be grouped, so 7/8 can be counted as 2+2+3 or 3+2+2. The first beat of each group gets an accent, and any beat can be switched between accent, normal and mute.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rest of the stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;React for the UI, with all of the timing outside React.&lt;/li&gt;
&lt;li&gt;Every page is prerendered to static HTML for each of the 9 languages and served from Cloudflare Pages.&lt;/li&gt;
&lt;li&gt;A service worker precaches the app, so after the first visit it works offline and can be installed as a PWA.&lt;/li&gt;
&lt;li&gt;No account and no cookies. Settings stay in the browser's local storage.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you try it, I would love to hear how the timing feels on your device at very high tempos, and what is missing: &lt;a href="https://unlimitedmetronome.com/" rel="noopener noreferrer"&gt;unlimitedmetronome.com&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>music</category>
    </item>
  </channel>
</rss>
