-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshazam.py
131 lines (102 loc) · 4.48 KB
/
shazam.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
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys
import mainWindow
from helpers import *
from DB_helpers import *
from USER_helpers import *
# ==============================================================================================
class ShazamApp(QtWidgets.QMainWindow, mainWindow.Ui_MainWindow):
def __init__(self):
super(ShazamApp, self).__init__()
self.setupUi(self)
# ==============================================================================================
def scan(self):
"""
scan is the main function that generates the fingerprint from the input files then initiates the comparing process then show the ouput in a table in the UI.
gets called when the user presses 'scan' button.
"""
self.sliderValue = (self.horizontalSlider.value())
weight_1 = self.sliderValue / 100
weight_2 = 1-weight_1
if(self.scanMode == 'One Song'):
userSong = Song(self.filePath)
userSongHashes = userSong.generateFingerprint()
self.similarityResults = compareFingerprint(userSongHashes)
elif(self.scanMode == 'Two Songs'):
songOne = Song(self.filePath[0])
songTwo = Song(self.filePath[1])
userWeightedAverageSongHashes = generateFingerprintUserMixing(
songOne, songTwo, weight_1, weight_2)
self.similarityResults = compareFingerprint(
userWeightedAverageSongHashes)
logger.debug(
"scan button returned the results successfully")
self.createTable()
# ==============================================================================================
def browseOneFile(self):
"""
browseFiles saves the file path that the user chose from the UI.
gets called when the user presses 'Browse a File' button.
:return: a string that represents thepath to the chosen file by the user
"""
options = QtWidgets.QFileDialog.Options()
options |= QtWidgets.QFileDialog.DontUseNativeDialog
self.filePath, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"ShazamApp",
"",
"Audio File (*.mp3)",
options=options)
self.scanMode = 'One Song'
logger.debug(
"Browse a File button returned the file path successfully")
# ==============================================================================================
def browseFiles(self):
"""
browseFiles saves the file paths that the user chose from the UI.
gets called when the user presses 'Browse Files' button.
:return: a list of paths to the chosen files by the user
"""
options = QtWidgets.QFileDialog.Options()
options |= QtWidgets.QFileDialog.DontUseNativeDialog
self.filePath, _ = QtWidgets.QFileDialog.getOpenFileNames(
None,
"ShazamApp",
"",
"Audio Files (*.mp3)",
options=options)
self.scanMode = 'Two Songs'
logger.debug(
"Browse Files button returned the file paths successfully")
# ==============================================================================================
def createTable(self):
"""
createTable creates the table in the UI and show the similarity index of each song.
"""
i = 0
numResults = len(self.similarityResults)
# Row count
self.tableWidget.setRowCount(numResults)
# Column count
self.tableWidget.setColumnCount(2)
self.tableWidget.setItem(0, 0, QTableWidgetItem("Song Name"))
self.tableWidget.setItem(0, 1, QTableWidgetItem("Matching Percentage"))
for songData in self.similarityResults:
self.tableWidget.setItem(i, 0, QTableWidgetItem(songData[0]))
self.tableWidget.setItem(
i, 1, QTableWidgetItem(str(songData[1])+'%'))
i = i+1
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.horizontalHeader().setSectionResizeMode(
QHeaderView.Stretch)
logger.debug("Table has been created successfully")
# ==============================================================================================
def main():
App = QtWidgets.QApplication(sys.argv)
main = ShazamApp()
main.show()
sys.exit(App.exec_())
if __name__ == '__main__':
main()