# Inside the Shazam Algorithm

How Shazam names a song from a few seconds of noisy audio: turning sound into a spectrogram, keeping only its peaks, hashing pairs of peaks and matching them against an index\.

By yuan hua

Topic: Computing, AI &amp; Electronics

[Open the interactive Jig](https://ask.jig.com/j/iKXrC6Wtlh5j)

## Transcript

### 1. Name that tune

A song is playing in a busy café and you want its name\. Shazam listens for a few seconds and answers almost at once\. This little machine shows how: the microphone on the left catches the sound, and each station along the way does one job\.

The method was published in 2003 by Avery Wang, one of Shazam's founders\. Drag the machine to look around it\.

### 2. Sound becomes a picture

First the sound is turned into a picture called a spectrogram\. Left to right is time\. Front to back is pitch, from low notes to high ones\. The taller and warmer a block, the louder that pitch is at that moment\.

Each slice of time is tiny: in one common setup, about 23 thousandths of a second\.

### 3. Keep only the brightest stars

Next, almost everything is thrown away\. Shazam keeps only the peaks: spots louder than everything around them, like the brightest stars in the sky\. This star map is called a constellation\. Switch on the café noise and watch: the chatter fills in the low blocks, but the peaks still stand out\.

Loudness itself is dropped too, so a quiet phone or a tinny speaker still gives the same stars\.

### 4. Connect the dots

One star becomes the anchor, shown in pink\. It is paired with a few stars inside a target zone just ahead of it in time, outlined in blue\. Each pair remembers two pitches and the gap between them, like connect\-the\-dots with a stopwatch\.

Every star takes a turn as an anchor\. One star alone is common, but a pair with a gap is a much rarer clue\.

### 5. Every pair becomes a code

Each pair is packed into one short number called a hash: first pitch, second pitch and gap\. A few seconds of music make hundreds of these codes\. Together they are the song's fingerprint, a tiny summary that cannot be turned back into music\.

Each code also notes when its anchor star played in your clip\. That matters at the end\.

### 6. A giant index, not a long search

Shazam never compares your clip with every song, one after another\. Its library works like the index at the back of a book: each code opens exactly one drawer, and that drawer lists every song containing the code and when it happens\. Press Look it up to open it\.

The 2003 paper describes a library of over a million tracks, searched in a few thousandths of a second\.

### 7. Line up the timing

Many songs share a few codes, so timing decides\. For every match, Shazam works out where in the song your clip must have started\. Wrong songs give scattered answers\. The right song agrees again and again, so one bar shoots up\.

A match can still stand out when only one or two codes in a hundred survive the noise\.

### 8. Found it\!

The tallest bar names the song, and the record spins up with your answer\. Listen, draw the stars, pair them, look up the codes, line up the timing: that is the whole trick\. Drag around to visit each station again\.

Shazam began in London in 1999\. Its first service, in 2002, worked by phone: you dialled 2580 and the song's name came back by text message\.
