How does AudioMatch recognise a song?

Contents

Part 1 · The big picture: the whole algorithm in plain words, one diagram and one key idea.
Part 2 · Walk-through: every step with its maths, a real trace from our code and the figures.
Part 3 · Presenting it: the 3-minute script, self-test questions, results, demo and Q&A.

Every number here comes from our code (audiomatch/fingerprint.py, matcher.py, database.py) or the README results. The figures in Part 2 come from a real run on the noisy test clip song_03_t12_8s_snr10.wav, an 8 s clip of song_03 starting at 12 s with added noise.


Part 1 · The big picture

The problem in one sentence

You tap record halfway through a chorus. Your phone calls that moment zero. The song has been playing for a while. AudioMatch has to work out which song this is and where the phone’s zero falls in it.

The idea in one paragraph

A recording is a messy list of numbers. Noise, the room and the phone’s microphone all change those numbers. Some things survive all of that, though. Which notes are loudest, and how far apart in time they are, stay the same. AudioMatch keeps only those landmarks. It turns each pair of landmarks into a short code called a key. It then looks those keys up in a database built from the full songs. If many keys from the clip point to the same song and the same starting point, that song is the answer.

The pipeline

flowchart LR
    A["🎤 Audio<br/>11,025 numbers/s"] --> B["1 · Chop<br/>1,024-sample frames,<br/>new one every 512"]
    B --> C["2 · Window<br/>Hann fade at the ends"]
    C --> D["3 · FFT<br/>frame → 512 frequency strengths"]
    D --> E["4 · Peaks<br/>keep local maxima<br/>(the 'stars')"]
    E --> F["5 · Pair<br/>anchor + next 10 peaks<br/>→ key (f₁, f₂, Δt)"]
    F --> G{"Adding a song<br/>or matching a clip?"}
    G -- add --> H[("6 · Store<br/>key → (song, time)")]
    G -- match --> I["7 · Look up + vote<br/>for (song, offset)"]
    H -.-> I
    I --> J["8 · Decide<br/>≥ 8 votes and ≥ 2× runner-up"]
StepPlain wordsOutput for our 8 s test clip
AudioAir pressure measured 11,025 times a second88,200 numbers
1–2 Chop and windowShort, overlapping slices with faded ends171 frames × 1,024 numbers
3 FFTHow strong each pitch is in each slice512 bins × 171 frames (the spectrogram)
4 PeaksThe loudest spots in their neighbourhood638 peaks
5 PairsEach peak joined with its next 10 peaks6,325 keys
6–7 Look up and voteEvery database match votes for a song and a start time3,682 votes across 8 songs
8 DecideIs one (song, start) pile clearly the biggest?song_03, 11.98 s (the truth is 12 s)

The key idea: slide two sounds into place

Imagine you hear a low note, then a high note. A frame is one tick of AudioMatch’s clock, 512 samples or about 46.4 ms.

Where you hear themLow noteHigh noteGap
Your clipframe 20frame 3212 frames
Song Aframe 120frame 13212 frames

Where does the clip start in Song A? Try it before opening the answer.

Two numbers do different jobs:

  • 12 is the gap inside the pair. It stays the same wherever you start recording, so we can look it up.
  • 100 is the shift between the recordings. It tells us where the clip might have started.

One pair casts one vote for “Song A, shift 100”. A single vote could be luck. When dozens of other pairs vote for the same song and the same shift, it is not luck.

The one-breath version

“We turn a clip into a map of frequencies over time. We link strong peaks into pairs and look the pairs up. When enough pairs point to the same song and the same start time, we have a match.”


Part 2 · Walk-through with the maths

Step 0 · Sound as numbers

A microphone measures air pressure. Our files store it as numbers , taken times a second. The code divides each 16-bit integer by 32,768, so each value lies between −1 and 1 (load_wav).

The sampling theorem says that a sample rate of can only represent frequencies up to :

That limit covers melody and most harmonics, and a lower rate means less data to process. An 8 s clip has samples.

Step 1 · Chop into overlapping frames

Music changes over time, so we analyse it in short slices called frames. Each frame has samples, about 92.9 ms. A new frame starts every samples, about 46.4 ms. is called the hop. Each frame shares half its samples with the next one.

Why overlap? A note that starts near the edge of one frame sits near the middle of the next one. Overlap stops short events from being missed.

Time resolution: one frame index is s. Every “time” in AudioMatch, including the final start time, is counted in these 46.4 ms ticks.

Step 2 · Fade the edges with a Hann window

Cutting a frame out of a wave usually leaves a sudden jump at each end. The FFT treats the frame as if it repeats forever, so it sees that jump as a sharp click. A click contains many frequencies, so energy smears into bins that were not really loud. This smearing is called spectral leakage. To reduce it, multiply each frame by a smooth bump that is 0 at both ends and 1 in the middle:

am-1-chop-window.png

Top: three overlapping frames on the real clip. Middle: the Hann window. Bottom: one frame before (grey) and after (blue) windowing.

