Kolton Ndoj

Shazam: turning songs into star maps

When you hold up your phone in a noisy café and it names the song in seconds

What problem does it solve?

How do you recognise a song from ten noisy seconds out of a library of millions?

The big idea

Turn the audio into a spectrogram (frequency vs time, using FFTs) and keep only the brightest peaks. You get a sparse 'constellation' of dots that survives noise, because chatter rarely drowns the loudest points. Single dots are too common to identify a song, so pair each peak with a few later ones and record (frequency 1, frequency 2, time gap) as a hash. Look up the hashes, and the true song is the one where many matches agree on the same time offset, like many stars lining up when you slide a transparent star map over the sky.

How it works, step by step

  1. 1. Record a few seconds and compute a spectrogram with repeated FFTs.
  2. 2. Keep only local peaks: the loudest points in their neighbourhood.
  3. 3. Pair each 'anchor' peak with several peaks just after it, making hashes (f₁, f₂, Δt).
  4. 4. Look up each hash in a giant database of hashes from every known song.
  5. 5. For each candidate song, count matches that share the same time offset; a tall spike means a match.

The key formula

hash=(f1,  f2,  t2−t1)\text{hash} = (f_1,\; f_2,\; t_2 - t_1)
f1f_1
frequency of the anchor peak
f2f_2
frequency of a later peak in its 'target zone'
t2−t1t_2-t_1
the time gap between them; it doesn't depend on where in the song you started recording

How fast is it?

Hash lookups are nearly instant, so speed barely depends on library size. The database is big: many hashes per second of every song, but each one is tiny. Noise and volume changes hurt little; drastic tempo or pitch changes (a live cover version) break it.

Try it yourself

Fun fact

Shazam's co-founder Avery Wang described the method in a 2003 paper called 'An Industrial-Strength Audio Search Algorithm'. Before smartphones, you used Shazam in the UK by dialling 2580 and holding your phone to the music; the answer came back as a text message. Apple bought Shazam in 2018.

Related

Open it in the Math Lab →

Media · Stretch