# How to detect repeating music?

Suppose you have a music data-file (for example mp3) with a single audio stream which contains an audio time-series for 3 hours, but you want to find out if that audio file (maybe some compilation) repeats itself after a while. Maybe it contains just a 1-hour piece of music, that is repeated twice.

How can I find out if there is a repetition of the same music in an audiofile? Are there tools for that? Maybe a python tool? Or maybe I can create one myself?

• You need to define what "repetition" means. Two songs with 1 bit difference would sound the same to even a trained ear. Dec 1, 2021 at 8:59

You can an autocorrelation over the whole file, but that's going to be rather expensive for 3 hours of music. You could try to segment it into individual songs and then cross correlate each song with each other song. That would be much more practical but would require segmentation first.

An alternative would be to do a feature extraction of each song and create a "fingerprint" based on a feature set and then you can simply compare fingerprints. That's what song identifying pass like https://www.shazam.com/ use. However, that's quite complicate to do.

As a simple solution extract the beginning (or any portion) of the song to be tested for repetition (you don't need much to detect a repetition) and then perform a streaming correlation which would be easy to do in python using scipy.signal.lfilter in python with the song segment time-reversed as the coefficients of the filter and stream through the sequence as the waveform being filtered. A threshold detection can determine the repetition point as the magnitude of the correlation would be proportional to the length of the filter coefficients used (and it's standard deviation, with the result for a match given as $$N\sigma$$). You could extend this into chunk processing of the file that pandas csvread supports really well.

You could further likely speed up the processing required by using FFT’s to do the correlation but as long as the initial sequence to correlate to isn’t too long the above using lfilter would be a simple, straightforward and robust approach for detecting repetition. Just make the filter long enough to meet your probability of false alarm versus probability of detection criteria.

For me the basic idea of Shazam algorithm is very very easy to code, I did it in less than 250 lines in python 10 years ago, and its realy works great ...

You can test the idea starting with the basic:

• First you will need generate fingerprint to all songs that you need, to do it you need build an Spectogram for each chunk audio (you maybe will test overlap in the future), the key here is split your spectogram in some ranges band, to keep it easy and basic try split your bins in 4 bands (course this is a basic example), ex. I choose from bin 50-200, 200-400, 400-700, 700-2048, if its is the best split ?? probably not lol (one big problem here, all songs sample rates need be the same obivious), if all song are sampled in 44100hz and you use FFT size 4096 the first spectrum band bin will cover:

44100*50/4096 = 538.33hz

44100*200/4096 = 2.153hz

wowww its is a big range frequency lol, but is just test to you understand, now for this chunk audio/spectrum you will need get the high bin/frequency from this band and store, do the same with the next 3 bins/frequency bands, at end you can say that you have generate an fingerprint for this chunk/segment ... for example you can have a peak for each band and generate you fingerprint as 33-301-450-1031 (333014501031), you will do it for the whole audio, chunk by chunk, at end you will have a lot of fingerprint, store all for this song... Ahaaa this is my 10 years ago test that generate fingerprints from David Guetta Feat. Rihanna - Who's That Chick, Shazam call this all fingerprints of Constelation Map

• Now I think you will want to test the algorithm by submitting some unknown audio to see if it works and finds in your base, the process here is the same (remember this unknow song need be sampled at same sample rate from the songs that you build your fingerprint database), but now for each chunk fingerprint you will compare if exist in you database, for example if in this chunk unkow song you get this fingerprint 33-301-450-1031 (333014501031) and if the same exist in you database I can say that you have one(1) match fingerprint for this song, and if your match continues increase in a linear time this is the same song :-) ...

I test it using 5 seconds samples to try match audios and works like a charm lol, surely you can use the same principle to find repeated points !