{"slug": "detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope", "title": "Detecting musical key and tempo on-device: the DSP behind BeatScope", "summary": "A developer built BeatScope, a free offline music analyzer for macOS/iPhone that detects BPM and musical key using a hand-rolled signal-processing pipeline. The tempo detection uses spectral flux, autocorrelation, and dynamic programming, while key detection relies on chromagram and Krumhansl–Schmuckler correlation. The approach avoids machine learning and cloud dependencies, achieving fast, on-device analysis.", "body_md": "I needed to read a track's **BPM** and **musical key** in seconds, offline, on a\n\nphone. No ML black box, no server. Here's the signal-processing pipeline I ended\n\nup with — hand-rolled, since the usual packages weren't an option.\n\n##\nTempo\n\nTempo is periodicity in the track's energy. The steps:\n\n-\n**Spectral flux → onsets.** Sum the positive frame-to-frame magnitude increases\nof the STFT. Peaks are onsets (kick, snare, synth attacks). That's the onset\nenvelope.\n\n-\n**Autocorrelation.** The lag where the onset envelope best matches itself is the\nbeat period.\n-\n**Comb over multiples.** Weight candidate periods by their multiples (½, 2×) so\ntempo isn't confused with its harmonics.\n-\n**Octave folding + prior.** Fold candidates into 60–180 BPM and softly prefer\ntypical dance tempos — kills the \"174 vs 87\" error.\n-\n**Median smoothing** for stability.\n\nFor the beat **grid**, I use dynamic programming (Ellis-style): reward beats that\n\nland on onsets, penalize deviation from a steady interval. The DP gives a grid\n\nthat doesn't drift.\n\n##\nKey\n\n-\n**Chromagram.** Fold the spectrum (~55–2000 Hz) into 12 pitch classes,\naccumulated with a leaky integrator (~8 s memory) → an averaged chroma vector.\n-\n**Krumhansl–Schmuckler correlation.** Correlate against 24 reference key\nprofiles (12 major + 12 minor); best match wins.\n\n-\n**Map to Camelot** for harmonic mixing. This lineage goes back to libKeyFinder.\n\n##\nHonest limits\n\n- Atonal/dense harmony blurs the chroma vector → less certain key.\n- Mid-track modulation → it reports the dominant key.\n- Weak percussion/rubato → fewer onsets, shakier BPM.\n\n##\nSources\n\n- Krumhansl,\n*Cognitive Foundations of Musical Pitch* (1990)\n- Sha'ath,\n*Estimation of Key in Digital Music Recordings* (2011)\n- Ellis,\n*Beat Tracking by Dynamic Programming* (2007)\n\nThe pipeline ships in **BeatScope**, a free offline analyzer for macOS/iPhone:\n\n[https://beatscope.pro](https://beatscope.pro) — happy to answer DSP questions in the comments.", "url": "https://wpnews.pro/news/detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope", "canonical_source": "https://dev.to/conkormedia/detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope-31me", "published_at": "2026-07-14 21:36:50+00:00", "updated_at": "2026-07-14 22:28:42.725317+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["BeatScope", "Krumhansl", "Sha'ath", "Ellis"], "alternates": {"html": "https://wpnews.pro/news/detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope", "markdown": "https://wpnews.pro/news/detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope.md", "text": "https://wpnews.pro/news/detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope.txt", "jsonld": "https://wpnews.pro/news/detecting-musical-key-and-tempo-on-device-the-dsp-behind-beatscope.jsonld"}}