from pydub import AudioSegment
from scipy import signal
from operator import itemgetter
import pyaudio
import numpy as np
import utils
import os
import sys
import warnings
import operator
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
FORMAT = pyaudio.paInt16
'''
Number of audio channels in the recording
'''
CHANNELS = 2
'''
Original sample rate of the recordings
'''
SAMPLE_RATE = 44100
'''
Sampling rate (after downsampling)
'''
FS = 8000
'''
Factor by which the original signal will be downsampled
'''
DECIMATION_FACTOR = SAMPLE_RATE/FS
'''
Size of the FFT window, affects frequency granularity (we saw this in class!)
'''
WINDOW_SIZE = 1024
'''
Degree to which a fingerprint can be paired with its neighbors --
higher will cause more fingerprints, but potentially better accuracy.
'''
FAN_VALUE = 15
'''
Ratio by which each window overlaps the previous and next window --
higher will cause more fingerprints, but higher granularity of offset matching
'''
OVERLAP_RATIO = 0.5
MAX_TIME = 150
path = os.getcwd()
warnings.filterwarnings("ignore", message="divide by zero encountered in log10")
warnings.filterwarnings("ignore", category=DeprecationWarning)
# Database with key=songname, value=[channel1, channel2]
SongDb = {}
#Goes through mp3s folder and adds each song to database
for filename in os.listdir(path + "/mp3s/"):
audiofile = AudioSegment.from_file(path + "/mp3s/" + filename)
data = np.fromstring(audiofile._data, np.int16)
channels = []
for chn in range(audiofile.channels):
channels.append(data[chn::audiofile.channels])
SongDb[filename[:-3]] = channels
print("Added to song database: " + str(filename[:-4]))
Added to song database: The Beatles - Let It Be
Added to song database: Michael Jackson - Billie Jean
Added to song database: Adele - Rolling in the deep
Added to song database: Rihanna - Disturbia
Added to song database: Whitney Houston - I Will Always Love You
def Preprocess(channels):
channel1 = channels[0]
channel2 = channels[1]
channelmean = ((channel1 + channel2)/2 - np.mean(channel1 + channel2))
resampled = signal.decimate(channelmean, int(DECIMATION_FACTOR))
return resampled
# Database with key=songname, value=processed signal
ProcessedDb = {}
#Processes each song and adds it to ProcessedDb
#Prints table of number of samples in for each song
print('{0:65}{1:22}{2:20}\n'.format('Song Name', 'Original #Samples', 'Processed #Samples'))
for song, sig in SongDb.items():
processed = Preprocess(sig)
ProcessedDb[song] = processed
original_duration = len(sig[0])
processed_duration = len(processed)
print('{0:50}{1:32d}{2:20d}'.format(song, original_duration, processed_duration))
Song Name Original #Samples Processed #Samples
The Beatles - Let It Be. 10718144 2143629
Michael Jackson - Billie Jean. 16920576 3384116
Adele - Rolling in the deep. 10111104 2022221
Rihanna - Disturbia. 10525824 2105165
Whitney Houston - I Will Always Love You. 11810476 2362096
def getSpectrogram(signal):
unprocessed = mlab.specgram(signal, WINDOW_SIZE, FS, noverlap=WINDOW_SIZE*OVERLAP_RATIO)[0]
processed = 10*np.log10(unprocessed)
processed[np.nonzero((processed == np.inf) | (processed == -np.inf))] = 0
return processed
''' TODO '''
# Database with key=songname, value=spectrogram
Spectrograms = {}
# Gets the spectrogram for each song and adds it to the Spectrograms database
# Plots each spectrogram
for song,sig in ProcessedDb.items():
Spectrograms[song] = getSpectrogram(sig)
plt.title(song)
plt.imshow(Spectrograms[song])
plt.show()
''' TODO '''
# Database with key=songname, value=array of local peaks
Peaks = {s:[] for s in list(ProcessedDb.keys())}
# Gets the local peaks for each song and adds it to the Peaks database
# Plots the peaks over the original spectrogram
for song in Spectrograms:
peaks = utils.get_2D_peaks(Spectrograms[song])
Peaks[song] = list(peaks[2])
plt.title(song)
plt.plot(peaks[1],peaks[0],"*", markersize=1)
plt.imshow(Spectrograms[song])
plt.show()
''' TODO '''
def getPairs(peaks):
peaks = list(peaks)
pairs = []
counter = {}
for peak in peaks:
counter[peak] = 0
for peak in peaks:
numMatches = 0
for other_peak in peaks:
if (other_peak[1] > peak[1] and other_peak[1] - peak[1] <= MAX_TIME and counter[peak] < FAN_VALUE and counter[other_peak] < FAN_VALUE):
pairs.append((peak[0], other_peak[0], other_peak[1]-peak[1]))
counter[peak] += 1
counter[other_peak] += 1
return pairs
''' TODO '''
# Database with key=fingerprint (f1, f2, tdelta), value=songname
LookUpTable = {}
# Get fingerprints for each song stores them in the LookUpTable database
# Prints a sample of the LookUpTable entries
for song in Peaks:
pairs = getPairs(Peaks[song])
for pair in pairs:
LookUpTable[pair] = song
print(len(LookUpTable))
to_print = 13
x = 0
for pair in LookUpTable:
if(LookUpTable[pair] == "The Beatles - Let It Be."):
print(x, pair, LookUpTable[pair])
x += 1
if (x >= to_print):
break
64512
0 (6, 10, 46) The Beatles - Let It Be.
1 (6, 15, 71) The Beatles - Let It Be.
2 (6, 19, 140) The Beatles - Let It Be.
3 (6, 23, 100) The Beatles - Let It Be.
4 (6, 61, 39) The Beatles - Let It Be.
5 (6, 61, 113) The Beatles - Let It Be.
6 (6, 76, 9) The Beatles - Let It Be.
7 (6, 77, 70) The Beatles - Let It Be.
8 (6, 92, 143) The Beatles - Let It Be.
9 (6, 122, 40) The Beatles - Let It Be.
10 (6, 122, 91) The Beatles - Let It Be.
11 (6, 122, 114) The Beatles - Let It Be.
12 (6, 133, 7) The Beatles - Let It Be.
# Database with key=songname, value=[channel1, channel2] for a snippet of the song
TestDb = {}
# Goes through test_mp3s folder and adds a snippet of each song to database
for filename in os.listdir("./test_mp3s/"):
audiofile = AudioSegment.from_file("./test_mp3s/" + filename)
data = np.fromstring(audiofile._data, np.int16)[SAMPLE_RATE*60:SAMPLE_RATE*75]
channels = []
for chn in range(audiofile.channels):
channels.append(data[chn::audiofile.channels])
TestDb[filename] = channels
print("Added to test database. : " + str(filename))
Added to test database. : Whitney Houston - I Will Always Love You (Official Music Video).mp3
Added to test database. : Rihanna - Disturbia.mp3
Added to test database. : Beatles - Let It Be [1970].mp3
Added to test database. : Let It Be (Remastered 2009).mp3
Added to test database. : Michael Jackson - Billie Jean (Official Music Video).mp3
Added to test database. : Adele - Rolling in the Deep.mp3
# Goes through test snippets and runs same fingerprinting process
# Prints out the number of matches for each song and confidence of prediction
for test in TestDb.keys():
print('\033[1mTesting: ' + test + '\033[0m \n')
Matches = {}
for song in SongDb.keys():
Matches[song] = 0
channels = TestDb[test]
preprocessed = Preprocess(channels)
spectrogram = getSpectrogram(preprocessed)
_, _, peaks = utils.get_2D_peaks(spectrogram)
pairs = getPairs(peaks)
for p in pairs:
match = LookUpTable.get(p, None)
if match:
Matches[match] += 1
prediction, count = max(Matches.items(), key=itemgetter(1))
for k,v in Matches.items():
if k == prediction:
print('\033[1m{0:50} ==> {1:10d} \033[0m'.format(k, v))
else:
print('{0:50} ==> {1:10d}'.format(k, v))
confidence = str(float(count)/sum(Matches.values())*100)[:5] + "%"
prediction = max(Matches.items(), key=itemgetter(1))
print(f'\nPrediction: {prediction[0]}')
print('\033[1m{0:10}: {1:10}\033[0m\n-----------------------------------------------------------------------\n\n'.format('Confidence', confidence))
Testing: Whitney Houston - I Will Always Love You (Official Music Video).mp3
The Beatles - Let It Be. ==> 0
Michael Jackson - Billie Jean. ==> 2
Adele - Rolling in the deep. ==> 0
Rihanna - Disturbia. ==> 0
Whitney Houston - I Will Always Love You. ==> 35
Prediction: Whitney Houston - I Will Always Love You.
Confidence: 94.59%
-----------------------------------------------------------------------
Testing: Rihanna - Disturbia.mp3
The Beatles - Let It Be. ==> 4
Michael Jackson - Billie Jean. ==> 0
Adele - Rolling in the deep. ==> 0
Rihanna - Disturbia. ==> 13
Whitney Houston - I Will Always Love You. ==> 3
Prediction: Rihanna - Disturbia.
Confidence: 65.0%
-----------------------------------------------------------------------
Testing: Beatles - Let It Be [1970].mp3
The Beatles - Let It Be. ==> 2
Michael Jackson - Billie Jean. ==> 0
Adele - Rolling in the deep. ==> 1
Rihanna - Disturbia. ==> 5
Whitney Houston - I Will Always Love You. ==> 0
Prediction: Rihanna - Disturbia.
Confidence: 62.5%
-----------------------------------------------------------------------
Testing: Let It Be (Remastered 2009).mp3
The Beatles - Let It Be. ==> 38
Michael Jackson - Billie Jean. ==> 0
Adele - Rolling in the deep. ==> 0
Rihanna - Disturbia. ==> 1
Whitney Houston - I Will Always Love You. ==> 1
Prediction: The Beatles - Let It Be.
Confidence: 95.0%
-----------------------------------------------------------------------
Testing: Michael Jackson - Billie Jean (Official Music Video).mp3
The Beatles - Let It Be. ==> 0
Michael Jackson - Billie Jean. ==> 16
Adele - Rolling in the deep. ==> 0
Rihanna - Disturbia. ==> 0
Whitney Houston - I Will Always Love You. ==> 0
Prediction: Michael Jackson - Billie Jean.
Confidence: 100.0%
-----------------------------------------------------------------------
Testing: Adele - Rolling in the Deep.mp3
The Beatles - Let It Be. ==> 0
Michael Jackson - Billie Jean. ==> 2
Adele - Rolling in the deep. ==> 14
Rihanna - Disturbia. ==> 3
Whitney Houston - I Will Always Love You. ==> 0
Prediction: Adele - Rolling in the deep.
Confidence: 73.68%
-----------------------------------------------------------------------