How the Heck does Shazam work?

An in-depth look at the technical mechanisms behind Shazam, exploring how it uses Fast Fourier Transform (FFT) and audio fingerprinting to identify songs in seconds.
You're at a coffee shop. A song comes on. It's right on the tip of your tongue. You pull out your phone, tap a button, and it tells you what it is in a few seconds.
How does a phone listen to a few seconds of music through a noisy room and instantly match it against millions of songs?
Your first instinct might be that the phone is listening to the melody or recognizing the lyrics. It's neither of those. What it's actually doing is far more clever.
Reverse Engineering Sound
Your phone's microphone uses an extremely thin membrane called a diaphragm to measure the vibrations in the air caused by sound. Those vibrations are converted into an electrical signal, which is digitized into a waveform: a sequence of numbers representing air pressure at each instant in time.
This is similar to how your eardrum catches those same pressure waves, but while your brain turns them into sound, your phone turns them into a sequence of numbers.
The raw waveform itself isn't very useful for identification. A song played louder produces a completely different waveform even though it's the same song. Two different songs can produce very similar waveforms, and the same song played in different environments can produce different waveforms.
The trick is to transform the waveform into something more useful for a computer. Your phone runs a mathematical operation called a Fast Fourier Transform (FFT) on small slices of the waveform. Each slice gets decomposed from a single complex wave into a list of the individual frequencies present at that moment. You can think of it as asking: which pure tones would you need to add together to recreate this slice of sound?
Stacking all those slices side by side gives us a spectrogram, which represents sound in three dimensions: time runs along the X-axis, frequency along the Y-axis, and the brightness of each point represents amplitude (how loud that frequency is at that moment).
What does the FFT actually do?
Any waveform, no matter how jagged, can be described as a sum of smooth sine waves at different frequencies, amplitudes, and phases. The Fast Fourier Transform is an efficient algorithm for decomposing a chunk of audio samples into exactly that list. Feed it 1,024 samples of raw audio (about 23 milliseconds at CD quality), and it returns a spectrum telling us how much energy is present at each frequency.
The "fast" part matters. A naive decomposition would take millions of operations per chunk. The FFT exploits symmetry in the math to do it in roughly a fraction of the time.
In real music, sound is much more complex than simple synthetic tones. Every song is a composition of many layers, each made of many frequencies. The phone samples incoming sound tens of thousands of times per second (typically 44,100 Hz). Each tiny slice of those samples gets fed through the FFT, and what comes out the other side is a format that the system can start to reason about.
Less Is More
Even for a computer, storing and searching all of that spectrogram data would be impossibly slow, so the algorithm does something counterintuitive: it throws almost all of it away.
By applying a threshold, the system keeps only the loudest peaks and discards the faint signals, leaving a sparse collection of dots. These dots represent the most acoustically significant landmarks in the recording. This is what makes the system robust to noise. Background noise adds low-level energy, but it rarely creates the single loudest peak in any given region.
On the flipside, this "fingerprint" approach is also what makes Shazam work poorly if you just sing into it. You're likely to generate different hashes than the original song. This is why newer, machine-learning-based systems are built to handle humming and singing, by matching on melody rather than exact frequencies.
Connecting the Dots
A single dot in the constellation isn't very useful on its own. A frequency of 1,200 Hz at some moment in time could appear in thousands of songs. But a pair of dots, say 1,200 Hz followed by 2,400 Hz exactly 0.3 seconds later, is a lot more specific.
The algorithm gives every peak a turn as an anchor. For each one, it defines a target zone and pairs the anchor with every peak inside that zone. Each pair generates a compact hash from three numbers: the two frequencies and the time difference between them. Because the hashes are built from exact frequencies and timing, they effectively act as fingerprints of a specific recording.
Finding The Perfect Match
Instead of searching every song one by one, computers use an Inverted Index. Instead of asking "which song matches this sequence?", the phone asks "for each of these hashes, which songs contain them?". It's the same idea as the index at the back of a book: rather than re-reading every page to find a word, you jump to the word's entry and see every page it lives on. This makes the lookup operation nearly instantaneous whether you have 100 songs or 100 million.
The final test is timing. If the time gaps between all the matching hashes agree, the system can be confident in the match. The whole process happens in fractions of a second, delivering the "magic" experience users expect.
Source: Hacker News















