<?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: Conkormedia</title>
    <description>The latest articles on DEV Community by Conkormedia (@conkormedia).</description>
    <link>https://dev.to/conkormedia</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%2F4029277%2Fcf59ae00-4cc3-4b97-b66b-2284fc275ead.png</url>
      <title>DEV Community: Conkormedia</title>
      <link>https://dev.to/conkormedia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/conkormedia"/>
    <language>en</language>
    <item>
      <title>Detecting musical key and tempo on-device: the DSP behind BeatScope</title>
      <dc:creator>Conkormedia</dc:creator>
      <pubDate>Tue, 14 Jul 2026 21:36:50 +0000</pubDate>
      <link>https://dev.to/conkormedia/detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope-31me</link>
      <guid>https://dev.to/conkormedia/detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope-31me</guid>
      <description>&lt;p&gt;I needed to read a track's &lt;strong&gt;BPM&lt;/strong&gt; and &lt;strong&gt;musical key&lt;/strong&gt; in seconds, offline, on a&lt;br&gt;
phone. No ML black box, no server. Here's the signal-processing pipeline I ended&lt;br&gt;
up with — hand-rolled, since the usual packages weren't an option.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tempo
&lt;/h2&gt;

&lt;p&gt;Tempo is periodicity in the track's energy. The steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Spectral flux → onsets.&lt;/strong&gt; Sum the positive frame-to-frame magnitude increases
of the STFT. Peaks are onsets (kick, snare, synth attacks). That's the onset
envelope.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   flux[n] = Σ_k max(0, |X[n,k]| - |X[n-1,k]|)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Autocorrelation.&lt;/strong&gt; The lag where the onset envelope best matches itself is the
beat period.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comb over multiples.&lt;/strong&gt; Weight candidate periods by their multiples (½, 2×) so
tempo isn't confused with its harmonics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Octave folding + prior.&lt;/strong&gt; Fold candidates into 60–180 BPM and softly prefer
typical dance tempos — kills the "174 vs 87" error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Median smoothing&lt;/strong&gt; for stability.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the beat &lt;strong&gt;grid&lt;/strong&gt;, I use dynamic programming (Ellis-style): reward beats that&lt;br&gt;
land on onsets, penalize deviation from a steady interval. The DP gives a grid&lt;br&gt;
that doesn't drift.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Chromagram.&lt;/strong&gt; Fold the spectrum (~55–2000 Hz) into 12 pitch classes,
accumulated with a leaky integrator (~8 s memory) → an averaged chroma vector.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Krumhansl–Schmuckler correlation.&lt;/strong&gt; Correlate against 24 reference key
profiles (12 major + 12 minor); best match wins.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   key = argmax_i corr(rotate(chroma, i), profile)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Map to Camelot&lt;/strong&gt; for harmonic mixing. This lineage goes back to libKeyFinder.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Honest limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Atonal/dense harmony blurs the chroma vector → less certain key.&lt;/li&gt;
&lt;li&gt;Mid-track modulation → it reports the dominant key.&lt;/li&gt;
&lt;li&gt;Weak percussion/rubato → fewer onsets, shakier BPM.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Sources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Krumhansl, &lt;em&gt;Cognitive Foundations of Musical Pitch&lt;/em&gt; (1990)&lt;/li&gt;
&lt;li&gt;Sha'ath, &lt;em&gt;Estimation of Key in Digital Music Recordings&lt;/em&gt; (2011)&lt;/li&gt;
&lt;li&gt;Ellis, &lt;em&gt;Beat Tracking by Dynamic Programming&lt;/em&gt; (2007)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pipeline ships in &lt;strong&gt;BeatScope&lt;/strong&gt;, a free offline analyzer for macOS/iPhone:&lt;br&gt;
&lt;a href="https://beatscope.pro" rel="noopener noreferrer"&gt;https://beatscope.pro&lt;/a&gt; — happy to answer DSP questions in the comments.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>mobile</category>
      <category>music</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
