-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
150 lines (97 loc) · 4.48 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.uic import loadUiType
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog, QMainWindow
import os
from os import path
import logging
import pyqtgraph as pg
from hashing_m import per_spec_hashs,mix
import pandas as pd
import matplotlib.pyplot as plt
from similarity_m import similarity
import numpy as np
from pydub import AudioSegment
from scipy import signal
import operator
THIS_FOLDER= path.dirname(path.abspath(__file__))
FORM_CLASS,_=loadUiType(path.join(THIS_FOLDER, "shazam.ui"))
logging.basicConfig(filename='LogFile.log', filemode='w', format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.INFO)
#self.logger = logging.getLogger()
#self.logger.setLevel(logging.DEBUG)
class MainApp(QtWidgets.QMainWindow,FORM_CLASS):
def __init__(self,parent=None):
super(MainApp,self).__init__(parent)
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
self.handle_UI()
self.starter_UI()
def handle_UI(self):
_translate = QtCore.QCoreApplication.translate
#self.horizontalLayout.addWidget(self.mixer)
self.loadmusic1.clicked.connect(lambda:self.music_load(1))
self.loadmusic2.clicked.connect(lambda:self.music_load(2))
self.mix_slider.sliderReleased.connect(lambda:self.func_mixer())
def starter_UI(self):
self.hashes=[]
self.mixer=[]
self.mix_slider.hide()
self.label_3.hide()
self.resultsTable.hide()
music= pd.read_csv('musics.csv')
self.DataB_songs = music[:].values
self.musics_names=[]
for i in range(0,len(self.DataB_songs)):
self.musics_names.append([self.DataB_songs[i][0]])
def music_load (self, action):
load_file = QtWidgets.QFileDialog.getOpenFileName(None, "Load Audio File %s",filter="*.mp3")
path=load_file[0]
audiofile = AudioSegment.from_mp3(path)[:60000]
self.data = np.array(audiofile.get_array_of_samples())
self.rate = audiofile.frame_rate
self.mixer.append(self.data)
if action==1:
hashes=per_spec_hashs(self.data,self.rate)
self.compare(hashes)
if action==2:
self.mix_slider.show()
self.label_3.show()
pass
def compare(self,hashes):
print(hashes)
self.similarity_index = similarity(self.DataB_songs,hashes)
print(self.similarity_index)
self.musics=[]
self.sortedMusics=[]
for i in range(0,len(self.musics_names)):
self.musics.append([self.musics_names[i][0],self.similarity_index[i]])
self.sortedMusics= sorted(self.musics, key=operator.itemgetter(1), reverse=True)
print(self.sortedMusics)
self.ui_table()
def func_mixer(self):
data = mix(self.mixer[0] ,self.mixer[1][0:len(self.mixer[0])],self.mix_slider.value()/100)
hashes=per_spec_hashs(data,self.rate)
self.compare(hashes)
def ui_table(self):
# pass
self.resultsTable.setColumnCount(2)
self.resultsTable.setRowCount(len(self.sortedMusics))
for row in range(len(self.musics)):
self.resultsTable.setItem(row, 0, QtWidgets.QTableWidgetItem(self.sortedMusics[row][0]))
self.resultsTable.setItem(row, 1, QtWidgets.QTableWidgetItem(str(round(self.sortedMusics[row][1], 2))+"%"))
self.resultsTable.item(row, 0).setBackground(QtGui.QColor(57, 65, 67))
self.resultsTable.item(row, 1).setBackground(QtGui.QColor(57, 65, 67))
self.resultsTable.verticalHeader().setSectionResizeMode(row, QtWidgets.QHeaderView.Stretch)
self.resultsTable.setHorizontalHeaderLabels(["Song_Name", "Similarity"])
for col in range(2):
self.resultsTable.horizontalHeader().setSectionResizeMode(col, QtWidgets.QHeaderView.Stretch)
self.resultsTable.horizontalHeaderItem(col).setBackground(QtGui.QColor(57, 65, 67))
self.resultsTable.show()
self.similarity_index.clear()
def main():
app = QtWidgets.QApplication(sys.argv)
window= MainApp()
window.show()
app.exec_()
if __name__ == '__main__':
main()