October 30, 2026 · 4 min read
A music visualizer that takes its colors from the album art: an OKLab palette and beats without Spotify
How my music visualizer pulls a palette out of album art with k-means in OKLab, and how it detects bands, beats and tempo without relying on Spotify. Plus the bug that only showed up on slow computers.
In this post
Old music players came with visualizers: you opened Winamp or Windows Media Player and the music turned into light. Today there are hardly any left, and the few that exist don’t know which song is playing. I wanted one that did, so I built Visualizer.
The idea is simple: colors from the cover, motion from the sound. This post covers both halves.

Why not Spotify
The first thing I tried was Spotify’s API, which until recently gave you the tempo, energy and even the bars of each song. That part of the API was shut down in 2024, and Spotify’s audio is protected: a web page can’t read it.
So I solved it the other way round. The web demo searches any song through the iTunes Search API, which needs no account and lets the browser use the artwork and a 30-second preview. It can also listen to the microphone, the computer’s audio, your own file or a built-in synth track. All the analysis happens in the browser: nothing is uploaded.
The palette: k-means in OKLab
Getting “the colors of an image” sounds easy until you try. Averaging the pixels gives you a sad brown. Clustering in RGB doesn’t work well either, because distance in RGB doesn’t match what the eye sees: two blues that look almost the same can be far apart, and a green and a grey can be close.
That’s why the clustering happens in OKLab, a color space designed so that distance does match perception:
- The cover is shrunk to 64×64 pixels. That’s plenty, and it makes the computation instant.
- The pixels are clustered with k-means++ and a fixed seed, so the same cover always gives the same palette.
- Each cluster is scored by size, chroma and lightness, and five roles are filled: background, primary, secondary, accent and highlight.
The odd cases taught me the most. A muted cover has to keep its hue: José Madero’s Querido… is dark green, and the visualizer had to look green, not grey. A black-and-white cover has no hue to keep, so it gets my brand’s lime as its accent.
When the song changes, palettes don’t jump: they interpolate in OKLab, so the transition passes through in-between colors that make sense.
The sound: bands, beats and tempo
The audio goes into a Web Audio AnalyserNode with a 4096-sample FFT. Four numbers come out of it every frame: bass, low-mids, mids and highs.
Each band has automatic gain, because a quiet microphone and a loudly mastered file both need to use the full range. Without it, the scenes barely moved with the microphone.
For beats I use spectral flux: how much the energy grew since the previous frame, only in the kick range (40 to 160 Hz). It’s a beat when that growth crosses an adaptive threshold: the mean of the last 1.5 seconds plus 1.6 standard deviations. If the music has no clear kick, it falls back to the flux of the whole spectrum.
The tempo comes from a histogram of the intervals between beats, folded into 80–180 BPM so a song at 70 isn’t mistaken for one at 140.
All of this lives in pure functions with tests: I feed them synthetic signals at 120 and 140 BPM and check they’re detected.
The slow-computer bug
On day one everything ran at over 100 frames per second on my laptop. With software rendering, though, the scenes moved but it never detected a beat.
The cause: the detector “warmed up” by counting frames. It waited for a number of frames before it started detecting, and below about 14 frames per second it never reached that minimum within its window. Now the warm-up is measured in time, not frames, and a test runs the detector at 12 frames per second.
Later I went further: the audio analysis runs at 60 Hz on its own timer, separate from drawing, and a beat is “held” until the next frame. That way a heavy scene can’t make the analysis miss hits.
What I learned
- The color space matters as much as the algorithm: the same k-means gives far better palettes in OKLab than in RGB.
- The odd cases (muted or black-and-white covers) decide whether the palette feels right.
- Nothing that depends on how fast the machine is should be counted in frames. Counting in time makes it work the same on a slow computer.
- Testing on a slow machine finds bugs a fast one hides.
Try it at visualizer.rossmel.com; the code is on GitHub.