Simple music fingerprinting using Chromaprint in Python

Have you ever heard or even use service like Shazam? Cool, right? No, we are not going to make something as magical as it 😜 But using chromaprint we can create audio fingerprint so that we can do music search by using a music sample.

Before we can use chromaprint python library, pyacoustid, we need to install chromaprint C library in our OS. The build instruction is in its repo. Or if you use Debian/Ubuntu, you can install it using apt (if there is any error, try to google up the error. Most of the time, it’s due to missing dependency):

sudo apt install ffmpeg acoustid-fingerprinter

Then you can create virtual environment (or not) and install the pip packages:

pip install pyacoustid

Now to generate a fingerprint from a file:

import acoustid
import chromaprint

duration, fp_encoded = acoustid.fingerprint_file('music.mp3')
fingerprint, version = chromaprint.decode_fingerprint(fp_encoded)
print(fingerprint)

The fingerprint will contain an array of 948 signed int that represent compact characteristic aka fingerprint of the audio file. It can also be visualized with the help of numpy and matplotlib:

pip install numpy matplotlib
import numpy as np
import matplotlib.pyplot as plt

...

fig = plt.figure()
bitmap = np.transpose(np.array([[b == '1' for b in list('{:32b}'.format(i & 0xffffffff))] for i in fingerprint]))
plt.imshow(bitmap)
Music fingerprint bitmap visualization

Now, let say we have another music file and want to check the similarity with the previous file. One way to do this is by calculating similarity between the fingerprints:

pip install fuzzywuzzy[speedup]
from fuzzywuzzy import fuzz

similarity = fuzz.ratio(sample_fingerprint, fingerprint)
print(similarity)

The similarity will contains percentage of similarity between sample_fingerprint and fingerprint calculated using fuzzy algorithm.

I also made a simple program that uses all the codes above. The program calculates similarity between files in two directories, find the best match and also visualize the fingerprints.

That’s all. Thanks for reading ☕

15
Leave a Reply

avatar
11 Comment threads
4 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
yohanes.gultom@gmail.comEndySpencerEndy L BermudezEndy BermĂșdez R Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Endy
Guest
Endy

Hello, this post is great. I’m trying to create a music service… I want to know how I can compare two big audio (radio fragments), then finding little similar fragments (songs), I need to split and extract the common fragments.
Thanks, beforehand!

Endy
Guest
Endy

Thanks. The text behind of link is very interesting, so I need to understand it. Can you explain me or recommend me something more, about that?

Endy
Guest
Endy

Thanks, for your explanation… I’ll researching.

Endy BermĂșdez R
Guest
Endy BermĂșdez R

Hi, Yohanes.
How are you?
I would like to know how to store the fingerprints in mongodb.

Endy L Bermudez
Guest
Endy L Bermudez

Hi. I would like to know, how I can do the first code on c++
//
duration, fp_encoded = acoustid.fingerprint_file(‘music.mp3’)
fingerprint, version = chromaprint.decode_fingerprint(fp_encoded)
print(fingerprint)

Spencer
Guest
Spencer

I tried typing in this code:

import acoustid
import chromaprint
duration, fp_encoded = acoustid.fingerprint_file(‘YTP.mp3’)
fingerprint, version = chromaprint.decode_fingerprint(fp_encoded)
print(fingerprint)

but I get an error saying:

raise FingerprintGenerationError(“audio could not be decoded”)
acoustid.FingerprintGenerationError: audio could not be decoded

What could be the issue?

Spencer
Guest
Spencer

Yes it seems that it is a dependency problem. : freetype: no [The C/C++ header for freetype (freetype2\ft2build.h) could not be found. You may need to install the development package.] png: no [The C/C++ header for png (png.h) could not be found. You may need to install the development package.] However when I download both freetype and png it still cannot find them on my system. for example I have done PS C:\Users\sptzk> pip install pypng Requirement already satisfied: pypng in c:…… and also tried to download from developers site. is there a way to make it recognize that I… Read more »

Endy
Guest
Endy

Hey Yohanes, me one more time.
If i don’t need to compare similarity, but i need to know if the fingerprint of an audio fragment is contained into a fingerprint of a Song… What can i do?
Example:
fragmentAudio = [4, 5, 6]
song = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10…]