Step 3 · The FFT: which pitches are in this frame?

What we want

For each frame, we want to know how strong each frequency is. The discrete Fourier transform (DFT) gives exactly that. It compares the frame with a pure wave at each frequency :

  • is a pure wave that completes cycles in the frame.
  • The sum is large when the frame “moves with” that wave. The magnitude is the strength of frequency .
  • Bin corresponds to the frequency

    For example, bin 41 ≈ 441 Hz (close to concert A, 440 Hz) and bin 61 ≈ 657 Hz.
  • For a real-valued signal, bins to mirror bins to . The code therefore keeps only the first 512 bins, covering 0 to 5,512 Hz.

am-2-one-fft.png

One frame of the real clip after the FFT. Each spike is a note or a harmonic that is present in that frame.

Why a fast Fourier transform

The DFT formula computes outputs, and each output is a sum of terms. That is multiplications, or 1,048,576 for . The FFT (Cooley–Tukey, radix-2) gets the same numbers with about steps. It splits the sum into even- and odd-indexed samples:

Since and repeat every , one pair of half-size results gives two outputs:

Repeat the split until each piece has 1 sample. The cost satisfies

because there are levels of splitting, with work per level. Our code does exactly this:

def fft(frames):
    n = frames.shape[-1]
    if n == 1:
        return np.asarray(frames, dtype=np.complex128)
    even = fft(frames[..., 0::2])                    # E
    odd  = fft(frames[..., 1::2])                    # O
    twiddle = np.exp(-2j * np.pi * np.arange(n // 2) / n)   # W^k
    return np.concatenate([even + twiddle * odd,     # X[k]
                           even - twiddle * odd],    # X[k + N/2]
                          axis=-1)

am-7-fft-cost.png

Measured (README): our FFT matches numpy.fft to within . It is 5× faster than a direct DFT at 1,024 samples and 18× faster at 4,096. The gap is smaller than the 100× in the operation count because Python’s recursion overhead is large. The trend is the same, though: the bigger , the bigger the win.

Step 4 · Spectrogram, then keep only the peaks (“stars”)

Stack every frame’s 512 magnitudes side by side to get the spectrogram:

It is an image: time runs left to right, pitch runs bottom to top, and brightness shows strength.

Most of that image is not useful, so AudioMatch keeps only peaks. A point is a peak when both of these hold (find_peaks, box_size=5, threshold=2):

  1. It is the loudest point in its 11 × 11 box. The box spans 5 bins and 5 frames on each side:
  2. It is clearly louder than its own frame’s typical level:

    The bar is set per frame, not for the whole song. With a single bar for the whole song, one loud moment would raise it so much that quiet passages lost all their peaks.

Why peaks? Noise adds energy everywhere, but a strong note usually stays the strongest thing in its neighbourhood. Some peaks still disappear in noise, so the next steps only need some of them to survive.

am-3-spectrogram-peaks.png

The real clip: horizontal bright lines are held notes. Cyan circles are the 638 peaks. Peaks scattered in the dark background are noise peaks. They rarely produce keys that match the database at a consistent offset, so the voting step ignores them.

Step 5 · Pair the peaks into keys (fingerprints)

Sort peaks by time. Treat each peak as an anchor and pair it with the next 10 peaks, called its targets. Each pair becomes one record (make_hashes, max_pair=10):

Count check: 638 peaks × 10 targets, minus the last 10 anchors that have fewer than 10 targets after them (10 + 9 + … + 1 = 55), gives 6,380 − 55 = 6,325 keys. That matches the trace.

am-4-pairs.png

One real anchor (blue) and its 10 targets. Several peaks often share a frame, so many targets sit at Δt = 0 or 1.

Why pairs instead of single peaks? A single peak is just one bin, one of 512 values, and every song contains bin 41 somewhere. A pair must match two frequencies and a gap, which gives up to possible keys. Random matches become much rarer.

  • Evidence (README baselines, 25 songs): single-peak lookup returned 241,508 database hits. Pair keys returned 20,853, about 12× fewer, with the same accuracy.
  • Why and not absolute times? is the gap inside the pair, the “12” from Part 1. It is the same whenever you start recording. The absolute time is stored separately because it is needed for voting.
A detail worth knowing

The code uses abs(anchor_time - target_time). Peaks are sorted by time first, so this is always .

Step 6 · Store the song (indexing)

When a song is added, the same Steps 0–5 run on the whole song. Each record goes into one SQLite table:

anchor_freqtarget_freqtime_gapsongtime_in_song
416112Song A120

An index on (anchor_freq, target_freq, time_gap) works like a hash table. Finding every row for a key does not scan the table. To match, the code puts the clip’s distinct keys in a temporary table and runs one JOIN (database.lookup). The result is key → [(song, time_in_song), …].

Step 7 · Vote for (song, offset)

The maths of why it works

Let the clip start frames into the true song. A landmark heard at clip time was played at song time

Rearranging gives

The offset is the same number for every correct hit, whatever moment in the clip the hit comes from. A wrong hit has a random key collision and a random . Wrong hits therefore spread across many different offsets. The vote counts every hit (matcher.vote):

for clip_key, clip_time in clip_hashes:
    for song, song_time in hits.get(clip_key, []):
        votes[song][song_time - clip_time] += 1

Plot every hit as a point (clip time, song time). Correct hits fall on the line , a diagonal. Random hits scatter.

am-5-scatter.png

Real data. Grey: all 504 hits that landed in song_03 (out of 3,682 across the 8 songs in the database). Blue: the 63 hits with offset 258 sit exactly on the line song time = clip time + 258.

Counting votes per offset turns that diagonal into a single spike:

am-6-votes.png

Left: song_03 has a spike of 63 votes at offset 258. A smaller bar of 20 votes sits at 259 because the clip started part-way between two frame ticks, so some peaks round one frame later. Right: the best wrong song, song_04, peaks at only 12.

Step 8 · Decide, then convert to seconds

For each song, take its best offset, the tallest pile, and that pile’s vote count. Sort the songs by that count, giving (rank). The app answers only if (decide, MIN_VOTES = 8, MIN_RATIO = 2):

  • The first test rejects matches with too little evidence.
  • The second test rejects matches that are not clearly better than the next song. This is why the app says “no match” for unknown songs and noise instead of guessing.

The start time in seconds is

Real trace: (song_03) and (song_04). Since and , the answer is a match. The start time is s, against a true value of 12 s.

Why the error is about 0.02 s: the offset is a whole number of frames. A true start of 12 s is frames (which is also why some votes land on 259), so the answer can be at most half a hop off: s. That matches the README result that start times were within 0.02 s.

Complexity summary

Let = samples per frame (1,024), = samples in the clip, and = database hits.

StageTimeWhy
FFT, one framesplit into halves times, with work per level
Spectrogramabout frames × each
Peaks and keysfixed 11 × 11 box and fixed 10 targets per peak
Look-up and voteabout per key through the index, plus one vote per hit
Matching a clip

Part 3 · Presenting it

Minute 1 · Slide two sounds into place

Use the table from Part 1. Ask the audience where the clip starts. Then play am-anim-SlideIntoPlace.mp4. Land the two numbers: 12 is the gap we look up, and 100 is the shift we vote for.

Minute 2 · Where did those two notes come from?

  1. Chop. The mic measures air vibration 11,025 times a second. Take 1,024 measurements at a time and slide forward 512. A Hann window fades the ends of each slice so a sharp cut adds less unwanted frequency content.
  2. Picture. The FFT measures the strength of each frequency in a slice. Put the columns side by side to make a spectrogram: which frequency was strong, and when.
  3. Stars. Keep a spot if it is the strongest in its 11 × 11 neighbourhood and above twice its frame’s median.
  4. Lines. Pair each peak with the next ten. Two peaks in bins 41 and 61, 12 frames apart, give the key (41, 61, 12).
  5. Vote. Look up each key. Each hit votes for (song, stored frame − clip frame). Our pair votes for (Song A, +100).

Minute 3 · Can you do it without looking?

Answer aloud, then tap to check.

What the results mean

If someone asks…What to say
How fast was recognition?”An 8-second clip took about 89 ms on 5 songs and 129 ms on 25 songs. Brute force, sliding the spectrogram over every song, took 1.26 s on 25 songs, about 10× slower.”
How robust is it to noise?”With 8-second clips, it was 100% correct down to 0 dB SNR, where the noise is as loud as the music. It was 80% correct at −10 dB.”
How accurate was the start time?”Within 0.02 s in our correctness tests. That is the limit set by the 46 ms frame tick.”
How short can a clip be?”At 0 dB noise: 100% correct from 5 s, 60% at 3 s, 10% at 1 s.”
Did the FFT itself help?”Against a direct DFT, our FFT was 5× faster at 1,024 samples and 18× at 4,096. All 9 algorithm tests and 19 API tests passed.”

Demo and Q&A reference

Animations and figures

All files are in the audiomatch figures/ folder next to this note:

FileWhat it shows
am-anim-SlideIntoPlace.mp4Part 1 key idea: the fixed gap vs the shift
am-anim-FFTSplit.mp4DFT → even/odd split →
am-anim-PeaksToKeys.mp4spectrogram → peaks → anchor/target key
am-anim-Voting.mp4votes stacking at one offset, decision rule
audiomatch_manim.pyManimGL source for the four videos
make_figures.pyregenerates the am-*.png figures from the project code

To re-render the animations with ManimGL (3Blue1Brown’s manimgl package, v1.7.2 tested):

pip install manimgl "setuptools<81"          # needs ffmpeg, and LaTeX for formulas
cd "audiomatch figures"
manimgl audiomatch_manim.py SlideIntoPlace       # live preview window
manimgl audiomatch_manim.py -a -w -m             # write all four scenes as 720p .mp4

Watch the worked example · At 1:01 the clip slides into place. At 1:14 the votes begin to stack.