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 ☕

0 0 vote
Article Rating
Subscribe
Notify of
guest
30 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Endy
Endy
2 years ago

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
Endy
2 years ago

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
Endy
2 years ago

Thanks, for your explanation… I’ll researching.

Endy BermĂșdez R
Endy BermĂșdez R
1 year ago

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

Endy L Bermudez
Endy L Bermudez
1 year ago

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
Spencer
1 year ago

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
Spencer
1 year ago

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
Endy
1 year ago

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…]

Yashwanth
Yashwanth
1 year ago

Hello. I really appreciate the post. A great work. I just have a small query.
Where are these fingerprints stored in Ubuntu System as I didn’t connect to any local database.
Is there any option to do so? If yes, please revert.
Cheers!

Yashwanth
Yashwanth
1 year ago

Thank you for clarifying my query.
Is it possible for you to provide me python code to store the above fingerprints in a local MySQL Database?
My motive is to store these fingerprints in a local database and later access them for verification purposes.
Cheers!

Abhijith
Abhijith
1 year ago

Hey I made a program to analyze bird sounds and print the species of birds as output. But it always gives me the wrong answer. For example, if I input a sample hawk sound, it shows more similarity to the dove sound. Any help?

jesus
jesus
8 months ago

hi, thank you for the article, when I use the line fig = plt.figure() or plt.imshow(bitmap)
I get errors: _tkinter.TclError: no display name and no $DISPLAY environment variable
what can it be ?

this is the code
import acoustid
import chromaprint
import numpy as np
import matplotlib.pyplot as plt

duration, fp_encoded = acoustid.fingerprint_file(‘music.mp3’)
fingerprint, version = chromaprint.decode_fingerprint(fp_encoded)
print(fingerprint)
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)

jesus
jesus
8 months ago

after printing the fingerprint array, these are the errors:
Traceback (most recent call last):
File “test.py”, line 9, in
fig = plt.figure()
File “/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py”, line 539, in figure
**kwargs)
File “/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py”, line 171, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File “/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py”, line 1049, in new_figure_manager_given_figure
window = Tk.Tk(className=”matplotlib”)
File “/usr/lib/python2.7/lib-tk/Tkinter.py”, line 1828, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

Gabriel F
Gabriel F
6 months ago

Hi, thanks for the tutorial, do you know more about how chromaprint make it’s fingerprint ? specific technical details on how it works. I’m trying to make identification of same word rode my the same personne at the same speed, but the scores are really bad…

Valli
Valli
5 months ago

Hello,this post is great.I am trying to do a simple song identification. I am running the code given in the post:
import acoustid
import chromaprint
duration, fp_encoded = acoustid.fingerprint_file(‘input_1.wav’)
fingerprint, version = chromaprint.decode_fingerprint(fp_encoded)
print(fingerprint)
I following error has been thrown:
raise NoBackendError(“fpcalc not found”)

NoBackendError: fpcalc not found
I am running the file on windows with anaconda.
I have searched blogs which specified about downloading fpcalc but I could not understand how to download it and install it.Can you please let me know?
Thank you

Ally
Ally
16 days ago

Hello,
I wanted to ask if ther is a problem if I use the code for a wav file, not an mp3 file.
Because I am trying, but I get the audio could not be decoded error.

Thanks